snmp.php 56 KB

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