switchstatus.php 19 KB

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