snmp.php 57 KB

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