snmp.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. <?php
  2. if (!defined("CONFIG")) die("Not defined");
  3. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
  4. function get_ifmib_index_table($ip, $snmp)
  5. {
  6. $ifmib_map = NULL;
  7. $is_mikrotik = walk_snmp($ip, $snmp, MIKROTIK_DHCP_SERVER);
  8. $mk_ros_version = 0;
  9. if ($is_mikrotik) {
  10. $mikrotik_version = walk_snmp($ip, $snmp, MIKROTIK_ROS_VERSION);
  11. $mk_ros_version = 6491;
  12. $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/', $mikrotik_version[MIKROTIK_ROS_VERSION], $matches);
  13. if ($result) {
  14. $mk_ros_version = $matches[1] * 1000 + $matches[2] * 10 + $matches[3];
  15. }
  16. }
  17. if ($mk_ros_version == 0 or $mk_ros_version > 6468) {
  18. #fdb_index => snmp_index
  19. $index_map_table = walk_snmp($ip, $snmp, IFMIB_IFINDEX_MAP);
  20. #get map snmp interfaces to fdb table
  21. if (isset($index_map_table) and count($index_map_table) > 0) {
  22. foreach ($index_map_table as $key => $value) {
  23. $key = trim($key);
  24. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  25. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  26. if ($result) {
  27. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  28. $ifmib_map[$fdb_index] = $value;
  29. }
  30. }
  31. }
  32. }
  33. #return simple map snmp_port_index = snmp_port_index
  34. if (empty($ifmib_map)) {
  35. #ifindex
  36. $index_table = walk_snmp($ip, $snmp, IFMIB_IFINDEX);
  37. if (isset($index_table) and count($index_table) > 0) {
  38. foreach ($index_table as $key => $value) {
  39. $key = trim($key);
  40. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  41. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  42. if ($result) {
  43. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  44. $ifmib_map[$fdb_index] = $value;
  45. }
  46. }
  47. }
  48. }
  49. return $ifmib_map;
  50. }
  51. #get mac table by selected snmp oid
  52. function get_mac_table($ip, $snmp, $oid, $index_map)
  53. {
  54. if (!isset($ip)) {
  55. return;
  56. }
  57. if (!isset($oid)) {
  58. return;
  59. }
  60. $mac_table = walk_snmp($ip, $snmp, $oid);
  61. if (isset($mac_table) and gettype($mac_table) == 'array' and count($mac_table) > 0) {
  62. foreach ($mac_table as $key => $value) {
  63. if (empty($value)) {
  64. continue;
  65. }
  66. if (empty($key)) {
  67. continue;
  68. }
  69. $key = trim($key);
  70. $value_raw = intval(trim(str_replace('INTEGER:', '', $value)));
  71. if (empty($value_raw)) {
  72. continue;
  73. }
  74. if (!empty($index_map)) {
  75. if (empty($index_map[$value_raw])) {
  76. $value = $value_raw;
  77. } else {
  78. $value = $index_map[$value_raw];
  79. }
  80. } else {
  81. $value = $value_raw;
  82. }
  83. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  84. $result = preg_match($pattern, $key, $matches);
  85. if (!empty($result)) {
  86. $mac_key = preg_replace('/^\./', '', $matches[0]);
  87. $fdb_table[$mac_key] = $value;
  88. }
  89. }
  90. }
  91. return $fdb_table;
  92. }
  93. #get ip interfaces
  94. function getIpAdEntIfIndex($db, $ip, $snmp)
  95. {
  96. if (!isset($ip)) {
  97. return;
  98. }
  99. #oid+ip = index
  100. $ip_table = walk_snmp($ip, $snmp, ipAdEntIfIndex);
  101. #oid+index=name
  102. $int_table = walk_snmp($ip, $snmp, ifDescr);
  103. $result = [];
  104. if (isset($ip_table) and gettype($ip_table) == 'array' and count($ip_table) > 0) {
  105. foreach ($ip_table as $key => $value) {
  106. if (empty($value)) {
  107. continue;
  108. }
  109. if (empty($key)) {
  110. continue;
  111. }
  112. $key = trim($key);
  113. $interface_index = intval(trim(str_replace('INTEGER:', '', $value)));
  114. if (empty($value)) {
  115. continue;
  116. }
  117. $interface_name = $int_table[ifDescr . '.' . $interface_index];
  118. $interface_name = trim(str_replace('STRING:', '', $interface_name));
  119. $interface_ip = trim(str_replace(ipAdEntIfIndex . '.', '', $key));
  120. if (empty($interface_name)) {
  121. continue;
  122. }
  123. $result[$interface_index]['ip'] = $interface_ip;
  124. $result[$interface_index]['index'] = $interface_index;
  125. $result[$interface_index]['name'] = $interface_name;
  126. //type: 0 - local, 1 - WAN
  127. $result[$interface_index]['type'] = 1;
  128. if (is_our_network($db, $interface_ip)) {
  129. $result[$interface_index]['type'] = 0;
  130. }
  131. }
  132. }
  133. return $result;
  134. }
  135. #get mac table by analyze all available tables
  136. function get_fdb_table($ip, $snmp)
  137. {
  138. if (!isset($ip)) {
  139. return;
  140. }
  141. $ifindex_map = get_ifmib_index_table($ip, $snmp);
  142. $fdb1_table = get_mac_table($ip, $snmp, MAC_TABLE_OID, $ifindex_map);
  143. if (!empty($fdb1_table)) {
  144. $fdb_table = $fdb1_table;
  145. } else {
  146. $fdb2_table = get_mac_table($ip, $snmp, MAC_TABLE_OID2, $ifindex_map);
  147. if (!empty($fdb2_table)) {
  148. $fdb_table = $fdb2_table;
  149. }
  150. }
  151. $snmp_cisco = $snmp;
  152. // maybe cisco?!
  153. if (!isset($fdb_table) or empty($fdb_table) or count($fdb_table) == 0) {
  154. $vlan_table = walk_snmp($ip, $snmp, CISCO_VLAN_OID);
  155. if (empty($vlan_table)) {
  156. return;
  157. }
  158. foreach ($vlan_table as $vlan_oid => $value) {
  159. if (empty($vlan_oid)) {
  160. continue;
  161. }
  162. $pattern = '/\.(\d{1,4})$/';
  163. $result = preg_match($pattern, $vlan_oid, $matches);
  164. if (!empty($result)) {
  165. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  166. if ($vlan_id > 1000 and $vlan_id < 1009) {
  167. continue;
  168. }
  169. $snmp_cisco["ro-community"] = $snmp["ro-community"] . '@' . $vlan_id;
  170. $fdb_vlan_table = get_mac_table($ip, $snmp_cisco, MAC_TABLE_OID, $ifindex_map);
  171. if (!isset($fdb_vlan_table) or !$fdb_vlan_table or count($fdb_vlan_table) == 0) {
  172. $fdb_vlan_table = get_mac_table($ip, $snmp_cisco, MAC_TABLE_OID2, $ifindex_map);
  173. }
  174. foreach ($fdb_vlan_table as $mac => $port) {
  175. if (!isset($mac)) {
  176. continue;
  177. }
  178. $fdb_table[$mac] = $port;
  179. }
  180. }
  181. }
  182. }
  183. return $fdb_table;
  184. }
  185. function check_snmp_access($ip, $snmp)
  186. {
  187. if (!isset($ip)) {
  188. return;
  189. }
  190. #check host up
  191. $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
  192. if (empty($status)) {
  193. return;
  194. }
  195. #check snmp
  196. $result = get_snmp($ip, $snmp, SYS_DESCR_MIB);
  197. if (empty($result)) {
  198. return;
  199. }
  200. return 1;
  201. }
  202. function get_port_state($port, $ip, $snmp)
  203. {
  204. if (!isset($port)) {
  205. return;
  206. }
  207. if (!isset($ip)) {
  208. return;
  209. }
  210. $port_oid = PORT_STATUS_OID . '.' . $port;
  211. $port_state = get_snmp($ip, $snmp, $port_oid);
  212. return $port_state;
  213. }
  214. function get_last_digit($oid)
  215. {
  216. if (!isset($oid)) {
  217. return;
  218. }
  219. $pattern = '/\.(\d{1,})$/';
  220. preg_match($pattern, $oid, $matches);
  221. return $matches[1];
  222. }
  223. function get_cisco_sensors($ip, $snmp, $mkey)
  224. {
  225. $index = get_last_digit($mkey);
  226. $result = parse_snmp_value(get_snmp($ip, $snmp, CISCO_SFP_SENSORS . "." . $index));
  227. $prec = parse_snmp_value(get_snmp($ip, $snmp, CISCO_SFP_PRECISION . "." . $index));
  228. if (!isset($prec)) {
  229. $prec = 1;
  230. }
  231. $result = round(trim($result) / (10 * $prec), 2);
  232. return $result;
  233. }
  234. function get_snmp_ifname($ip, $snmp, $port)
  235. {
  236. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
  237. if (empty($port_name)) {
  238. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
  239. }
  240. if (empty($port_name)) {
  241. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFALIAS . "." . $port));
  242. }
  243. return $port_name;
  244. }
  245. function get_snmp_interfaces($ip, $snmp)
  246. {
  247. $result = [];
  248. $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFNAME);
  249. if (empty($ifmib_list)) {
  250. $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFDESCR);
  251. }
  252. if (empty($ifmib_list)) {
  253. $ifmib_list = walk_snmp($ip, $snmp, IFMIB_IFALIAS);
  254. }
  255. if (!empty($ifmib_list)) {
  256. foreach ($ifmib_list as $key => $value) {
  257. $key = trim($key);
  258. $value = parse_snmp_value($value);
  259. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  260. $int_index = preg_replace('/^\./', '', $matches[0]);
  261. $result[$int_index] = $value;
  262. }
  263. }
  264. }
  265. return $result;
  266. }
  267. function walk_snmp($ip, $snmp, $oid)
  268. {
  269. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  270. $result = NULL;
  271. $version = $snmp["version"];
  272. if ($version == 3) {
  273. $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);
  274. }
  275. if ($version == 2) {
  276. $result = snmp2_real_walk($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
  277. }
  278. if ($version == 1) {
  279. $result = snmprealwalk($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
  280. }
  281. return $result;
  282. }
  283. function getSnmpAccess($device)
  284. {
  285. $result['port'] = 161;
  286. $result['version'] = $device['snmp_version'];
  287. $result['ro-community'] = $device['community'];
  288. $result['rw-community'] = $device['rw_community'];
  289. #snmpv3
  290. $result['auth-proto'] = $device['snmp3_auth_proto'];
  291. $result['priv-proto'] = $device['snmp3_priv_proto'];
  292. $result['ro-user'] = $device['snmp3_user_ro'];
  293. $result['rw-user'] = $device['snmp3_user_rw'];
  294. $result['ro-password'] = $device['snmp3_user_ro_password'];
  295. $result['rw-password'] = $device['snmp3_user_rw_password'];
  296. return $result;
  297. }
  298. function get_snmp_module_id($modules_oids, $port_name)
  299. {
  300. $port_name = preg_quote(trim($port_name), '/');
  301. foreach ($modules_oids as $key => $value) {
  302. $pattern = '/' . $port_name . '/i';
  303. preg_match($pattern, $value, $matches);
  304. if (isset($matches[0])) {
  305. $module_id = get_last_digit($key);
  306. return $module_id;
  307. }
  308. }
  309. return;
  310. }
  311. function get_sfp_status($vendor_id, $port, $ip, $snmp, $modules_oids)
  312. {
  313. if (!isset($vendor_id)) {
  314. return;
  315. }
  316. if (!isset($port)) {
  317. return;
  318. }
  319. if (!isset($ip)) {
  320. return;
  321. }
  322. try {
  323. $status = '';
  324. // eltex
  325. if ($vendor_id == 2) {
  326. $sfp_vendor = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_VENDOR . "." . $port));
  327. if (!empty($sfp_vendor)) {
  328. $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
  329. $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
  330. $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
  331. $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
  332. $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
  333. $temp = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_temp));
  334. $volt = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_volt));
  335. $circut = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_circut));
  336. $tx = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_tx));
  337. $rx = parse_snmp_value(get_snmp($ip, $snmp, $sfp_status_rx));
  338. $sfp_sn = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_SN . "." . $port));
  339. $sfp_freq = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_FREQ . "." . $port));
  340. if (!isset($sfp_freq) or $sfp_freq == 65535) {
  341. $sfp_freq = 'unspecified';
  342. }
  343. $sfp_length = parse_snmp_value(get_snmp($ip, $snmp, ELTEX_SFP_LENGTH . "." . $port));
  344. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  345. if (!empty($sfp_status_temp) and $temp > 0.1) {
  346. $status .= 'Temp: ' . $temp . " C";
  347. }
  348. if (!empty($sfp_status_volt) and $volt > 0.1) {
  349. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  350. }
  351. if (!empty($sfp_status_circut) and $circut > 0.1) {
  352. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  353. }
  354. if (!empty($sfp_status_tx) and $tx > 0.1) {
  355. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  356. }
  357. if (!empty($sfp_status_rx) and $rx > 0.1) {
  358. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  359. }
  360. $status .= '<br>';
  361. return $status;
  362. }
  363. return;
  364. }
  365. // snr
  366. if ($vendor_id == 6) {
  367. $sfp_vendor = parse_snmp_value(get_snmp($ip, $snmp, SNR_SFP_VendorName . "." . $port));
  368. if (!empty($sfp_vendor) and $sfp_vendor != 'NULL') {
  369. $oid_sfp_model_name = SNR_SFP_ModelName . "." . $port;
  370. $oid_sfp_type_name = SNR_SFP_TypeName . "." . $port;
  371. $oid_sfp_bitrate = SNR_SFP_BitRate . "." . $port;
  372. $oid_sfp_status_volt = SNR_SFP_VOLT . "." . $port;
  373. $oid_sfp_status_circut = SNR_SFP_BIAS . "." . $port;
  374. $oid_sfp_status_tx = SNR_SFP_TX . "." . $port;
  375. $oid_sfp_status_rx = SNR_SFP_RX . "." . $port;
  376. $oid_sfp_length = SNR_SFP_WaveLength . "." . $port;
  377. $volt = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_volt));
  378. $circut = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_circut));
  379. $tx = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_tx));
  380. $rx = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_status_rx));
  381. $sfp_model_name = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_model_name));
  382. $sfp_type_name = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_type_name));
  383. $sfp_freq = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_bitrate));
  384. $sfp_length = parse_snmp_value(get_snmp($ip, $snmp, $oid_sfp_length));
  385. $status = 'Vendor: ' . $sfp_vendor . ' ' . $sfp_model_name . ' ' . $sfp_type_name . ' Speed: ' . $sfp_freq . ' Freq: ' . $sfp_length . '<br>';
  386. if (!empty($sfp_status_volt) and $volt > 0.1) {
  387. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  388. }
  389. if (!empty($sfp_status_circut) and $circut > 0.1) {
  390. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  391. }
  392. if (!empty($sfp_status_tx) and $tx > 0.1) {
  393. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  394. }
  395. if (!empty($sfp_status_rx) and $rx > 0.1) {
  396. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  397. }
  398. $status .= '<br>';
  399. return $status;
  400. }
  401. return;
  402. }
  403. // cisco
  404. if ($vendor_id == 16) {
  405. // get interface names
  406. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
  407. if (empty($port_name)) {
  408. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
  409. }
  410. // search module indexes
  411. $port_name = preg_quote(trim($port_name), '/');
  412. foreach ($modules_oids as $key => $value) {
  413. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  414. preg_match($pattern, $value, $matches);
  415. if (isset($matches[0])) {
  416. $temp = get_cisco_sensors($ip, $snmp, $key);
  417. continue;
  418. }
  419. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  420. preg_match($pattern, $value, $matches);
  421. if (isset($matches[0])) {
  422. $volt = get_cisco_sensors($ip, $snmp, $key);
  423. continue;
  424. }
  425. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  426. preg_match($pattern, $value, $matches);
  427. if (isset($matches[0])) {
  428. $circut = get_cisco_sensors($ip, $snmp, $key);
  429. continue;
  430. }
  431. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  432. preg_match($pattern, $value, $matches);
  433. if (isset($matches[0])) {
  434. $tx = get_cisco_sensors($ip, $snmp, $key);
  435. continue;
  436. }
  437. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  438. preg_match($pattern, $value, $matches);
  439. if (isset($matches[0])) {
  440. $rx = get_cisco_sensors($ip, $snmp, $key);
  441. continue;
  442. }
  443. }
  444. if (!empty($temp) and $temp > 0) {
  445. $status .= 'Temp: ' . $temp . " C";
  446. }
  447. if (!empty($volt) and $volt > 0) {
  448. $status .= ' Volt: ' . $volt . ' V';
  449. }
  450. if (!empty($circut) and $circut > 0) {
  451. $status .= ' Circut: ' . $circut . ' mA';
  452. }
  453. if (!empty($tx) and abs($tx) > 0.1) {
  454. $status .= ' Tx: ' . $tx . ' dBm';
  455. }
  456. if (!empty($rx) and abs($rx) > 0.1) {
  457. $status .= ' Rx: ' . $rx . ' dBm';
  458. }
  459. if (!empty($status)) {
  460. $status = preg_replace('/"/', '', $status);
  461. $status .= '<br>';
  462. }
  463. return $status;
  464. }
  465. // huawei
  466. if ($vendor_id == 3) {
  467. // get interface names
  468. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFNAME . "." . $port));
  469. if (empty($port_name)) {
  470. $port_name = parse_snmp_value(get_snmp($ip, $snmp, IFMIB_IFDESCR . "." . $port));
  471. }
  472. // search module indexes
  473. $port_name = preg_quote(trim($port_name), '/');
  474. foreach ($modules_oids as $key => $value) {
  475. $pattern = '/' . $port_name . '/i';
  476. preg_match($pattern, $value, $matches);
  477. if (isset($matches[0])) {
  478. $module_id = get_last_digit($key);
  479. unset($result);
  480. $result = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_VENDOR . "." . $module_id));
  481. if (!empty($result)) {
  482. $sfp_vendor = $result;
  483. }
  484. unset($result);
  485. $result = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_SPEED . "." . $module_id));
  486. if (!empty($result)) {
  487. list($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  488. if ($sfp_type == 0) {
  489. $sfp_type = 'MultiMode';
  490. }
  491. if ($sfp_type == 1) {
  492. $sfp_type = 'SingleMode';
  493. }
  494. }
  495. $volt = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_VOLT . "." . $module_id));
  496. $circut = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
  497. $tx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_OPTTX . "." . $module_id));
  498. $rx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_OPTRX . "." . $module_id));
  499. if (!isset($tx)) {
  500. $tx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_TX . "." . $module_id));
  501. }
  502. if (!isset($rx)) {
  503. $rx = parse_snmp_value(get_snmp($ip, $snmp, HUAWEI_SFP_RX . "." . $module_id));
  504. }
  505. if (!empty($sfp_vendor)) {
  506. $status .= ' Name:' . $sfp_vendor . '<br>';
  507. }
  508. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  509. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  510. if ($volt > 0) {
  511. $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
  512. }
  513. if (!empty($circut) and $circut > 0) {
  514. $status .= ' Circut: ' . $circut . ' mA <br>';
  515. }
  516. if (!empty($tx)) {
  517. $tx = preg_replace('/"/', '', $tx);
  518. try {
  519. list($tx_dbm, $pattern) = explode('.', $tx);
  520. $tx_dbm = round(floatval(trim($tx_dbm)) / 100, 2);
  521. } catch (Exception $e) {
  522. $tx_dbm = 0;
  523. }
  524. if (abs($tx_dbm) > 0.1) {
  525. $status .= ' Tx: ' . $tx_dbm . ' dBm';
  526. }
  527. }
  528. if (!empty($rx)) {
  529. $rx = preg_replace('/"/', '', $rx);
  530. try {
  531. list($rx_dbm, $pattern) = explode('.', $rx);
  532. $rx_dbm = round(floatval(trim($rx_dbm)) / 100, 2);
  533. } catch (Exception $e) {
  534. $rx_dbm = 0;
  535. }
  536. if (abs($rx_dbm) > 0.1) {
  537. $status .= ' Rx: ' . $rx_dbm . ' dBm';
  538. }
  539. }
  540. break;
  541. }
  542. }
  543. if (isset($status)) {
  544. $status = preg_replace('/"/', '', $status);
  545. $status .= '<br>';
  546. }
  547. return $status;
  548. }
  549. } catch (Exception $e) {
  550. return;
  551. }
  552. }
  553. function get_switch_vlans($vendor, $ip, $snmp)
  554. {
  555. $switch_vlans = [];
  556. $port_status = [];
  557. $vlan_status = [];
  558. //cisco...
  559. if ($vendor == 16) {
  560. //all vlan at switch
  561. $vlan_list = walk_snmp($ip, $snmp, vtpVlanName);
  562. if (empty($vlan_list)) {
  563. return;
  564. }
  565. foreach ($vlan_list as $key => $value) {
  566. if (empty($value)) {
  567. $value = '';
  568. }
  569. $key = trim($key);
  570. $value = parse_snmp_value($value);
  571. $vlan_id = NULL;
  572. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  573. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  574. }
  575. //skip service vlan
  576. if (preg_match('/(1002|1003|1004|1005)/', $vlan_id)) {
  577. continue;
  578. }
  579. if (isset($vlan_id) and !empty($vlan_id)) {
  580. $switch_vlans[$vlan_id] = $value;
  581. }
  582. }
  583. //native vlan for port - get list of all ports
  584. $pvid_list = walk_snmp($ip, $snmp, vlanTrunkPortNativeVlan);
  585. if (!empty($pvid_list)) {
  586. foreach ($pvid_list as $key => $value) {
  587. if (empty($value)) {
  588. $value = '';
  589. }
  590. $key = trim($key);
  591. $value = parse_snmp_value($value);
  592. $port = NULL;
  593. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  594. $port = preg_replace('/^\./', '', $matches[0]);
  595. }
  596. if (isset($port) and !empty($port)) {
  597. $port_status[$port]['native'] = $value;
  598. }
  599. }
  600. }
  601. //pvid
  602. $pvid_list = walk_snmp($ip, $snmp, vmVlanPvid);
  603. if (!empty($pvid_list)) {
  604. foreach ($pvid_list as $key => $value) {
  605. if (empty($value)) {
  606. $value = '';
  607. }
  608. $key = trim($key);
  609. $value = parse_snmp_value($value);
  610. $port = NULL;
  611. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  612. $port = preg_replace('/^\./', '', $matches[0]);
  613. }
  614. if (isset($port) and !empty($port)) {
  615. $port_status[$port]['pvid'] = $value;
  616. }
  617. }
  618. }
  619. //init port config
  620. foreach ($port_status as &$port) {
  621. if (!is_array($port)) {
  622. continue;
  623. }
  624. if (!isset($port['pvid'])) {
  625. $port['pvid'] = $port['native'];
  626. }
  627. $port['untagged'] = '';
  628. $port['tagged'] = '';
  629. }
  630. unset($port);
  631. //get vlan list at ports
  632. $egress_vlan = walk_snmp($ip, $snmp, vlanTrunkPortVlansEnabled);
  633. if (!empty($egress_vlan)) {
  634. $j = 0;
  635. foreach ($egress_vlan as $key => $value) {
  636. $j++;
  637. if (empty($value)) {
  638. $value = '';
  639. }
  640. $key = trim($key);
  641. $value = parse_snmp_value($value);
  642. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  643. $port = preg_replace('/^\./', '', $matches[0]);
  644. }
  645. if (isset($port) and !empty($port)) {
  646. //skip access ports
  647. if (!is_array($port_status[$port]) or !isset($port_status[$port]['pvid']) or !isset($port_status[$port]['native'])) {
  648. continue;
  649. }
  650. if ($port_status[$port]['pvid'] != $port_status[$port]['native']) {
  651. continue;
  652. }
  653. //get vlan at port in hex
  654. $hex_value = preg_replace('/\s+/', '', $value);
  655. $bin_value = strHexToBin($hex_value);
  656. //analyze switch vlans
  657. foreach ($switch_vlans as $vlan_id => $vlan_name) {
  658. if (isset($bin_value[$vlan_id]) and $bin_value[$vlan_id] == '1') {
  659. $port_status[$port]['tagged'] = $port_status[$port]['tagged'] . ',' . $vlan_id;
  660. }
  661. }
  662. }
  663. }
  664. }
  665. //remove lliding ,
  666. foreach ($port_status as &$port) {
  667. if (!is_array($port)) {
  668. continue;
  669. }
  670. $port['untagged'] = preg_replace('/^,/', '', $port['untagged']);
  671. $port['tagged'] = preg_replace('/^,/', '', $port['tagged']);
  672. }
  673. unset($port);
  674. return $port_status;
  675. }
  676. //standart switches
  677. //tplink
  678. if ($vendor == 69) {
  679. //pvid for port
  680. $pvid_list = walk_snmp($ip, $snmp, TPLINK_dot1qPortVlanEntry);
  681. if (!empty($pvid_list)) {
  682. foreach ($pvid_list as $key => $value) {
  683. if (empty($value)) {
  684. $value = '';
  685. }
  686. $key = trim($key);
  687. $value = parse_snmp_value($value);
  688. $port = NULL;
  689. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  690. $port = preg_replace('/^\./', '', $matches[0]);
  691. }
  692. if (isset($port) and !empty($port)) {
  693. $port_status[$port]['pvid'] = $value;
  694. }
  695. }
  696. }
  697. return $port_status;
  698. }
  699. //default
  700. //pvid for port
  701. $pvid_list = walk_snmp($ip, $snmp, dot1qPortVlanEntry);
  702. if (!empty($pvid_list)) {
  703. foreach ($pvid_list as $key => $value) {
  704. if (empty($value)) {
  705. $value = '';
  706. }
  707. $key = trim($key);
  708. $value = parse_snmp_value($value);
  709. $port = NULL;
  710. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  711. $port = preg_replace('/^\./', '', $matches[0]);
  712. }
  713. if (isset($port) and !empty($port)) {
  714. $port_status[$port]['pvid'] = $value;
  715. }
  716. }
  717. }
  718. //init port config
  719. foreach ($port_status as &$port) {
  720. if (!is_array($port)) {
  721. continue;
  722. }
  723. $port['native'] = $port['pvid'];
  724. $port['untagged'] = '';
  725. $port['tagged'] = '';
  726. }
  727. unset($port);
  728. //all vlan at switch
  729. $vlan_list = walk_snmp($ip, $snmp, dot1qVlanStaticName);
  730. if (empty($vlan_list)) {
  731. return $port_status;
  732. }
  733. foreach ($vlan_list as $key => $value) {
  734. if (empty($value)) {
  735. $value = '';
  736. }
  737. $key = trim($key);
  738. $value = parse_snmp_value($value);
  739. $vlan_id = NULL;
  740. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  741. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  742. }
  743. if (isset($vlan_id) and !empty($vlan_id)) {
  744. $switch_vlans[$vlan_id] = $value;
  745. }
  746. }
  747. $forbidden_vlan = walk_snmp($ip, $snmp, dot1qVlanForbiddenEgressPorts);
  748. if (!empty($forbidden_vlan)) {
  749. foreach ($forbidden_vlan as $key => $value) {
  750. if (empty($value)) {
  751. $value = '';
  752. }
  753. $key = trim($key);
  754. $value = parse_snmp_value($value);
  755. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  756. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  757. }
  758. if (isset($vlan_id) and !empty($vlan_id)) {
  759. $hex_value = preg_replace('/\s+/', '', $value);
  760. $hex_value = preg_replace('/0*$/', '', $hex_value);
  761. $bin_value = strHexToBin($hex_value);
  762. for ($i = 0; $i < strlen($bin_value); $i++) {
  763. $port = $i + 1;
  764. $vlan_status['forbidden_vlan'][$vlan_id][$port] = $bin_value[$i];
  765. if ($bin_value[$i] == '1') {
  766. $port_status[$port]['forbidden'] .= ',' . $vlan_id;
  767. }
  768. }
  769. }
  770. }
  771. }
  772. $untagged_vlan = walk_snmp($ip, $snmp, dot1qVlanStaticUntaggedPorts);
  773. if (!empty($untagged_vlan)) {
  774. foreach ($untagged_vlan as $key => $value) {
  775. if (empty($value)) {
  776. $value = '';
  777. }
  778. $key = trim($key);
  779. $value = parse_snmp_value($value);
  780. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  781. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  782. }
  783. if (isset($vlan_id) and !empty($vlan_id)) {
  784. $hex_value = preg_replace('/\s+/', '', $value);
  785. $hex_value = preg_replace('/0*$/', '', $hex_value);
  786. $bin_value = strHexToBin($hex_value);
  787. for ($i = 0; $i < strlen($bin_value); $i++) {
  788. $port = $i + 1;
  789. $vlan_status['untagged_vlan'][$vlan_id][$port] = $bin_value[$i];
  790. if ($bin_value[$i] == '1') {
  791. if (isset($vlan_status['forbidden_vlan']) and $vlan_status['forbidden_vlan'][$vlan_id][$port] == '0') {
  792. $port_status[$port]['untagged'] .= ',' . $vlan_id;
  793. } else {
  794. $vlan_status['untagged_vlan'][$vlan_id][$port] = '0';
  795. }
  796. }
  797. }
  798. }
  799. }
  800. }
  801. $egress_vlan = walk_snmp($ip, $snmp, dot1qVlanStaticEgressPorts);
  802. if (!empty($egress_vlan)) {
  803. foreach ($egress_vlan as $key => $value) {
  804. if (empty($value)) {
  805. $value = '';
  806. }
  807. $key = trim($key);
  808. $value = parse_snmp_value($value);
  809. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  810. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  811. }
  812. //exclude vlan 1 from tagged vlan
  813. if ($vlan_id == '1') {
  814. continue;
  815. }
  816. if (isset($vlan_id) and !empty($vlan_id)) {
  817. $hex_value = preg_replace('/\s+/', '', $value);
  818. $hex_value = preg_replace('/0*$/', '', $hex_value);
  819. $bin_value = strHexToBin($hex_value);
  820. for ($i = 0; $i < strlen($bin_value); $i++) {
  821. $port = $i + 1;
  822. $vlan_status['egress_vlan'][$vlan_id][$port] = $bin_value[$i];
  823. //analyze egress & untagged vlans
  824. if ($bin_value[$i] == '1') {
  825. if ((!isset($vlan_status['untagged_vlan'][$vlan_id][$port]) or $vlan_status['untagged_vlan'][$vlan_id][$port] == '0') and
  826. (!isset($vlan_status['forbidden_vlan'][$vlan_id][$port]) or $vlan_status['forbidden_vlan'][$vlan_id][$port] == '0') and
  827. (!isset($port_status[$port]['pvid']) or $port_status[$port]['pvid'] != $vlan_id)
  828. ) {
  829. $vlan_status['tagged_vlan'][$vlan_id][$port] = '1';
  830. $port_status[$port]['tagged'] .= ',' . $vlan_id;
  831. } else {
  832. $vlan_status['tagged_vlan'][$vlan_id][$port] = '0';
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. foreach ($port_status as &$port) {
  840. if (!is_array($port)) {
  841. continue;
  842. }
  843. $port['untagged'] = preg_replace('/^,/', '', $port['untagged']);
  844. $port['tagged'] = preg_replace('/^,/', '', $port['tagged']);
  845. }
  846. unset($port);
  847. return $port_status;
  848. }
  849. function get_port_vlan($vendor, $port, $port_index, $ip, $snmp)
  850. {
  851. if (!isset($port_index)) {
  852. return;
  853. }
  854. if (!isset($ip)) {
  855. return;
  856. }
  857. //default - default port index
  858. $port_oid = dot1qPortVlanEntry . "." . $port_index;
  859. //tplink
  860. if ($vendor == 69) {
  861. $port_oid = TPLINK_dot1qPortVlanEntry . "." . $port_index;
  862. }
  863. //huawei
  864. if ($vendor == 3) {
  865. $port_oid = dot1qPortVlanEntry . "." . $port;
  866. }
  867. //allied telesys
  868. if ($vendor == 8) {
  869. $port_oid = dot1qPortVlanEntry . "." . $port;
  870. }
  871. $port_vlan = get_snmp($ip, $snmp, $port_oid);
  872. $port_vlan = preg_replace('/.*\:/', '', $port_vlan);
  873. $port_vlan = intval(trim($port_vlan));
  874. return $port_vlan;
  875. }
  876. function get_ports_poe_state($vendor_id, $ip, $snmp)
  877. {
  878. if (!isset($vendor_id)) {
  879. return;
  880. }
  881. if (!isset($ip)) {
  882. return;
  883. }
  884. // default poe oid
  885. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE;
  886. if ($vendor_id == 3) {
  887. $poe_status = HUAWEI_POE_OID;
  888. }
  889. if ($vendor_id == 6) {
  890. $poe_status = SNR_POE_OID;
  891. }
  892. if ($vendor_id == 8) {
  893. $poe_status = ALLIED_POE_OID;
  894. }
  895. if ($vendor_id == 9) {
  896. $poe_status = MIKROTIK_POE_OID;
  897. }
  898. if ($vendor_id == 10) {
  899. $poe_status = NETGEAR_POE_OID;
  900. }
  901. if ($vendor_id == 15) {
  902. $poe_status = HP_POE_OID;
  903. }
  904. if ($vendor_id == 69) {
  905. $poe_status = TPLINK_POE_OID;
  906. }
  907. $result = [];
  908. $c_state = walk_snmp($ip, $snmp, $poe_status);
  909. if (isset($c_state) and !empty($c_state)) {
  910. foreach ($c_state as $key => $value) {
  911. if (empty($value)) {
  912. $value = '';
  913. }
  914. $key = trim($key);
  915. $value = parse_snmp_value($value);
  916. $port = NULL;
  917. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  918. $port = preg_replace('/^\./', '', $matches[0]);
  919. $result[$port] = $value;
  920. // patch for mikrotik
  921. if ($vendor_id == 9) {
  922. if ($value == 1) {
  923. $result[$port] = 2;
  924. }
  925. if ($value > 1) {
  926. $result[$port] = 1;
  927. }
  928. }
  929. //patch for tplink
  930. if ($vendor_id == 69) {
  931. if ($value == 0) {
  932. $result[$port] = 2;
  933. }
  934. if ($value >= 1) {
  935. $result[$port] = 1;
  936. }
  937. }
  938. }
  939. }
  940. }
  941. return $result;
  942. }
  943. function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $snmp)
  944. {
  945. if (!isset($port)) {
  946. return;
  947. }
  948. if (!isset($ip)) {
  949. return;
  950. }
  951. // default poe oid
  952. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  953. if ($vendor_id == 3) {
  954. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  955. }
  956. if ($vendor_id == 6) {
  957. $poe_status = SNR_POE_OID . "." . $port_snmp_index;
  958. }
  959. if ($vendor_id == 8) {
  960. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  961. }
  962. if ($vendor_id == 15) {
  963. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  964. }
  965. if ($vendor_id == 9) {
  966. $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
  967. }
  968. if ($vendor_id == 10) {
  969. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  970. }
  971. if ($vendor_id == 69) {
  972. $poe_status = TPLINK_POE_OID . "." . $port;
  973. }
  974. $result = '';
  975. $c_state = get_snmp($ip, $snmp, $poe_status);
  976. if (isset($c_state) and !empty($c_state)) {
  977. $p_state = parse_snmp_value($c_state);
  978. if (empty($p_state)) {
  979. return NULL;
  980. }
  981. // patch for mikrotik
  982. if ($vendor_id == 9) {
  983. if ($p_state == 1) {
  984. return 2;
  985. }
  986. if ($p_state > 1) {
  987. return 1;
  988. }
  989. }
  990. //patch for tplink
  991. if ($vendor_id == 69) {
  992. if ($p_state == 0) {
  993. return 2;
  994. }
  995. if ($p_state >= 1) {
  996. return 1;
  997. }
  998. }
  999. return $p_state;
  1000. }
  1001. return NULL;
  1002. }
  1003. function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $snmp, $state)
  1004. {
  1005. if (!isset($ip)) {
  1006. return;
  1007. }
  1008. //default poe status
  1009. $poe_enable = 1;
  1010. $poe_disable = 2;
  1011. // default poe oid
  1012. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  1013. if ($vendor_id == 3) {
  1014. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  1015. }
  1016. if ($vendor_id == 8) {
  1017. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  1018. }
  1019. if ($vendor_id == 15) {
  1020. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  1021. }
  1022. if ($vendor_id == 10) {
  1023. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  1024. }
  1025. if ($vendor_id == 69) {
  1026. $poe_status = TPLINK_POE_OID . "." . $port;
  1027. $poe_enable = 1;
  1028. $poe_disable = 0;
  1029. }
  1030. if ($state) {
  1031. // enable port
  1032. $c_state = set_snmp($ip, $snmp, $poe_status, 'i', $poe_enable);
  1033. return $c_state;
  1034. } else {
  1035. // disable port
  1036. $c_state = set_snmp($ip, $snmp, $poe_status, 'i', $poe_disable);
  1037. return $c_state;
  1038. }
  1039. }
  1040. function get_ports_poe_detail($vendor_id, $ip, $snmp)
  1041. {
  1042. if (!isset($vendor_id)) {
  1043. return;
  1044. }
  1045. if (!isset($ip)) {
  1046. return;
  1047. }
  1048. $result = [];
  1049. $poe_class = PETH_PSE_PORT_POE_CLASS;
  1050. // eltex
  1051. if ($vendor_id == 2) {
  1052. $poe_power = ELTEX_POE_USAGE;
  1053. $poe_current = ELTEX_POE_CURRENT;
  1054. $poe_volt = ELTEX_POE_VOLT;
  1055. }
  1056. // huawei
  1057. if ($vendor_id == 3) {
  1058. $poe_power = HUAWEI_POE_USAGE;
  1059. $poe_current = HUAWEI_POE_CURRENT;
  1060. $poe_volt = HUAWEI_POE_VOLT;
  1061. }
  1062. // snr
  1063. if ($vendor_id == 6) {
  1064. $poe_class = SNR_POE_CLASS;
  1065. $poe_power = SNR_POE_USAGE;
  1066. $poe_current = SNR_POE_CURRENT;
  1067. $poe_volt = SNR_POE_VOLT;
  1068. }
  1069. // AT
  1070. if ($vendor_id == 8) {
  1071. $poe_power = ALLIED_POE_USAGE;
  1072. $poe_current = ALLIED_POE_CURRENT;
  1073. $poe_volt = ALLIED_POE_VOLT;
  1074. }
  1075. // mikrotik
  1076. if ($vendor_id == 9) {
  1077. $poe_power = MIKROTIK_POE_USAGE;
  1078. $poe_current = MIKROTIK_POE_CURRENT;
  1079. $poe_volt = MIKROTIK_POE_VOLT;
  1080. }
  1081. // netgear
  1082. if ($vendor_id == 10) {
  1083. $poe_power = NETGEAR_POE_USAGE;
  1084. $poe_current = NETGEAR_POE_CURRENT;
  1085. $poe_volt = NETGEAR_POE_VOLT;
  1086. }
  1087. // HP
  1088. if ($vendor_id == 15) {
  1089. $poe_power = HP_POE_USAGE;
  1090. $poe_volt = HP_POE_VOLT;
  1091. }
  1092. // TP-Link
  1093. if ($vendor_id == 69) {
  1094. $poe_power = TPLINK_POE_USAGE;
  1095. $poe_current = TPLINK_POE_CURRENT;
  1096. $poe_volt = TPLINK_POE_VOLT;
  1097. $poe_class = TPLINK_POE_CLASS;
  1098. }
  1099. if (isset($poe_power)) {
  1100. $c_power = walk_snmp($ip, $snmp, $poe_power);
  1101. if (isset($c_power)) {
  1102. foreach ($c_power as $key => $value) {
  1103. if (empty($value)) {
  1104. $value = 'INT:0';
  1105. }
  1106. $key = trim($key);
  1107. $p_power = parse_snmp_value($value);
  1108. $port = NULL;
  1109. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1110. $port = preg_replace('/^\./', '', $matches[0]);
  1111. $result[$port]['power'] = 0;
  1112. $result[$port]['power_display'] = '';
  1113. switch ($vendor_id) {
  1114. case 9: //mikrotik
  1115. $p_power = round($p_power / 10, 2);
  1116. break;
  1117. case 69: //tplink
  1118. $p_power = round($p_power / 10, 2);
  1119. break;
  1120. default:
  1121. $p_power = round($p_power / 1000, 2);
  1122. break;
  1123. }
  1124. if ($p_power > 0) {
  1125. $result[$port]['power'] = $p_power;
  1126. $result[$port]['power_display'] = 'P: ' . $p_power . ' W';
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. if (isset($poe_current)) {
  1133. $c_current = walk_snmp($ip, $snmp, $poe_current);
  1134. if (isset($c_current)) {
  1135. foreach ($c_current as $key => $value) {
  1136. if (empty($value)) {
  1137. $value = 'INT:0';
  1138. }
  1139. $key = trim($key);
  1140. $p_current = parse_snmp_value($value);
  1141. $port = NULL;
  1142. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1143. $port = preg_replace('/^\./', '', $matches[0]);
  1144. $result[$port]['current'] = 0;
  1145. $result[$port]['current_display'] = '';
  1146. if ($p_current > 0) {
  1147. $result[$port]['current'] = $p_current;
  1148. $result[$port]['current_display'] = 'C: ' . $p_current . ' mA';
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. if (isset($poe_volt)) {
  1155. $c_volt = walk_snmp($ip, $snmp, $poe_volt);
  1156. if (isset($c_volt)) {
  1157. foreach ($c_volt as $key => $value) {
  1158. if (empty($value)) {
  1159. $value = 'INT:0';
  1160. }
  1161. $key = trim($key);
  1162. $p_volt = parse_snmp_value($value);
  1163. $port = NULL;
  1164. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1165. $port = preg_replace('/^\./', '', $matches[0]);
  1166. $result[$port]['volt'] = 0;
  1167. $result[$port]['volt_display'] = '';
  1168. switch ($vendor_id) {
  1169. case 2:
  1170. case 8:
  1171. $p_volt = round($p_volt / 1000, 2);
  1172. break;
  1173. case 9:
  1174. case 69:
  1175. $p_volt = round($p_volt / 10, 2);
  1176. break;
  1177. case 15:
  1178. $p_volt = round($p_volt / 100, 2);
  1179. break;
  1180. }
  1181. if ($p_volt > 0 and $result[$port]['power'] > 0) {
  1182. $result[$port]['volt'] = $p_volt;
  1183. $result[$port]['volt_display'] = ' V: ' . $p_volt . " V";
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. if (isset($poe_class)) {
  1190. $c_class = walk_snmp($ip, $snmp, $poe_class);
  1191. if (isset($c_class)) {
  1192. foreach ($c_class as $key => $value) {
  1193. if (empty($value)) {
  1194. $value = 'INT:0';
  1195. }
  1196. $key = trim($key);
  1197. $p_class = parse_snmp_value($value);
  1198. $port = NULL;
  1199. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1200. $port = preg_replace('/^\./', '', $matches[0]);
  1201. $result[$port]['class'] = 0;
  1202. $result[$port]['class_display'] = '';
  1203. switch ($vendor_id) {
  1204. case 69:
  1205. if ($p_class > 0 and $result[$port]['power'] > 0) {
  1206. if ($p_class == 7) {
  1207. $p_class = 'class-not-defined';
  1208. }
  1209. $result[$port]['class_display'] = 'Class: ' . $p_class;
  1210. $result[$port]['class'] = $p_class;
  1211. }
  1212. break;
  1213. default:
  1214. if ($p_class > 0 and $result[$port]['power'] > 0) {
  1215. $result[$port]['class_display'] = 'Class: ' . ($p_class - 1);
  1216. $result[$port]['class'] = $p_class - 1;
  1217. }
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. foreach ($result as &$port) {
  1225. if (!isset($port['power'])) {
  1226. $port['power'] = 0;
  1227. }
  1228. if (!isset($port['current'])) {
  1229. $port['current'] = 0;
  1230. }
  1231. if (!isset($port['volt'])) {
  1232. $port['volt'] = 0;
  1233. }
  1234. if (!isset($port['class'])) {
  1235. $port['class'] = 0;
  1236. }
  1237. }
  1238. unset($port);
  1239. return $result;
  1240. }
  1241. function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $snmp)
  1242. {
  1243. if (!isset($port) or !isset($port_snmp_index)) {
  1244. return;
  1245. }
  1246. if (!isset($ip)) {
  1247. return;
  1248. }
  1249. if (!isset($community)) {
  1250. $community = 'public';
  1251. }
  1252. if (!isset($version)) {
  1253. $version = '2';
  1254. }
  1255. $result = '';
  1256. $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
  1257. // eltex
  1258. if ($vendor_id == 2) {
  1259. $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
  1260. $poe_current = ELTEX_POE_CURRENT . '.' . $port_snmp_index;
  1261. $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
  1262. }
  1263. // huawei
  1264. if ($vendor_id == 3) {
  1265. $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
  1266. $poe_current = HUAWEI_POE_CURRENT . '.' . $port_snmp_index;
  1267. $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
  1268. }
  1269. // snr
  1270. if ($vendor_id == 6) {
  1271. $poe_class = SNR_POE_CLASS . '.' . $port_snmp_index;
  1272. $poe_power = SNR_POE_USAGE . '.' . $port_snmp_index;
  1273. $poe_current = SNR_POE_CURRENT . '.' . $port_snmp_index;
  1274. $poe_volt = SNR_POE_VOLT . '.' . $port_snmp_index;
  1275. }
  1276. // AT
  1277. if ($vendor_id == 8) {
  1278. $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
  1279. $poe_current = ALLIED_POE_CURRENT . '.' . $port_snmp_index;
  1280. $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
  1281. }
  1282. // mikrotik
  1283. if ($vendor_id == 9) {
  1284. $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
  1285. $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
  1286. $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
  1287. }
  1288. // netgear
  1289. if ($vendor_id == 10) {
  1290. $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
  1291. $poe_current = NETGEAR_POE_CURRENT . '.' . $port_snmp_index;
  1292. $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
  1293. }
  1294. // HP
  1295. if ($vendor_id == 15) {
  1296. $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
  1297. $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
  1298. }
  1299. // TP-Link
  1300. if ($vendor_id == 69) {
  1301. $poe_power = TPLINK_POE_USAGE . '.' . $port;
  1302. $poe_current = TPLINK_POE_CURRENT . '.' . $port;
  1303. $poe_volt = TPLINK_POE_VOLT . '.' . $port;
  1304. $poe_class = TPLINK_POE_CLASS . "." . $port;
  1305. }
  1306. if (isset($poe_power)) {
  1307. $c_power = get_snmp($ip, $snmp, $poe_power);
  1308. if (isset($c_power)) {
  1309. $p_power = parse_snmp_value($c_power);
  1310. switch ($vendor_id) {
  1311. case 9:
  1312. $p_power = round($p_power / 10, 2);
  1313. break;
  1314. case 69:
  1315. $p_power = round($p_power / 10, 2);
  1316. break;
  1317. default:
  1318. $p_power = round($p_power / 1000, 2);
  1319. break;
  1320. }
  1321. if ($p_power > 0) {
  1322. $result .= ' P: ' . $p_power . ' W';
  1323. }
  1324. }
  1325. }
  1326. if (isset($poe_current)) {
  1327. $c_current = get_snmp($ip, $snmp, $poe_current);
  1328. if (isset($c_current)) {
  1329. $p_current = parse_snmp_value($c_current);
  1330. if ($p_current > 0) {
  1331. $result .= ' C: ' . $p_current . ' mA';
  1332. }
  1333. }
  1334. }
  1335. if (isset($poe_volt)) {
  1336. $c_volt = get_snmp($ip, $snmp, $poe_volt);
  1337. if (isset($c_volt)) {
  1338. $p_volt = parse_snmp_value($c_volt);
  1339. switch ($vendor_id) {
  1340. case 2:
  1341. case 8:
  1342. $p_volt = round($p_volt / 1000, 2);
  1343. break;
  1344. case 9:
  1345. case 69:
  1346. $p_volt = round($p_volt / 10, 2);
  1347. break;
  1348. case 15:
  1349. $p_volt = round($p_volt / 100, 2);
  1350. break;
  1351. }
  1352. if ($p_volt > 0 and $p_power > 0) {
  1353. $result .= ' V: ' . $p_volt . " V";
  1354. }
  1355. }
  1356. }
  1357. if (isset($poe_class)) {
  1358. $c_class = get_snmp($ip, $snmp, $poe_class);
  1359. if (isset($c_class)) {
  1360. $p_class = parse_snmp_value($c_class);
  1361. switch ($vendor_id) {
  1362. case 69:
  1363. if ($p_class > 0 and $p_power > 0) {
  1364. if ($p_class == 7) {
  1365. $p_class = 'class-not-defined';
  1366. }
  1367. $result .= ' Class: ' . $p_class;
  1368. }
  1369. break;
  1370. default:
  1371. if ($p_class > 0 and $p_power > 0) {
  1372. $result .= ' Class: ' . ($p_class - 1);
  1373. }
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. return $result;
  1379. }
  1380. function get_snmp($ip, $snmp, $oid)
  1381. {
  1382. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  1383. $result = NULL;
  1384. try {
  1385. $version = $snmp["version"];
  1386. if ($version == 3) {
  1387. $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);
  1388. }
  1389. if ($version == 2) {
  1390. $result = snmp2_get($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
  1391. }
  1392. if ($version == 1) {
  1393. $result = snmpget($ip, $snmp["ro-community"], $oid, SNMP_timeout, SNMP_retry);
  1394. }
  1395. if (empty($result)) {
  1396. $result = NULL;
  1397. }
  1398. } catch (Exception $e) {
  1399. # echo 'Caught exception: ', $e->getMessage(), "\n";
  1400. $result = NULL;
  1401. }
  1402. return $result;
  1403. }
  1404. function set_snmp($ip, $snmp, $oid, $field, $value)
  1405. {
  1406. $result = false;
  1407. try {
  1408. $version = $snmp["version"];
  1409. if ($version == 3) {
  1410. $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);
  1411. }
  1412. if ($version == 2) {
  1413. $result = snmp2_set($ip, $snmp["rw-community"], $oid, $field, $value, SNMP_timeout, SNMP_retry);
  1414. }
  1415. if ($version == 1) {
  1416. $result = snmpset($ip, $snmp["rw-community"], $oid, $field, $value, SNMP_timeout, SNMP_retry);
  1417. }
  1418. } catch (Exception $e) {
  1419. # echo 'Caught exception: ', $e->getMessage(), "\n";
  1420. $result = false;
  1421. }
  1422. return $result;
  1423. }
  1424. function set_port_state($vendor_id, $port, $ip, $snmp, $state)
  1425. {
  1426. // port -> snmp_index!!!
  1427. if (!isset($port)) {
  1428. return;
  1429. }
  1430. if (!isset($ip)) {
  1431. return;
  1432. }
  1433. $port_status = PORT_ADMIN_STATUS_OID . '.' . $port;
  1434. if ($state == 1) {
  1435. // enable port
  1436. $c_state = set_snmp($ip, $snmp, $port_status, 'i', 1);
  1437. return $c_state;
  1438. } else {
  1439. // disable port
  1440. $c_state = set_snmp($ip, $snmp, $port_status, 'i', 2);
  1441. return $c_state;
  1442. }
  1443. }
  1444. function get_ports_state_detail($ip, $snmp)
  1445. {
  1446. if (!isset($ip)) {
  1447. return;
  1448. }
  1449. $result = [];
  1450. //post status
  1451. $p_state = walk_snmp($ip, $snmp, PORT_STATUS_OID);
  1452. if (!empty($p_state)) {
  1453. foreach ($p_state as $key => $value) {
  1454. if (empty($value)) {
  1455. $value = '';
  1456. }
  1457. $key = trim($key);
  1458. $value = parse_snmp_value($value);
  1459. $port = NULL;
  1460. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1461. $port = preg_replace('/^\./', '', $matches[0]);
  1462. $result[$port]['status'] = $value;
  1463. $result[$port]['admin_status'] = 0;
  1464. $result[$port]['speed'] = 0;
  1465. $result[$port]['errors'] = 0;
  1466. }
  1467. }
  1468. }
  1469. //admin state
  1470. $p_admin = walk_snmp($ip, $snmp, PORT_ADMIN_STATUS_OID);
  1471. if (!empty($p_admin)) {
  1472. foreach ($p_admin as $key => $value) {
  1473. if (empty($value)) {
  1474. $value = '';
  1475. }
  1476. $key = trim($key);
  1477. $value = parse_snmp_value($value);
  1478. $port = NULL;
  1479. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1480. $port = preg_replace('/^\./', '', $matches[0]);
  1481. $result[$port]['admin_status'] = $value;
  1482. }
  1483. }
  1484. }
  1485. //port speed
  1486. $p_speed = walk_snmp($ip, $snmp, PORT_SPEED_OID);
  1487. if (!empty($p_speed)) {
  1488. foreach ($p_speed as $key => $value) {
  1489. if (empty($value)) {
  1490. $value = 'INT:0';
  1491. }
  1492. $key = trim($key);
  1493. $value = parse_snmp_value($value);
  1494. $port = NULL;
  1495. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1496. $port = preg_replace('/^\./', '', $matches[0]);
  1497. $result[$port]['speed'] = $value;
  1498. }
  1499. }
  1500. }
  1501. //errors at
  1502. $p_errors = walk_snmp($ip, $snmp, PORT_ERRORS_OID);
  1503. if (!empty($p_errors)) {
  1504. foreach ($p_errors as $key => $value) {
  1505. if (empty($value)) {
  1506. $value = 'INT:0';
  1507. }
  1508. $key = trim($key);
  1509. $value = parse_snmp_value($value);
  1510. $port = NULL;
  1511. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  1512. $port = preg_replace('/^\./', '', $matches[0]);
  1513. $result[$port]['errors'] = $value;
  1514. }
  1515. }
  1516. }
  1517. return $result;
  1518. }
  1519. function get_port_state_detail($port, $ip, $snmp)
  1520. {
  1521. if (!isset($port)) {
  1522. return;
  1523. }
  1524. if (!isset($ip)) {
  1525. return;
  1526. }
  1527. // if (!is_up($ip)) { return; }
  1528. $oper = PORT_STATUS_OID . '.' . $port;
  1529. $admin = PORT_ADMIN_STATUS_OID . '.' . $port;
  1530. $speed = PORT_SPEED_OID . '.' . $port;
  1531. $errors = PORT_ERRORS_OID . '.' . $port;
  1532. $result = '';
  1533. $c_state = get_snmp($ip, $snmp, $oper);
  1534. $p_state = parse_snmp_value($c_state);
  1535. $c_admin = get_snmp($ip, $snmp, $admin);
  1536. $p_admin = parse_snmp_value($c_admin);
  1537. if ($p_state == 1) {
  1538. $c_speed = get_snmp($ip, $snmp, $speed);
  1539. } else {
  1540. $c_speed = 'INT:0';
  1541. }
  1542. $p_speed = parse_snmp_value($c_speed);
  1543. $c_errors = get_snmp($ip, $snmp, $errors);
  1544. $p_errors = parse_snmp_value($c_errors);
  1545. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  1546. return $result;
  1547. }
  1548. function parse_snmp_value($value)
  1549. {
  1550. if (empty($value)) {
  1551. return NULL;
  1552. }
  1553. if (!preg_match('/:/', $value)) {
  1554. return NULL;
  1555. }
  1556. list($p_type, $p_value) = explode(':', $value);
  1557. if (empty($p_value)) {
  1558. return NULL;
  1559. }
  1560. $p_value = trim($p_value);
  1561. $p_value = preg_replace('/^\"/', '', $p_value);
  1562. $p_value = preg_replace('/\"$/', '', $p_value);
  1563. $p_value = trim($p_value);
  1564. return $p_value;
  1565. }
  1566. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  1567. snmp_set_enum_print(1);