switchstatus.php 20 KB

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