| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764 |
- <?php
- if (!defined("CONFIG")) die("Not defined");
- require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
- function get_ifmib_index_table($ip, $snmp)
- {
- $ifmib_map = [];
- $is_mikrotik = walk_snmp($ip, $snmp, MIKROTIK_DHCP_SERVER);
- $mk_ros_version = 0;
- if ($is_mikrotik) {
- $mikrotik_version = walk_snmp($ip, $snmp, MIKROTIK_ROS_VERSION);
- $mk_ros_version = 6491;
- $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/', $mikrotik_version[MIKROTIK_ROS_VERSION], $matches);
- if ($result) {
- $mk_ros_version = $matches[1] * 1000 + $matches[2] * 10 + $matches[3];
- }
- }
- if ($mk_ros_version == 0 or $mk_ros_version > 6468) {
- //fdb_index => snmp_index
- $index_map_table = walk_snmp($ip, $snmp, IFMIB_IFINDEX_MAP);
- //get map snmp interfaces to fdb table
- if (isset($index_map_table) and count($index_map_table) > 0) {
- foreach ($index_map_table as $key => $value) {
- $key = trim($key);
- $value = intval(trim(str_replace('INTEGER:', '', $value)));
- $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
- if ($result) {
- $fdb_index = preg_replace('/^\./', '', $matches[0]);
- $ifmib_map[$fdb_index] = $value;
- }
- }
- }
- }
- //return simple map snmp_port_index = snmp_port_index
- if (empty($ifmib_map)) {
- //ifindex
- $index_table = walk_snmp($ip, $snmp, IFMIB_IFINDEX);
- if (isset($index_table) and count($index_table) > 0) {
- foreach ($index_table as $key => $value) {
- $key = trim($key);
- $value = intval(trim(str_replace('INTEGER:', '', $value)));
- $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
- if ($result) {
- $fdb_index = preg_replace('/^\./', '', $matches[0]);
- $ifmib_map[$fdb_index] = $value;
- }
- }
- }
- }
- return $ifmib_map;
- }
- //get mac table by selected snmp oid
- function get_mac_table($ip, $snmp, $oid, $index_map)
- {
- $fdb_table = [];
- if (!isset($ip)) {
- return $fdb_table;
- }
- if (!isset($oid)) {
- return $fdb_table;
- }
- $mac_table = walk_snmp($ip, $snmp, $oid);
- if (isset($mac_table) and gettype($mac_table) == 'array' and count($mac_table) > 0) {
- foreach ($mac_table as $key => $value) {
- if (empty($value)) {
- continue;
- }
- if (empty($key)) {
- continue;
- }
- $key = trim($key);
- $value_raw = intval(trim(str_replace('INTEGER:', '', $value)));
- if (empty($value_raw)) {
- continue;
- }
- if (!empty($index_map)) {
- if (empty($index_map[$value_raw])) {
- $value = $value_raw;
- } else {
- $value = $index_map[$value_raw];
- }
- } else {
- $value = $value_raw;
- }
- $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
- $result = preg_match($pattern, $key, $matches);
- if (!empty($result)) {
- $mac_key = preg_replace('/^\./', '', $matches[0]);
- $fdb_table[$mac_key] = $value;
- }
- }
- }
- return $fdb_table;
- }
- // Get IP interfaces with speed
- function getIpAdEntIfIndex($db, $ip, $snmp)
- {
- $result = [];
- if (!isset($ip)) {
- return $result;
- }
- // Получаем данные по IP → interface index
- $ip_table = walk_snmp($ip, $snmp, ipAdEntIfIndex);
- // Получаем имена интерфейсов
- $int_table = walk_snmp($ip, $snmp, ifDescr);
- // Получаем скорости портов
- $speed_table = walk_snmp($ip, $snmp, PORT_SPEED_OID);
- if (is_array($ip_table) && !empty($ip_table)) {
- foreach ($ip_table as $key => $value) {
- if (empty($key) || empty($value)) {
- continue;
- }
- $key = trim($key);
- // Извлекаем index интерфейса из значения ipAdEntIfIndex
- $interface_index = intval(trim(str_replace('INTEGER:', '', $value)));
- if ($interface_index <= 0) {
- continue;
- }
- // Получаем имя интерфейса
- $int_key = ifDescr . '.' . $interface_index;
- if (!isset($int_table[$int_key])) {
- continue;
- }
- $interface_name = trim(str_replace('STRING:', '', $int_table[$int_key]));
- if (empty($interface_name)) {
- continue;
- }
- // Получаем IP-адрес из OID ключа
- $interface_ip = trim(str_replace(ipAdEntIfIndex . '.', '', $key));
- // Определяем тип интерфейса
- $interface_type = is_our_network($db, $interface_ip) ? 0 : 1;
- // Получаем скорость (если есть)
- $speed = null;
- $speed_key = PORT_SPEED_OID . '.' . $interface_index;
- if (isset($speed_table[$speed_key])) {
- $speed_raw = $speed_table[$speed_key];
- if (empty($speed_raw)) {
- $speed_raw = 'INT:0';
- }
- $speed = parse_snmp_value($speed_raw);
- if (!empty($speed)) { $speed = intdiv($speed, 1000); }
- }
- // Формируем запись
- $result[$interface_index] = [
- 'ip' => $interface_ip,
- 'index' => $interface_index,
- 'name' => $interface_name,
- 'interface_type' => $interface_type,
- 'speed' => $speed ?? 10000000,
- ];
- }
- }
- return $result;
- }
- //get mac table by analyze all available tables
- function get_fdb_table($ip, $snmp)
- {
- $fdb_table = [];
- if (!isset($ip)) {
- return $fdb_table;
- }
- $ifindex_map = get_ifmib_index_table($ip, $snmp);
- $fdb1_table = get_mac_table($ip, $snmp, MAC_TABLE_OID, $ifindex_map);
- if (!empty($fdb1_table)) {
- $fdb_table = $fdb1_table;
- } else {
- $fdb2_table = get_mac_table($ip, $snmp, MAC_TABLE_OID2, $ifindex_map);
- if (!empty($fdb2_table)) {
- $fdb_table = $fdb2_table;
- }
- }
- $snmp_cisco = $snmp;
- // maybe cisco?!
- if (empty($fdb_table) or count($fdb_table) == 0) {
- $vlan_table = walk_snmp($ip, $snmp, CISCO_VLAN_OID);
- if (!isset($fdb_table) or empty($vlan_table)) { return $fdb_table; }
- foreach ($vlan_table as $vlan_oid => $value) {
- if (empty($vlan_oid)) {
- continue;
- }
- $pattern = '/\.(\d{1,4})$/';
- $result = preg_match($pattern, $vlan_oid, $matches);
- if (!empty($result)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- if ($vlan_id > 1000 and $vlan_id < 1009) {
- continue;
- }
- $snmp_cisco["ro-community"] = $snmp["ro-community"] . '@' . $vlan_id;
- $fdb_vlan_table = get_mac_table($ip, $snmp_cisco, MAC_TABLE_OID, $ifindex_map);
- if (!isset($fdb_vlan_table) or !$fdb_vlan_table or count($fdb_vlan_table) == 0) {
- $fdb_vlan_table = get_mac_table($ip, $snmp_cisco, MAC_TABLE_OID2, $ifindex_map);
- }
- foreach ($fdb_vlan_table as $mac => $port) {
- if (!isset($mac)) {
- continue;
- }
- $fdb_table[$mac] = $port;
- }
- }
- }
- }
- return $fdb_table;
- }
- function is_host_up(string $ip, int $maxLoss = 5, int $count = 3): bool
- {
- $cmd = sprintf(
- 'ping -W 1 -i 1 -c %d %s 2>&1',
- $count,
- escapeshellarg($ip)
- );
- exec($cmd, $out, $ret);
- // Ищем строку вида: "3 packets transmitted, 3 received, 0% packet loss"
- foreach ($out as $line) {
- if (preg_match(
- '/(\d+)\s+packets transmitted,\s+(\d+)\s+received.*?(\d+)% packet loss/i',
- $line,
- $m
- )) {
- $loss = (int)$m[3];
- return $loss <= $maxLoss;
- }
- }
- // Если статистики нет вообще — считаем хост недоступным
- return false;
- }
- function check_snmp_access($ip, $snmp)
- {
- if (!isset($ip)) {
- return;
- }
- //check host up
- if (!is_host_up($ip)) { return; }
- //check snmp
- $result = get_snmp($ip, $snmp, SYS_DESCR_MIB);
- if (empty($result)) {
- return;
- }
- return 1;
- }
- function get_port_state($port, $ip, $snmp)
- {
- if (!isset($port)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- $port_oid = PORT_STATUS_OID . '.' . $port;
- $port_state = get_snmp($ip, $snmp, $port_oid);
- return $port_state;
- }
- function get_last_digit($oid)
- {
- if (!isset($oid)) {
- return;
- }
- $pattern = '/\.(\d{1,})$/';
- preg_match($pattern, $oid, $matches);
- return $matches[1];
- }
- function get_cisco_sensors($ip, $snmp, $mkey)
- {
- $index = get_last_digit($mkey);
- $result = parse_snmp_value(get_snmp($ip, $snmp, CISCO_SFP_SENSORS . "." . $index));
- $prec = parse_snmp_value(get_snmp($ip, $snmp, CISCO_SFP_PRECISION . "." . $index));
- if (!isset($prec)) {
- $prec = 1;
- }
- $result = round(trim($result) / (10 * $prec), 2);
- return $result;
- }
- function get_snmp_ifname($ip, $snmp, $port)
- {
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
- if (empty($port_name)) {
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
- }
- if (empty($port_name)) {
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFALIAS . "." . $port));
- }
- return $port_name;
- }
- function get_snmp_interfaces($ip, $snmp)
- {
- $result = [];
- $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFNAME);
- if (empty($ifmib_list)) {
- $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFDESCR);
- }
- if (empty($ifmib_list)) {
- $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFALIAS);
- }
- if (!empty($ifmib_list)) {
- foreach ($ifmib_list as $key => $value) {
- $key = trim($key);
- $value = parse_snmp_value($value);
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $int_index = preg_replace('/^\./', '', $matches[0]);
- $result[$int_index] = $value;
- }
- }
- }
- return $result;
- }
- function walk_snmp($ip, $snmp, $oid)
- {
- snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
- $result = NULL;
- $version = $snmp["version"];
- if ($version == 3) {
- $result = snmp3_real_walk($ip, $snmp["ro-user"], 'authPriv', $snmp['auth-proto'], $snmp['ro-password'], $snmp["priv-proto"], $snmp["ro-password"], $oid, SNMP_timeout, SNMP_retry);
- }
- if ($version == 2) {
- $result = snmp2_real_walk($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
- }
- if ($version == 1) {
- $result = snmprealwalk($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
- }
- return $result;
- }
- function getSnmpAccess($device)
- {
- $result['port'] = 161;
- $result['version'] = $device['snmp_version'];
- $result['ro-community'] = $device['community'];
- $result['rw-community'] = $device['rw_community'];
- //snmpv3
- $result['auth-proto'] = $device['snmp3_auth_proto'];
- $result['priv-proto'] = $device['snmp3_priv_proto'];
- $result['ro-user'] = $device['snmp3_user_ro'];
- $result['rw-user'] = $device['snmp3_user_rw'];
- $result['ro-password'] = $device['snmp3_user_ro_password'];
- $result['rw-password'] = $device['snmp3_user_rw_password'];
- return $result;
- }
- function get_snmp_module_id($modules_oids, $port_name)
- {
- $port_name = preg_quote(trim($port_name), '/');
- foreach ($modules_oids as $key => $value) {
- $pattern = '/' . $port_name . '/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $module_id = get_last_digit($key);
- return $module_id;
- }
- }
- return;
- }
- function get_sfp_status($vendor_id, $port, $ip, $snmp, $modules_oids)
- {
- if (!isset($vendor_id)) {
- return;
- }
- if (!isset($port)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- try {
- $status = '';
- // eltex
- if ($vendor_id == 2) {
- $sfp_vendor = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_VENDOR . "." . $port));
- if (!empty($sfp_vendor)) {
- $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
- $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
- $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
- $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
- $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
- $temp = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_temp));
- $volt = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_volt));
- $circut = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_circut));
- $tx = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_tx));
- $rx = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_rx));
- $sfp_sn = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_SN . "." . $port));
- $sfp_freq = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_FREQ . "." . $port));
- if (!isset($sfp_freq) or $sfp_freq == 65535) {
- $sfp_freq = 'unspecified';
- }
- $sfp_length = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_LENGTH . "." . $port));
- $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
- if (!empty($sfp_status_temp) && is_numeric($temp) && abs(floatval($temp)) > 0.1) {
- $status .= 'Temp: ' . $temp . " C";
- }
- if (!empty($sfp_status_volt) && is_numeric($volt) && abs(floatval($volt)) > 100000) {
- $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
- }
- if (!empty($sfp_status_circut) && is_numeric($circut) && abs(floatval($circut)) > 100) {
- $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
- }
- if (!empty($sfp_status_tx) && is_numeric($tx) && abs(floatval($tx)) > 100) {
- $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
- }
- if (!empty($sfp_status_rx) && is_numeric($rx) && abs(floatval($rx)) > 100) {
- $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
- }
- $status .= '<br>';
- return $status;
- }
- return;
- }
- // snr
- if ($vendor_id == 6) {
- $sfp_vendor = parse_snmp_value(get_snmp($ip, $snmp, SNR_SFP_VendorName . "." . $port));
- if (!empty($sfp_vendor) and $sfp_vendor != 'NULL') {
- $oid_sfp_model_name = SNR_SFP_ModelName . "." . $port;
- $oid_sfp_type_name = SNR_SFP_TypeName . "." . $port;
- $oid_sfp_bitrate = SNR_SFP_BitRate . "." . $port;
- $oid_sfp_status_volt = SNR_SFP_VOLT . "." . $port;
- $oid_sfp_status_circut = SNR_SFP_BIAS . "." . $port;
- $oid_sfp_status_tx = SNR_SFP_TX . "." . $port;
- $oid_sfp_status_rx = SNR_SFP_RX . "." . $port;
- $oid_sfp_length = SNR_SFP_WaveLength . "." . $port;
- $volt = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_volt));
- $circut = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_circut));
- $tx = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_tx));
- $rx = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_rx));
- $sfp_model_name = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_model_name));
- $sfp_type_name = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_type_name));
- $sfp_freq = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_bitrate));
- $sfp_length = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_length));
- $status = 'Vendor: ' . $sfp_vendor . ' ' . $sfp_model_name . ' ' . $sfp_type_name . ' Speed: ' . $sfp_freq . ' Freq: ' . $sfp_length . '<br>';
- if (!empty($volt)) {
- $status .= ' Volt: ' . $volt . ' V';
- }
- if (!empty($circut)) {
- $status .= ' Circut: ' . $circut . ' mA';
- }
- if (!empty($tx)) {
- $status .= ' Tx: ' . $tx . ' dBm';
- }
- if (!empty($rx)) {
- $status .= ' Rx: ' . $rx . ' dBm';
- }
- $status .= '<br>';
- return $status;
- }
- return;
- }
- // cisco
- if ($vendor_id == 16) {
- // get interface names
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
- if (empty($port_name)) {
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
- }
- // search module indexes
- $port_name = preg_quote(trim($port_name), '/');
- foreach ($modules_oids as $key => $value) {
- $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $temp = get_cisco_sensors($ip, $snmp, $key);
- continue;
- }
- $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $volt = get_cisco_sensors($ip, $snmp, $key);
- continue;
- }
- $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $circut = get_cisco_sensors($ip, $snmp, $key);
- continue;
- }
- $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $tx = get_cisco_sensors($ip, $snmp, $key);
- continue;
- }
- $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $rx = get_cisco_sensors($ip, $snmp, $key);
- continue;
- }
- }
- if (!empty($temp) && is_numeric($temp) && abs(floatval($temp)) > 0) {
- $status .= 'Temp: ' . $temp . " C";
- }
- if (!empty($volt) && is_numeric($volt) && abs(floatval($volt)) > 0) {
- $status .= ' Volt: ' . $volt . ' V';
- }
- if (!empty($circut) && is_numeric($circut) && abs(floatval($circut)) > 0) {
- $status .= ' Circut: ' . $circut . ' mA';
- }
- if (!empty($tx) && is_numeric($tx) && abs(floatval($tx)) > 0.1) {
- $status .= ' Tx: ' . $tx . ' dBm';
- }
- if (!empty($rx) && is_numeric($rx) && abs(floatval($rx)) > 0.1) {
- $status .= ' Rx: ' . $rx . ' dBm';
- }
- if (!empty($status)) {
- $status = preg_replace('/"/', '', $status);
- $status .= '<br>';
- }
- return $status;
- }
- // huawei
- if ($vendor_id == 3) {
- // get interface names
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
- if (empty($port_name)) {
- $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
- }
- // search module indexes
- $port_name = preg_quote(trim($port_name), '/');
- foreach ($modules_oids as $key => $value) {
- $pattern = '/' . $port_name . '/i';
- preg_match($pattern, $value, $matches);
- if (isset($matches[0])) {
- $module_id = get_last_digit($key);
- unset($result);
- $result = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_VENDOR . "." . $module_id));
- if (!empty($result)) {
- $sfp_vendor = $result;
- }
- unset($result);
- $result = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_SPEED . "." . $module_id));
- if (!empty($result)) {
- list($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
- if ($sfp_type == 0) {
- $sfp_type = 'MultiMode';
- }
- if ($sfp_type == 1) {
- $sfp_type = 'SingleMode';
- }
- }
- $volt = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_VOLT . "." . $module_id));
- $circut = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
- $tx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_OPTTX . "." . $module_id));
- $rx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_OPTRX . "." . $module_id));
- if (!isset($tx)) {
- $tx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_TX . "." . $module_id));
- }
- if (!isset($rx)) {
- $rx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_RX . "." . $module_id));
- }
- if (!empty($sfp_vendor)) {
- $status .= ' Name:' . $sfp_vendor . '<br>';
- }
- // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
- // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
- if (!empty($volt) && is_numeric($volt) && abs(floatval($volt)) > 1000) {
- $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
- }
- if (!empty($circut) && is_numeric($circut) && abs(floatval($circut)) > 0) {
- $status .= ' Circut: ' . $circut . ' mA <br>';
- }
- if (!empty($tx)) {
- $tx = preg_replace('/"/', '', $tx);
- try {
- list($tx_dbm, $pattern) = explode('.', $tx);
- $tx_dbm = round(floatval(trim($tx_dbm)) / 100, 2);
- } catch (Exception $e) {
- $tx_dbm = 0;
- }
- if (abs($tx_dbm) > 0.1) {
- $status .= ' Tx: ' . $tx_dbm . ' dBm';
- }
- }
- if (!empty($rx)) {
- $rx = preg_replace('/"/', '', $rx);
- try {
- list($rx_dbm, $pattern) = explode('.', $rx);
- $rx_dbm = round(floatval(trim($rx_dbm)) / 100, 2);
- } catch (Exception $e) {
- $rx_dbm = 0;
- }
- if (abs($rx_dbm) > 0.1) {
- $status .= ' Rx: ' . $rx_dbm . ' dBm';
- }
- }
- break;
- }
- }
- if (!empty($status)) {
- $status = preg_replace('/"/', '', $status);
- $status .= '<br>';
- }
- return $status;
- }
- } catch (Exception $e) {
- return;
- }
- }
- function get_switch_vlans($vendor, $ip, $snmp)
- {
- $switch_vlans = [];
- $port_status = [];
- $vlan_status = [];
- //cisco...
- if ($vendor == 16) {
- //all vlan at switch
- $vlan_list = walk_snmp($ip, $snmp, vtpVlanName);
- if (empty($vlan_list)) {
- return $vlan_status;
- }
- foreach ($vlan_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $vlan_id = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- }
- //skip service vlan
- if (preg_match('/(1002|1003|1004|1005)/', $vlan_id)) {
- continue;
- }
- if (isset($vlan_id) and !empty($vlan_id)) {
- $switch_vlans[$vlan_id] = $value;
- }
- }
- //native vlan for port - get list of all ports
- $pvid_list = walk_snmp($ip, $snmp, vlanTrunkPortNativeVlan);
- if (!empty($pvid_list)) {
- foreach ($pvid_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($port) and !empty($port)) {
- $port_status[$port]['native'] = $value;
- }
- }
- }
- //pvid
- $pvid_list = walk_snmp($ip, $snmp, vmVlanPvid);
- if (!empty($pvid_list)) {
- foreach ($pvid_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($port) and !empty($port)) {
- $port_status[$port]['pvid'] = $value;
- }
- }
- }
- //init port config
- foreach ($port_status as &$port) {
- if (!is_array($port)) {
- continue;
- }
- if (!isset($port['pvid'])) {
- $port['pvid'] = $port['native'];
- }
- $port['untagged'] = '';
- $port['tagged'] = '';
- }
- unset($port);
- //get vlan list at ports
- $egress_vlan = walk_snmp($ip, $snmp, vlanTrunkPortVlansEnabled);
- if (!empty($egress_vlan)) {
- $j = 0;
- foreach ($egress_vlan as $key => $value) {
- $j++;
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($port) and !empty($port)) {
- //skip access ports
- if (!is_array($port_status[$port]) or !isset($port_status[$port]['pvid']) or !isset($port_status[$port]['native'])) {
- continue;
- }
- if ($port_status[$port]['pvid'] != $port_status[$port]['native']) {
- continue;
- }
- //get vlan at port in hex
- $hex_value = preg_replace('/\s+/', '', $value);
- $bin_value = strHexToBin($hex_value);
- //analyze switch vlans
- foreach ($switch_vlans as $vlan_id => $vlan_name) {
- if (isset($bin_value[$vlan_id]) and $bin_value[$vlan_id] == '1') {
- $port_status[$port]['tagged'] = $port_status[$port]['tagged'] . ',' . $vlan_id;
- }
- }
- }
- }
- }
- //remove lliding ,
- foreach ($port_status as &$port) {
- if (!is_array($port)) {
- continue;
- }
- $port['untagged'] = preg_replace('/^,/', '', $port['untagged']);
- $port['tagged'] = preg_replace('/^,/', '', $port['tagged']);
- }
- unset($port);
- return $port_status;
- }
- //standart switches
- //tplink
- if ($vendor == 69) {
- //pvid for port
- $pvid_list = walk_snmp($ip, $snmp, TPLINK_dot1qPortVlanEntry);
- if (!empty($pvid_list)) {
- foreach ($pvid_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($port) and !empty($port)) {
- $port_status[$port]['pvid'] = $value;
- }
- }
- }
- return $port_status;
- }
- //default
- //pvid for port
- $pvid_list = walk_snmp($ip, $snmp, dot1qPortVlanEntry);
- if (!empty($pvid_list)) {
- foreach ($pvid_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($port) and !empty($port)) {
- $port_status[$port]['pvid'] = $value;
- }
- }
- }
- //init port config
- foreach ($port_status as &$port) {
- if (!is_array($port)) {
- continue;
- }
- $port['native'] = $port['pvid'];
- $port['untagged'] = '';
- $port['tagged'] = '';
- }
- unset($port);
- //all vlan at switch
- $vlan_list = walk_snmp($ip, $snmp, dot1qVlanStaticName);
- if (empty($vlan_list)) {
- return $port_status;
- }
- foreach ($vlan_list as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $vlan_id = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($vlan_id) and !empty($vlan_id)) {
- $switch_vlans[$vlan_id] = $value;
- }
- }
- $forbidden_vlan = walk_snmp($ip, $snmp, dot1qVlanForbiddenEgressPorts);
- if (!empty($forbidden_vlan)) {
- foreach ($forbidden_vlan as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($vlan_id) and !empty($vlan_id)) {
- $hex_value = preg_replace('/\s+/', '', $value);
- $hex_value = preg_replace('/0*$/', '', $hex_value);
- $bin_value = strHexToBin($hex_value);
- for ($i = 0; $i < strlen($bin_value); $i++) {
- $port = $i + 1;
- $vlan_status['forbidden_vlan'][$vlan_id][$port] = $bin_value[$i];
- if ($bin_value[$i] == '1') {
- $port_status[$port]['forbidden'] .= ',' . $vlan_id;
- }
- }
- }
- }
- }
- $untagged_vlan = walk_snmp($ip, $snmp, dot1qVlanStaticUntaggedPorts);
- if (!empty($untagged_vlan)) {
- foreach ($untagged_vlan as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- }
- if (isset($vlan_id) and !empty($vlan_id)) {
- $hex_value = preg_replace('/\s+/', '', $value);
- $hex_value = preg_replace('/0*$/', '', $hex_value);
- $bin_value = strHexToBin($hex_value);
- for ($i = 0; $i < strlen($bin_value); $i++) {
- $port = $i + 1;
- $vlan_status['untagged_vlan'][$vlan_id][$port] = $bin_value[$i];
- if ($bin_value[$i] == '1') {
- if (isset($vlan_status['forbidden_vlan']) and $vlan_status['forbidden_vlan'][$vlan_id][$port] == '0') {
- $port_status[$port]['untagged'] .= ',' . $vlan_id;
- } else {
- $vlan_status['untagged_vlan'][$vlan_id][$port] = '0';
- }
- }
- }
- }
- }
- }
- $egress_vlan = walk_snmp($ip, $snmp, dot1qVlanStaticEgressPorts);
- if (!empty($egress_vlan)) {
- foreach ($egress_vlan as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $vlan_id = preg_replace('/^\./', '', $matches[0]);
- }
- //exclude vlan 1 from tagged vlan
- if ($vlan_id == '1') {
- continue;
- }
- if (isset($vlan_id) and !empty($vlan_id)) {
- $hex_value = preg_replace('/\s+/', '', $value);
- $hex_value = preg_replace('/0*$/', '', $hex_value);
- $bin_value = strHexToBin($hex_value);
- for ($i = 0; $i < strlen($bin_value); $i++) {
- $port = $i + 1;
- $vlan_status['egress_vlan'][$vlan_id][$port] = $bin_value[$i];
- //analyze egress & untagged vlans
- if ($bin_value[$i] == '1') {
- if ((!isset($vlan_status['untagged_vlan'][$vlan_id][$port]) or $vlan_status['untagged_vlan'][$vlan_id][$port] == '0') and
- (!isset($vlan_status['forbidden_vlan'][$vlan_id][$port]) or $vlan_status['forbidden_vlan'][$vlan_id][$port] == '0') and
- (!isset($port_status[$port]['pvid']) or $port_status[$port]['pvid'] != $vlan_id)
- ) {
- $vlan_status['tagged_vlan'][$vlan_id][$port] = '1';
- $port_status[$port]['tagged'] .= ',' . $vlan_id;
- } else {
- $vlan_status['tagged_vlan'][$vlan_id][$port] = '0';
- }
- }
- }
- }
- }
- }
- foreach ($port_status as &$port) {
- if (!is_array($port)) {
- continue;
- }
- $port['untagged'] = preg_replace('/^,/', '', $port['untagged']);
- $port['tagged'] = preg_replace('/^,/', '', $port['tagged']);
- }
- unset($port);
- return $port_status;
- }
- function get_port_vlan($vendor, $port, $port_index, $ip, $snmp)
- {
- if (!isset($port_index)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- //default - default port index
- $port_oid = dot1qPortVlanEntry . "." . $port_index;
- //tplink
- if ($vendor == 69) {
- $port_oid = TPLINK_dot1qPortVlanEntry . "." . $port_index;
- }
- //huawei
- if ($vendor == 3) {
- $port_oid = dot1qPortVlanEntry . "." . $port;
- }
- //allied telesys
- if ($vendor == 8) {
- $port_oid = dot1qPortVlanEntry . "." . $port;
- }
- $port_vlan = get_snmp($ip, $snmp, $port_oid);
- $port_vlan = preg_replace('/.*\:/', '', $port_vlan);
- $port_vlan = intval(trim($port_vlan));
- return $port_vlan;
- }
- function get_ports_poe_state($vendor_id, $ip, $snmp)
- {
- if (!isset($vendor_id)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- // default poe oid
- $poe_status = PETH_PSE_PORT_ADMIN_ENABLE;
- if ($vendor_id == 3) {
- $poe_status = HUAWEI_POE_OID;
- }
- if ($vendor_id == 6) {
- $poe_status = SNR_POE_OID;
- }
- if ($vendor_id == 8) {
- $poe_status = ALLIED_POE_OID;
- }
- if ($vendor_id == 9) {
- $poe_status = MIKROTIK_POE_OID;
- }
- if ($vendor_id == 10) {
- $poe_status = NETGEAR_POE_OID;
- }
- if ($vendor_id == 15) {
- $poe_status = HP_POE_OID;
- }
- if ($vendor_id == 69) {
- $poe_status = TPLINK_POE_OID;
- }
- $result = [];
- $c_state = walk_snmp($ip, $snmp, $poe_status);
- if (isset($c_state) and !empty($c_state)) {
- foreach ($c_state as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port] = $value;
- // patch for mikrotik
- if ($vendor_id == 9) {
- if ($value == 1) {
- $result[$port] = 2;
- }
- if ($value > 1) {
- $result[$port] = 1;
- }
- }
- //patch for tplink
- if ($vendor_id == 69) {
- if ($value == 0) {
- $result[$port] = 2;
- }
- if ($value >= 1) {
- $result[$port] = 1;
- }
- }
- }
- }
- }
- return $result;
- }
- function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $snmp)
- {
- if (!isset($port)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- // default poe oid
- $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
- if ($vendor_id == 3) {
- $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 6) {
- $poe_status = SNR_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 8) {
- $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 15) {
- $poe_status = HP_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 9) {
- $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 10) {
- $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 69) {
- $poe_status = TPLINK_POE_OID . "." . $port;
- }
- $result = '';
- $c_state = get_snmp($ip, $snmp, $poe_status);
- if (isset($c_state) and !empty($c_state)) {
- $p_state = parse_snmp_value($c_state);
- if (empty($p_state)) {
- return NULL;
- }
- // patch for mikrotik
- if ($vendor_id == 9) {
- if ($p_state == 1) {
- return 2;
- }
- if ($p_state > 1) {
- return 1;
- }
- }
- //patch for tplink
- if ($vendor_id == 69) {
- if ($p_state == 0) {
- return 2;
- }
- if ($p_state >= 1) {
- return 1;
- }
- }
- return $p_state;
- }
- return NULL;
- }
- function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $snmp, $state)
- {
- if (!isset($ip)) {
- return;
- }
- //default poe status
- $poe_enable = 1;
- $poe_disable = 2;
- // default poe oid
- $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
- if ($vendor_id == 3) {
- $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 8) {
- $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 15) {
- $poe_status = HP_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 10) {
- $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
- }
- if ($vendor_id == 69) {
- $poe_status = TPLINK_POE_OID . "." . $port;
- $poe_enable = 1;
- $poe_disable = 0;
- }
- if ($state) {
- // enable port
- $c_state = set_snmp($ip, $snmp, $poe_status, 'i', $poe_enable);
- return $c_state;
- } else {
- // disable port
- $c_state = set_snmp($ip, $snmp, $poe_status, 'i', $poe_disable);
- return $c_state;
- }
- }
- function get_ports_poe_detail($vendor_id, $ip, $snmp)
- {
- if (!isset($vendor_id)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- $result = [];
- $poe_class = PETH_PSE_PORT_POE_CLASS;
- // eltex
- if ($vendor_id == 2) {
- $poe_power = ELTEX_POE_USAGE;
- $poe_current = ELTEX_POE_CURRENT;
- $poe_volt = ELTEX_POE_VOLT;
- }
- // huawei
- if ($vendor_id == 3) {
- $poe_power = HUAWEI_POE_USAGE;
- $poe_current = HUAWEI_POE_CURRENT;
- $poe_volt = HUAWEI_POE_VOLT;
- }
- // snr
- if ($vendor_id == 6) {
- $poe_class = SNR_POE_CLASS;
- $poe_power = SNR_POE_USAGE;
- $poe_current = SNR_POE_CURRENT;
- $poe_volt = SNR_POE_VOLT;
- }
- // AT
- if ($vendor_id == 8) {
- $poe_power = ALLIED_POE_USAGE;
- $poe_current = ALLIED_POE_CURRENT;
- $poe_volt = ALLIED_POE_VOLT;
- }
- // mikrotik
- if ($vendor_id == 9) {
- $poe_power = MIKROTIK_POE_USAGE;
- $poe_current = MIKROTIK_POE_CURRENT;
- $poe_volt = MIKROTIK_POE_VOLT;
- }
- // netgear
- if ($vendor_id == 10) {
- $poe_power = NETGEAR_POE_USAGE;
- $poe_current = NETGEAR_POE_CURRENT;
- $poe_volt = NETGEAR_POE_VOLT;
- }
- // HP
- if ($vendor_id == 15) {
- $poe_power = HP_POE_USAGE;
- $poe_volt = HP_POE_VOLT;
- }
- // TP-Link
- if ($vendor_id == 69) {
- $poe_power = TPLINK_POE_USAGE;
- $poe_current = TPLINK_POE_CURRENT;
- $poe_volt = TPLINK_POE_VOLT;
- $poe_class = TPLINK_POE_CLASS;
- }
- if (isset($poe_power)) {
- $c_power = walk_snmp($ip, $snmp, $poe_power);
- if (isset($c_power)) {
- foreach ($c_power as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $p_power = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['power'] = 0;
- $result[$port]['power_display'] = '';
- switch ($vendor_id) {
- case 9: //mikrotik
- $p_power = round($p_power / 10, 2);
- break;
- case 69: //tplink
- $p_power = round($p_power / 10, 2);
- break;
- default:
- $p_power = round($p_power / 1000, 2);
- break;
- }
- if ($p_power > 0) {
- $result[$port]['power'] = $p_power;
- $result[$port]['power_display'] = 'P: ' . $p_power . ' W';
- }
- }
- }
- }
- }
- if (isset($poe_current)) {
- $c_current = walk_snmp($ip, $snmp, $poe_current);
- if (isset($c_current)) {
- foreach ($c_current as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $p_current = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['current'] = 0;
- $result[$port]['current_display'] = '';
- if ($p_current > 0) {
- $result[$port]['current'] = $p_current;
- $result[$port]['current_display'] = 'C: ' . $p_current . ' mA';
- }
- }
- }
- }
- }
- if (isset($poe_volt)) {
- $c_volt = walk_snmp($ip, $snmp, $poe_volt);
- if (isset($c_volt)) {
- foreach ($c_volt as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $p_volt = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['volt'] = 0;
- $result[$port]['volt_display'] = '';
- switch ($vendor_id) {
- case 2:
- case 8:
- $p_volt = round($p_volt / 1000, 2);
- break;
- case 9:
- case 69:
- $p_volt = round($p_volt / 10, 2);
- break;
- case 15:
- $p_volt = round($p_volt / 100, 2);
- break;
- }
- if ($p_volt > 0 and $result[$port]['power'] > 0) {
- $result[$port]['volt'] = $p_volt;
- $result[$port]['volt_display'] = ' V: ' . $p_volt . " V";
- }
- }
- }
- }
- }
- if (isset($poe_class)) {
- $c_class = walk_snmp($ip, $snmp, $poe_class);
- if (isset($c_class)) {
- foreach ($c_class as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $p_class = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['class'] = 0;
- $result[$port]['class_display'] = '';
- switch ($vendor_id) {
- case 69:
- if ($p_class > 0 and $result[$port]['power'] > 0) {
- if ($p_class == 7) {
- $p_class = 'class-not-defined';
- }
- $result[$port]['class_display'] = 'Class: ' . $p_class;
- $result[$port]['class'] = $p_class;
- }
- break;
- default:
- if ($p_class > 0 and $result[$port]['power'] > 0) {
- $result[$port]['class_display'] = 'Class: ' . ($p_class - 1);
- $result[$port]['class'] = $p_class - 1;
- }
- break;
- }
- }
- }
- }
- }
- foreach ($result as &$port) {
- if (!isset($port['power'])) {
- $port['power'] = 0;
- }
- if (!isset($port['current'])) {
- $port['current'] = 0;
- }
- if (!isset($port['volt'])) {
- $port['volt'] = 0;
- }
- if (!isset($port['class'])) {
- $port['class'] = 0;
- }
- }
- unset($port);
- return $result;
- }
- function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $snmp)
- {
- if (!isset($port) or !isset($port_snmp_index)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- if (!isset($community)) {
- $community = 'public';
- }
- if (!isset($version)) {
- $version = '2';
- }
- $result = '';
- $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
- // eltex
- if ($vendor_id == 2) {
- $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = ELTEX_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
- }
- // huawei
- if ($vendor_id == 3) {
- $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = HUAWEI_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
- }
- // snr
- if ($vendor_id == 6) {
- $poe_class = SNR_POE_CLASS . '.' . $port_snmp_index;
- $poe_power = SNR_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = SNR_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = SNR_POE_VOLT . '.' . $port_snmp_index;
- }
- // AT
- if ($vendor_id == 8) {
- $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = ALLIED_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
- }
- // mikrotik
- if ($vendor_id == 9) {
- $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
- }
- // netgear
- if ($vendor_id == 10) {
- $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
- $poe_current = NETGEAR_POE_CURRENT . '.' . $port_snmp_index;
- $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
- }
- // HP
- if ($vendor_id == 15) {
- $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
- $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
- }
- // TP-Link
- if ($vendor_id == 69) {
- $poe_power = TPLINK_POE_USAGE . '.' . $port;
- $poe_current = TPLINK_POE_CURRENT . '.' . $port;
- $poe_volt = TPLINK_POE_VOLT . '.' . $port;
- $poe_class = TPLINK_POE_CLASS . "." . $port;
- }
- if (isset($poe_power)) {
- $c_power = get_snmp($ip, $snmp, $poe_power);
- if (isset($c_power)) {
- $p_power = parse_snmp_value($c_power);
- switch ($vendor_id) {
- case 9:
- $p_power = round($p_power / 10, 2);
- break;
- case 69:
- $p_power = round($p_power / 10, 2);
- break;
- default:
- $p_power = round($p_power / 1000, 2);
- break;
- }
- if ($p_power > 0) {
- $result .= ' P: ' . $p_power . ' W';
- }
- }
- }
- if (isset($poe_current)) {
- $c_current = get_snmp($ip, $snmp, $poe_current);
- if (isset($c_current)) {
- $p_current = parse_snmp_value($c_current);
- if ($p_current > 0) {
- $result .= ' C: ' . $p_current . ' mA';
- }
- }
- }
- if (isset($poe_volt)) {
- $c_volt = get_snmp($ip, $snmp, $poe_volt);
- if (isset($c_volt)) {
- $p_volt = parse_snmp_value($c_volt);
- switch ($vendor_id) {
- case 2:
- case 8:
- $p_volt = round($p_volt / 1000, 2);
- break;
- case 9:
- case 69:
- $p_volt = round($p_volt / 10, 2);
- break;
- case 15:
- $p_volt = round($p_volt / 100, 2);
- break;
- }
- if ($p_volt > 0 and $p_power > 0) {
- $result .= ' V: ' . $p_volt . " V";
- }
- }
- }
- if (isset($poe_class)) {
- $c_class = get_snmp($ip, $snmp, $poe_class);
- if (isset($c_class)) {
- $p_class = parse_snmp_value($c_class);
- switch ($vendor_id) {
- case 69:
- if ($p_class > 0 and $p_power > 0) {
- if ($p_class == 7) {
- $p_class = 'class-not-defined';
- }
- $result .= ' Class: ' . $p_class;
- }
- break;
- default:
- if ($p_class > 0 and $p_power > 0) {
- $result .= ' Class: ' . ($p_class - 1);
- }
- break;
- }
- }
- }
- return $result;
- }
- function get_snmp($ip, $snmp, $oid)
- {
- snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
- $result = NULL;
- try {
- $version = $snmp["version"];
- if ($version == 3) {
- $result = snmp3_get($ip, $snmp["ro-user"], 'authPriv', $snmp['auth-proto'], $snmp['ro-password'], $snmp["priv-proto"], $snmp["ro-password"], $oid, SNMP_timeout, SNMP_retry);
- }
- if ($version == 2) {
- $result = snmp2_get($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
- }
- if ($version == 1) {
- $result = snmpget($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
- }
- if (empty($result)) {
- $result = NULL;
- }
- } catch (Exception $e) {
- // echo 'Caught exception: ', $e->getMessage(), "\n";
- $result = NULL;
- }
- return $result;
- }
- function set_snmp($ip, $snmp, $oid, $field, $value)
- {
- $result = false;
- try {
- $version = $snmp["version"];
- if ($version == 3) {
- $result = snmp3_set($ip, $snmp["rw-user"], 'authPriv', $snmp['auth-proto'], $snmp['rw-password'], $snmp["priv-proto"], $snmp["rw-password"], $oid, $field, $value, SNMP_timeout, SNMP_retry);
- }
- if ($version == 2) {
- $result = snmp2_set($ip, $snmp["rw-community"], $oid, $field, $value, SNMP_timeout, SNMP_retry);
- }
- if ($version == 1) {
- $result = snmpset($ip, $snmp["rw-community"], $oid, $field, $value, SNMP_timeout, SNMP_retry);
- }
- } catch (Exception $e) {
- // echo 'Caught exception: ', $e->getMessage(), "\n";
- $result = false;
- }
- return $result;
- }
- function set_port_state($vendor_id, $port, $ip, $snmp, $state)
- {
- // port -> snmp_index!!!
- if (!isset($port)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- $port_status = PORT_ADMIN_STATUS_OID . '.' . $port;
- if ($state == 1) {
- // enable port
- $c_state = set_snmp($ip, $snmp, $port_status, 'i', 1);
- return $c_state;
- } else {
- // disable port
- $c_state = set_snmp($ip, $snmp, $port_status, 'i', 2);
- return $c_state;
- }
- }
- function get_ports_state_detail($ip, $snmp)
- {
- if (!isset($ip)) {
- return;
- }
- $result = [];
- //post status
- $p_state = walk_snmp($ip, $snmp, PORT_STATUS_OID);
- if (!empty($p_state)) {
- foreach ($p_state as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['status'] = $value;
- $result[$port]['admin_status'] = 0;
- $result[$port]['speed'] = 0;
- $result[$port]['errors'] = 0;
- }
- }
- }
- //admin state
- $p_admin = walk_snmp($ip, $snmp, PORT_ADMIN_STATUS_OID);
- if (!empty($p_admin)) {
- foreach ($p_admin as $key => $value) {
- if (empty($value)) {
- $value = '';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['admin_status'] = $value;
- }
- }
- }
- //port speed
- $p_speed = walk_snmp($ip, $snmp, PORT_SPEED_OID);
- if (!empty($p_speed)) {
- foreach ($p_speed as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['speed'] = $value;
- }
- }
- }
- //errors at
- $p_errors = walk_snmp($ip, $snmp, PORT_ERRORS_OID);
- if (!empty($p_errors)) {
- foreach ($p_errors as $key => $value) {
- if (empty($value)) {
- $value = 'INT:0';
- }
- $key = trim($key);
- $value = parse_snmp_value($value);
- $port = NULL;
- if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
- $port = preg_replace('/^\./', '', $matches[0]);
- $result[$port]['errors'] = $value;
- }
- }
- }
- return $result;
- }
- function get_port_state_detail($port, $ip, $snmp)
- {
- if (!isset($port)) {
- return;
- }
- if (!isset($ip)) {
- return;
- }
- // if (!is_up($ip)) { return; }
- $oper = PORT_STATUS_OID . '.' . $port;
- $admin = PORT_ADMIN_STATUS_OID . '.' . $port;
- $speed = PORT_SPEED_OID . '.' . $port;
- $errors = PORT_ERRORS_OID . '.' . $port;
- $result = '';
- $c_state = get_snmp($ip, $snmp, $oper);
- $p_state = parse_snmp_value($c_state);
- $c_admin = get_snmp($ip, $snmp, $admin);
- $p_admin = parse_snmp_value($c_admin);
- if ($p_state == 1) {
- $c_speed = get_snmp($ip, $snmp, $speed);
- } else {
- $c_speed = 'INT:0';
- }
- $p_speed = parse_snmp_value($c_speed);
- $c_errors = get_snmp($ip, $snmp, $errors);
- $p_errors = parse_snmp_value($c_errors);
- $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
- return $result;
- }
- function parse_snmp_value($value)
- {
- if (empty($value)) {
- return '';
- }
- if (!preg_match('/:/', $value)) {
- return '';
- }
- list($p_type, $p_value) = explode(':', $value);
- if (empty($p_value)) {
- return '';
- }
- $p_value = trim($p_value);
- $p_value = preg_replace('/^\"/', '', $p_value);
- $p_value = preg_replace('/\"$/', '', $p_value);
- $p_value = preg_replace('/^NULL$/', '', $p_value);
- $p_value = trim($p_value);
- return $p_value;
- }
- snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
- snmp_set_enum_print(1);
|