switchstatus.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/auth.php");
  3. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/languages/" . HTML_LANG . ".php");
  4. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/idfilter.php");
  5. $device = get_record($db_link, 'devices', "id = ?", [$id]);
  6. $snmp = getSnmpAccess($device);
  7. $device_model = get_record($db_link, 'device_models', "id = ?", [$device['device_model_id']]);
  8. // Перегенерация SNMP-индексов
  9. if (getPOST("regensnmp") !== null) {
  10. $snmp_index = (int)getPOST("f_snmp_start", null, 1);
  11. $sSQL = "SELECT id, port FROM device_ports WHERE device_ports.device_id = ? ORDER BY id";
  12. $flist = get_records_sql($db_link, $sSQL, [$id]);
  13. LOG_DEBUG($db_link, "Recalc snmp_index for device id: $id with start $snmp_index");
  14. foreach ($flist as $row) {
  15. $snmp_val = $row['port'] + $snmp_index - 1;
  16. update_record($db_link, "device_ports", "id = ?", ['snmp_index' => $snmp_val], [$row['id']]);
  17. }
  18. header("Location: " . $_SERVER["REQUEST_URI"]);
  19. exit;
  20. }
  21. // Включение PoE
  22. if (getPOST("poe_on") !== null && $device['snmp_version'] > 0) {
  23. $poe_on = getPOST("poe_on", null, []);
  24. if (!empty($poe_on) && is_array($poe_on)) {
  25. foreach ($poe_on as $port_index) {
  26. $port_index = (int)$port_index;
  27. if ($port_index <= 0) continue;
  28. $port = get_record_sql($db_link,
  29. "SELECT port FROM device_ports WHERE device_id = ? AND snmp_index = ?",
  30. [$id, $port_index]
  31. );
  32. if (!empty($port)) {
  33. LOG_DEBUG($db_link, "Device id: $id enable poe at port {$port['port']} snmp index $port_index");
  34. set_port_poe_state($device['vendor_id'], $port['port'], $port_index, $device['ip'], $snmp, 1);
  35. }
  36. }
  37. }
  38. header("Location: " . $_SERVER["REQUEST_URI"]);
  39. exit;
  40. }
  41. // Отключение PoE
  42. if (getPOST("poe_off") !== null && $device['snmp_version'] > 0) {
  43. $poe_off = getPOST("poe_off", null, []);
  44. if (!empty($poe_off) && is_array($poe_off)) {
  45. foreach ($poe_off as $port_index) {
  46. $port_index = (int)$port_index;
  47. if ($port_index <= 0) continue;
  48. $port = get_record_sql($db_link,
  49. "SELECT port FROM device_ports WHERE device_id = ? AND snmp_index = ?",
  50. [$id, $port_index]
  51. );
  52. if (!empty($port)) {
  53. LOG_DEBUG($db_link, "Device id: $id disable poe at port {$port['port']} snmp index $port_index");
  54. set_port_poe_state($device['vendor_id'], $port['port'], $port_index, $device['ip'], $snmp, 0);
  55. }
  56. }
  57. }
  58. header("Location: " . $_SERVER["REQUEST_URI"]);
  59. exit;
  60. }
  61. // Включение портов
  62. if (getPOST("port_on") !== null && $device['snmp_version'] > 0) {
  63. $port_on = getPOST("port_on", null, []);
  64. if (!empty($port_on) && is_array($port_on)) {
  65. foreach ($port_on as $port_index) {
  66. $port_index = (int)$port_index;
  67. if ($port_index <= 0) continue;
  68. LOG_DEBUG($db_link, "Device id: $id enable port with snmp index $port_index");
  69. set_port_state($device['vendor_id'], $port_index, $device['ip'], $snmp, 1);
  70. }
  71. }
  72. header("Location: " . $_SERVER["REQUEST_URI"]);
  73. exit;
  74. }
  75. // Отключение портов
  76. if (getPOST("port_off") !== null && $device['snmp_version'] > 0) {
  77. $port_off = getPOST("port_off", null, []);
  78. if (!empty($port_off) && is_array($port_off)) {
  79. foreach ($port_off as $port_index) {
  80. $port_index = (int)$port_index;
  81. if ($port_index <= 0) continue;
  82. LOG_DEBUG($db_link, "Device id: $id disable port with snmp index $port_index");
  83. set_port_state($device['vendor_id'], $port_index, $device['ip'], $snmp, 0);
  84. }
  85. }
  86. header("Location: " . $_SERVER["REQUEST_URI"]);
  87. exit;
  88. }
  89. unset($_POST);
  90. if (!apply_device_lock($db_link, $id)) {
  91. header("Location: /admin/devices/editdevice.php?id=" . $id . "&status=locked");
  92. exit;
  93. }
  94. $user_info = get_record_sql($db_link, "SELECT * FROM user_list WHERE id=?", [ $device['user_id'] ]);
  95. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  96. print_device_submenu($page_url);
  97. print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['login']);
  98. ?>
  99. <div id="contsubmenu">
  100. <form name="def" action="switchstatus.php?id=<?php echo $id; ?>" method="post">
  101. <?php
  102. print "<br>\n";
  103. print "<b>" . WEB_device_port_state_list . "&nbsp" . $device['device_name'] . " - " . $device['ip'] . "</b><br>\n";
  104. $snmp_ok = 0;
  105. $vlan_list = [];
  106. $vlan_at_port_by_snmp = 1;
  107. if (!empty($device['ip']) and $device['snmp_version'] > 0) {
  108. $snmp_ok = check_snmp_access($device['ip'], $snmp);
  109. $modules_oids = NULL;
  110. if ($snmp_ok) {
  111. update_record($db_link, 'devices', 'id=?', [ 'nagios_status'=> 'UP' ], [ $id ]);
  112. $modules_oids = walk_snmp($device["ip"], $snmp, CISCO_MODULES);
  113. $vlan_list = get_switch_vlans($device['vendor_id'], $device['ip'], $snmp);
  114. //if port number 1 not exists - try detect by snmp interface index
  115. if (isset($vlan_list['1'])) {
  116. $vlan_at_port_by_snmp = 0;
  117. }
  118. }
  119. } else {
  120. $snmp_ok = 0;
  121. }
  122. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  123. print "<tr>\n";
  124. print "<td>id</td>\n";
  125. print "<td>" . WEB_device_port_number . "</td>\n";
  126. print "<td>" . WEB_device_port_name . "</td>\n";
  127. print "<td>" . WEB_device_port_snmp_index . "</td>\n";
  128. print "<td>" . WEB_device_connected_endpoint . "</td>\n";
  129. print "<td>" . WEB_cell_description . "</td>\n";
  130. print "<td>" . WEB_device_port_uplink . "</td>\n";
  131. print "<td>" . WEB_nagios . "</td>\n";
  132. print "<td>" . WEB_cell_skip . "</td>\n";
  133. print "<td>" . WEB_cell_vlan . "</td>\n";
  134. print "<td>" . WEB_device_snmp_port_oid_name . "</td>\n";
  135. print "<td>" . WEB_device_port_speed . "</td>\n";
  136. print "<td>" . WEB_device_port_errors . "</td>\n";
  137. print "<td>" . WEB_cell_mac_count . "</td>\n";
  138. print "<td>" . WEB_msg_additional . "</td>\n";
  139. if ($device_model['poe_out']) {
  140. print "<td>" . WEB_device_poe_control . "</td>\n";
  141. }
  142. print "<td>" . WEB_device_port_control . "</td>\n";
  143. print "</tr>\n";
  144. $sSQL = "SELECT * FROM device_ports WHERE device_ports.device_id=? ORDER BY port";
  145. $ports = get_records_sql($db_link, $sSQL, [ $id ]);
  146. foreach ($ports as $row) {
  147. print "<tr align=center>\n";
  148. $cl = "down";
  149. $new_info = NULL;
  150. $display_vlan = $row['vlan'];
  151. if (!empty($row['untagged_vlan'])) {
  152. if ($row['untagged_vlan'] != $row['vlan']) {
  153. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  154. $replacement = '${1},${2},${3},${4},${5}<br>U:';
  155. $display_untagged = preg_replace($pattern, $replacement, $row['untagged_vlan']);
  156. $display_vlan .= ";U:" . $display_untagged;
  157. }
  158. }
  159. if (!empty($row['tagged_vlan'])) {
  160. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  161. $replacement = '${1},${2},${3},${4},${5}<br>T:';
  162. $display_tagged = preg_replace($pattern, $replacement, $row['tagged_vlan']);
  163. $display_vlan .= ";T:" . $display_tagged;
  164. }
  165. //fix empty port names
  166. if (empty($row['port_name'])) {
  167. $row['port_name'] = $row['port'];
  168. $new_info['port_name'] = $row['port'];
  169. }
  170. if ($snmp_ok) {
  171. $port_state_detail = get_port_state_detail($row['snmp_index'], $device['ip'], $snmp);
  172. list($poper, $padmin, $pspeed, $perrors) = explode(';', $port_state_detail);
  173. if ($poper == 1) {
  174. $cl = "up";
  175. }
  176. if ($poper >= 2) {
  177. if ($padmin >= 2) {
  178. $cl = "shutdown";
  179. } else {
  180. $cl = "down";
  181. }
  182. }
  183. }
  184. print "<td class='" . $cl . "' style='padding:0'><input type=checkbox name=d_port_index[] value=" . $row['snmp_index'] . " ></td>\n";
  185. print "<td class='" . $cl . "'><a href=\"#\" onclick=\"" . open_window_url("editport.php?id=" . $row['id']) . " return false;\">" . $row['port'] . "</a></td>\n";
  186. print "<td class='" . $cl . "' >" . $row['port_name'] . "</td>\n";
  187. print "<td class='" . $cl . "' >" . $row['snmp_index'] . "</td>\n";
  188. print "<td class='" . $cl . "'>";
  189. if (isset($row['target_port_id']) and $row['target_port_id'] > 0) {
  190. print_device_port($db_link, $row['target_port_id']);
  191. } else {
  192. print_auth_port($db_link, $row['id'], TRUE);
  193. }
  194. print "</td>\n";
  195. print "<td class='" . $cl . "' >" . get_port_description($db_link, $row['id'], $row['description']) . "</td>\n";
  196. print_td_yes($row['uplink'],FALSE,$cl);
  197. print_td_yes($row['nagios'],FALSE,$cl);
  198. print_td_yes($row['skip'],FALSE,$cl);
  199. $poe_info = "";
  200. $ifname = $row['ifname'];
  201. if ($snmp_ok) {
  202. //sfp information
  203. $sfp_status = get_sfp_status($device['vendor_id'], $row['snmp_index'], $device['ip'], $snmp, $modules_oids);
  204. //interface name
  205. $ifname = get_snmp_ifname($device['ip'], $snmp, $row['snmp_index']);
  206. //poe information
  207. if ($device_model['poe_out']) {
  208. $poe_status = get_port_poe_state($device['vendor_id'], $row['port'], $row['snmp_index'], $device['ip'], $snmp);
  209. if (isset($poe_status)) {
  210. if ($poe_status == 1) {
  211. $port_poe_detail = get_port_poe_detail($device['vendor_id'], $row['port'], $row['snmp_index'], $device['ip'], $snmp);
  212. if (empty($port_poe_detail)) {
  213. $poe_info = 'POE:on';
  214. } else {
  215. $poe_info = $port_poe_detail;
  216. }
  217. }
  218. if ($poe_status == 2) {
  219. $poe_info = "POE:Off";
  220. }
  221. }
  222. }
  223. //vlans at port
  224. if (!empty($vlan_list)) {
  225. if ($vlan_at_port_by_snmp) {
  226. if (!empty($vlan_list[$row['snmp_index']])) {
  227. if (!empty($vlan_list[$row['snmp_index']]['pvid'])) {
  228. if ($vlan_list[$row['snmp_index']]['pvid'] >= 1 and $vlan_list[$row['snmp_index']]['pvid'] <= 4094) {
  229. $new_info['vlan'] = $vlan_list[$row['snmp_index']]['pvid'];
  230. } else {
  231. $new_info['vlan'] = 1;
  232. }
  233. }
  234. if (!empty($vlan_list[$row['snmp_index']]['tagged'])) {
  235. $new_info['tagged_vlan'] = $vlan_list[$row['snmp_index']]['tagged'];
  236. } else {
  237. $new_info['tagged_vlan'] = '';
  238. }
  239. if (!empty($vlan_list[$row['snmp_index']]['untagged'])) {
  240. $new_info['untagged_vlan'] = $vlan_list[$row['snmp_index']]['untagged'];
  241. } else {
  242. $new_info['untagged_vlan'] = '';
  243. }
  244. }
  245. } else {
  246. if (!empty($vlan_list[$row['port']])) {
  247. if (!empty($vlan_list[$row['port']]['pvid'])) {
  248. if ($vlan_list[$row['port']]['pvid'] >= 1 and $vlan_list[$row['port']]['pvid'] <= 4094) {
  249. $new_info['vlan'] = $vlan_list[$row['port']]['pvid'];
  250. } else {
  251. $new_info['vlan'] = 1;
  252. }
  253. }
  254. if (!empty($vlan_list[$row['port']]['tagged'])) {
  255. $new_info['tagged_vlan'] = $vlan_list[$row['port']]['tagged'];
  256. } else {
  257. $new_info['tagged_vlan'] = '';
  258. }
  259. if (!empty($vlan_list[$row['port']]['untagged'])) {
  260. $new_info['untagged_vlan'] = $vlan_list[$row['port']]['untagged'];
  261. } else {
  262. $new_info['untagged_vlan'] = '';
  263. }
  264. }
  265. }
  266. $display_vlan = '';
  267. if (!empty($new_info['vlan'])) {
  268. $display_vlan = $new_info['vlan'];
  269. }
  270. if (!empty($new_info['untagged_vlan'])) {
  271. if ($new_info['untagged_vlan'] != $new_info['vlan']) {
  272. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  273. $replacement = '${1},${2},${3},${4},${5}<br>U:';
  274. $display_untagged = preg_replace($pattern, $replacement, $new_info['untagged_vlan']);
  275. $display_vlan .= ";U:" . $display_untagged;
  276. }
  277. }
  278. if (!empty($new_info['tagged_vlan'])) {
  279. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  280. $replacement = '${1},${2},${3},${4},${5}<br>T:';
  281. $display_tagged = preg_replace($pattern, $replacement, $new_info['tagged_vlan']);
  282. $display_vlan .= ";T:" . $display_tagged;
  283. }
  284. }
  285. //interface name
  286. if (!empty($ifmib_list[$row['snmp_index']])) {
  287. $ifname = $ifmib_list[$row['snmp_index']];
  288. }
  289. if (!isset($row['ifname']) or $row['ifname'] !== $ifname) {
  290. $new_info['ifname'] = $ifname;
  291. }
  292. }
  293. //fix port information
  294. if ($snmp_ok) {
  295. update_record($db_link, "device_ports", "id=?", $new_info, [ $row['id'] ]);
  296. }
  297. $ifname = compact_port_name($ifname);
  298. $f_cacti_url = get_cacti_graph($device['ip'], $row['snmp_index']);
  299. if (empty(get_const('torrus_url')) and (empty($f_cacti_url))) {
  300. $snmp_url = $ifname;
  301. } else {
  302. if (isset($f_cacti_url)) {
  303. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($f_cacti_url) . "return false;\">" . $ifname . "</a>";
  304. }
  305. if (!empty(get_const('torrus_url'))) {
  306. $normed_ifname = str_replace("/", "_", $ifname);
  307. $normed_ifname = str_replace(".", "_", $normed_ifname);
  308. $normed_ifname = trim(str_replace(" ", "_", $normed_ifname));
  309. $t_url = str_replace("HOST_IP", $device['ip'], get_const('torrus_url'));
  310. $t_url = str_replace("IF_NAME", $normed_ifname, $t_url);
  311. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($t_url) . "return false;\">" . $ifname . "</a>";
  312. }
  313. }
  314. print "<td class='" . $cl . "'>" . $display_vlan . "</td>\n";
  315. print "<td class='" . $cl . "'>" . $snmp_url . "</td>\n";
  316. $speed = "0";
  317. $cl_speed = $cl;
  318. if ($pspeed == 0) {
  319. $speed = "";
  320. }
  321. if ($pspeed == 10000000) {
  322. $speed = "10M";
  323. $cl_speed = "speed10M";
  324. }
  325. if ($pspeed == 100000000) {
  326. $speed = "100M";
  327. $cl_speed = "speed100M";
  328. }
  329. if ($pspeed == 1000000000) {
  330. $speed = "1G";
  331. $cl_speed = "speed1G";
  332. }
  333. if ($pspeed == 10000000000) {
  334. $speed = "10G";
  335. $cl_speed = "speed10G";
  336. }
  337. if ($pspeed == 4294967295) {
  338. $speed = "10G";
  339. $cl_speed = "speed10G";
  340. }
  341. if ($pspeed == 10) {
  342. $speed = "10G";
  343. $cl_speed = "speed10G";
  344. }
  345. print "<td class=\"$cl_speed\">" . $speed . "</td>\n";
  346. $cl_error = $cl;
  347. if ($perrors > 0) {
  348. $cl_error = "crc";
  349. }
  350. print "<td class=\"$cl_error\">" . $perrors . "</td>\n";
  351. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($f_cacti_url) . " return false;\">" . $ifname . "</a>";
  352. print "<td class='" . $cl . "' ><button onclick=\"" . open_window_url('portmactable.php?id=' . $row['id']) . " return false;\">" . $row['last_mac_count'] . "</button></td>\n";
  353. print "<td class='" . $cl . "'>" . $sfp_status . " " . $poe_info . "</td>\n";
  354. if ($device_model['poe_out']) {
  355. if (isset($poe_status) and !$row['skip']) {
  356. print "<td class=\"data\">";
  357. if ($device['vendor_id'] != 9) {
  358. if ($poe_status == 2) {
  359. print "<button name='poe_on[]' value='{$row['snmp_index']}'>" . WEB_device_poe_on . "</button>";
  360. }
  361. if ($poe_status == 1) {
  362. print "<button name='poe_off[]' value='{$row['snmp_index']}'>" . WEB_device_poe_off . "</button>";
  363. }
  364. } else {
  365. print WEB_msg_unsupported;
  366. }
  367. print "</td>\n";
  368. } else {
  369. print "<td class=\"data\">";
  370. print WEB_msg_unsupported;
  371. print "</td>\n";
  372. }
  373. }
  374. if (isset($padmin) and !$row['uplink'] and !$row['skip']) {
  375. print "<td class=\"data\">";
  376. if ($device['vendor_id'] != 9) {
  377. if ($padmin >= 2) {
  378. print "<button name='port_on[]' value='{$row['snmp_index']}'>" . WEB_device_port_on . "</button>";
  379. }
  380. if ($padmin == 1) {
  381. print "<button name='port_off[]' value='{$row['snmp_index']}'>" . WEB_device_port_off . "</button>";
  382. }
  383. } else {
  384. print WEB_msg_unsupported;
  385. }
  386. print "</td>\n";
  387. }
  388. print "</tr>";
  389. }
  390. print "<tr>\n";
  391. print "</table>\n";
  392. ?>
  393. <div>
  394. <?php echo WEB_device_first_port_snmp_value; ?>
  395. &nbsp
  396. <input type='text' name='f_snmp_start' value=1>
  397. <input type='submit' name='regensnmp' value='<?php echo WEB_device_recalc_snmp_port ?>'>
  398. </div>
  399. <?php
  400. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  401. print "<tr><td>" . WEB_port_status . "</td></tr>\n";
  402. print "<tr><td class=\"down\">" . WEB_port_oper_down . "</td>";
  403. print "<td class=\"up\">" . WEB_port_oper_up . "</td>";
  404. print "<td class=\"shutdown\">" . WEB_port_admin_shutdown . "</td><tr>\n";
  405. print "</table>\n";
  406. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  407. print "<tr><td>" . WEB_port_speed . "</td></tr>\n";
  408. print "<tr><td class=\"speed10M\">" . WEB_port_speed_10 . "</td><td class=\"speed100M\">" . WEB_port_speed_100 . "</td><td class=\"speed1G\">" . WEB_port_speed_1G . "</td><td class=\"speed10G\">" . WEB_port_speed_10G . "</td><tr>\n";
  409. print "</table>\n";
  410. print "</form>";
  411. unset_lock_discovery($db_link, $id);
  412. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php");
  413. ?>