switchstatus.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . $language . ".php");
  4. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/idfilter.php");
  5. $switch=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. }
  18. if (isset($_POST['poe_on']) and $switch['snmp_version']>0) {
  19. $len = is_array($_POST['poe_on']) ? count($_POST['poe_on']) : 0;
  20. for ($i = 0; $i < $len; $i ++) {
  21. $port_index = intval($_POST['poe_on'][$i]);
  22. LOG_DEBUG($db_link, "Device id: $id enable poe at port snmp index $port_index");
  23. set_port_poe_state($switch['vendor_id'], $port_index, $switch['ip'], $switch['rw_community'], $switch['snmp_version'], 1);
  24. }
  25. header("Location: " . $_SERVER["REQUEST_URI"]);
  26. }
  27. if (isset($_POST['poe_off']) and $switch['snmp_version']>0) {
  28. $len = is_array($_POST['poe_off']) ? count($_POST['poe_off']) : 0;
  29. for ($i = 0; $i < $len; $i ++) {
  30. $port_index = intval($_POST['poe_off'][$i]);
  31. LOG_DEBUG($db_link, "Device id: $id disable poe at port snmp index $port_index");
  32. set_port_poe_state($switch['vendor_id'], $port_index, $switch['ip'], $switch['rw_community'], $switch['snmp_version'], 0);
  33. }
  34. header("Location: " . $_SERVER["REQUEST_URI"]);
  35. }
  36. if (isset($_POST['port_on']) and $switch['snmp_version']>0) {
  37. $len = is_array($_POST['port_on']) ? count($_POST['port_on']) : 0;
  38. for ($i = 0; $i < $len; $i ++) {
  39. $port_index = intval($_POST['port_on'][$i]);
  40. LOG_DEBUG($db_link, "Device id: $id enable port with snmp index $port_index");
  41. set_port_state($switch['vendor_id'], $port_index, $switch['ip'], $switch['rw_community'], $switch['snmp_version'], 1);
  42. }
  43. header("Location: " . $_SERVER["REQUEST_URI"]);
  44. }
  45. if (isset($_POST['port_off']) and $switch['snmp_version']>0) {
  46. $len = is_array($_POST['port_off']) ? count($_POST['port_off']) : 0;
  47. for ($i = 0; $i < $len; $i ++) {
  48. $port_index = intval($_POST['port_off'][$i]);
  49. LOG_DEBUG($db_link, "Device id: $id disable port with snmp index $port_index");
  50. set_port_state($switch['vendor_id'], $port_index, $switch['ip'], $switch['rw_community'], $switch['snmp_version'], 0);
  51. }
  52. header("Location: " . $_SERVER["REQUEST_URI"]);
  53. }
  54. unset($_POST);
  55. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  56. print_editdevice_submenu($page_url,$id);
  57. ?>
  58. <div id="cont">
  59. <form name="def" action="switchstatus.php?id=<? echo $id; ?>" method="post">
  60. <?php
  61. print "<br>\n";
  62. print "<b>Состояние портов ".$switch['device_name']." - ".$switch['ip']."</b><br>\n";
  63. if ($switch['snmp_version']>0) {
  64. $snmp_ok = check_snmp_access($switch['ip'], $switch['community'], $switch['snmp_version']);
  65. if ($snmp_ok) {
  66. global $cisco_modules;
  67. if ($switch['snmp_version'] == 2) {
  68. $modules_oids = snmp2_real_walk($switch['ip'], $switch['community'], $cisco_modules);
  69. }
  70. if ($switch['snmp_version'] == 1) {
  71. $modules_oids = snmprealwalk($switch['ip'], $switch['community'], $cisco_modules);
  72. }
  73. }
  74. } else { $snmp_ok = 0; }
  75. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  76. print "<tr>\n";
  77. print "<td>id</td>\n";
  78. print "<td>N</td>\n";
  79. print "<td>Порт</td>\n";
  80. print "<td>snmp</td>\n";
  81. print "<td>Юзер|Device</td>\n";
  82. print "<td>Комментарий</td>\n";
  83. print "<td>Uplink</td>\n";
  84. print "<td>Nagios</td>\n";
  85. print "<td>Skip</td>\n";
  86. print "<td>Vlan</td>\n";
  87. print "<td>IfName</td>\n";
  88. print "<td>Speed</td>\n";
  89. print "<td>Errors</td>\n";
  90. print "<td>Mac count</td>\n";
  91. print "<td>Additional</td>\n";
  92. print "<td>POE Control</td>\n";
  93. print "<td>Port Control</td>\n";
  94. print "</tr>\n";
  95. $sSQL = "SELECT * FROM device_ports WHERE device_ports.device_id=$id ORDER BY port";
  96. $ports=get_records_sql($db_link,$sSQL);
  97. foreach ($ports as $row) {
  98. print "<tr align=center>\n";
  99. $cl = "up";
  100. if (!isset($row['port_name']) or empty($row['port_name'])) {
  101. $new_port_info['port_name']=$row['port'];
  102. $row['port_name']=$row['port'];
  103. update_record($db_link, "device_ports", "id=".$row['id'], $new_port_info);
  104. }
  105. if (isset($switch['ip']) and ($switch['ip'] != '') and $snmp_ok) {
  106. $port_state_detail = get_port_state_detail($row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
  107. list ($poper, $padmin, $pspeed, $perrors) = explode(';', $port_state_detail);
  108. if (preg_match('/up/i', $poper)) {
  109. $cl = "up";
  110. }
  111. if (preg_match('/down/i', $poper)) {
  112. if (preg_match('/down/i', $padmin)) {
  113. $cl = "shutdown";
  114. } else {
  115. $cl = "down";
  116. }
  117. }
  118. }
  119. print "<td class=\"$cl\" style='padding:0'><input type=checkbox name=d_port_index[] value=".$row['snmp_index']." ></td>\n";
  120. print "<td class=\"$cl\"><a href=\"editport.php?id=".$row['id']."\">" . $row['port'] . "</a></td>\n";
  121. print "<td class=\"$cl\" >" . $row['port_name'] . "</td>\n";
  122. print "<td class=\"$cl\" >" . $row['snmp_index'] . "</td>\n";
  123. print "<td class=\"$cl\">";
  124. if (isset($row['target_port_id']) and $row['target_port_id'] > 0) {
  125. print_device_port($db_link, $row['target_port_id']);
  126. } else {
  127. print_auth_port($db_link, $row['id']);
  128. }
  129. print "</td>\n";
  130. print "<td class=\"$cl\">" . $row['comment'] . "</td>\n";
  131. print "<td class=\"$cl\" >" . get_qa($row['uplink']) . "</td>\n";
  132. print "<td class=\"$cl\" >" . get_qa($row['nagios']) . "</td>\n";
  133. print "<td class=\"$cl\" >" . get_qa($row['skip']) . "</td>\n";
  134. $poe_info="POE:None";
  135. $vlan = $row['vlan'];
  136. $ifname= $row['ifName'];
  137. if ($snmp_ok) {
  138. $vlan = get_port_vlan($row['port'], $row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
  139. $ifname = get_snmp_ifname($switch['ip'], $switch['community'], $switch['snmp_version'], $row['snmp_index']);
  140. $sfp_status = get_sfp_status($switch['vendor_id'], $row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version'], $modules_oids);
  141. $poe_status = get_port_poe_state($switch['vendor_id'], $row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version']);
  142. if (isset($poe_status)) {
  143. if ($poe_status == 1) {
  144. $port_poe_detail = get_port_poe_detail($switch['vendor_id'], $row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version']);
  145. $poe_info="POE:On " . $port_poe_detail;
  146. }
  147. if ($poe_status == 2) { $poe_info="POE:Off"; }
  148. }
  149. if (!isset($vlan)) { $vlan = $row['vlan']; } else {
  150. if ($row['vlan']!==$vlan) {
  151. $new_port_info['vlan']=$vlan;
  152. update_record($db_link, "device_ports", "id=".$row['id'], $new_port_info);
  153. }
  154. }
  155. if (empty($row['ifName']) or $row['ifName']!==$ifname) {
  156. $new_port_info['ifName']=$ifname;
  157. update_record($db_link, "device_ports", "id=".$row['id'], $new_port_info);
  158. }
  159. }
  160. global $torrus_url;
  161. $f_cacti_url = get_cacti_graph($switch['ip'], $row['snmp_index']);
  162. if (! isset($torrus_url) and (! isset($f_cacti_url))) { $snmp_url=$ifname; }
  163. else {
  164. if (isset($f_cacti_url)) { $snmp_url = "<a href=\"$f_cacti_url\">" . $ifname . "</a>"; }
  165. if (isset($torrus_url)) {
  166. $normed_ifname = trim(str_replace("/", "_", $ifname));
  167. $normed_ifname = trim(str_replace(".", "_", $normed_ifname));
  168. $normed_ifname = trim(str_replace(" ", "_", $normed_ifname));
  169. $pattern = '/cisco/i';
  170. preg_match($pattern, $switch['device_model'], $matches);
  171. if (isset($matches[0])) { $normed_ifname = trim(str_replace("Gi", "GigabitEthernet", $normed_ifname)); }
  172. $t_url = str_replace("HOST_IP", $switch['ip'], $torrus_url);
  173. $t_url = str_replace("IF_NAME", $normed_ifname, $t_url);
  174. $snmp_url = "<a href=\"$t_url\">" . $ifname . "</a>";
  175. }
  176. }
  177. print "<td class=\"$cl\">" . $vlan . "</td>\n";
  178. print "<td class=\"$cl\">" . $snmp_url . "</td>\n";
  179. $speed = "0";
  180. $cl_speed = $cl;
  181. if ($pspeed == 0) { $speed = ""; }
  182. if ($pspeed == 10000000) { $speed = "10M"; $cl_speed = "speed10M"; }
  183. if ($pspeed == 100000000) { $speed = "100M"; $cl_speed = "speed100M"; }
  184. if ($pspeed == 1000000000) { $speed = "1G"; $cl_speed = "speed1G"; }
  185. if ($pspeed == 10000000000) { $speed = "10G"; $cl_speed = "speed10G"; }
  186. if ($pspeed == 4294967295) { $speed = "10G"; $cl_speed = "speed10G"; }
  187. if ($pspeed == 10) { $speed = "10G"; $cl_speed = "speed10G"; }
  188. print "<td class=\"$cl_speed\">" . $speed . "</td>\n";
  189. $cl_error = $cl;
  190. if ($perrors > 0) { $cl_error = "crc"; }
  191. print "<td class=\"$cl_error\">" . $perrors . "</td>\n";
  192. 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";
  193. print "<td class=\"$cl\">" . $sfp_status. " ". $poe_info."</td>\n";
  194. if (isset($poe_status) and ! $row['skip'] and ! $switch['is_router']) {
  195. print "<td class=\"data\">";
  196. if ($switch['vendor_id'] != 9) {
  197. if ($poe_status == 2) {
  198. print "<button name='poe_on[]' value='{$row['snmp_index']}'>POE On</button>";
  199. }
  200. if ($poe_status == 1) {
  201. print "<button name='poe_off[]' value='{$row['snmp_index']}'>POE Off</button>";
  202. }
  203. } else {
  204. print "Not supported";
  205. }
  206. print "</td>\n";
  207. } else {
  208. print "<td>Not supported</td>\n";
  209. }
  210. if (isset($padmin) and ! $row['uplink'] and ! $row['skip'] and ! $switch['is_router']) {
  211. print "<td class=\"data\">";
  212. if ($switch['vendor_id'] != 9) {
  213. if (preg_match('/down/i', $padmin)) {
  214. print "<button name='port_on[]' value='{$row['snmp_index']}'>Enable port</button>";
  215. }
  216. if (preg_match('/up/i', $padmin)) {
  217. print "<button name='port_off[]' value='{$row['snmp_index']}'>Shutdown port</button>";
  218. }
  219. } else {
  220. print "Not supported";
  221. }
  222. print "</td>\n";
  223. }
  224. print "</tr>";
  225. }
  226. print "<tr>\n";
  227. print "<td colspan=10>snmp start &nbsp<input type=\"text\" name='f_snmp_start' value=1></td>\n";
  228. print "<td><input type=\"submit\" name=\"regensnmp\" value=\"Обновить snmp\"></td>\n";
  229. print "</tr>\n";
  230. print "</table>\n";
  231. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  232. print "<tr><td>Port status</td></tr>\n";
  233. print "<tr><td class=\"down\">Oper down</td><td class=\"up\">Oper up</td><td class=\"shutdown\">Admin shutdown</td><tr>\n";
  234. print "</table>\n";
  235. print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
  236. print "<tr><td>Port speed</td></tr>\n";
  237. print "<tr><td class=\"speed10M\">10M</td><td class=\"speed100M\">100M</td><td class=\"speed1G\">1G</td><td class=\"speed10G\">10G</td><tr>\n";
  238. print "</table>\n";
  239. print "</form>";
  240. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.small.php");
  241. ?>