Location: PHPKode > projects > Network Managment / Inventory System > clearsite/include/aloe/cron/aloe_dbwalk.php
#!/usr/bin/php
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2006-2008 Xinn.org                                        |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | ClearSite/ALOE, A Long Overdue Enterprise network managment solution!   |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Xinn.org. See     |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://aloeusers.net                                                    |
 | http://clearsite.sourceforge.net                                        |
 | http://aloenms.net                                                      |
 | http://clear-site.net                                                   |
 | http://clear-site.info                                                  |
 | http://xinn.org/ClearSite.html                                          |
 +-------------------------------------------------------------------------+
*/

$configpath = str_replace(array("/aloe/cron/aloe_dbwalk.php", "\aloe\cron\aloe_dbwalk.php"), "", __FILE__) . "/config.php";
include_once($configpath);
include_once("$cs_base_path/include/aloe/aloe_config.php");

include_once("$base_path/lib/snmp.php");
include_once("$base_path/../db_config.php");
include_once("$base_path/aloe_rarparray.php");

// Grabs variables passed from aloe_cron.php

$device_ID      = $argv[1];
$record_updated = date('Y-m-d H:i:s');

mysql_connect("$DBhost:$DBport",$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$device_info_query = "SELECT t1.device_IP, t1.device_SNMP_Read, t1.device_type_ID, t2.template_table, t1.snmpver, t1.pingable, t1.new_device FROM devices AS t1, snmp_template_list AS t2 WHERE t1.device_ID = \"$device_ID\" and t1.template_ID = t2.template_ID";
$device_info_results = mysql_query($device_info_query);
$device_info_row = mysql_fetch_assoc($device_info_results);
$device_IP        = $device_info_row["device_IP"];
$device_SNMP_Read = $device_info_row["device_SNMP_Read"];
$device_type_ID   = $device_info_row["device_type_ID"];
$template_table   = $device_info_row["template_table"];
$snmpver          = $device_info_row["snmpver"];
$pingable         = $device_info_row["pingable"];
if ($device_info_row["new_device"] == "Y") {
  $new_device_query = "UPDATE devices SET new_device = \"N\" WHERE device_id = \"$device_ID\"";
  $new_device_result = mysql_query($new_device_query);
};
$start_time = microtime(true);

if ($pingable == "Y") {
  php_ping($device_IP) or die("Device not responding to PING");
};

// Creates crawl arrays using templates from database

$mib_anchor = array();
$anchor_list = array();
$vlan_crawls = array("none");

$template_sqlquery = "SELECT mib, oid, anchor, vlan, method FROM $template_table WHERE walk_type = \"db\" or walk_type = \"both\" ORDER BY mib DESC";
$template_result = mysql_query($template_sqlquery)or die ("MySQL Error: " . mysql_error() . "\nQuery: " . $template_sqlquery . "\n\n");
while ($template_row = mysql_fetch_assoc($template_result)) {
  $mib_anchor[$template_row["mib"]] = $template_row["anchor"];
  $already_anchor = array_search($template_row["anchor"], $anchor_list);
  if ($template_row["anchor"] != "anchor" and empty($already_anchor)) {
    $anchor_list[] = $template_row["anchor"];
  };
  if ($template_row["vlan"] == "no") {
    if ($template_row["method"] == "walk") {
      ${$template_row["mib"] . "_resarray"} = aloe_walk($device_IP, $device_SNMP_Read, $template_row["mib"], $template_row["oid"], $snmpver);
    } else {
      ${$template_row["mib"] . "_resarray"}[] = snmpget($device_IP, $device_SNMP_Read, $template_row["oid"]);
    };
  } else if ($template_row["vlan"] == "yes") {
    $vlan_crawls[] = $template_row["mib"];
    ${$template_row["mib"] . "_resarray"} = array();
    foreach($vlanindex_resarray as $vlandex => $vlandex2) {
      $temp_array = aloe_walk($device_IP, "$device_SNMP_Read@$vlandex", $template_row["mib"], $template_row["oid"], $snmpver);
      ${$template_row["mib"] . "_resarray"} = safe_array_merge(${$template_row["mib"] . "_resarray"}, $temp_array);
    };
  };
};

foreach($ifphysaddress_resarray as $pak => $pav) {
  $pav = str_replace("-", ":", $pav);
  $temp_pav_array = split(":", $pav);
  foreach($temp_pav_array as $tpval) {
    if (strlen($tpval) == "1") {
      $tpval = "0$tpval";
    };
    if(isset($newpav)) {
      $newpav .= ":$tpval";
    } else {
      $newpav = "$tpval";
    };
  };
  $ifphysaddress_resarray[$pak] = $newpav;
  unset($newpav);
};

if ($device_type_ID == "4" or $device_type_ID == "6" or $device_type_ID == "8") {
  // Enters data from crawls into DB based on ifname
  foreach ($ifname_resarray as $key => $value) {
    $value = str_replace("/", "-", $value);
    $portkey = array_search($key, $portifindex_resarray);
    if ($template_table == "snmp_template_cisco_switch_oneU") {
      if (!empty($portduplex_resarray[$portkey])) {
        if ($portduplex_resarray[$portkey] == "1") {
          $portduplex_resarray[$portkey] = "2";
        } else if ($portduplex_resarray[$portkey] == "2") {
          $portduplex_resarray[$portkey] = "1";
        } else if ($portduplex_resarray[$portkey] == "3") {
          $portduplex_resarray[$portkey] = "4";
        };
      };
    };

    $db_value_string = "device_ID=\"$device_ID\"";
    foreach ($mib_anchor as $makey => $mavalue) {
      $isvlancrawl = array_search($makey, $vlan_crawls);
      if (empty($isvlancrawl)) {
        if ($mavalue == "ifname" or $makey == "ifname") {
          if ($makey != "iftype" and $makey != "ifspeed" and $makey != "iflastchange") {
            if (isset(${$makey . "_resarray"}[$key])) {
              $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
            };
          } else if ($makey == "iflastchange") {
            list($lastchange_days, $lastchange_hours, $lastchange_minutes, $lastchange_seconds) = explode("-", $iflastchange_resarray[$key]);
            $db_value_string .= ", iflastchange=\"$lastchange_days days, $lastchange_hours hrs, $lastchange_minutes min and " . round($lastchange_seconds) . " sec ago\"";
          } else if ($makey == "iftype") {
            $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
            $iftype_ID_result = mysql_query($iftype_ID_query);
            $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
            if (empty($iftype_ID_row["iftype"])) {
              $iftype_insert_query = "INSERT INTO interface_type SET iftype=\"$iftype_resarray[$key]\"";
              $iftype_insert_result = mysql_query($iftype_insert_query);
              $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
              $iftype_ID_result = mysql_query($iftype_ID_query);
              $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
            };
            $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype_ID]\"";
          } else if ($makey == "ifspeed") {
            $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
            $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
            $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
            if (empty($ifspeed_ID_row["ifspeed_ID"])) {
              if ($ifspeed_resarray[$key] >= "1000000000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000000000 . " Gb";
              } else if ($ifspeed_resarray[$key] >= "1000000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000000 . " Mb";
              } else if ($ifspeed_resarray[$key] >= "1000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000 . " Kb";
              } else {
                $ifspeed_display  = "$ifspeed_resarray[$key] bps";
              };
              $ifspeed_insert_query = "INSERT INTO interface_speed SET ifspeed=\"$ifspeed_resarray[$key]\", ifspeed_display=\"$ifspeed_display\"";
              $ifspeed_insert_result = mysql_query($ifspeed_insert_query);
              $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
              $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
              $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
            };
            $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed_ID]\"";
          };
        } else if ($mavalue != "anchor") {
          if (!empty(${$makey . "_resarray"}[$portkey])) {
            $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$portkey] . "\"";
          };
        };
      };
    };

    $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
    $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;

    $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
    $ifname_exists_result = mysql_query($ifname_exists_query);
    $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
    if (empty($ifname_exists_row)) {
      $table_insert_query .= ", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\n\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
    } else {
      $table_update_query .= " WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\n\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
    };
  };

  // Get IPs of CDP neighbors
  $ignore_index = array();
  $cdpneighbor_ip = array();
  $cdpneighbor_host_name = array();
  foreach($cdpcacheaddress_resarray as $key => $value) {
    $temp_array = split("\.", $key);
    $ignore_index[] = $temp_array[0];
    $split_value = split('-', $value);
    $temp_ip = "";
    if (count($split_value) > 1) {
      for ($x=0; $x<4; $x++) {
        $split_value[$x] = hexdec($split_value[$x]);
        if (empty($temp_ip)) {
          $temp_ip = $split_value[$x];
        } else {
          $temp_ip .= ".$split_value[$x]";
        };
      };
    };
    $cdpneighbor_ip[$temp_array[0]] = $temp_ip;
    if (!empty($temp_ip)) {
      $cdpneighbor_host_name[$temp_array[0]] = gethostbyaddr($temp_ip);
    } else {
      $cdpneighbor_host_name[$temp_array[0]] = "";
    };
  };

  // Enters data from vlan crawls into DB based on ifname

  foreach($bridgetoifindex_resarray as $key => $value) {
    $port = $ifname_resarray[$value];
    $vbps = array_keys($vlanbridgeport_resarray, "$key");
    foreach($vbps as $vbps_value) {
      if (isset($mactable_resarray[$vbps_value])) {
        $mactable_resarray[$vbps_value] = strtolower(str_replace("-", ":", $mactable_resarray[$vbps_value]));
        if ($mactable_resarray[$vbps_value] != $ifphysaddress_resarray[$value]) {
          if (isset($mac_value)) {
            $mac_value .= ", $mactable_resarray[$vbps_value]";
          } else {
            $mac_value = $mactable_resarray[$vbps_value];
          };
        };
      };
    };
    if (!isset($mac_value)) {
      $mac_value = "";
    };

    //ignores cdp neighbors
    if (isset($cdpneighbor_ip[$value])) {
      $host_ip = $cdpneighbor_ip[$value];
      $host_name1 = $cdpneighbor_host_name[$value];
      $mac_value = "";
      $iscdpneighbor = "Y";
    } else {
      $iscdpneighbor = "N";
      $temp_mac_array = split(", ", $mac_value);
      foreach($temp_mac_array as $tma_value) {
        $temp_host_ip   = array_search("$tma_value", $ip_mac_array);
        if (!empty($temp_host_ip)) {
          $temp_host_name = gethostbyaddr($temp_host_ip);
          if (isset($host_ip) and !empty($host_ip)) {
            $host_ip .= ", $temp_host_ip";
            $host_name1 .= ", $temp_host_name";
          } else {
            $host_ip = $temp_host_ip;
            $host_name1 = $temp_host_name;
          };
        } else {
          $host_ip = "";
          $host_name1 = "";
        };
      };
    };
    $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$port\" and device_ID=\"$device_ID\"";
    $ifname_exists_result = mysql_query($ifname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifname_exists_query . "\n\n");
    $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
    if (empty($ifname_exists_row)) {
      $table_insert_query = "INSERT INTO interfaces SET iscdpneighbor=\"$iscdpneighbor\", mac=\"$mac_value\", host_ip=\"$host_ip\", host_name=\"$host_name1\", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
    } else {
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", iscdpneighbor=\"$iscdpneighbor\", mac=\"$mac_value\", host_ip=\"$host_ip\", host_name=\"$host_name1\" WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
    };
    unset($mac_value, $host_ip);
  };

  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };

  foreach($iptoifindex_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", interfaceip=\"$key\", interfacemask=\"$interfacemask_resarray[$key]\" WHERE $device_ID=\"$device_ID\" and ifindex=\"$value\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };

} else if ($device_type_ID == "9") {
  // Enters data from crawls into DB based on ifname
  foreach ($ifname_resarray as $key => $value) {
    if (array_search($value, $vlanids_resarray) === FALSE) {
      $value = str_replace("/", "-", $value);
      $portkey = array_search($key, $portifindex_resarray);
      $db_value_string = "device_ID=\"$device_ID\"";
      foreach ($mib_anchor as $makey => $mavalue) {
        if ($mavalue == "ifname" or $makey == "ifname") {
          if ($makey != "iftype" and $makey != "ifspeed" and $makey != "iflastchange") {
            if (isset(${$makey . "_resarray"}[$key])) {
              $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
            };
          } else if ($makey == "iflastchange") {
            list($lastchange_days, $lastchange_hours, $lastchange_minutes, $lastchange_seconds) = explode("-", $iflastchange_resarray[$key]);
            $db_value_string .= ", iflastchange=\"$lastchange_days days, $lastchange_hours hrs, $lastchange_minutes min and " . round($lastchange_seconds) . " sec ago\"";
          } else if ($makey == "iftype") {
            $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
            $iftype_ID_result = mysql_query($iftype_ID_query);
            $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
            if (empty($iftype_ID_row["iftype"])) {
              $iftype_insert_query = "INSERT INTO interface_type SET iftype=\"$iftype_resarray[$key]\"";
              $iftype_insert_result = mysql_query($iftype_insert_query);
              $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
              $iftype_ID_result = mysql_query($iftype_ID_query);
              $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
            };
            $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype_ID]\"";
          } else if ($makey == "ifspeed") {
            $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
            $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
            $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
            if (empty($ifspeed_ID_row["ifspeed_ID"])) {
              if ($ifspeed_resarray[$key] >= "1000000000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000000000 . " Gb";
              } else if ($ifspeed_resarray[$key] >= "1000000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000000 . " Mb";
              } else if ($ifspeed_resarray[$key] >= "1000") {
                $ifspeed_display  = $ifspeed_resarray[$key] / 1000 . " Kb";
              } else {
                $ifspeed_display  = "$ifspeed_resarray[$key] bps";
              };
              $ifspeed_insert_query = "INSERT INTO interface_speed SET ifspeed=\"$ifspeed_resarray[$key]\", ifspeed_display=\"$ifspeed_display\"";
              $ifspeed_insert_result = mysql_query($ifspeed_insert_query);
              $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
              $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
              $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
            };
            $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed_ID]\"";
          };
        } else if ($mavalue != "anchor") {
          if (!empty(${$makey . "_resarray"}[$portkey])) {
            $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$portkey] . "\"";
          };
        };
      };
      
      $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;
      
      $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
      $ifname_exists_result = mysql_query($ifname_exists_query);
      $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
      if (empty($ifname_exists_row)) {
        $table_insert_query .= ", added_to_DB = NOW()";
        $table_insert_result = mysql_query($table_insert_query)or die ("\n\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\n\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };

  foreach($mactoifname_resarray as $key => $value) {
    if (isset($ifname_resarray[$value])) {
      $port = $ifname_resarray[$value];
      if (isset($mactable_resarray[$key])) {
        $mactable_resarray[$key] = strtolower(str_replace("-", ":", $mactable_resarray[$key]));
        if ($mactable_resarray[$key] != $ifphysaddress_resarray[$value]) {
          if (isset($mac_value)) {
            $mac_value .= ", $mactable_resarray[$key]";
          } else {
            $mac_value = $mactable_resarray[$key];
          };
        };
      };
      if (!isset($mac_value)) {
        $mac_value = "";
      };
      
      $temp_mac_array = split(", ", $mac_value);
      foreach($temp_mac_array as $tma_value) {
        $temp_host_ip   = array_search("$tma_value", $ip_mac_array);
        if (!empty($temp_host_ip)) {
          $temp_host_name = gethostbyaddr($temp_host_ip);
          if (isset($host_ip) and !empty($host_ip)) {
            $host_ip .= ", $temp_host_ip";
            $host_name1 .= ", $temp_host_name";
          } else {
            $host_ip = $temp_host_ip;
            $host_name1 = $temp_host_name;
          };
        } else {
          $host_ip = "";
          $host_name1 = "";
        };
      };
      
      $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$port\" and device_ID=\"$device_ID\"";
      $ifname_exists_result = mysql_query($ifname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifname_exists_query . "\n\n");
      $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
      if (empty($ifname_exists_row)) {
        $table_insert_query = "INSERT INTO interfaces SET iscdpneighbor=\"N\", mac=\"$mac_value\", host_ip=\"$host_ip\", host_name=\"$host_name1\", added_to_DB = NOW(), device_ID=\"$device_ID\"";
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", iscdpneighbor=\"N\", mac=\"$mac_value\", host_ip=\"$host_ip\", host_name=\"$host_name1\" WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
      unset($mac_value, $host_ip);
    };
  };

  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };

} else if ($device_type_ID == "3") {
  // Enters data from crawls into DB based on ifname
  $cdpneighbor_ip = array();
  $cdpneighbor_host_name = array();
  foreach($cdpcacheaddress_resarray as $key => $value) {
    $temp_array = split("\.", $key);
    $split_value = split('-', $value);
    $temp_ip = "";
    if (count($split_value) > 1) {
      for ($x=0; $x<4; $x++) {
        $split_value[$x] = hexdec($split_value[$x]);
        if (empty($temp_ip)) {
          $temp_ip = $split_value[$x];
        } else {
          $temp_ip .= ".$split_value[$x]";
        };
      };
    };
    $cdpneighbor_ip[$temp_array[0]] = $temp_ip;
    if (!empty($temp_ip)) {
      $cdpneighbor_host_name[$temp_array[0]] = gethostbyaddr($temp_ip);
    } else {
      $cdpneighbor_host_name[$temp_array[0]] = "";
    };
  };

  foreach ($ifname_resarray as $key => $value) {
    $db_value_string = "device_ID=\"$device_ID\"";
    foreach ($mib_anchor as $makey => $mavalue) {
      if ($mavalue == "ifname" or $makey == "ifname") {
        if ($makey != "iftype" and $makey != "ifspeed") {
          $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
        } else if ($makey == "iftype") {
          $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
          $iftype_ID_result = mysql_query($iftype_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
          if (empty($iftype_ID_row["iftype"])) {
            $iftype_ID_row["iftype"] = "1";
          };
          $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype]\"";
        } else if ($makey == "ifspeed") {
          $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
          $ifspeed_ID_result = mysql_query($ifspeed_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
          if (empty($ifspeed_ID_row["ifspeed"])) {
            $ifspeed_ID_row["ifspeed"] = "1";
          };
          $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed]\"";
        };
      };
    };
    if (isset($cdpneighbor_ip[$key])) {
      $db_value_string .= ", host_ip=\"$cdpneighbor_ip[$key]\", host_name=\"$cdpneighbor_host_name[$key]\", iscdpneighbor=\"Y\"";
    };
    $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
    $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;
    $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
    $ifname_exists_result = mysql_query($ifname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
    if (empty($ifname_exists_row)) {
      $table_insert_query .= ", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    } else {
      $table_update_query .= " WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($iptoifindex_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", interfaceip=\"$key\", interfacemask=\"$interfacemask_resarray[$key]\" WHERE device_ID=\"$device_ID\" and ifindex=\"$value\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
} else if ($device_type_ID == "1") {

  // Enters data from crawls into DB based on svname
  foreach ($svname_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO css_sv SET device_ID=\"$device_ID\", sv_name=\"$value\", sv_ip=\"$svip_resarray[$key]\", sv_status=\"$svstatus_resarray[$key]\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE css_sv SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", sv_name=\"$value\", sv_ip=\"$svip_resarray[$key]\", sv_status=\"$svstatus_resarray[$key]\", ifindex=\"$key\"";
      $svname_exists_query = "SELECT sv_ID FROM css_sv WHERE sv_name=\"$value\" and device_ID=\"$device_ID\"";
      $svname_exists_result = mysql_query($svname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
      $svname_exists_row = mysql_fetch_assoc($svname_exists_result);
      if (empty($svname_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
      } else {
        $table_update_query .= " WHERE sv_ID=\"$svname_exists_row[sv_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
      };
    };
  };

  // Enters data from crawls into DB based on owner
  foreach ($owner_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO css_owner SET device_ID=\"$device_ID\", owner_name=\"$value\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE css_owner SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", owner_name=\"$value\", ifindex=\"$key\"";
      $owner_exists_query = "SELECT owner_ID FROM css_owner WHERE owner_name=\"$value\" and device_ID=\"$device_ID\"";
      $owner_exists_result = mysql_query($owner_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $owner_exists_query . "\n\n");
      $owner_exists_row = mysql_fetch_assoc($owner_exists_result);
      if (empty($owner_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE owner_ID=\"$owner_exists_row[owner_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };

  // Enters data from crawls into DB based on crname
  foreach ($crname_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $owner_ID_query = "SELECT owner_ID FROM css_owner WHERE owner_name=\"$crowner_resarray[$key]\" and device_ID=\"$device_ID\"";
      $owner_ID_result = mysql_query($owner_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $owner_ID_query . "\n\n");
      $owner_ID_row = mysql_fetch_assoc($owner_ID_result);
      $table_insert_query = "INSERT INTO css_cr SET device_ID=\"$device_ID\", cr_name=\"$value\", cr_vip=\"$crvip_resarray[$key]\", cr_status=\"$crstatus_resarray[$key]\", owner_ID=\"$owner_ID_row[owner_ID]\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE css_cr SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", cr_name=\"$value\", cr_vip=\"$crvip_resarray[$key]\", cr_status=\"$crstatus_resarray[$key]\", owner_ID=\"$owner_ID_row[owner_ID]\", ifindex=\"$key\"";
      $crname_exists_query = "SELECT cr_ID FROM css_cr WHERE cr_name=\"$value\" and device_ID=\"$device_ID\"";
      $crname_exists_result = mysql_query($crname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $crname_exists_query . "\n\n");
      $crname_exists_row = mysql_fetch_assoc($crname_exists_result);
      if (empty($crname_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cr_ID=\"$crname_exists_row[cr_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };

  // Enters data from crawls into DB based on ifdescr
  foreach ($ifdescr_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
      $iftype_ID_result = mysql_query($iftype_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $iftype_ID_query . "\n\n");
      $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
      if (empty($iftype_ID_row["iftype"])) {
        $iftype_ID_row["iftype"] = "1";
      };
      $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
      $ifspeed_ID_result = mysql_query($ifspeed_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifspeed_ID_query . "\n\n");
      $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
      if (empty($ifspeed_ID_row["ifspeed"])) {
        $ifspeed_ID_row["ifspeed"] = "1";
      };
      $table_insert_query = "INSERT INTO interfaces SET device_ID=\"$device_ID\", ifdescr=\"$value\", iftype_ID=\"$iftype_ID_row[iftype]\", ifspeed_ID=\"$ifspeed_ID_row[ifspeed]\", ifadminstatus=\"$ifadminstatus_resarray[$key]\", ifoperstatus=\"$ifoperstatus_resarray[$key]\", iflastchange=\"$iflastchange_resarray[$key]\", added_to_DB = NOW()";
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifdescr=\"$value\", iftype_ID=\"$iftype_ID_row[iftype]\", ifspeed_ID=\"$ifspeed_ID_row[ifspeed]\", ifadminstatus=\"$ifadminstatus_resarray[$key]\", ifoperstatus=\"$ifoperstatus_resarray[$key]\", iflastchange=\"$iflastchange_resarray[$key]\"";
      $ifdescr_exists_query = "SELECT interface_ID FROM interfaces WHERE ifdescr=\"$value\" and device_ID=\"$device_ID\"";
      $ifdescr_exists_result = mysql_query($ifdescr_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifdescr_exists_query . "\n\n");
      $ifdescr_exists_row = mysql_fetch_assoc($ifdescr_exists_result);
      if (empty($ifdescr_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE interface_ID=\"$ifdescr_exists_row[interface_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
  // Enters data from crawls into DB based on cpuname
  foreach ($cpuname_resarray as $key => $value) {
    if ($value != "U") {
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", cpu_name=\"$value\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", cpu_name=\"$value\", ifindex=\"$key\"";
      $cpu_name_exists_query = "SELECT cpu_ID FROM cpus WHERE device_ID=\"$device_ID\" and ifindex=\"$key\"";
      $cpu_name_exists_result = mysql_query($cpu_name_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $cpu_name_exists_query . "\n\n");
      $cpu_name_exists_row = mysql_fetch_assoc($cpu_name_exists_result);
      if (empty($cpu_name_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"cpu_name_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
} else if ($device_type_ID == "2" || $device_type_ID == "10") {
  // Enters data from crawls into DB based on ifname
  foreach ($ifindex_resarray as $key => $value) {
    $db_value_string = "device_ID=\"$device_ID\"";
    foreach ($mib_anchor as $makey => $mavalue) {
      if ($mavalue == "ifindex" or $makey == "ifindex") {
        if ($makey != "iftype" and $makey != "ifspeed") {
          $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
        } else if ($makey == "iftype") {
          $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
          $iftype_ID_result = mysql_query($iftype_ID_query);
          $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
          if (empty($iftype_ID_row["iftype"])) {
            $iftype_insert_query = "INSERT INTO interface_type SET iftype=\"$iftype_resarray[$key]\"";
            $iftype_insert_result = mysql_query($iftype_insert_query);
            $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
            $iftype_ID_result = mysql_query($iftype_ID_query);
            $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
          };
          $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype_ID]\"";
        } else if ($makey == "ifspeed") {
          $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
          $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
          $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
          if (empty($ifspeed_ID_row["ifspeed"])) {
            if ($ifspeed_resarray[$key] >= "1000000000") {
              $ifspeed_display  = $ifspeed_resarray[$key] / 1000000000 . " GB";
            } else if ($ifspeed_resarray[$key] >= "1000000") {
              $ifspeed_display  = $ifspeed_resarray[$key] / 1000000 . " MB";
            } else if ($ifspeed_resarray[$key] >= "1000") {
              $ifspeed_display  = $ifspeed_resarray[$key] / 1000 . " kB";
            } else {
              $ifspeed_display  = "$ifspeed_resarray[$key] bps";
            };
            $ifspeed_insert_query = "INSERT INTO interface_speed SET ifspeed=\"$ifspeed_resarray[$key]\", ifspeed_display=\"$ifspeed_display\"";
            $ifspeed_insert_result = mysql_query($ifspeed_insert_query);
            $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
            $ifspeed_ID_result = mysql_query($ifspeed_ID_query);
            $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
          };
          $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed_ID]\"";
        };
      };
    };
    $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
    $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;
    $ifindex_exists_query = "SELECT interface_ID FROM interfaces WHERE ifindex=\"$value\" and device_ID=\"$device_ID\"";
    $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
    if (empty($ifindex_exists_row)) {
      $table_insert_query .= ", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    } else {
      $table_update_query .= " WHERE interface_ID=\"$ifindex_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  if ($device_type_ID == "2") {
    foreach($iptoifindex_resarray as $key => $value) {
      if ($value != "U") {
        $value = str_replace("/", "-", $value);
        $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", interfaceip=\"$key\", interfacemask=\"$interfacemask_resarray[$key]\" WHERE $device_ID=\"$device_ID\" and ifindex=\"$value\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
      };
    };
  };
  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
} else if ($device_type_ID == "7") {
  // Enters data from crawls into DB based on ifname
  foreach ($ifname_resarray as $key => $value) {
    $db_value_string = "device_ID=\"$device_ID\"";
    foreach ($mib_anchor as $makey => $mavalue) {
      if ($mavalue == "ifname" or $makey == "ifname") {
        if ($makey != "iftype" and $makey != "ifspeed") {
          $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
        } else if ($makey == "iftype") {
          $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
          $iftype_ID_result = mysql_query($iftype_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
          if (empty($iftype_ID_row["iftype"])) {
            $iftype_ID_row["iftype"] = "1";
          };
          $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype]\"";
        } else if ($makey == "ifspeed") {
          $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
          $ifspeed_ID_result = mysql_query($ifspeed_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
          if (empty($ifspeed_ID_row["ifspeed"])) {
            $ifspeed_ID_row["ifspeed"] = "1";
          };
          $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed]\"";
        };
      };
    };
    $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
    $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;
    $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
    $ifname_exists_result = mysql_query($ifname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
    if (empty($ifname_exists_row)) {
      $table_insert_query .= ", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    } else {
      $table_update_query .= " WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($iptoifindex_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", interfaceip=\"$key\", interfacemask=\"$interfacemask_resarray[$key]\" WHERE device_ID=\"$device_ID\" and ifindex=\"$value\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
  foreach ($client_type_resarray as $key => $value) {
    if ($value != "U") {
      if (!empty($client_name_resarray[$key]) and $client_name_resarray[$key] != "00") {
        $client_name = "";
        $split_client_name = split('-', $client_name_resarray[$key]);
        for ($x=0; $x<count($split_client_name); $x++) {
          $split_client_name[$x] = chr(hexdec($split_client_name[$x]));
          if (empty($client_name)) {
            $client_name = $split_client_name[$x];
          } else {
            $client_name .= "$split_client_name[$x]";
          };
        };
        $client_name = trim($client_name);
        $client_assigned_address = "";
        $split_client_assigned_address = split('-', $client_assigned_address_resarray[$key]);
        for ($x=0; $x<count($split_client_assigned_address); $x++) {
          $split_client_assigned_address[$x] = hexdec($split_client_assigned_address[$x]);
          if (empty($client_assigned_address)) {
            $client_assigned_address = $split_client_assigned_address[$x];
          } else {
            $client_assigned_address .= ".$split_client_assigned_address[$x]";
          };
        };
        $client_assigned_address = trim($client_assigned_address);
        $table_insert_query = "INSERT INTO wap_clients SET device_ID=\"$device_ID\", client_name=\"$client_name\", client_assigned_address=\"$client_assigned_address\", client_type_ID=\"$value\", added_to_DB = NOW()";
        $table_update_query = "UPDATE wap_clients SET device_ID=\"$device_ID\", client_name=\"$client_name\", client_assigned_address=\"$client_assigned_address\", client_type_ID=\"$value\"";
        $ifindex_exists_query = "SELECT client_ID FROM wap_clients WHERE client_name=\"$client_name\"";
        $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
        $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
        if (empty($ifindex_exists_row)) {
          $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
        } else {
          $table_update_query .= " WHERE client_ID=\"$ifindex_exists_row[client_ID]\"";
          $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
        };
      };
    };
  };
} else if ($device_type_ID == "5") {
  // Enters data from crawls into DB based on ifname
  foreach ($ifindex_resarray as $key => $value) {
    $db_value_string = "device_ID=\"$device_ID\"";
    foreach ($mib_anchor as $makey => $mavalue) {
      if ($mavalue == "ifname" or $makey == "ifname") {
        if ($makey != "iftype" and $makey != "ifspeed") {
          $db_value_string .= ", $makey=\"" . ${$makey . "_resarray"}[$key] . "\"";
        } else if ($makey == "iftype") {
          $iftype_ID_query = "SELECT iftype_ID FROM interface_type WHERE iftype = \"$iftype_resarray[$key]\"";
          $iftype_ID_result = mysql_query($iftype_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $iftype_ID_row = mysql_fetch_assoc($iftype_ID_result);
          if (empty($iftype_ID_row["iftype"])) {
            $iftype_ID_row["iftype"] = "1";
          };
          $db_value_string .= ", iftype_ID=\"$iftype_ID_row[iftype]\"";
        } else if ($makey == "ifspeed") {
          $ifspeed_ID_query = "SELECT ifspeed_ID FROM interface_speed WHERE ifspeed = \"$ifspeed_resarray[$key]\"";
          $ifspeed_ID_result = mysql_query($ifspeed_ID_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
          $ifspeed_ID_row = mysql_fetch_assoc($ifspeed_ID_result);
          if (empty($ifspeed_ID_row["ifspeed"])) {
            $ifspeed_ID_row["ifspeed"] = "1";
          };
          $db_value_string .= ", ifspeed_ID=\"$ifspeed_ID_row[ifspeed]\"";
        };
      };
    };
    $table_insert_query = "INSERT INTO interfaces SET " . $db_value_string;
    $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", " . $db_value_string;
    $ifname_exists_query = "SELECT interface_ID FROM interfaces WHERE ifindex=\"$value\" and device_ID=\"$device_ID\"";
    $ifname_exists_result = mysql_query($ifname_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    $ifname_exists_row = mysql_fetch_assoc($ifname_exists_result);
    if (empty($ifname_exists_row)) {
      $table_insert_query .= ", added_to_DB = NOW()";
      $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    } else {
      $table_update_query .= " WHERE interface_ID=\"$ifname_exists_row[interface_ID]\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($iptoifindex_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_update_query = "UPDATE interfaces SET record_updated=\"$record_updated\", interfaceip=\"$key\", interfacemask=\"$interfacemask_resarray[$key]\" WHERE device_ID=\"$device_ID\" and ifindex=\"$value\"";
      $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $sqlquery . "\n\n");
    };
  };
  foreach($cpu5min_resarray as $key => $value) {
    if ($value != "U") {
      $value = str_replace("/", "-", $value);
      $table_insert_query = "INSERT INTO cpus SET device_ID=\"$device_ID\", ifindex=\"$key\", added_to_DB = NOW()";
      $table_update_query = "UPDATE cpus SET record_updated=\"$record_updated\", device_ID=\"$device_ID\", ifindex=\"$key\"";
      $ifindex_exists_query = "SELECT cpu_ID FROM cpus WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE cpu_ID=\"$ifindex_exists_row[cpu_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
  foreach ($username_resarray as $key => $value) {
    if ($value != "U") {
      $value = strtolower($value);
      print "INSERT INTO vpn_sessions SET device_ID=\"$device_ID\", username=\"$value\", pubip=\"$pubip_resarray[$key]\", privip=\"$privip_resarray[$key]\", sessionest=\"$sessionest_resarray[$key]\", sessionlength=\"$sessionlength_resarray[$key]\", session_type_ID=\"$session_type_resarray[$key]\"\n";
      $table_insert_query = "INSERT INTO vpn_sessions SET device_ID=\"$device_ID\", username=\"$value\", pubip=\"$pubip_resarray[$key]\", privip=\"$privip_resarray[$key]\", sessionest=\"$sessionest_resarray[$key]\", sessionlength=\"$sessionlength_resarray[$key]\", session_type_ID=\"$session_type_resarray[$key]\", added_to_DB = NOW()";
      $table_update_query = "UPDATE vpn_sessions SET device_ID=\"$device_ID\", username=\"$value\", pubip=\"$pubip_resarray[$key]\", privip=\"$privip_resarray[$key]\", sessionest=\"$sessionest_resarray[$key]\", sessionlength=\"$sessionlength_resarray[$key]\", session_type_ID=\"$session_type_resarray[$key]\"";
      $ifindex_exists_query = "SELECT session_ID FROM vpn_sessions WHERE username=\"$value\"";
      $ifindex_exists_result = mysql_query($ifindex_exists_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $ifindex_exists_query . "\n\n");
      $ifindex_exists_row = mysql_fetch_assoc($ifindex_exists_result);
      if (empty($ifindex_exists_row)) {
        $table_insert_result = mysql_query($table_insert_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_insert_query . "\n\n");
      } else {
        $table_update_query .= " WHERE session_ID=\"$ifindex_exists_row[session_ID]\"";
        $table_update_result = mysql_query($table_update_query)or die ("\nMySQL Error: " . mysql_error() . "\n\nQuery: " . $table_update_query . "\n\n");
      };
    };
  };
};

$end_time = microtime(true);
$total_time = $end_time - $start_time;
print "$device_IP finished\nStart Time: $start_time\nEnd Time: $end_time\nTotal Time: $total_time\n\n";
mysql_close();
?>
Return current item: Network Managment / Inventory System