index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. #-
  3. # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. #
  27. #
  28. include_once 'db.inc.php';
  29. include_once 'swreg_web.inc.php';
  30. require_once 'MDB2.php';
  31. require_once 'Log.php';
  32. require_once 'functions.php';
  33. $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
  34. $options = array('result_buffering', false);
  35. $dbh = MDB2::factory($dsn, $options);
  36. if (PEAR::isError($dbh)) {
  37. die($dbh->getMessage());
  38. }
  39. $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
  40. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Physical Switches');
  41. if ($logger === FALSE) {
  42. die("Failed to open logfile.\n");
  43. }
  44. $mask = Log::UPTO(LOG_LEVEL);
  45. $logger->setMask($mask);
  46. $base = get_base($_SERVER['SCRIPT_NAME']);
  47. $errors = array();
  48. if (isset($_REQUEST['prev_checked'])) {
  49. $pc = $_REQUEST['prev_checked'];
  50. $pas = $_REQUEST['prev_assigned_switch'];
  51. $c = $_REQUEST['checked'];
  52. $as = $_REQUEST['assigned_switch'];
  53. $d = $_REQUEST['delete'];
  54. foreach ($pc as $swsn => $value) {
  55. if ($d[$swsn] == 1) {
  56. $sql = "DELETE FROM DEVICE_MAP WHERE serial_number='{$swsn}'";
  57. $res = $dbh->query($sql);
  58. if (PEAR::isError($res)) {
  59. array_push($errors, "Failed to delete switch {$swsn}: {$res->getUserInfo()}");
  60. } else {
  61. $logger->info("User {$_SERVER['REMOTE_USER']} deleted physical switch {$swsn}");
  62. }
  63. continue;
  64. }
  65. if ($value != $c[$swsn] || $pas[$swsn] != $as[$swsn]) {
  66. $nc = ($c[$swsn] == 1) ? 1 : 0;
  67. if ($nc == 1 && $as == '__BOGUS__') {
  68. array_push($errors, "You must select a logical switch assignment for $swsn");
  69. } else {
  70. if ($nc == 0 || $as == '__BOGUS__') {
  71. $nas = "NULL";
  72. } else {
  73. $nas = "'{$as[$swsn]}'";
  74. }
  75. $sql = "UPDATE DEVICE_MAP SET checked_out='{$nc}', assigned_switch = {$nas} WHERE serial_number='{$swsn}'";
  76. $res = $dbh->query($sql);
  77. if (PEAR::isError($res)) {
  78. array_push($errors, "Failed to update $swsn: '{$res->getUserInfo()}'");
  79. } else {
  80. $logger->info("User {$_SERVER['REMOTE_USER']} updated physical switches, checked_out={$nc}, assigned_switch={$nas} for serial_number={$swsn}");
  81. }
  82. }
  83. }
  84. }
  85. }
  86. $wc = '';
  87. if (isset($_REQUEST['filter'])) {
  88. switch ($_REQUEST['filter']) {
  89. case 'a':
  90. $wc = '';
  91. break;
  92. case 'p':
  93. $wc = 'WHERE assigned_switch IS NOT NULL';
  94. break;
  95. case 'up':
  96. $wc = 'WHERE assigned_switch IS NULL';
  97. break;
  98. case 'vf':
  99. $wc = 'WHERE provisioned_status=0';
  100. break;
  101. case 'vs':
  102. $wc = 'WHERE provisioned_status=1';
  103. break;
  104. case 'r':
  105. $wc = 'WHERE device_status=2';
  106. break;
  107. case 'ur':
  108. $wc = 'WHERE device_status=0 OR device_status=1';
  109. break;
  110. default:
  111. if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) {
  112. $wc = "WHERE pid = '{$match[1]}'";
  113. } else {
  114. $wc = '';
  115. }
  116. break;
  117. }
  118. }
  119. if (isset($_REQUEST['switch_name'])) {
  120. $sql = "SELECT * FROM DEVICE_MAP WHERE serial_number LIKE '%{$_REQUEST['switch_name']}%' OR assigned_switch LIKE '%{$_REQUEST['switch_name']}%'";
  121. $res = $dbh->query($sql);
  122. if (PEAR::isError($res)) {
  123. echo "<p><font color=\"red\">Error querying for physical switches: {$res->getUserInfo()}</font></p>\r\n";
  124. exit(1);
  125. }
  126. } else {
  127. $sql = 'SELECT * FROM DEVICE_MAP ' . $wc . ' ORDER BY serial_number';
  128. $res = $dbh->query($sql);
  129. if (PEAR::isError($res)) {
  130. echo "<p><font color=\"red\">Error querying for physical switches: {$res->getUserInfo()}</font></p>\r\n";
  131. exit(1);
  132. }
  133. }
  134. $sql = 'SELECT name, pid FROM SWITCHES ORDER BY name';
  135. $ls_res = $dbh->query($sql);
  136. if (PEAR::isError($ls_res)) {
  137. echo "<p><font color=\"red\">Error querying for logical switches: {$ls_res->getUserInfo()}</font></p>\r\n";
  138. exit(1);
  139. }
  140. $lswitches = array();
  141. while ($row = $ls_res->fetchRow()) {
  142. if (!isset($lswitches[$row['pid']])) {
  143. $lswitches[$row['pid']] = array('__BOGUS__', $row['name']);
  144. } else {
  145. array_push($lswitches[$row['pid']], $row['name']);
  146. }
  147. }
  148. $switches = array();
  149. while ($row = $res->fetchRow()) {
  150. array_push($switches, $row);
  151. }
  152. $sql = 'SELECT DISTINCT(pid) AS pid FROM DEVICE_MAP';
  153. $res = $dbh->query($sql);
  154. if (PEAR::isError($res)) {
  155. echo "<p><font color=\"red\">Error querying for PIDs: {$res->getUserInfo()}</font></p>\r\n";
  156. exit(1);
  157. }
  158. $pids = array();
  159. while ($row = $res->fetchRow()) {
  160. $pids[$row['pid']] = TRUE;
  161. }
  162. $sql = 'SELECT assigned_switch FROM DEVICE_MAP WHERE assigned_switch IS NOT NULL';
  163. $res = $dbh->query($sql);
  164. if (PEAR::isError($res)) {
  165. echo "<p><font color=\"red\">Error querying for assigned switches: {$res->getUserInfo()}</font></p>\r\n";
  166. exit(1);
  167. }
  168. $used = array();
  169. while ($row = $res->fetchRow()) {
  170. $used[$row['assigned_switch']] = TRUE;
  171. }
  172. $filt = NULL;
  173. if (!isset($_REQUEST['filter'])) {
  174. $filt = '';
  175. } else {
  176. $filt = $_REQUEST['filter'];
  177. }
  178. print_header(TOOL_NAME . ": Physical Switches");
  179. ?>
  180. <body class="flex" onload='refresh();'>
  181. <script language="javascript">
  182. var deletes = 0;
  183. function refresh() {
  184. setTimeout(function() {
  185. window.location.replace(window.location.pathname + window.location.search);
  186. }, 300000);
  187. }
  188. </script>
  189. <div id="headswreg">
  190. <div class="apage">
  191. <div id="header">
  192. <h1><?=TOOL_NAME?>: Physical Switches</h1>
  193. </div>
  194. <br/>
  195. <div>
  196. Total Physical Switches Displayed: <b><?=count($switches)?></b>
  197. </div>
  198. </div>
  199. </div>
  200. <div class="apage">
  201. <form method="POST" name="search_form" action="<?=$_SERVER['PHP_SELF']?>">
  202. <table class="noborder" summary="Filter Table">
  203. <tr>
  204. <td class="left_act">Show Only:
  205. <select name="filter" onChange="MM_jumpMenu('parent', this, 0)">
  206. <option value="<?=$_SERVER['PHP_SELF']?>?filter=a" <?=($filt == 'a') ? 'selected' : ''?>>All</option>
  207. <option value="<?=$_SERVER['PHP_SELF']?>?filter=p" <?=($filt == 'p') ? 'selected' : ''?>>Provisioned</option>
  208. <option value="<?=$_SERVER['PHP_SELF']?>?filter=up" <?=($filt == 'up') ? 'selected' : ''?>>Unprovisioned</option>
  209. <option value="<?=$_SERVER['PHP_SELF']?>?filter=vf" <?=($filt == 'vf') ? 'selected' : ''?>>Verification Failed</option>
  210. <option value="<?=$_SERVER['PHP_SELF']?>?filter=vs" <?=($filt == 'vs') ? 'selected' : ''?>>Verification Succeeded</option>
  211. <option value="<?=$_SERVER['PHP_SELF']?>?filter=r" <?=($filt == 'r') ? 'selected' : ''?>>Reachable</option>
  212. <option value="<?=$_SERVER['PHP_SELF']?>?filter=ur" <?=($filt == 'ur') ? 'selected' : ''?>>Unreachable</option>
  213. <?php
  214. foreach ($pids as $lsp => $value) {
  215. $selected = ($filt == "pid:$lsp") ? 'selected' : '';
  216. ?>
  217. <option value="<?=$_SERVER['PHP_SELF']?>?filter=pid:<?=$lsp?>" <?=$selected?>><?=$lsp?></option>
  218. <?php
  219. }
  220. ?>
  221. </select></td>
  222. <td class="right">Switch Search:
  223. <input type="text" size="16" name="switch_name" value="<?=$_REQUEST['switch_name']?>">
  224. <input type="image" value="Submit" name="search" src="/images/submit_button.png">
  225. </td>
  226. </tr>
  227. </table>
  228. </form>
  229. <br/>
  230. <table class="noborder" summary="Control Table">
  231. <tr>
  232. <td class="left"><a href="<?=$base?>/add_phys.php">Add Physical Switch</a> |
  233. <a href="#" onClick="window.open('<?=$base?>/export.php')">Export to Prime / DNS</a> |
  234. <a href="<?=$base?>/logicsw.php">Logical Switches</a></td>
  235. </tr>
  236. </table>
  237. <form name="mod_phys_switch_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit='if (deletes > 0) { return confirm("Are you sure you want to delete these " + deletes + " physical switch(es)?"); }'>
  238. <table class="swreg" summary="Button Table">
  239. <tr>
  240. <td><input type="reset" value="Reset" name="reset">
  241. <input type="submit" value="Submit" name="submit"></td>
  242. </tr>
  243. </table>
  244. <br/>
  245. <div align="center">
  246. <?php
  247. foreach ($errors as $error) {
  248. ?>
  249. <p class="error"><?=$error?></p>
  250. <?php
  251. }
  252. ?>
  253. </div>
  254. <table class="swreg" summary="Physical Switch Table">
  255. <tr>
  256. <th class="delete">Row No.</th>
  257. <th class="delete">Delete?</th>
  258. <th class="delete">Provisioned?</th>
  259. <th class="headlink">Serial Number</th>
  260. <th class="headlink">Product ID</th>
  261. <th class="headlink">MAC Address</th>
  262. <th class="headlink">Max Ports</th>
  263. <th class="headlink">Assigned Logical Switch</th>
  264. <th class="delete">Provision Status</th>
  265. <th class="delete">Reachability</th>
  266. </tr>
  267. <?php
  268. foreach (array_merge($_GET, $_POST) as $name => $value) {
  269. if (!is_array($value)) {
  270. ?>
  271. <input type="hidden" name="<?=$name?>" value="<?=$value?>">
  272. <?php
  273. }
  274. }
  275. $i = 0;
  276. foreach ($switches as $row) {
  277. $sn = $row['serial_number'];
  278. $checked = FALSE;
  279. if ($row['checked_out'] == 1) {
  280. $checked = TRUE;
  281. }
  282. ?>
  283. <input type="hidden" name="prev_checked[<?=$sn?>]" value="<?=$row['checked_out']?>">
  284. <input type="hidden" name="prev_assigned_switch[<?=$sn?>]" value="<?=$row['assigned_switch']?>">
  285. <tr <?=get_row_color($i)?>>
  286. <td><?=$i + 1?>.</td>
  287. <td><input type="checkbox" name="delete[<?=$sn?>]" value="1" onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
  288. <td><input id="checked_<?=$i?>" type="checkbox" name="checked[<?=$sn?>]" value="1" <?=($checked) ? 'checked' : ''?>></td>
  289. <td><?=$row['serial_number']?></td>
  290. <td><?=$row['pid']?></td>
  291. <td><?=$row['mac']?></td>
  292. <td><?=$row['max_ports']?></td>
  293. <td><select id="asw<?=$i?>" name="assigned_switch[<?=$sn?>]" onChange='var elem = document.getElementById("checked_<?=$i?>"); if (this.value == "__BOGUS__") { elem.checked = false; } else { elem.checked = true; }'>
  294. <?php
  295. $has_avail = FALSE;
  296. foreach ($lswitches[$row['pid']] as $lsw) {
  297. $selected = ($row['assigned_switch'] == $lsw) ? "selected" : "";
  298. if ($selected == '' && isset($used[$lsw]) && $used[$lsw] === TRUE) {
  299. continue;
  300. }
  301. if ($lsw == '__BOGUS__') {
  302. ?>
  303. <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
  304. <?php
  305. } else {
  306. $has_avail = TRUE;
  307. ?>
  308. <option value="<?=$lsw?>" <?=$selected?>><?=$lsw?></option>
  309. <?php
  310. }
  311. }
  312. if (!$has_avail) {
  313. ?>
  314. <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
  315. <?php
  316. }
  317. ?>
  318. </select>
  319. &nbsp;<a href="#" onClick='var lsw = document.getElementById("asw<?=$i?>"); if (lsw.value == "__BOGUS__") { alert("There is no logical switch associated to this device."); return false; } else { window.open("<?=$base?>/logicsw.php?switch_name=" + lsw.value, "Logical switch " + lsw.value, "height=480,width=980"); return false; }'><img src="/images/switch.gif" border="0" title="View logical switch"></a>&nbsp;<a href="#" onClick='var pros = document.getElementById("pros<?=$i?>"); var lsw = document.getElementById("asw<?=$i?>"); if (pros.value == "0") { alert("This switch config has not yet been generated."); return false; } else { window.open("<?=$base?>/show_config.php?cfg=" + lsw.value + "-config.txt&dname=" + lsw.value, "Config for switch " + lsw.value, "height=650,width=980"); return false; }'><img src="/images/mag.gif" border="0" title="View switch config"></a>
  320. </td>
  321. <?php
  322. $color = '#FFFFFF';
  323. $rtext = 'N/A';
  324. $rtitle = 'Not yet provisioned';
  325. if ($checked) {
  326. $rtext = '&nbsp;';
  327. if ($row['provisioned_status'] == 1) {
  328. $color = '#00FF00';
  329. $rtitle = 'Config validated successfully';
  330. } else {
  331. $color = '#FF0000';
  332. $rtitle = 'Config validation failed';
  333. }
  334. }
  335. ?>
  336. <td style="background: <?=$color?>;" title="<?=$rtitle?>"><?=$rtext?><input type="hidden" id="pros<?=$i?>" value="<?=$row['provisioned_status']?>"></td>
  337. <?php
  338. $color = '#FFFFFF';
  339. $rtext = 'N/A';
  340. $rtitle = 'Not yet provisioned';
  341. if ($checked) {
  342. $rtext = '&nbsp;';
  343. if ($row['device_status'] == 0) {
  344. $color = '#FF0000';
  345. $rtitle = 'Device was never reachable';
  346. } else if ($row['device_status'] == 1) {
  347. $color = '#FFFF00';
  348. $rtitle = 'Device has been reachable, but is not reachable now';
  349. } else {
  350. $color = '#00FF00';
  351. $rtitle = 'Device is reachable';
  352. }
  353. }
  354. ?>
  355. <td style="background: <?=$color?>;" title="<?=$rtitle?>"><?=$rtext?></td>
  356. </tr>
  357. <?php
  358. $i++;
  359. }
  360. ?>
  361. </table>
  362. <br/>
  363. <table class="swreg" summary="Button Table">
  364. <tr>
  365. <td><input type="reset" value="Reset" name="reset">
  366. <input type="submit" value="Submit" name="submit"></td>
  367. </tr>
  368. </table>
  369. </form>
  370. </div>
  371. </body>
  372. </html>
  373. <?php
  374. cleanup();
  375. ?>