| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709 |
- <?php
- if (!defined("CONFIG")) die("Not defined");
- require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
- function get_ifmib_index_table($ip, $snmp)
- {
- $ifmib_map = NULL;
- $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)
- {
- if (!isset($ip)) {
- return;
- }
- if (!isset($oid)) {
- return;
- }
- $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
- function getIpAdEntIfIndex($db, $ip, $snmp)
- {
- if (!isset($ip)) {
- return;
- }
- #oid+ip = index
- $ip_table = walk_snmp($ip, $snmp, ipAdEntIfIndex);
- #oid+index=name
- $int_table = walk_snmp($ip, $snmp, ifDescr);
- $result = [];
- if (isset($ip_table) and gettype($ip_table) == 'array' and count($ip_table) > 0) {
- foreach ($ip_table as $key => $value) {
- if (empty($value)) {
- continue;
- }
- if (empty($key)) {
- continue;
- }
- $key = trim($key);
- $interface_index = intval(trim(str_replace('INTEGER:', '', $value)));
- if (empty($value)) {
- continue;
- }
- $interface_name = $int_table[ifDescr . '.' . $interface_index];
- $interface_name = trim(str_replace('STRING:', '', $interface_name));
- $interface_ip = trim(str_replace(ipAdEntIfIndex . '.', '', $key));
- if (empty($interface_name)) {
- continue;
- }
- $result[$interface_index]['ip'] = $interface_ip;
- $result[$interface_index]['index'] = $interface_index;
- $result[$interface_index]['name'] = $interface_name;
- //type: 0 - local, 1 - WAN
- $result[$interface_index]['type'] = 1;
- if (is_our_network($db, $interface_ip)) {
- $result[$interface_index]['type'] = 0;
- }
- }
- }
- return $result;
- }
- #get mac table by analyze all available tables
- function get_fdb_table($ip, $snmp)
- {
- if (!isset($ip)) {
- return;
- }
- $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 (!isset($fdb_table) or empty($fdb_table) or count($fdb_table) == 0) {
- $vlan_table = walk_snmp($ip, $snmp, CISCO_VLAN_OID);
- if (empty($vlan_table)) {
- return;
- }
- 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 check_snmp_access($ip, $snmp)
- {
- if (!isset($ip)) {
- return;
- }
- #check host up
- $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
- if (empty($status)) {
- 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) and $temp > 0.1) {
- $status .= 'Temp: ' . $temp . " C";
- }
- if (!empty($sfp_status_volt) and $volt > 0.1) {
- $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
- }
- if (!empty($sfp_status_circut) and $circut > 0.1) {
- $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
- }
- if (!empty($sfp_status_tx) and $tx > 0.1) {
- $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
- }
- if (!empty($sfp_status_rx) and $rx > 0.1) {
- $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($sfp_status_volt) and $volt > 0.1) {
- $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
- }
- if (!empty($sfp_status_circut) and $circut > 0.1) {
- $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
- }
- if (!empty($sfp_status_tx) and $tx > 0.1) {
- $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
- }
- if (!empty($sfp_status_rx) and $rx > 0.1) {
- $status .= ' Rx: ' . round($rx / 1000, 2) . ' 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) and $temp > 0) {
- $status .= 'Temp: ' . $temp . " C";
- }
- if (!empty($volt) and $volt > 0) {
- $status .= ' Volt: ' . $volt . ' V';
- }
- if (!empty($circut) and $circut > 0) {
- $status .= ' Circut: ' . $circut . ' mA';
- }
- if (!empty($tx) and abs($tx) > 0.1) {
- $status .= ' Tx: ' . $tx . ' dBm';
- }
- if (!empty($rx) and abs($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 ($volt > 0) {
- $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
- }
- if (!empty($circut) and $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 (isset($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;
- }
- 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 NULL;
- }
- if (!preg_match('/:/', $value)) {
- return NULL;
- }
- list($p_type, $p_value) = explode(':', $value);
- if (empty($p_value)) {
- return NULL;
- }
- $p_value = trim($p_value);
- $p_value = preg_replace('/^\"/', '', $p_value);
- $p_value = preg_replace('/\"$/', '', $p_value);
- $p_value = trim($p_value);
- return $p_value;
- }
- snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
- snmp_set_enum_print(1);
|