function initZabbixHost($hostgroupDatas)
{
    $allObjects = scanRealmByText('object');
    foreach ($allObjects as $object) {
        // get group data
        $groups = array();
        $parentRacks = getResidentRacksData($object['id']);
        foreach ($parentRacks as $key => $rack) {
            array_push($groups, array('groupid' => $hostgroupDatas[$rack['name']]));
        }
        // set interfaces
        $interfaces = array();
        $allocs = getObjectIPAllocations($object['id']);
        $current_ips = array();
        foreach ($allocs as $alloc) {
            $interface = array('type' => 1, "main" => 0, "useip" => 1, "ip" => $alloc["addrinfo"]["ip"], "dns" => "", "port" => "10050");
            array_push($interfaces, $interface);
        }
        if (count($interfaces) < 1) {
            $interface = array('type' => 1, "main" => 1, "useip" => 1, "ip" => "127.0.0.1", "dns" => "", "port" => "10050");
            array_push($interfaces, $interface);
        } else {
            $interfaces[0]['main'] = 1;
        }
        // insert host
        $params = array('host' => $object['name'], 'groups' => $groups, 'interfaces' => $interfaces);
        $result = doPost('host.create', $params);
        // set result
        $id = isset($result['result']) ? $result['result']['hostids'][0] : -1;
        if ($id < 0) {
            http_response_code(500);
            exit;
        }
    }
}
function initRackTablesObject($rackDatas)
{
    // zabbix host data
    $params = array('output' => 'extend');
    $result = doPost('host.get', $params);
    $hosts = isset($result['result']) ? $result['result'] : array();
    $objectDatas = array();
    foreach ($hosts as $host) {
        $object_type_id = 4;
        $object_name = isset($host['host']) ? $host['host'] : "";
        $object_label = "";
        $object_asset_no = "";
        $taglist = array();
        $has_problems = $host['status'] == 0 ? 'no' : 'yes';
        $object_id = commitAddObject($object_name, $object_label, $object_type_id, $object_asset_no, $taglist);
        usePreparedUpdateBlade('Object', array('has_problems' => $has_problems), array('id' => $object_id));
        $objectDatas[$host['hostid']] = $object_id;
        // set hostgroup
        $params = array('output' => array('name'), 'hostids' => array($host['hostid']));
        $result = doPost('hostgroup.get', $params);
        $hostgroups = isset($result['result']) ? $result['result'] : array();
        $rack_ids = array();
        $_REQUEST = array();
        foreach ($hostgroups as $hostgroup) {
            if (isset($rackDatas[$hostgroup['name']])) {
                $rack = $rackDatas[$hostgroup['name']];
                amplifyCell($rack);
                array_push($rack_ids, $rack['id']);
                $height = $rack['height'];
                for ($i = 0; $i < 3; $i++) {
                    for ($j = $height; $j > 0; $j--) {
                        if ($rack[$j][$i]['state'] == 'F') {
                            # state == "T" : mounted
                            $_REQUEST['atom_' . $rack['id'] . "_{$j}" . "_{$i}"] = 'on';
                            break 2;
                        }
                    }
                }
            }
        }
        $_REQUEST['object_id'] = $object_id;
        $_REQUEST['rackmulti'] = $rack_ids;
        $object = spotEntity('object', $object_id);
        $changecnt = 0;
        // Get a list of rack ids which are parents of the object
        $parentRacks = reduceSubarraysToColumn(getParents($object, 'rack'), 'id');
        $workingRacksData = array();
        foreach ($_REQUEST['rackmulti'] as $cand_id) {
            if (!isset($workingRacksData[$cand_id])) {
                $rackData = spotEntity('rack', $cand_id);
                amplifyCell($rackData);
                $workingRacksData[$cand_id] = $rackData;
            } else {
                $rackData = $workingRacksData[$cand_id];
            }
            $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
            // It's zero-U mounted to this rack on the form, but not in the DB.  Mount it.
            if (isset($_REQUEST["zerou_{$cand_id}"]) && !in_array($cand_id, $parentRacks)) {
                if ($is_ro) {
                    continue;
                }
                $changecnt++;
                commitLinkEntities('rack', $cand_id, 'object', $object_id);
            }
            // It's not zero-U mounted to this rack on the form, but it is in the DB.  Unmount it.
            if (!isset($_REQUEST["zerou_{$cand_id}"]) && in_array($cand_id, $parentRacks)) {
                if ($is_ro) {
                    continue;
                }
                $changecnt++;
                commitUnlinkEntities('rack', $cand_id, 'object', $object_id);
            }
        }
        foreach ($workingRacksData as &$rd) {
            applyObjectMountMask($rd, $object_id);
        }
        $oldMolecule = getMoleculeForObject($object_id);
        foreach ($workingRacksData as $rack_id => $rackData) {
            $is_ro = !rackModificationPermitted($rackData, 'updateObjectAllocation', FALSE);
            if ($is_ro || !processGridForm($rackData, 'F', 'T', $object_id)) {
                continue;
            }
            $changecnt++;
            // Reload our working copy after form processing.
            $rackData = spotEntity('rack', $cand_id);
            amplifyCell($rackData);
            applyObjectMountMask($rackData, $object_id);
            $workingRacksData[$rack_id] = $rackData;
        }
        if ($changecnt) {
            // Log a record.
            $newMolecule = getMoleculeForObject($object_id);
            global $remote_username, $sic;
            usePreparedInsertBlade('MountOperation', array('object_id' => $object_id, 'old_molecule_id' => count($oldMolecule) ? createMolecule($oldMolecule) : NULL, 'new_molecule_id' => count($newMolecule) ? createMolecule($newMolecule) : NULL, 'user_name' => $remote_username, 'comment' => empty($sic['comment']) ? NULL : $sic['comment']));
        }
        // set IP
        $params = array('output' => 'extend', 'hostids' => $host['hostid']);
        $result = doPost('hostinterface.get', $params);
        $hostinterfaces = isset($result['result']) ? $result['result'] : array();
        foreach ($hostinterfaces as $interface) {
            if (isset($interface['ip']) && $interface['ip'] != '127.0.0.1') {
                $allocs = getObjectIPAllocations($object_id);
                $current_ips = array();
                foreach ($allocs as $alloc) {
                    $ip = $alloc["addrinfo"]["ip"];
                    $current_ips[$ip] = $ip;
                }
                $ip = $interface['ip'];
                if (!in_array($ip, array_values($current_ips))) {
                    // new IP
                    $ip_bin = ip_parse($ip);
                    if (null == getIPAddressNetworkId($ip_bin)) {
                        // if ip is not exists, adding it into RackTables IPv4Prefix.
                        $range = substr($ip, 0, strripos($ip, '.')) . '.0/24';
                        $vlan_ck = NULL;
                        $net_id = createIPv4Prefix($range, 'admim', isCheckSet('is_connected'), $taglist);
                        $net_cell = spotEntity('ipv4net', $net_id);
                        if (isset($vlan_ck)) {
                            if (considerConfiguredConstraint($net_cell, 'VLANIPV4NET_LISTSRC')) {
                                commitSupplementVLANIPv4($vlan_ck, $net_id);
                            }
                        }
                    }
                    bindIPToObject($ip_bin, $object_id, "", "");
                }
            }
        }
    }
    return $objectDatas;
}
Exemplo n.º 3
0
function renderIPForObject($object_id)
{
    function printNewItemTR($default_type, $has_children)
    {
        global $aat;
        printOpFormIntro('add');
        echo '<tr><td>';
        printImageHREF('add', 'allocate', TRUE);
        echo '</td>';
        if ($has_children) {
            echo '<td>&nbsp</td>';
        }
        echo "<td class=tdleft><input type='text' size='10' name='bond_name' tabindex=100></td>\n";
        echo "<td class=tdleft><input type=text name='ip' tabindex=101></td>\n";
        if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
            echo '<td colspan=2>&nbsp;</td>';
        }
        echo '<td>';
        printSelect($aat, array('name' => 'bond_type', 'tabindex' => 102), $default_type);
        echo '</td><td>&nbsp;</td><td>';
        printImageHREF('add', 'allocate', TRUE, 103);
        echo '</td></tr></form>';
    }
    global $aat;
    $children = getObjectContentsList($object_id);
    $has_children = count($children) ? TRUE : FALSE;
    startPortlet('Allocations');
    echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'><tr>\n";
    echo '<th>&nbsp;</th>';
    if ($has_children) {
        echo '<th>Object</th>';
    }
    echo '<th>OS interface</th>';
    echo '<th>IP address</th>';
    if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
        echo '<th>network</th>';
        echo '<th>routed by</th>';
    }
    echo '<th>type</th>';
    echo '<th>misc</th>';
    echo '<th>&nbsp;</th>';
    echo '</tr>';
    $alloc_list = '';
    // most of the output is stored here
    $used_alloc_types = array();
    $allocs = getObjectIPAllocations($object_id, TRUE);
    // note how many allocs each object has
    if ($has_children) {
        $alloc_count = array();
        foreach ($allocs as $alloc) {
            $alloc_count[$alloc['object_id']] = isset($alloc_count[$alloc['object_id']]) ? $alloc_count[$alloc['object_id']] + 1 : 1;
        }
        $last_object_id = 0;
    }
    foreach ($allocs as $alloc) {
        if (!isset($used_alloc_types[$alloc['type']])) {
            $used_alloc_types[$alloc['type']] = 0;
        }
        $used_alloc_types[$alloc['type']]++;
        $rendered_alloc = callHook('getRenderedAlloc', $alloc['object_id'], $alloc);
        $alloc_list .= getOutputOf('printOpFormIntro', 'upd', array('ip' => $alloc['addrinfo']['ip']));
        $alloc_list .= "<tr class='{$rendered_alloc['tr_class']}' valign=top>";
        $alloc_list .= '<td>' . getOpLink(array('op' => 'del', 'ip' => $alloc['addrinfo']['ip']), '', 'delete', 'Delete this IP address') . '</td>';
        if ($has_children and $last_object_id != $alloc['object_id']) {
            $rowspan = 1;
            $rowspan = $alloc_count[$alloc['object_id']] > 1 ? 'rowspan="' . $alloc_count[$alloc['object_id']] . '"' : '';
            $object_name = $alloc['object_id'] == $object_id ? $alloc['object_name'] : formatPortLink($alloc['object_id'], $alloc['object_name'], NULL, NULL);
            $alloc_list .= "<td class=tdleft {$rowspan}>{$object_name}</td>";
            $last_object_id = $alloc['object_id'];
        }
        $alloc_list .= "<td class=tdleft><input type='text' name='bond_name' value='{$alloc['osif']}' size=10>" . $rendered_alloc['td_name_suffix'] . '</td>';
        $alloc_list .= $rendered_alloc['td_ip'];
        if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
            $alloc_list .= $rendered_alloc['td_network'];
            $alloc_list .= $rendered_alloc['td_routed_by'];
        }
        $alloc_list .= '<td>' . getSelect($aat, array('name' => 'bond_type'), $alloc['type']) . '</td>';
        $alloc_list .= $rendered_alloc['td_peers'];
        $alloc_list .= '<td>' . getImageHREF('save', 'Save changes', TRUE) . '</td>';
        $alloc_list .= "</form></tr>\n";
    }
    asort($used_alloc_types, SORT_NUMERIC);
    $most_popular_type = empty($used_alloc_types) ? 'regular' : array_last(array_keys($used_alloc_types));
    if ($list_on_top = getConfigVar('ADDNEW_AT_TOP') != 'yes') {
        echo $alloc_list;
    }
    printNewItemTR($most_popular_type, $has_children);
    if (!$list_on_top) {
        echo $alloc_list;
    }
    echo "</table><br>\n";
    finishPortlet();
}
Exemplo n.º 4
0
function renderIPForObject($object_id)
{
    function printNewItemTR($default_type)
    {
        global $aat;
        printOpFormIntro('add');
        echo "<tr><td>";
        // left btn
        printImageHREF('add', 'allocate', TRUE);
        echo "</td>";
        echo "<td class=tdleft><input type='text' size='10' name='bond_name' tabindex=100></td>\n";
        // if-name
        echo "<td class=tdleft><input type=text name='ip' tabindex=101></td>\n";
        // IP
        if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
            echo "<td colspan=2>&nbsp;</td>";
        }
        // network, routed by
        echo '<td>';
        printSelect($aat, array('name' => 'bond_type', 'tabindex' => 102), $default_type);
        // type
        echo "</td><td>&nbsp;</td><td>";
        // misc
        printImageHREF('add', 'allocate', TRUE, 103);
        // right btn
        echo "</td></tr></form>";
    }
    global $aat;
    startPortlet('Allocations');
    echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'><tr>\n";
    echo '<th>&nbsp;</th>';
    echo '<th>OS interface</th>';
    echo '<th>IP address</th>';
    if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
        echo '<th>network</th>';
        echo '<th>routed by</th>';
    }
    echo '<th>type</th>';
    echo '<th>misc</th>';
    echo '<th>&nbsp;</th>';
    echo '</tr>';
    $alloc_list = '';
    // most of the output is stored here
    $used_alloc_types = array();
    foreach (getObjectIPAllocations($object_id) as $alloc) {
        if (!isset($used_alloc_types[$alloc['type']])) {
            $used_alloc_types[$alloc['type']] = 0;
        }
        $used_alloc_types[$alloc['type']]++;
        $rendered_alloc = callHook('getRenderedAlloc', $object_id, $alloc);
        $alloc_list .= getOutputOf('printOpFormIntro', 'upd', array('ip' => $alloc['addrinfo']['ip']));
        $alloc_list .= "<tr class='{$rendered_alloc['tr_class']}' valign=top>";
        $alloc_list .= "<td>" . getOpLink(array('op' => 'del', 'ip' => $alloc['addrinfo']['ip']), '', 'delete', 'Delete this IP address') . "</td>";
        $alloc_list .= "<td class=tdleft><input type='text' name='bond_name' value='{$alloc['osif']}' size=10>" . $rendered_alloc['td_name_suffix'] . "</td>";
        $alloc_list .= $rendered_alloc['td_ip'];
        if (getConfigVar('EXT_IPV4_VIEW') == 'yes') {
            $alloc_list .= $rendered_alloc['td_network'];
            $alloc_list .= $rendered_alloc['td_routed_by'];
        }
        $alloc_list .= '<td>' . getSelect($aat, array('name' => 'bond_type'), $alloc['type']) . "</td>";
        $alloc_list .= $rendered_alloc['td_peers'];
        $alloc_list .= "<td>" . getImageHREF('save', 'Save changes', TRUE) . "</td>";
        $alloc_list .= "</form></tr>\n";
    }
    asort($used_alloc_types, SORT_NUMERIC);
    $most_popular_type = empty($used_alloc_types) ? 'regular' : array_last(array_keys($used_alloc_types));
    if ($list_on_top = getConfigVar('ADDNEW_AT_TOP') != 'yes') {
        echo $alloc_list;
    }
    printNewItemTR($most_popular_type);
    if (!$list_on_top) {
        echo $alloc_list;
    }
    echo "</table><br>\n";
    finishPortlet();
}
Exemplo n.º 5
0
         echo json_encode(array("error" => "In racktables the rack " . $_REQUEST["name"] . "is not existed."));
         return;
     }
     amplifyCell($rack);
     echo json_encode($rack);
     return;
     // api.php?method=update_object_ip&object_name=test&ip[]=192.168.10.1&ip[]=192.168.10.2
 // api.php?method=update_object_ip&object_name=test&ip[]=192.168.10.1&ip[]=192.168.10.2
 case "update_object_ip":
     require_once 'inc/init.php';
     $object_id = getEntityIdByName("object", $_REQUEST["object_name"]);
     if ($object_id < 0) {
         echo json_encode(array("error", "In racktables the object " . $_REQUEST["object_name"] . "is not existed."));
         return;
     }
     $allocs = getObjectIPAllocations($object_id);
     $current_ips = array();
     foreach ($allocs as $alloc) {
         $ip = $alloc["addrinfo"]["ip"];
         $current_ips[$ip] = $ip;
     }
     $ips = $_REQUEST["ip"];
     // delete already exists IP
     $ips = array_flip(array_flip($ips));
     $result = array();
     $no_exists = array();
     foreach ($ips as $ip) {
         if (in_array($ip, array_values($current_ips))) {
             // already exists IP
             unset($current_ips[$ip]);
             array_push($result, $ip);