switchstatus.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. $modules_oids = walk_snmp($device["ip"], $snmp, CISCO_MODULES);
  112. $vlan_list = get_switch_vlans($device['vendor_id'], $device['ip'], $snmp);
  113. //if port number 1 not exists - try detect by snmp interface index
  114. if (isset($vlan_list['1'])) {
  115. $vlan_at_port_by_snmp = 0;
  116. }
  117. }
  118. } else {
  119. $snmp_ok = 0;
  120. }
  121. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  122. print "<tr>\n";
  123. print "<td>id</td>\n";
  124. print "<td>" . WEB_device_port_number . "</td>\n";
  125. print "<td>" . WEB_device_port_name . "</td>\n";
  126. print "<td>" . WEB_device_port_snmp_index . "</td>\n";
  127. print "<td>" . WEB_device_connected_endpoint . "</td>\n";
  128. print "<td>" . WEB_cell_description . "</td>\n";
  129. print "<td>" . WEB_device_port_uplink . "</td>\n";
  130. print "<td>" . WEB_nagios . "</td>\n";
  131. print "<td>" . WEB_cell_skip . "</td>\n";
  132. print "<td>" . WEB_cell_vlan . "</td>\n";
  133. print "<td>" . WEB_device_snmp_port_oid_name . "</td>\n";
  134. print "<td>" . WEB_device_port_speed . "</td>\n";
  135. print "<td>" . WEB_device_port_errors . "</td>\n";
  136. print "<td>" . WEB_cell_mac_count . "</td>\n";
  137. print "<td>" . WEB_msg_additional . "</td>\n";
  138. if ($device_model['poe_out']) {
  139. print "<td>" . WEB_device_poe_control . "</td>\n";
  140. }
  141. print "<td>" . WEB_device_port_control . "</td>\n";
  142. print "</tr>\n";
  143. $sSQL = "SELECT * FROM device_ports WHERE device_ports.device_id=? ORDER BY port";
  144. $ports = get_records_sql($db_link, $sSQL, [ $id ]);
  145. foreach ($ports as $row) {
  146. print "<tr align=center>\n";
  147. $cl = "down";
  148. $new_info = NULL;
  149. $display_vlan = $row['vlan'];
  150. if (!empty($row['untagged_vlan'])) {
  151. if ($row['untagged_vlan'] != $row['vlan']) {
  152. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  153. $replacement = '${1},${2},${3},${4},${5}<br>U:';
  154. $display_untagged = preg_replace($pattern, $replacement, $row['untagged_vlan']);
  155. $display_vlan .= ";U:" . $display_untagged;
  156. }
  157. }
  158. if (!empty($row['tagged_vlan'])) {
  159. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  160. $replacement = '${1},${2},${3},${4},${5}<br>T:';
  161. $display_tagged = preg_replace($pattern, $replacement, $row['tagged_vlan']);
  162. $display_vlan .= ";T:" . $display_tagged;
  163. }
  164. //fix empty port names
  165. if (empty($row['port_name'])) {
  166. $row['port_name'] = $row['port'];
  167. $new_info['port_name'] = $row['port'];
  168. }
  169. if ($snmp_ok) {
  170. $port_state_detail = get_port_state_detail($row['snmp_index'], $device['ip'], $snmp);
  171. list($poper, $padmin, $pspeed, $perrors) = explode(';', $port_state_detail);
  172. if ($poper == 1) {
  173. $cl = "up";
  174. }
  175. if ($poper >= 2) {
  176. if ($padmin >= 2) {
  177. $cl = "shutdown";
  178. } else {
  179. $cl = "down";
  180. }
  181. }
  182. }
  183. print "<td class='" . $cl . "' style='padding:0'><input type=checkbox name=d_port_index[] value=" . $row['snmp_index'] . " ></td>\n";
  184. print "<td class='" . $cl . "'><a href=\"#\" onclick=\"" . open_window_url("editport.php?id=" . $row['id']) . " return false;\">" . $row['port'] . "</a></td>\n";
  185. print "<td class='" . $cl . "' >" . $row['port_name'] . "</td>\n";
  186. print "<td class='" . $cl . "' >" . $row['snmp_index'] . "</td>\n";
  187. print "<td class='" . $cl . "'>";
  188. if (isset($row['target_port_id']) and $row['target_port_id'] > 0) {
  189. print_device_port($db_link, $row['target_port_id']);
  190. } else {
  191. print_auth_port($db_link, $row['id'], TRUE);
  192. }
  193. print "</td>\n";
  194. print "<td class='" . $cl . "' >" . get_port_description($db_link, $row['id'], $row['description']) . "</td>\n";
  195. print_td_yes($row['uplink'],FALSE,$cl);
  196. print_td_yes($row['nagios'],FALSE,$cl);
  197. print_td_yes($row['skip'],FALSE,$cl);
  198. $poe_info = "";
  199. $ifname = $row['ifName'];
  200. if ($snmp_ok) {
  201. //sfp information
  202. $sfp_status = get_sfp_status($device['vendor_id'], $row['snmp_index'], $device['ip'], $snmp, $modules_oids);
  203. //interface name
  204. $ifname = get_snmp_ifname($device['ip'], $snmp, $row['snmp_index']);
  205. //poe information
  206. if ($device_model['poe_out']) {
  207. $poe_status = get_port_poe_state($device['vendor_id'], $row['port'], $row['snmp_index'], $device['ip'], $snmp);
  208. if (isset($poe_status)) {
  209. if ($poe_status == 1) {
  210. $port_poe_detail = get_port_poe_detail($device['vendor_id'], $row['port'], $row['snmp_index'], $device['ip'], $snmp);
  211. if (empty($port_poe_detail)) {
  212. $poe_info = 'POE:on';
  213. } else {
  214. $poe_info = $port_poe_detail;
  215. }
  216. }
  217. if ($poe_status == 2) {
  218. $poe_info = "POE:Off";
  219. }
  220. }
  221. }
  222. //vlans at port
  223. if (!empty($vlan_list)) {
  224. if ($vlan_at_port_by_snmp) {
  225. if (!empty($vlan_list[$row['snmp_index']])) {
  226. if (!empty($vlan_list[$row['snmp_index']]['pvid'])) {
  227. if ($vlan_list[$row['snmp_index']]['pvid'] >= 1 and $vlan_list[$row['snmp_index']]['pvid'] <= 4094) {
  228. $new_info['vlan'] = $vlan_list[$row['snmp_index']]['pvid'];
  229. } else {
  230. $new_info['vlan'] = 1;
  231. }
  232. }
  233. if (!empty($vlan_list[$row['snmp_index']]['tagged'])) {
  234. $new_info['tagged_vlan'] = $vlan_list[$row['snmp_index']]['tagged'];
  235. } else {
  236. $new_info['tagged_vlan'] = '';
  237. }
  238. if (!empty($vlan_list[$row['snmp_index']]['untagged'])) {
  239. $new_info['untagged_vlan'] = $vlan_list[$row['snmp_index']]['untagged'];
  240. } else {
  241. $new_info['untagged_vlan'] = '';
  242. }
  243. }
  244. } else {
  245. if (!empty($vlan_list[$row['port']])) {
  246. if (!empty($vlan_list[$row['port']]['pvid'])) {
  247. if ($vlan_list[$row['port']]['pvid'] >= 1 and $vlan_list[$row['port']]['pvid'] <= 4094) {
  248. $new_info['vlan'] = $vlan_list[$row['port']]['pvid'];
  249. } else {
  250. $new_info['vlan'] = 1;
  251. }
  252. }
  253. if (!empty($vlan_list[$row['port']]['tagged'])) {
  254. $new_info['tagged_vlan'] = $vlan_list[$row['port']]['tagged'];
  255. } else {
  256. $new_info['tagged_vlan'] = '';
  257. }
  258. if (!empty($vlan_list[$row['port']]['untagged'])) {
  259. $new_info['untagged_vlan'] = $vlan_list[$row['port']]['untagged'];
  260. } else {
  261. $new_info['untagged_vlan'] = '';
  262. }
  263. }
  264. }
  265. $display_vlan = '';
  266. if (!empty($new_info['vlan'])) {
  267. $display_vlan = $new_info['vlan'];
  268. }
  269. if (!empty($new_info['untagged_vlan'])) {
  270. if ($new_info['untagged_vlan'] != $new_info['vlan']) {
  271. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  272. $replacement = '${1},${2},${3},${4},${5}<br>U:';
  273. $display_untagged = preg_replace($pattern, $replacement, $new_info['untagged_vlan']);
  274. $display_vlan .= ";U:" . $display_untagged;
  275. }
  276. }
  277. if (!empty($new_info['tagged_vlan'])) {
  278. $pattern = '/(\d+),(\d+),(\d+),(\d+),(\d+),/';
  279. $replacement = '${1},${2},${3},${4},${5}<br>T:';
  280. $display_tagged = preg_replace($pattern, $replacement, $new_info['tagged_vlan']);
  281. $display_vlan .= ";T:" . $display_tagged;
  282. }
  283. }
  284. //interface name
  285. if (!empty($ifmib_list[$row['snmp_index']])) {
  286. $ifname = $ifmib_list[$row['snmp_index']];
  287. }
  288. if (!isset($row['ifName']) or $row['ifName'] !== $ifname) {
  289. $new_info['ifName'] = $ifname;
  290. }
  291. }
  292. //fix port information
  293. if ($snmp_ok) {
  294. update_record($db_link, "device_ports", "id=?", $new_info, [ $row['id'] ]);
  295. }
  296. $ifname = compact_port_name($ifname);
  297. $f_cacti_url = get_cacti_graph($device['ip'], $row['snmp_index']);
  298. if (empty(get_const('torrus_url')) and (empty($f_cacti_url))) {
  299. $snmp_url = $ifname;
  300. } else {
  301. if (isset($f_cacti_url)) {
  302. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($f_cacti_url) . "return false;\">" . $ifname . "</a>";
  303. }
  304. if (!empty(get_const('torrus_url'))) {
  305. $normed_ifname = str_replace("/", "_", $ifname);
  306. $normed_ifname = str_replace(".", "_", $normed_ifname);
  307. $normed_ifname = trim(str_replace(" ", "_", $normed_ifname));
  308. $t_url = str_replace("HOST_IP", $device['ip'], get_const('torrus_url'));
  309. $t_url = str_replace("IF_NAME", $normed_ifname, $t_url);
  310. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($t_url) . "return false;\">" . $ifname . "</a>";
  311. }
  312. }
  313. print "<td class='" . $cl . "'>" . $display_vlan . "</td>\n";
  314. print "<td class='" . $cl . "'>" . $snmp_url . "</td>\n";
  315. $speed = "0";
  316. $cl_speed = $cl;
  317. if ($pspeed == 0) {
  318. $speed = "";
  319. }
  320. if ($pspeed == 10000000) {
  321. $speed = "10M";
  322. $cl_speed = "speed10M";
  323. }
  324. if ($pspeed == 100000000) {
  325. $speed = "100M";
  326. $cl_speed = "speed100M";
  327. }
  328. if ($pspeed == 1000000000) {
  329. $speed = "1G";
  330. $cl_speed = "speed1G";
  331. }
  332. if ($pspeed == 10000000000) {
  333. $speed = "10G";
  334. $cl_speed = "speed10G";
  335. }
  336. if ($pspeed == 4294967295) {
  337. $speed = "10G";
  338. $cl_speed = "speed10G";
  339. }
  340. if ($pspeed == 10) {
  341. $speed = "10G";
  342. $cl_speed = "speed10G";
  343. }
  344. print "<td class=\"$cl_speed\">" . $speed . "</td>\n";
  345. $cl_error = $cl;
  346. if ($perrors > 0) {
  347. $cl_error = "crc";
  348. }
  349. print "<td class=\"$cl_error\">" . $perrors . "</td>\n";
  350. $snmp_url = "<a href=\"#\" onclick=\"" . open_window_url($f_cacti_url) . " return false;\">" . $ifname . "</a>";
  351. print "<td class='" . $cl . "' ><button onclick=\"" . open_window_url('portmactable.php?id=' . $row['id']) . " return false;\">" . $row['last_mac_count'] . "</button></td>\n";
  352. print "<td class='" . $cl . "'>" . $sfp_status . " " . $poe_info . "</td>\n";
  353. if ($device_model['poe_out']) {
  354. if (isset($poe_status) and !$row['skip']) {
  355. print "<td class=\"data\">";
  356. if ($device['vendor_id'] != 9) {
  357. if ($poe_status == 2) {
  358. print "<button name='poe_on[]' value='{$row['snmp_index']}'>" . WEB_device_poe_on . "</button>";
  359. }
  360. if ($poe_status == 1) {
  361. print "<button name='poe_off[]' value='{$row['snmp_index']}'>" . WEB_device_poe_off . "</button>";
  362. }
  363. } else {
  364. print WEB_msg_unsupported;
  365. }
  366. print "</td>\n";
  367. } else {
  368. print "<td class=\"data\">";
  369. print WEB_msg_unsupported;
  370. print "</td>\n";
  371. }
  372. }
  373. if (isset($padmin) and !$row['uplink'] and !$row['skip']) {
  374. print "<td class=\"data\">";
  375. if ($device['vendor_id'] != 9) {
  376. if ($padmin >= 2) {
  377. print "<button name='port_on[]' value='{$row['snmp_index']}'>" . WEB_device_port_on . "</button>";
  378. }
  379. if ($padmin == 1) {
  380. print "<button name='port_off[]' value='{$row['snmp_index']}'>" . WEB_device_port_off . "</button>";
  381. }
  382. } else {
  383. print WEB_msg_unsupported;
  384. }
  385. print "</td>\n";
  386. }
  387. print "</tr>";
  388. }
  389. print "<tr>\n";
  390. print "</table>\n";
  391. ?>
  392. <div>
  393. <?php echo WEB_device_first_port_snmp_value; ?>
  394. &nbsp
  395. <input type='text' name='f_snmp_start' value=1>
  396. <input type='submit' name='regensnmp' value='<?php echo WEB_device_recalc_snmp_port ?>'>
  397. </div>
  398. <?php
  399. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  400. print "<tr><td>" . WEB_port_status . "</td></tr>\n";
  401. print "<tr><td class=\"down\">" . WEB_port_oper_down . "</td>";
  402. print "<td class=\"up\">" . WEB_port_oper_up . "</td>";
  403. print "<td class=\"shutdown\">" . WEB_port_admin_shutdown . "</td><tr>\n";
  404. print "</table>\n";
  405. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  406. print "<tr><td>" . WEB_port_speed . "</td></tr>\n";
  407. 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";
  408. print "</table>\n";
  409. print "</form>";
  410. unset_lock_discovery($db_link, $id);
  411. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.simple.php");
  412. ?>