// 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/swreg.inc.php'; require_once 'Log.php'; require_once 'functions.php'; $dsn = "$db_driver:host=$db_host;dbname=$db_name"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $dbh = new PDO($dsn, $db_user, $db_pass, $options); } catch (PDOException $e) { die($e->getMessage()); } $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Physical Switches'); if ($logger === false) { die("Failed to open logfile.\n"); } $mask = Log::MAX(LOG_LEVEL); $logger->setMask($mask); $base = get_base($_SERVER['SCRIPT_NAME']); $ruser = ''; if (isset($_SERVER['REMOTE_USER'])) { $ruser = $_SERVER['REMOTE_USER']; } $errors = array(); if (isset($_REQUEST['prev_checked'])) { $pc = $_REQUEST['prev_checked']; $pas = $_REQUEST['prev_assigned_switch']; $c = (isset($_REQUEST['checked'])) ? $_REQUEST['checked'] : array(); $as = $_REQUEST['assigned_switch']; $d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array(); $rz = (isset($_REQUEST['reztp'])) ? $_REQUEST['reztp'] : array(); foreach ($pc as $swsn => $value) { $curr_del = (isset($d[$swsn])) ? $d[$swsn] : 0; $curr_rz = (isset($rz[$swsn])) ? $rz[$swsn] : 0; if ($curr_del == 1) { $sql = "DELETE FROM DEVICE_MAP WHERE serial_number=:sn"; try { $sth = $dbh->prepare($sql); $sth->execute(['sn' => $swsn]); $sth->closeCursor(); $logger->info("User {$ruser} deleted physical switch {$swsn}"); } catch (PDOException $e) { array_push($errors, "Failed to delete switch {$swsn}: {$e->getMessage()}"); } continue; } if ($curr_rz == 1) { $sql = "UPDATE DEVICE_MAP SET should_re_ztp=:rz WHERE serial_number=:sn"; try { $sth = $dbh->prepare($sql); $sth->execute(['sn' => $swsn, 'rz' => 2]); $sth->closeCursor(); $logger->info("User {$ruser} set physical switch {$swsn} to re-ZTP"); } catch (PDOException $e) { array_push($errors, "Failed to set switch {$swsn} to re-ZTP: {$e->getMessage()}"); } } $curr_checked = (isset($c[$swsn])) ? $c[$swsn] : 0; if ($value != $curr_checked || $pas[$swsn] != $as[$swsn]) { $nc = $curr_checked; $additional = ''; 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'; $additional = ", provisioned_status = '" . PROVISION_UNKNOWN . "', device_status = '" . REACHABILITY_NEVER_REACHABLE . "'"; } else { $nas = "'{$as[$swsn]}'"; } $sql = "UPDATE DEVICE_MAP SET checked_out='{$nc}', assigned_switch = {$nas} {$additional} WHERE serial_number='{$swsn}'"; try { $dbh->query($sql); $logger->info("User {$ruser} updated physical switches, checked_out={$nc}, assigned_switch={$nas} for serial_number={$swsn}"); } catch (PDOException $e) { array_push($errors, "Failed to update $swsn: '{$e->getMessage()}'"); } } } } } $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 'vu': $wc = 'WHERE provisioned_status=\'' . PROVISION_UNKNOWN . '\''; break; case 'vi': $wc = 'WHERE provisioned_status=\'' . PROVISION_IN_PROGRESS . '\''; break; case 'vf': $wc = 'WHERE provisioned_status=\'' . PROVISION_FAIL . '\''; break; case 'vs': $wc = 'WHERE provisioned_status=\'' . PROVISION_SUCCESS . '\''; break; case 'r': $wc = 'WHERE device_status=\'' . REACHABILITY_REACHABLE . '\''; break; case 'ur': $wc = 'WHERE provisioned_status=\'' . PROVISION_SUCCESS . '\' AND (device_status=\'' . REACHABILITY_NEVER_REACHABLE . '\' OR device_status=\'' . REACHABILITY_NOW_UNREACHABLE . '\')'; break; default: if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) { $wc = "WHERE pid = '{$match[1]}'"; } else { $wc = ''; } break; } } $res = null; if (isset($_REQUEST['switch_name'])) { $sql = "SELECT * FROM DEVICE_MAP WHERE serial_number LIKE :swname1 OR assigned_switch LIKE :swname2 ORDER BY LPAD(lower(serial_number), 10, 0)"; try { $res = $dbh->prepare($sql); $res->execute(['swname1' => "%{$_REQUEST['switch_name']}%", 'swname2' => "%{$_REQUEST['switch_name']}%"]); } catch (PDOException $e) { echo "

Error querying for physical switches: {$e->getMessage()}

\r\n"; exit(1); } } else { $sql = 'SELECT * FROM DEVICE_MAP ' . $wc . ' ORDER BY LPAD(lower(serial_number), 10, 0)'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for physical switches: {$e->getMessage()}

\r\n"; exit(1); } } $sql = 'SELECT name, pid FROM SWITCHES WHERE ip_address IS NOT NULL ORDER BY name'; $ls_res = null; try { $ls_res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for logical switches: {$e->getMessage()}

\r\n"; exit(1); } $lswitches = array(); while ($row = $ls_res->fetch()) { 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->fetch()) { array_push($switches, $row); } $sql = 'SELECT DISTINCT(pid) AS pid FROM DEVICE_MAP'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for PIDs: {$e->getMessage()}

\r\n"; exit(1); } $pids = array(); while ($row = $res->fetch()) { $pids[$row['pid']] = true; } $sql = 'SELECT assigned_switch FROM DEVICE_MAP WHERE assigned_switch IS NOT NULL'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for assigned switches: {$e->getMessage()}

\r\n"; exit(1); } $used = array(); while ($row = $res->fetch()) { $used[$row['assigned_switch']] = true; } $filt = null; if (!isset($_REQUEST['filter'])) { $filt = ''; } else { $filt = $_REQUEST['filter']; } print_header(TOOL_NAME . ': Physical Switches'); ?>

Show Only: Switch Search:

Add Physical Switch | Export to Prime / DNS | Export To Ansible | Logical Switches
$value) { if (!is_array($value)) { ?>