示例#1
0
文件: ophandlers.php 项目: xtha/salt
function updateRow()
{
    assertUIntArg('row_id');
    assertUIntArg('location_id', TRUE);
    assertStringArg('name');
    commitUpdateObject($_REQUEST['row_id'], $_REQUEST['name'], NULL, NULL, NULL, NULL);
    global $pageno;
    if ($pageno == 'row') {
        updateObjectAttributes($_REQUEST['row_id']);
    }
    $rowData = spotEntity('row', $_REQUEST['row_id']);
    // location_id was submitted, but no link exists - create it
    if ($_REQUEST['location_id'] > 0 && !$rowData['location_id']) {
        commitLinkEntities('location', $_REQUEST['location_id'], 'row', $_REQUEST['row_id']);
    }
    // location_id was submitted, but it doesn't match the existing link - update it
    if ($_REQUEST['location_id'] > 0 && $_REQUEST['location_id'] != $rowData['location_id']) {
        commitUpdateEntityLink('location', $rowData['location_id'], 'row', $_REQUEST['row_id'], 'location', $_REQUEST['location_id'], 'row', $_REQUEST['row_id']);
    }
    // no parent_id was submitted, but a link exists - delete it
    if ($_REQUEST['location_id'] == 0 && $rowData['location_id']) {
        commitUnlinkEntities('location', $rowData['location_id'], 'row', $_REQUEST['row_id']);
    }
    showFuncMessage(__FUNCTION__, 'OK', array($_REQUEST['name']));
}
function snmpgeneric_tabhandler($object_id)
{
    //	sg_var_dump_html($_POST);
    if (isset($_POST['asnewobject']) && $_POST['asnewobject'] == "1") {
        $newobject_name = $_POST['object_name'];
        $newobject_label = $_POST['object_label'];
        $newobject_type_id = $_POST['object_type_id'];
        $newobject_asset_no = $_POST['object_asset_no'];
        if (sg_checkObjectNameUniqueness($newobject_name, $newobject_type_id)) {
            $object_id = commitAddObject($newobject_name, $newobject_label, $newobject_type_id, $newobject_asset_no);
            $_POST['asnewobject'] = "0";
            parse_str($_SERVER['QUERY_STRING'], $query_string);
            $query_string['object_id'] = $object_id;
            $_SERVER['QUERY_STRING'] = http_build_query($query_string);
            list($path, $qs) = explode('?', $_SERVER['REQUEST_URI'], 2);
            $_SERVER['REQUEST_URI'] = $path . '?' . $_SERVER['QUERY_STRING'];
            // switch to new object
            echo '<body>';
            echo '<body onload="document.forms[\'newobject\'].submit();">';
            echo '<form method=POST id=newobject action=' . $_SERVER['REQUEST_URI'] . '>';
            foreach ($_POST as $name => $value) {
                echo "<input type=hidden name={$name} value={$value}>";
            }
            echo '<input type=submit id="submitbutton" tabindex="1" value="Show List">';
            echo '</from></body>';
            exit;
        } else {
            showError("Object with name: \"{$newobject_name}\" already exists!!!");
            $_POST['snmpconfig'] = "0";
        }
    }
    // save snmp settings
    if (isset($_POST['save']) && $_POST['save'] == "1") {
        // TODO save only on success !!
        $object = spotEntity('object', $object_id);
        $snmpvalues[0] = 'SNMP';
        $snmpnames = array('host', 'version', 'community');
        if ($_POST['version'] == "v3") {
            $snmpnames = array_merge($snmpnames, array('sec_level', 'auth_protocol', 'auth_passphrase', 'priv_protocol', 'priv_passphrase'));
        }
        foreach ($snmpnames as $key => $value) {
            if (isset($_POST[$value])) {
                switch ($value) {
                    case "auth_passphrase":
                    case "priv_passphrase":
                        $snmpvalues[$key + 1] = base64_encode($_POST[$value]);
                        break;
                    default:
                        $snmpvalues[$key + 1] = $_POST[$value];
                }
            }
        }
        //	sg_var_dump_html($snmpvalues);
        $newsnmpstr = implode($snmpvalues, ":");
        $snmpstr = strtok($object['comment'], "\n\r");
        $snmpstrarray = explode(':', $snmpstr);
        $setcomment = "set";
        if ($snmpstrarray[0] == "SNMP") {
            if ($newsnmpstr == $snmpstr) {
                $setcomment = "ok";
            } else {
                $setcomment = "update";
            }
        }
        if ($setcomment != "ok") {
            if ($setcomment == "update") {
                $comment = str_replace($snmpstr, $newsnmpstr, $object['comment']);
            } else {
                $comment = "{$newsnmpstr}\n" . $object['comment'];
            }
            //	echo "$snmpnewstr ".$object['comment']." --> $comment";
            commitUpdateObject($object_id, $object['name'], NULL, $object['has_problems'], NULL, $comment);
            showNotice("{$setcomment} SNMP Settings: {$newsnmpstr}");
        }
    }
    if (isset($_POST['snmpconfig']) && $_POST['snmpconfig'] == '1') {
        snmpgeneric_list($object_id);
    } else {
        snmpgeneric_snmpconfig($object_id);
    }
}
示例#3
0
文件: api.php 项目: xtha/salt
 case 'edit_object':
     require_once 'inc/init.php';
     // check required args
     genericAssertion('object_id', 'uint0');
     genericAssertion('object_name', 'string0');
     genericAssertion('object_label', 'string0');
     genericAssertion('object_asset_no', 'string0');
     genericAssertion('object_comment', 'string0');
     genericAssertion('object_type_id', 'uint');
     // TODO: really required for API?
     $object_id = $_REQUEST['object_id'];
     // make this transactional, so we can double check the whole upate before committing at the end
     global $dbxlink, $sic;
     $dbxlink->beginTransaction();
     // TODO: may need to wrap this in a try/catch block to redirect to API exception response
     commitUpdateObject($object_id, $_REQUEST['object_name'], $_REQUEST['object_label'], isCheckSet('object_has_problems', 'yesno'), $_REQUEST['object_asset_no'], $_REQUEST['object_comment']);
     // update optional attributes
     // get the valid / old values for the object
     $oldvalues = getAttrValues($object_id);
     // look for values to be updated.
     //   note: in UI, a "num_attrs" input is used to loop and search for update fields
     foreach ($_REQUEST as $name => $value) {
         if (preg_match('/^attr_(\\d+)$/', $name, $matches)) {
             $attr_id = $matches[1];
             // make sure the attribute actually exists in the object
             if (!array_key_exists($attr_id, $oldvalues)) {
                 throw new InvalidRequestArgException('attr_id', $attr_id, 'malformed request');
             }
             // convert date arguments
             if ('date' == $oldvalues[$attr_id]['type']) {
                 // if given date looks like UNIX timestamp, leave as-is,
示例#4
0
function commitUpdateRack($rack_id, $new_row_id, $new_name, $new_height, $new_has_problems, $new_asset_no, $new_comment)
{
    // Can't shrink a rack if rows being deleted contain mounted objects
    $check_result = usePreparedSelectBlade('SELECT COUNT(*) AS count FROM RackSpace WHERE rack_id = ? AND unit_no > ?', array($rack_id, $new_height));
    $check_row = $check_result->fetch(PDO::FETCH_ASSOC);
    unset($check_result);
    if ($check_row['count'] > 0) {
        throw new InvalidArgException('new_height', $new_height, 'Cannot shrink rack, objects are still mounted there');
    }
    // Determine if the row changed
    $old_rack = spotEntity('rack', $rack_id);
    $old_row_id = $old_rack['row_id'];
    if ($old_row_id != $new_row_id) {
        // Move it to the specified row
        usePreparedUpdateBlade('EntityLink', array('parent_entity_id' => $new_row_id), array('child_entity_type' => 'rack', 'child_entity_id' => $rack_id));
        // Set the sort_order attribute so it's placed at the end of the new row
        $rowInfo = getRowInfo($new_row_id);
        usePreparedUpdateBlade('AttributeValue', array('uint_value' => $rowInfo['count']), array('object_id' => $rack_id, 'attr_id' => 29));
        // Reset the sort order of the old row
        resetRackSortOrder($old_row_id);
    }
    // Update the height
    commitUpdateAttrValue($rack_id, 27, $new_height);
    // Update the rack
    commitUpdateObject($rack_id, $new_name, NULL, $new_has_problems, $new_asset_no, $new_comment);
    recordObjectHistory($rack_id);
}
示例#5
0
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();
}