Пример #1
0
 public function install($path, $replace = false, $mib_name = 'optional')
 {
     global $config;
     include_once $config['library_path'] . '/mib_parser.php';
     $mp = new MibParser();
     $mp->add_mib($path, $mib_name);
     $mp->generate();
     if (isset($mp->mib) && isset($mp->oids) && $mp->mib) {
         /* check if this mib has already been installed */
         $existing = db_fetch_cell("SELECT 1 FROM snmpagent_mibs WHERE `name` = '" . $mp->mib . "'");
         if ($existing) {
             if ($replace == false) {
                 unset($mp->oids);
                 unset($mp->mib);
                 return false;
             } else {
                 $this->uninstall();
             }
         }
         db_execute("INSERT INTO snmpagent_mibs SET `id` = 0, `name` = '" . $mp->mib . "', `file` = '" . $path . "'");
         foreach ($mp->oids as $object_name => $object_params) {
             if ($object_params["otype"] != "TEXTUAL-CONVENTION") {
                 db_execute_prepared("INSERT INTO `snmpagent_cache` (`oid`, `name`, `mib`, `type`, `otype`, `kind`, `max-access`, `description`) VALUES ('" . $object_params["oid"] . "','" . $object_name . "','" . $object_params["mib"] . "','" . $object_params["syntax"] . "','" . $object_params["otype"] . "','" . $object_params["kind"] . "','" . $object_params["max-access"] . "','" . nl2br($object_params["description"]) . "')");
                 if ($object_params["otype"] == "NOTIFICATION-TYPE") {
                     foreach ($object_params["objects"] as $notication_object_index => $notication_object) {
                         db_execute("INSERT INTO `snmpagent_cache_notifications` (`name`, `mib`, `attribute`, `sequence_id`) VALUES ('" . $object_name . "','" . $object_params["mib"] . "','" . $notication_object . "','" . $notication_object_index . "')");
                     }
                 }
             } else {
                 db_execute_prepared("INSERT INTO `snmpagent_cache_textual_conventions` (`name`, `mib`, `type`, `description`) VALUES ('" . $object_name . "','" . $object_params["mib"] . "','" . $object_params["syntax"] . "','" . nl2br($object_params["description"]) . "')");
             }
         }
         unset($mp->oids);
         unset($mp->mib);
     } else {
         return false;
     }
 }
Пример #2
0
 /**
  * Parse a MIB file
  *
  * @param string $mibtext
  * @param boolean $full
  */
 function parse_mib($mibtext, $mib_name, $full = false)
 {
     $tokens = MibParser::get_tokens($mibtext);
     $cnt = count($tokens);
     $rec = array();
     for ($index = 0; $index < $cnt; $index++) {
         if ($tokens[$index] == 'DEFINITIONS' && $tokens[$index + 1] == "::=" && $tokens[$index + 2] == "BEGIN") {
             $mib_name = $tokens[$index - 1];
             $this->mib = $mib_name;
         } else {
             if (in_array($tokens[$index], array('OBJECT-IDENTITY', 'OBJECT-TYPE', 'OBJECT-GROUP', 'NOTIFICATION-GROUP', 'MODULE-IDENTITY', 'NOTIFICATION-TYPE'))) {
                 if ($tokens[$index - 1] != ',' && $tokens[$index + 1] != 'FROM' && $tokens[$index + 1] != 'MACRO') {
                     if (isset($rec['NAME']) && isset($rec['VALUE'])) {
                         $this->parsed[] = $rec;
                     }
                     $rec = array('NAME' => $tokens[$index - 1], 'MIB' => $mib_name, 'TYPE' => $tokens[$index]);
                 }
             } elseif ($tokens[$index] == "TEXTUAL-CONVENTION") {
                 if ($tokens[$index - 1] == '::=') {
                     if (isset($rec['NAME']) && isset($rec['VALUE'])) {
                         $this->parsed[] = $rec;
                     }
                     $rec = array('NAME' => $tokens[$index - 2], 'MIB' => $mib_name, 'TYPE' => $tokens[$index], 'VALUE' => "TEXTUAL-CONVENTION");
                 }
             } elseif ($tokens[$index] == 'OBJECT') {
                 if ($tokens[$index + 1] == 'IDENTIFIER' && $tokens[$index - 1] != '(' && $tokens[$index - 1] != '::=' && $tokens[$index - 1] != 'SYNTAX' && $tokens[$index - 2] != '(') {
                     if (isset($rec['NAME']) && isset($rec['VALUE'])) {
                         $this->parsed[] = $rec;
                     }
                     $rec = array('NAME' => $tokens[$index - 1], 'MIB' => $mib_name, 'TYPE' => $tokens[$index]);
                 }
             } elseif ($tokens[$index] == '{') {
                 MibParser::parse_bracket_token($tokens, $index, '{', '}');
             } elseif (isset($rec['NAME'])) {
                 if ($tokens[$index] == '::=' && $tokens[$index + 1] != "TEXTUAL-CONVENTION") {
                     $rec['VALUE'] = MibParser::parse_simple_token($tokens, $index);
                     $this->parsed[] = $rec;
                     $rec = array();
                 } elseif ($full) {
                     if ($tokens[$index] == 'ACCESS') {
                         $rec['ACCESS'] = MibParser::parse_simple_token($tokens, $index, array('read-only', 'not-accessible', 'read-write'));
                     } elseif ($tokens[$index] == 'OBJECTS') {
                         $rec['OBJECTS'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'NOTIFICATIONS') {
                         $rec['NOTIFICATIONS'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'DEFVAL') {
                         $rec['DEFVAL'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'DESCRIPTION') {
                         $rec['DESCRIPTION'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'INDEX') {
                         $rec['INDEX'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'MAX-ACCESS') {
                         $rec['MAX-ACCESS'] = MibParser::parse_simple_token($tokens, $index, array('read-only', 'not-accessible', 'read-write', 'read-create', 'accessible-for-notify'));
                     } elseif ($tokens[$index] == 'REFERENCE') {
                         $rec['REFERENCE'] = MibParser::parse_simple_token($tokens, $index);
                     } elseif ($tokens[$index] == 'STATUS') {
                         $rec['STATUS'] = MibParser::parse_simple_token($tokens, $index, array('current', 'deprecated', 'obsolete', 'mandatory'));
                     } elseif ($tokens[$index] == 'SYNTAX') {
                         $rec['SYNTAX'] = MibParser::parse_SYNTAX_token($tokens, $index);
                     } elseif ($tokens[$index] == 'UNITS') {
                         $rec['UNITS'] = MibParser::parse_simple_token($tokens, $index);
                     }
                 }
             }
         }
     }
     if (isset($rec['NAME']) && isset($rec['VALUE'])) {
         $this->parsed[] = $rec;
     }
 }