function __construct($cfg) { snmp_set_quick_print(TRUE); if ((string) $cfg->host !== '') { $this->host = (string) $cfg->host; } if ((string) $cfg->port !== '') { $this->port = intval((string) $cfg->port); } if ((string) $cfg->community !== '') { $this->community = (string) $cfg->community; } foreach ($cfg->mib as $mib) { snmp_read_mib((string) $mib); } }
<?php /* Datapump - Devices from NMS to GTS (SNMP Version) */ error_reporting(E_ALL ^ E_NOTICE); // Read in mib snmp_read_mib("/usr/share/snmp/mibs/IDIRECT-REMOTE-MIB.txt"); // SNMP Query NMS Server $a = snmp2_real_walk("204.9.216.250", "dcsatnetwork", "1.3.6.1.4.1.13732"); // Convert returned data to usable array foreach ($a as $idx => $val) { list($branch, $snmpid) = explode(".", $idx); list($tree, $node) = explode("::", $branch); list($type, $value) = explode(":", $val, 2); switch ($node) { case 'nmstate': $netmodem[$snmpid][$node] = substr($value, strpos($value, "(") + 1, 1); break; default: $netmodem[$snmpid][$node] = trim($value); } } //print_r($netmodem); foreach ($netmodem as $nm) { if ($nm[typeid] == "remote(3)") { echo "Target[" . $nm[nmid] . "]: downstreamtotalKiloBytes." . $nm[nmdid] . "&upstreamtotalKiloBytes." . $nm[nmdid] . ":dcsatnetwork@204.9.216.250:::::2 * 1000\n"; echo "MaxBytes[" . $nm[nmid] . "]: 100000\n"; echo "RouterName[" . $nm[nmid] . "]: nmname." . $nm[nmdid] . "\n"; echo "Title[" . $nm[nmid] . "]: " . $nm[nmname] . " Traffic\n"; echo "YLegend[" . $nm[nmid] . "]: Bits per Second\n"; echo "LegendI[" . $nm[nmid] . "]: Downstream:\n"; echo "LegendO[" . $nm[nmid] . "]: Upstream:\n";
function checkHost($host_id) { global $config, $start, $seed, $key; // All time/dates will be stored in timestamps; // Get Collector Lastrun Information $storage_lastrun = read_config_option('mikrotik_storage_lastrun'); $trees_lastrun = read_config_option('mikrotik_trees_lastrun'); $users_lastrun = read_config_option('mikrotik_users_lastrun'); $queues_lastrun = read_config_option('mikrotik_queues_lastrun'); $interfaces_lastrun = read_config_option('mikrotik_interfaces_lastrun'); $processor_lastrun = read_config_option('mikrotik_processor_lastrun'); $wireless_aps_lastrun = read_config_option('mikrotik_wireless_aps_lastrun'); $wireless_reg_lastrun = read_config_option('mikrotik_wireless_reg_lastrun'); // Get Collection Frequencies (in seconds) $storage_freq = read_config_option('mikrotik_storage_freq'); $trees_freq = read_config_option('mikrotik_trees_freq'); $users_freq = read_config_option('mikrotik_users_freq'); $queues_freq = read_config_option('mikrotik_queues_freq'); $interfaces_freq = read_config_option('mikrotik_interfaces_freq'); $processor_freq = read_config_option('mikrotik_processor_freq'); $wireless_aps_freq = read_config_option('mikrotik_wireless_aps_freq'); $wireless_reg_freq = read_config_option('mikrotik_wireless_reg_freq'); /* remove the key process and insert the set a process lock */ if (!empty($key)) { db_execute("DELETE FROM plugin_mikrotik_processes WHERE pid={$key}"); } db_execute("REPLACE INTO plugin_mikrotik_processes (pid, taskid) VALUES (" . getmypid() . ", {$seed})"); /* obtain host information */ $host = db_fetch_row("SELECT * FROM host WHERE id={$host_id}"); if (function_exists('snmp_read_mib')) { debug('Function snmp_read_mib() EXISTS!'); snmp_read_mib($config['base_path'] . '/plugins/mikrotik/MIKROTIK-MIB.txt'); } else { putenv('MIBS=all'); } collect_system($host); if (runCollector($start, $users_lastrun, $users_freq)) { collect_users($host); // Remove old records db_execute_prepared('DELETE FROM plugin_mikrotik_users WHERE userType=0 AND name RLIKE "' . read_config_option('mikrotik_user_exclusion') . '" AND present = 0 AND host_id = ? AND last_seen < FROM_UNIXTIME(UNIX_TIMESTAMP()-' . read_config_option('mikrotik_user_exclusion_ttl') . ')', array($host['id'])); } if (runCollector($start, $trees_lastrun, $trees_freq)) { collect_trees($host); } if (runCollector($start, $queues_lastrun, $queues_freq)) { collect_queues($host); collect_pppoe_users_api($host); } if (runCollector($start, $interfaces_lastrun, $interfaces_freq)) { collect_interfaces($host); } if (runCollector($start, $processor_lastrun, $processor_freq)) { collect_processor($host); } if (runCollector($start, $storage_lastrun, $storage_freq)) { collect_storage($host); } if (runCollector($start, $wireless_aps_lastrun, $wireless_aps_freq)) { collect_wireless_aps($host); } if (runCollector($start, $wireless_reg_lastrun, $wireless_reg_freq)) { collect_wireless_reg($host); } if (!function_exists('snmp_read_mib')) { putenv('MIBS='); } /* remove the process lock */ db_execute('DELETE FROM plugin_mikrotik_processes WHERE pid=' . getmypid()); }
<?php if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $time = microtime(1); snmp_read_mib("mibs/DFL860E_A1_FW_v2.27.03.25-MIB.txt"); $servers = array('10.10.1.213' => array(), '10.10.10.213' => array(), '10.10.11.213' => array(), '10.10.12.213' => array(), '10.11.2.13' => array()); $cols = array('Name', 'CurrentBps'); $names = array(); $measure = isset($_GET['m']) ? $_GET['m'] : null; $bpsDiv = $measure == 'B' ? 8 : 1; $bpsPrefix = $measure == 'B' ? 'B' : 'b'; foreach ($servers as $server => $info) { $data = snmp2_walk($server, "1234567890", "dfl860eRules"); for ($i = 0, $l = count($data) / 3; $i < $l; $i++) { $key = explode(': ', $data[$i + $l], 2); $val = explode(': ', $data[$i + $l * 2], 2); $servers[$server][$key[1]] = $val[1]; } } echo json_encode($servers); die; } ?> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>DFL-860e Rule overview</title> <link rel="stylesheet" href="logger/assets/css/bootstrap.min.css"> <link rel="stylesheet" href="logger/assets/css/bootstrap-datetimepicker.min.css" />
<?php require_once dirname(__FILE__) . '/snmp_include.inc'; echo "Checking error handling\n"; var_dump(snmp_read_mib()); var_dump(snmp_read_mib(dirname(__FILE__) . '/cannotfindthisfile')); echo "Checking working\n"; var_dump(snmp_read_mib($mibdir . '/SNMPv2-MIB.txt'));