function renameObjectPorts() { $object_id = getBypassValue(); $n = 0; foreach (getObjectPortsAndLinks($object_id) as $port) { $canon_pn = shortenPortName($port['name'], $port['object_id']); if ($canon_pn != $port['name']) { commitUpdatePort($object_id, $port['id'], $canon_pn, $port['oif_id'], $port['label'], $port['l2address'], $port['reservation_comment']); $n++; } } if ($n) { showSuccess("Renamed {$n} ports"); } else { showNotice("Nothing renamed"); } }
function copyLotOfObjects() { global $dbxlink; $dbrollback = 0; if (!$dbxlink->beginTransaction()) { throw new RTDatabaseError("can not start transaction"); } // do we need this ? $log = emptyLog(); $taglist = isset($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array(); assertUIntArg('global_type_id', TRUE); assertStringArg('namelist', TRUE); $global_type_id = $_REQUEST['global_type_id']; $source_object_id = $_REQUEST['object_id']; $source_object = spotEntity('object', $source_object_id); amplifyCell($source_object); // only call amplifyCell_object_Backend_Port if we have function linkmgmt_linkPorts from linkmgmt.php if (function_exists('amplifyCell_object_Backend_Port') && function_exists('linkmgmt_linkPorts')) { amplifyCell_object_Backend_Port($source_object); } if ($global_type_id == 0 or !strlen($_REQUEST['namelist'])) { // Log something reasonable with showError Here // We do not have names to copy our object to ! // Pls check what makes $global_type_id == 0 an error $log = mergeLogs($log, oneLiner(186)); return; } // The name extractor below was stolen from ophandlers.php:addMultiPorts() $names1 = explode("\n", $_REQUEST['namelist']); $names2 = array(); foreach ($names1 as $line) { $parts = explode('\\r', $line); reset($parts); if (!strlen($parts[0])) { continue; } else { $names2[] = rtrim($parts[0]); } } foreach ($names2 as $name_or_csv) { $label = ''; $asset_no = ''; $object_name = ''; $regexp = '/^\\"([^\\"]*)\\","([^\\"]*)\\","([^\\"]*)\\"/'; $object_name_or_csv = htmlspecialchars_decode($name_or_csv, ENT_QUOTES); // error_log( "$regexp $object_name" ); if (preg_match($regexp, $object_name_or_csv, $matches)) { $object_name = $matches[1]; $label = $matches[2]; $asset_no = $matches[3]; } else { $object_name = $name_or_csv; } try { $object_id = commitAddObject($object_name, $label, $global_type_id, $asset_no, $taglist); if (!$object_id) { throw new RTDatabaseError("could not create {$object_name}"); } $info = spotEntity('object', $object_id); amplifyCell($info); foreach ($source_object['ports'] as $source_port) { $update_port = 0; foreach ($info['ports'] as $new_port) { if ($new_port['name'] == $source_port['name']) { commitUpdatePort($object_id, $new_port['id'], $new_port['name'], $new_port['oif_id'], $source_port['label'], ""); $update_port = 1; } } if ($update_port) { true; } else { commitAddPort($object_id, $source_port['name'], sprintf("%s-%s", $source_port['iif_id'], $source_port['oif_id']), $source_port['label'], ""); } } // Copy Backendlinks only start if we ghave function linkmgmt_linkPorts from linkmgmt.php if (function_exists('amplifyCell_object_Backend_Port') && function_exists('linkmgmt_linkPorts')) { $info = spotEntity('object', $object_id); amplifyCell($info); amplifyCell_object_Backend_Port($info); /* showError( '<div align="left"><pre>\n===== Source Object ======\n\n' . varDumpToString ( $source_object ) . '\n\n===== New Object ======\n\n' . varDumpToString ( $info ) . '</pre></div>' ); */ $name_by_id = array(); foreach ($info['BackendPorts'] as $new_be_port) { $name_by_id[$new_be_port['name']] = $new_be_port['id']; } $linked_ports = array(); foreach ($source_object['BackendPorts'] as $source_be_port) { if ($source_be_port['object_id'] == $source_be_port['remote_object_id']) { // We have a Port that has the own object as remote object we want to copy this type of Linko // We have backend Links $new_be_port_a = $name_by_id[$source_be_port['name']]; $new_be_port_b = $name_by_id[$source_be_port['remote_name']]; if ($new_be_port_a && $new_be_port_b && !array_key_exists($new_be_port_a, $linked_ports) && !array_key_exists($new_be_port_b, $linked_ports)) { // error_log ( sprintf ('new_be_port_a %s // new_be_port_b %s // cableid %s', $new_be_port_a , $new_be_port_b, $source_be_port['cableid'] )); $ret_val = linkmgmt_linkPorts($new_be_port_a, $new_be_port_b, 'back', $source_be_port['cableid']); // error_log ( sprintf (' linkmgmt_linkPorts ret val: "%s" ', $ret_val)) ; if ($ret_val) { throw new RTDatabaseError("could not copy Backend Links for {$object_name} because: {$ret_val}"); } else { $linked_ports[$new_be_port_a] = True; $linked_ports[$new_be_port_b] = True; } } } } } // Copy attributes foreach (getAttrValues($source_object_id) as $record) { $value = $record['value']; switch ($record['type']) { case 'uint': case 'float': case 'string': $value = $record['value']; break; case 'dict': $value = $record['key']; break; default: } if (permitted(NULL, NULL, NULL, array(array('tag' => '$attr_' . $record['id'])))) { if (empty($value)) { commitUpdateAttrValue($object_id, $record['id']); } else { commitUpdateAttrValue($object_id, $record['id'], $value); } } else { showError('Permission denied, "' . $record['name'] . '" can not be set'); } } //$log = mergeLogs ($log, oneLiner (5, array ('<a href="' . // makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . // '">' . $info['dname'] . '</a>')) //); showSuccess(sprintf("Copied Object %s ; new Object: %s", $source_object['name'], formatPortLink($object_id, $info['dname'], 1, '', ''))); } catch (RTDatabaseError $e) { error_log("rolling back DB"); $dbrollback = 1; $dbxlink->rollBack(); $log = mergeLogs($log, oneLiner(147, array($object_name))); throw new RTDatabaseError($e->getMessage() . sprintf(' (%s)', $name_or_csv)); } } if (!$dbrollback) { $dbxlink->commit(); } // return buildWideRedirectURL ($log); }
function addMultiPorts() { assertStringArg('format'); assertStringArg('input'); assertStringArg('port_type'); $format = $_REQUEST['format']; $port_type = $_REQUEST['port_type']; $object_id = $_REQUEST['object_id']; // Input lines are escaped, so we have to explode and to chop by 2-char // \n and \r respectively. $lines1 = explode("\n", $_REQUEST['input']); foreach ($lines1 as $line) { $parts = explode('\\r', $line); reset($parts); if (!strlen($parts[0])) { continue; } else { $lines2[] = rtrim($parts[0]); } } $ports = array(); foreach ($lines2 as $line) { switch ($format) { case 'fisxii': $words = explode(' ', preg_replace('/[[:space:]]+/', ' ', $line)); list($slot, $port) = explode('/', $words[0]); $ports[] = array('name' => "e {$slot}/{$port}", 'l2address' => $words[8], 'label' => "slot {$slot} port {$port}"); break; case 'c3600asy': $words = explode(' ', preg_replace('/[[:space:]]+/', ' ', trim(substr($line, 3)))); /* How Async Lines are Numbered in Cisco 3600 Series Routers http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml Understanding 16- and 32-Port Async Network Modules http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml */ $async = $words[0]; $slot = floor(($async - 1) / 32); $octalgroup = floor(($async - 1 - $slot * 32) / 8); $cable = $async - $slot * 32 - $octalgroup * 8; $og_label[0] = 'async 0-7'; $og_label[1] = 'async 8-15'; $og_label[2] = 'async 16-23'; $og_label[3] = 'async 24-31'; $ports[] = array('name' => "async {$async}", 'l2address' => '', 'label' => "slot {$slot} " . $og_label[$octalgroup] . " cable {$cable}"); break; case 'fiwg': $words = explode(' ', preg_replace('/[[:space:]]+/', ' ', $line)); $ifnumber = $words[0] * 1; $ports[] = array('name' => "e {$ifnumber}", 'l2address' => "{$words[8]}", 'label' => "{$ifnumber}"); break; case 'ssv1': $words = explode(' ', $line); if (!strlen($words[0]) or !strlen($words[1])) { continue; } $ports[] = array('name' => $words[0], 'l2address' => $words[1], 'label' => ''); break; default: throw new InvalidRequestArgException('format', $format); break; } } // Create ports, if they don't exist. $added_count = $updated_count = $error_count = 0; foreach ($ports as $port) { $port_ids = getPortIDs($object_id, $port['name']); if (!count($port_ids)) { commitAddPort($object_id, $port['name'], $port_type, $port['label'], $port['l2address']); $added_count++; } elseif (count($port_ids) == 1) { commitUpdatePort($object_id, $port_ids[0], $port['name'], $port_type, $port['label'], $port['l2address']); $updated_count++; } } return showFuncMessage(__FUNCTION__, 'OK', array($added_count, $updated_count, $error_count)); }
function Update() { // Read uploaded file $lines = file($_FILES['userfile']['tmp_name']); // add file contents to facter array foreach ($lines as $line_num => $line) { $tmpfacter = explode("=>", $line, 2); $facter[trim($tmpfacter[0])] = str_replace('"', '', trim($tmpfacter[1])); } // Fix fqdn since all fields have \n inn them $facter['fqdn'] = str_replace("\n", "", $facter['fqdn']); // Check if it's an existing machine // 2011-08-31 <*****@*****.**> // * expanded query to try to match via facter Serialnumber to be able to // match unnamed HW assets. Serial is more precise and less likely to be changed. if (array_key_exists('serialnumber', $facter) && strlen($facter['serialnumber']) > 0 && $facter['serialnumber'] != 'Not Specified') { $query = "select id from RackObject where name = \"{$facter['fqdn']}\" OR asset_no = \"{$facter['serialnumber']}\" LIMIT 1"; } else { $query = "select id from RackObject where name = \"{$facter['fqdn']}\" LIMIT 1"; } unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; } // If it's a new machine if (!isset($id)) { // Check to see if it's a physical machine and get the correct id for Server if ($facter['is_virtual'] == "false") { // Find server id $query = "select dict_key from Dictionary where dict_value='Server' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(); if ($resultarray) { $virtual = $resultarray[0]['dict_key']; } } else { // Find virtual id $query = "select dict_key from Dictionary where dict_value='VM' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $virtual = $resultarray[0]['dict_key']; } } // Add the new machine $newmachine = commitAddObject($facter['fqdn'], "", $virtual, $value = ""); $type_id = getObjectTypeID($newmachine); } else { // Just set some fields I use later down for updating $newmachine = $id; $machineupdate = 1; $type_id = getObjectTypeID($newmachine); } // 2011-08-31 <*****@*****.**> // * Update (unique) name of object. if (array_key_exists('serialnumber', $facter) && strlen($facter['serialnumber']) > 0 && $facter['serialnumber'] != 'Not Specified') { unset($result); $query = "select * from RackObject where asset_no = \"{$facter['serialnumber']}\" LIMIT 1"; $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; $label = $resultarray[0]['label']; // Update FQDN commitUpdateObject($id, $facter['fqdn'], $label, 'no', $facter['serialnumber'], 'Facter Import::Update Common Name'); } } // Find HW type id // 2011-08-31 <*****@*****.**> // * adjust format to match default Dictionary Sets if ($facter['is_virtual'] == "false") { $iHWTemp = preg_match('([a-zA-Z]{1,})', $facter['manufacturer'], $matches); $sManufacturer = $matches[0]; $sHW = preg_replace('(\\ )', '\\1%GPASS%', $facter['productname']); $sHWType = $sManufacturer . ' ' . $sHW; $query = "select id from Attribute where name='HW type' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; // Update HW type $hw_dict_key = getdict($sHWType, $chapter = 11); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = $hw_dict_key); } //Also Check if HYPERVISOR if (isset($facter['is_hypervisor'])) { $query = "select id from Attribute where name REGEXP '^ *Hypervisor Type\$' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; // Update Hypervisor type $hypervisor_type = $facter['is_hypervisor']; $hypervisor_type_dict_key = getdict($hw = $hypervisor_type, $chapter = 10005); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = $hypervisor_type_dict_key); } //Set Value to Yes $query = "select id from Attribute where name REGEXP '^ *Hypervisor' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; // Update Hypervisor type $hypervisor = "Yes"; $hypervisor_dict_key = getdict($hypervisor, $chapter = 29); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = $hypervisor_dict_key); } //Find Running VMs $vms = explode(',', $facter['vms']); $vm_count = count($vms); for ($i = 0; $i < $vm_count; $i++) { //addToParent addVmToParent($vms[$i], $newmachine); } } else { $query = "select id from Attribute where name REGEXP '^ *Hypervisor' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; // Update Hypervisor type $hypervisor = "No"; $hypervisor_dict_key = getdict($hypervisor, $chapter = 29); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = ""); } } } // Find SW type id (OS) $query = "select id from Attribute where name REGEXP '^ *SW type\$' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $id = $resultarray[0]['id']; // Update SW type (OS) $osrelease = $facter['operatingsystem'] . '%GSKIP%' . $facter['operatingsystem'] . ' V' . $facter['operatingsystemrelease']; $os_dict_key = getdict($hw = $osrelease, $chapter = 13); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = $os_dict_key); } //Generic to read in from file $manifest_file = fopen("../plugins/manifest", "r") or die("Could not open manifest, make sure it is in the websrv root and called manifest \n"); while (!feof($manifest_file)) { $tmp_line = fgets($manifest_file); if (!empty($tmp_line) && !preg_match("/\\/\\//", $tmp_line)) { @(list($Fact, $Attr, $Chapter) = array_map('trim', explode(',', $tmp_line, 3))); //check for multi-facter names if (strstr($Fact, '.')) { @(list($Fact1, $Fact2) = array_map('trim', explode('.', $Fact))); $value = $facter[$Fact1] . ' ' . $facter[$Fact2]; if (!isset($facter[$Fact1]) || !isset($facter[$Fact2])) { echo "WARNING: {$Fact1} or {$Fact2} does not exist in Facter for this object \n"; continue; } } else { if (!isset($facter[$Fact])) { echo "WARNING: {$Fact} does not exist in Facter for this object \n"; continue; } else { $value = $facter[$Fact]; } } $query = "select id from Attribute where name REGEXP '^ *{$Attr}' LIMIT 1"; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); $id = $resultarray[0]['id']; if (!valid($type_id, $id)) { echo "WARNING: Not a valid Mapping for {$Fact} to {$Attr} for objectType {$type_id} \n"; } else { if ($resultarray) { if (!empty($Chapter)) { $name = $value; $name_dict_key = getdict($hw = $name, $chapter = $Chapter); commitUpdateAttrValue($object_id = $newmachine, $attr_id = $id, $value = $name_dict_key); } else { if (preg_match("/[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}/", $value) || preg_match("/[0-9]{1,2}\\-[0-9]{1,2}\\-[0-9]{4}/", $value) || preg_match("/[0-9]{4}\\-[0-9]{1,2}\\-[0-9]{1,2}/", $value)) { //handle dates commitUpdateAttrValue($newmachine, $id, strtotime($value)); } else { commitUpdateAttrValue($newmachine, $id, $value); } } } } } } fclose($manifest_file); // Add network interfaces // Create an array with interfaces $nics = explode(',', $facter['interfaces']); // Go through all interfaces and add IP and MAC $count = count($nics); for ($i = 0; $i < $count; $i++) { // Remove newline from the field $nics[$i] = str_replace("\n", "", $nics[$i]); // We generally don't monitor sit interfaces. // We don't do this for lo interfaces, too // 2011-08-31 <*****@*****.**> // * Only Document real interfaces, dont do bridges, bonds, vlan-interfaces // when they have no IP defined. if (preg_match('(_|^(bond|lo|sit|vnet|virbr|veth|peth))', $nics[$i]) != 0) { // do nothing } else { // Get IP if (isset($facter['ipaddress_' . $nics[$i]])) { $ip = $facter['ipaddress_' . $nics[$i]]; } // Get MAC if (isset($facter['macaddress_' . $nics[$i]])) { $mac = $facter['macaddress_' . $nics[$i]]; } //check if VM or not if ($facter['is_virtual'] == "false") { // Find 1000Base-T id $query = "select id from PortOuterInterface where oif_name REGEXP '^ *1000Base-T\$' LIMIT 1"; } else { // Find virtual port id $query = "select id from PortOuterInterface where oif_name REGEXP '^ *virtual port\$' LIMIT 1"; } unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $nictypeid = $resultarray[0]['id']; } // Remove newline from ip $ip = str_replace("\n", "", $ip); // Check to se if the interface has an ip assigned $query = "SELECT object_id FROM IPv4Allocation where object_id={$newmachine} and name=\"{$nics[$i]}\""; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { unset($id); $ipcheck = $resultarray; } // Check if it's been configured a port already $query = "SELECT id,iif_id FROM Port where object_id={$newmachine} and name=\"{$nics[$i]}\""; unset($result); $result = usePreparedSelectBlade($query); $resultarray = $result->fetchAll(PDO::FETCH_ASSOC); if ($resultarray) { $portid = $resultarray[0]['id']; unset($id); $portcheck = $resultarray; } // Add/update port // 2011-08-31 <*****@*****.**> // * Don't touch already existing complex ports if ($resultarray[0]['type'] != 9) { if (count($portcheck) == 1) { commitUpdatePort($newmachine, $portid, $nics[$i], $nictypeid, "Ethernet port", "{$mac}", NULL); } else { commitAddPort($object_id = $newmachine, $nics[$i], $nictypeid, 'Ethernet port', "{$mac}"); } } else { //We've got a complex port, don't touch it, it raises an error with 'Database error: foreign key violation' } if (count($ipcheck) == 1) { if ($ip) { updateAddress(ip_parse($ip), $newmachine, $nics[$i], 'regular'); } } else { if ($ip) { bindIpToObject(ip_parse($ip), $newmachine, $nics[$i], 'regular'); } } unset($portcheck); unset($ipcheck); unset($ip); unset($mac); } } //uncomment to start using auto tags //addTagToObject($facter, $newmachine); return buildRedirectURL(); }