Location: PHPKode > projects > Network Managment / Inventory System > clearsite/include/aloe/lib/snmp.php
<?php

// Sets snmp options
if (function_exists('snmp_set_quick_print')) {
  snmp_set_quick_print(1);
};
if (function_exists('snmp_set_oid_numeric_print')) {
  snmp_set_oid_numeric_print(1);
};

function lastIndexOf($givenindex, $crawl_value) {
  $clen = strlen($crawl_value);
  if (substr($givenindex, 0, $clen) == $crawl_value) {
    $index = substr($givenindex, $clen + 1);
  } else {
    $index = substr($givenindex, strrpos($givenindex, ".") + 1);
  };
  return $index;
};

function windows_snmp_string($string) {
  /* strip off all leading junk (the oid and stuff) */
  $string = trim(ereg_replace(".*= ?", "", $string));
  /* remove ALL quotes */
  $string = str_replace(array("\"", "'", ">", "<", "\\"), "", $string);

  /* Account for invalid type messages */
  if (substr_count($string, "Wrong Type")) {
    $string = strrev($string);
    if ($position = strpos($string, ":")) {
      $string = trim(strrev(substr($string, 0, $position)));
    } else {
      $string = trim(strrev($string));
    };
  };

  /* Remove invalid chars */
  $k = strlen($string);
  for ($i=0; $i < $k; $i++) {
    if ((ord($string[$i]) <= 31) || (ord($string[$i]) >= 127)) {
      $string[$i] = " ";
    };
  };
  $string = trim($string);

  if ((substr_count($string, "Hex-STRING:")) || (substr_count($string, "Hex:"))) {
    /* strip of the 'Hex-STRING:' */
    $string = eregi_replace("Hex-STRING: ?", "", $string);
    $string = eregi_replace("Hex: ?", "", $string);

    $string_array = split(" ", $string);

    /* loop through each string character and make ascii */
    $string = "";
    $hexval = "";
    $ishex  = false;
    for ($i=0;($i<sizeof($string_array));$i++) {
      if (strlen($string_array[$i])) {
        $string .= chr(hexdec($string_array[$i]));
        $hexval .= str_pad($string_array[$i], 2, "0", STR_PAD_LEFT);
        if (($i+1) < count($string_array)) {
          $hexval .= ":";
        };
        if ((hexdec($string_array[$i]) <= 31) || (hexdec($string_array[$i]) >= 127)) {
          if ((($i+1) == sizeof($string_array)) && ($string_array[$i] == 0)) {
            /* do nothing */
          } else {
            $ishex = true;
          };
        };
      };
    };

    if ($ishex) {
      $string = $hexval;
    };
  } else if (preg_match("/(hex:\?)?([a-fA-F0-9]{1,2}(:|\s)){5}/", $string)) {
    $octet = "";
    /* strip of the 'hex:' */
    $string = eregi_replace("hex: ?", "", $string);
    /* split the hex on the delimiter */
    $octets = preg_split("/\s|:/", $string);
    /* loop through each octet and format it accordingly */
    for ($i=0;($i<count($octets));$i++) {
      $octet .= str_pad($octets[$i], 2, "0", STR_PAD_LEFT);
      if (($i+1) < count($octets)) {
        $octet .= ":";
      };
    };
    /* copy the final result and make it upper case */
    $string = strtoupper($octet);
  } else if (preg_match("/Timeticks:\s\((\d+)\)\s/", $string, $matches)) {
    $string = $matches[1];
  };
  $string = eregi_replace("(hex|counter(32|64)|gauge|gauge(32|64)|float|ipaddress|string|integer):", "", $string);
  return trim($string);
};

function aloe_walk($host, $community, $crawlname, $crawlvalue, $snmpver) {
  global $clearsite_server_os;
  $ignore_ifname = array("VLAN-",
                         "Do",
                         "Vl",
                         "Nu",
                         "VL",
                         "lo0");
  $file_name_chars = array(":\\\\",
                           "://",
                           "\\",
                           "/",
                           "\:",
                           ":",
                           "\*",
                           "\?",
                           "\<",
                           "\>",
                           "\|",
                           "\"",
                           " ");
  if ($snmpver == "2") {
    $results = snmp2_real_walk("$host", "$community", "$crawlvalue", 3000000);
  } else if ($snmpver == "1") {
    $results = snmprealwalk("$host", "$community", "$crawlvalue", 3000000);
  };

  if (!empty($results)) {
    foreach($results as $givenarray[0] => $givenarray[1]) {
      if ($givenarray[1] == "" or $givenarray[1] == " ") {
        $givenarray[1] = "U";
      };
      list($givenindex,$givenvalue) = $givenarray;
      if (strpos($clearsite_server_os, "Windows") > 0) {
        $givenvalue = windows_snmp_string($givenvalue);
      };
      $givenvalue = str_replace("\"", "", $givenvalue);
      $givenvalue = trim($givenvalue);
      $givenvalue = str_replace($file_name_chars, "-", $givenvalue);
      $indexnum = lastIndexOf($givenindex, $crawlvalue);
      $add = 1;
      if ($crawlname == "ifname") {
        foreach($ignore_ifname as $ii_value) {
          $iilen = strlen($ii_value);
          if (substr($givenvalue, 0, $iilen) == $ii_value) {
            $add = 0;
          };
        };
        if ($add == "1") {
          $resarray[$indexnum] = $givenvalue;
        };
      } else if ($crawlname == "vlanindex") {
        if ($indexnum < 1000) {
          $resarray[$indexnum] = $givenvalue;
        };
      } else {
        $resarray[$indexnum] = $givenvalue;
      };
    };
  } else {
    $resarray = array();
  };
  unset($results);
  return $resarray;
};

?>
Return current item: Network Managment / Inventory System