snmp.php 58 KB

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