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