123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- #-
- # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- # SUCH DAMAGE.
- #
- #
- include_once 'db.inc.php';
- include_once 'swreg_web.inc.php';
- require_once 'MDB2.php';
- require_once 'Log.php';
- require_once 'functions.php';
- $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
- $options = array('result_buffering', false);
- $dbh = MDB2::factory($dsn, $options);
- if (PEAR::isError($dbh)) {
- die($dbh->getMessage());
- }
- $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
- $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Physical Switches');
- if ($logger === FALSE) {
- die("Failed to open logfile.\n");
- }
- $mask = Log::UPTO(LOG_LEVEL);
- $logger->setMask($mask);
- $base = get_base($_SERVER['SCRIPT_NAME']);
- $errors = array();
- if (isset($_REQUEST['prev_checked'])) {
- $pc = $_REQUEST['prev_checked'];
- $pas = $_REQUEST['prev_assigned_switch'];
- $c = $_REQUEST['checked'];
- $as = $_REQUEST['assigned_switch'];
- $d = $_REQUEST['delete'];
- foreach ($pc as $swsn => $value) {
- if ($d[$swsn] == 1) {
- $sql = "DELETE FROM DEVICE_MAP WHERE serial_number='{$swsn}'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to delete switch {$swsn}: {$res->getUserInfo()}");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} deleted physical switch {$swsn}");
- }
- continue;
- }
- if ($value != $c[$swsn] || $pas[$swsn] != $as[$swsn]) {
- $nc = ($c[$swsn] == 1) ? 1 : 0;
- if ($nc == 1 && $as == '__BOGUS__') {
- array_push($errors, "You must select a logical switch assignment for $swsn");
- } else {
- if ($nc == 0 || $as == '__BOGUS__') {
- $nas = "NULL";
- } else {
- $nas = "'{$as[$swsn]}'";
- }
- $sql = "UPDATE DEVICE_MAP SET checked_out='{$nc}', assigned_switch = {$nas} WHERE serial_number='{$swsn}'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to update $swsn: '{$res->getUserInfo()}'");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} updated physical switches, checked_out={$nc}, assigned_switch={$nas} for serial_number={$swsn}");
- }
- }
- }
- }
- }
- $wc = '';
- if (isset($_REQUEST['filter'])) {
- switch ($_REQUEST['filter']) {
- case 'a':
- $wc = '';
- break;
- case 'p':
- $wc = 'WHERE assigned_switch IS NOT NULL';
- break;
- case 'up':
- $wc = 'WHERE assigned_switch IS NULL';
- break;
- case 'vf':
- $wc = 'WHERE provisioned_status=0';
- break;
- case 'vs':
- $wc = 'WHERE provisioned_status=1';
- break;
- case 'r':
- $wc = 'WHERE device_status=2';
- break;
- case 'ur':
- $wc = 'WHERE device_status=0 OR device_status=1';
- break;
- default:
- if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) {
- $wc = "WHERE pid = '{$match[1]}'";
- } else {
- $wc = '';
- }
- break;
- }
- }
- if (isset($_REQUEST['switch_name'])) {
- $sql = "SELECT * FROM DEVICE_MAP WHERE serial_number LIKE '%{$_REQUEST['switch_name']}%' OR assigned_switch LIKE '%{$_REQUEST['switch_name']}%'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for physical switches: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- } else {
- $sql = 'SELECT * FROM DEVICE_MAP ' . $wc . ' ORDER BY serial_number';
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for physical switches: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- }
- $sql = 'SELECT name, pid FROM SWITCHES ORDER BY name';
- $ls_res = $dbh->query($sql);
- if (PEAR::isError($ls_res)) {
- echo "<p><font color=\"red\">Error querying for logical switches: {$ls_res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- $lswitches = array();
- while ($row = $ls_res->fetchRow()) {
- if (!isset($lswitches[$row['pid']])) {
- $lswitches[$row['pid']] = array('__BOGUS__', $row['name']);
- } else {
- array_push($lswitches[$row['pid']], $row['name']);
- }
- }
- $switches = array();
- while ($row = $res->fetchRow()) {
- array_push($switches, $row);
- }
- $sql = 'SELECT DISTINCT(pid) AS pid FROM DEVICE_MAP';
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for PIDs: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- $pids = array();
- while ($row = $res->fetchRow()) {
- $pids[$row['pid']] = TRUE;
- }
- $sql = 'SELECT assigned_switch FROM DEVICE_MAP WHERE assigned_switch IS NOT NULL';
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for assigned switches: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- $used = array();
- while ($row = $res->fetchRow()) {
- $used[$row['assigned_switch']] = TRUE;
- }
- $filt = NULL;
- if (!isset($_REQUEST['filter'])) {
- $filt = '';
- } else {
- $filt = $_REQUEST['filter'];
- }
- print_header(TOOL_NAME . ": Physical Switches");
- ?>
- <body class="flex" onload='refresh();'>
- <script language="javascript">
- var deletes = 0;
- function refresh() {
- setTimeout(function() {
- window.location.replace(window.location.pathname + window.location.search);
- }, 300000);
- }
- </script>
- <div id="headswreg">
- <div class="apage">
- <div id="header">
- <h1><?=TOOL_NAME?>: Physical Switches</h1>
- </div>
- <br/>
- <div>
- Total Physical Switches Displayed: <b><?=count($switches)?></b>
- </div>
- </div>
- </div>
- <div class="apage">
- <form method="POST" name="search_form" action="<?=$_SERVER['PHP_SELF']?>">
- <table class="noborder" summary="Filter Table">
- <tr>
- <td class="left_act">Show Only:
- <select name="filter" onChange="MM_jumpMenu('parent', this, 0)">
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=a" <?=($filt == 'a') ? 'selected' : ''?>>All</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=p" <?=($filt == 'p') ? 'selected' : ''?>>Provisioned</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=up" <?=($filt == 'up') ? 'selected' : ''?>>Unprovisioned</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=vf" <?=($filt == 'vf') ? 'selected' : ''?>>Verification Failed</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=vs" <?=($filt == 'vs') ? 'selected' : ''?>>Verification Succeeded</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=r" <?=($filt == 'r') ? 'selected' : ''?>>Reachable</option>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=ur" <?=($filt == 'ur') ? 'selected' : ''?>>Unreachable</option>
- <?php
- foreach ($pids as $lsp => $value) {
- $selected = ($filt == "pid:$lsp") ? 'selected' : '';
- ?>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=pid:<?=$lsp?>" <?=$selected?>><?=$lsp?></option>
- <?php
- }
- ?>
- </select></td>
- <td class="right">Switch Search:
- <input type="text" size="16" name="switch_name" value="<?=$_REQUEST['switch_name']?>">
- <input type="image" value="Submit" name="search" src="/images/submit_button.png">
- </td>
- </tr>
- </table>
- </form>
- <br/>
- <table class="noborder" summary="Control Table">
- <tr>
- <td class="left"><a href="<?=$base?>/add_phys.php">Add Physical Switch</a> |
- <a href="#" onClick="window.open('<?=$base?>/export.php')">Export to Prime / DNS</a> |
- <a href="<?=$base?>/logicsw.php">Logical Switches</a></td>
- </tr>
- </table>
- <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)?"); }'>
- <table class="swreg" summary="Button Table">
- <tr>
- <td><input type="reset" value="Reset" name="reset">
- <input type="submit" value="Submit" name="submit"></td>
- </tr>
- </table>
- <br/>
- <div align="center">
- <?php
- foreach ($errors as $error) {
- ?>
- <p class="error"><?=$error?></p>
- <?php
- }
- ?>
- </div>
- <table class="swreg" summary="Physical Switch Table">
- <tr>
- <th class="delete">Row No.</th>
- <th class="delete">Delete?</th>
- <th class="delete">Provisioned?</th>
- <th class="headlink">Serial Number</th>
- <th class="headlink">Product ID</th>
- <th class="headlink">MAC Address</th>
- <th class="headlink">Max Ports</th>
- <th class="headlink">Assigned Logical Switch</th>
- <th class="delete">Provision Status</th>
- <th class="delete">Reachability</th>
- </tr>
- <?php
- foreach (array_merge($_GET, $_POST) as $name => $value) {
- if (!is_array($value)) {
- ?>
- <input type="hidden" name="<?=$name?>" value="<?=$value?>">
- <?php
- }
- }
- $i = 0;
- foreach ($switches as $row) {
- $sn = $row['serial_number'];
- $checked = FALSE;
- if ($row['checked_out'] == 1) {
- $checked = TRUE;
- }
- ?>
- <input type="hidden" name="prev_checked[<?=$sn?>]" value="<?=$row['checked_out']?>">
- <input type="hidden" name="prev_assigned_switch[<?=$sn?>]" value="<?=$row['assigned_switch']?>">
- <tr <?=get_row_color($i)?>>
- <td><?=$i + 1?>.</td>
- <td><input type="checkbox" name="delete[<?=$sn?>]" value="1" onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
- <td><input id="checked_<?=$i?>" type="checkbox" name="checked[<?=$sn?>]" value="1" <?=($checked) ? 'checked' : ''?>></td>
- <td><?=$row['serial_number']?></td>
- <td><?=$row['pid']?></td>
- <td><?=$row['mac']?></td>
- <td><?=$row['max_ports']?></td>
- <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; }'>
- <?php
- $has_avail = FALSE;
- foreach ($lswitches[$row['pid']] as $lsw) {
- $selected = ($row['assigned_switch'] == $lsw) ? "selected" : "";
- if ($selected == '' && isset($used[$lsw]) && $used[$lsw] === TRUE) {
- continue;
- }
- if ($lsw == '__BOGUS__') {
- ?>
- <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
- <?php
- } else {
- $has_avail = TRUE;
- ?>
- <option value="<?=$lsw?>" <?=$selected?>><?=$lsw?></option>
- <?php
- }
- }
- if (!$has_avail) {
- ?>
- <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
- <?php
- }
- ?>
- </select>
- <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> <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>
- </td>
- <?php
- $color = '#FFFFFF';
- $rtext = 'N/A';
- $rtitle = 'Not yet provisioned';
- if ($checked) {
- $rtext = ' ';
- if ($row['provisioned_status'] == 1) {
- $color = '#00FF00';
- $rtitle = 'Config validated successfully';
- } else {
- $color = '#FF0000';
- $rtitle = 'Config validation failed';
- }
- }
- ?>
- <td style="background: <?=$color?>;" title="<?=$rtitle?>"><?=$rtext?><input type="hidden" id="pros<?=$i?>" value="<?=$row['provisioned_status']?>"></td>
- <?php
- $color = '#FFFFFF';
- $rtext = 'N/A';
- $rtitle = 'Not yet provisioned';
- if ($checked) {
- $rtext = ' ';
- if ($row['device_status'] == 0) {
- $color = '#FF0000';
- $rtitle = 'Device was never reachable';
- } else if ($row['device_status'] == 1) {
- $color = '#FFFF00';
- $rtitle = 'Device has been reachable, but is not reachable now';
- } else {
- $color = '#00FF00';
- $rtitle = 'Device is reachable';
- }
- }
- ?>
- <td style="background: <?=$color?>;" title="<?=$rtitle?>"><?=$rtext?></td>
- </tr>
- <?php
- $i++;
- }
- ?>
- </table>
- <br/>
- <table class="swreg" summary="Button Table">
- <tr>
- <td><input type="reset" value="Reset" name="reset">
- <input type="submit" value="Submit" name="submit"></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
- <?php
- cleanup();
- ?>
|