snmp.php 57 KB

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