function getRacktablesObjects($type_id, $attr_id, $attr_value) { $r = usePreparedSelectBlade('select o.id, o.name, o.objtype_id, o.label, i.ip, i.type, i.name as ifname, av.attr_id as zabbix_flag_id, av.uint_value as zabbix_flag_value, av2.attr_id as sw_type_id, av2.uint_value as sw_type_value from Object AS o INNER JOIN IPv4Allocation as i ON (o.id = i.object_id AND o.objtype_id = ?) INNER JOIN AttributeValue av on (av.object_id = o.id AND av.attr_id = ? AND av.uint_value = ?) LEFT JOIN AttributeValue av2 on (av2.object_id = o.id AND av2.attr_id = ?)', array($type_id, $attr_id, $attr_value, RACKTABLES_SOFTWARE_TYPE_ID)); $items = $r->fetchAll(PDO::FETCH_ASSOC); $result = array(); foreach ($items as $item) { $ip = ip_format(ip4_int2bin($item['ip'])); if (!isset($result[$item['id']])) { if (!empty($item['ifname'])) { $item['ip'] = array($item['ifname'] => $ip); } else { $item['ip'] = array($ip); } $result[$item['id']] = $item; } else { if (is_array($result[$item['id']]['ip'])) { if (!empty($item['ifname'])) { $result[$item['id']]['ip'][$item['ifname']] = $ip; } else { $result[$item['id']]['ip'][] = $ip; } } } } return $result; }
function addSLBIPLink($link_row) { global $dbxlink; $do_transaction = !isTransactionActive(); if ($do_transaction) { $dbxlink->beginTransaction(); } // lock on vip $result = usePreparedSelectBlade("SELECT * FROM VSIPs WHERE vip = ? FOR UPDATE", array($link_row['vip'])); unset($result); $ret = usePreparedInsertBlade('VSEnabledIPs', $link_row); $result = usePreparedSelectBlade("SELECT proto, vport FROM VSEnabledPorts vep INNER JOIN VSEnabledIPs vei USING (vs_id, object_id, rspool_id) WHERE vei.object_id = ? AND vip = ? HAVING COUNT(distinct proto,vport) != COUNT(vport)", array($link_row['object_id'], $link_row['vip'])); if ($row = $result->fetch(PDO::FETCH_ASSOC, 0)) { unset($result); if ($do_transaction) { $dbxlink->rollBack(); } throw new RTDatabaseError(sprintf("Duplicate link of %s [%s]:%s to object #%d", $row['proto'], ip_format($link_row['vip']), $row['vport'], $link_row['object_id'])); } if ($do_transaction) { $dbxlink->commit(); } return $ret; }
function makeIPAllocLink($ip_bin, $alloc, $display_ifname = FALSE) { $object_name = !isset($object_name) || !strlen($object_name) ? formatEntityName(spotEntity('object', $alloc['object_id'])) : $alloc['object_name']; $title = $display_ifname ? '' : "{$alloc['name']} @ {$object_name}"; return '<a href="' . makeHref(array('page' => 'object', 'tab' => 'default', 'object_id' => $alloc['object_id'], 'hl_ip' => ip_format($ip_bin))) . '"' . ' title="' . htmlspecialchars($title, ENT_QUOTES) . '"' . ">" . ($display_ifname ? $alloc['name'] . '@' : '') . $object_name . "</a>"; }
function createTriplet() { global $dbxlink; $object_id = assertUIntArg('object_id'); $vs_id = assertUIntArg('vs_id'); $rspool_id = assertUIntArg('rspool_id'); $vips = genericAssertion('enabled_vips', 'array0'); $ports = genericAssertion('enabled_ports', 'array0'); $vsinfo = spotEntity('ipvs', $vs_id); amplifyCell($vsinfo); try { $dbxlink->beginTransaction(); foreach ($vsinfo['vips'] as $vip) { if (in_array(ip_format($vip['vip']), $vips)) { addSLBIPLink(array('object_id' => $object_id, 'vs_id' => $vs_id, 'rspool_id' => $rspool_id, 'vip' => $vip['vip'])); } } foreach ($vsinfo['ports'] as $port) { if (in_array($port['proto'] . '-' . $port['vport'], $ports)) { addSLBPortLink(array('object_id' => $object_id, 'vs_id' => $vs_id, 'rspool_id' => $rspool_id, 'proto' => $port['proto'], 'vport' => $port['vport'])); } } $dbxlink->commit(); } catch (RTDatabaseError $e) { $dbxlink->rollBack(); throw $e; } showSuccess("SLB triplet created"); }
function getifTable() { $this->ifTable['ifIndex'] = $this->snmpdevice->walk('ifIndex', TRUE); $this->indexcount = count($this->ifTable['ifIndex']); $this->ifTable['ifDescr'] = $this->snmpdevice->walk('ifDescr', TRUE); $this->ifTable['ifAlias'] = $this->snmpdevice->walk('ifAlias', TRUE); $this->ifTable['ifName'] = $this->snmpdevice->walk('ifName', TRUE); $this->ifTable['ifType'] = $this->snmpdevice->walk('ifType', TRUE); $this->ifTable['ifSpeed'] = $this->snmpdevice->walk('ifSpeed', TRUE); /* notation changes when SNMP_VALUE_PLAIN is set string -> hex!! */ $this->ifTable['ifPhysAddress'] = $this->snmpdevice->walk('ifPhysAddress', TRUE); $this->ifTable['ifOperStatus'] = $this->snmpdevice->walk('ifOperStatus', TRUE); $this->ifTable['ifInOctets'] = $this->snmpdevice->walk('ifInOctets', TRUE); $this->ifTable['ifOutOctets'] = $this->snmpdevice->walk('ifOutOctets', TRUE); $this->ifTable['ifConnectorPresent'] = $this->snmpdevice->walk('ifConnectorPresent', TRUE); $this->ifTable['ipaddress'] = array(); /* ip address v4 only ipaddrtable */ $ipAdEntIfIndex = $this->snmpdevice->walk('ipAdEntIfIndex'); if (!empty($ipAdEntIfIndex)) { $ipAdEntNetMask = $this->snmpdevice->walk('ipAdEntNetMask'); /* all addresses per interface */ reset($ipAdEntNetMask); foreach ($ipAdEntIfIndex as $oid => $value) { $netmask = current($ipAdEntNetMask); next($ipAdEntNetMask); $ipaddr = preg_replace('/.*\\.([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)$/', '$1', $oid); $ifindex = array_search($value, $this->ifTable['ifIndex']); if ($netmask != '0.0.0.0') { $maskbits = 32 - log((ip2long($netmask) ^ 0xffffffff) + 1, 2); $net = ip2long($ipaddr) & ip2long($netmask); $bcast = $net | ip2long($netmask) ^ 0xffffffff; $this->ifTable['ipaddress'][$ifindex][$ipaddr] = array('addrtype' => 'ipv4', 'maskbits' => $maskbits, 'net' => long2ip($net), 'bcast' => long2ip($bcast)); } } unset($oid); unset($value); // sg_var_dump_html($this->ipaddress); // sg_var_dump_html($ipAdEntIfIndex); // sg_var_dump_html($ipAdEntNetMask); } /* ipadentifindex */ /* ipv4 ipv6 ipaddresstable */ /* also overwrites ipv4 from ipaddrtable */ $ipAddressIfIndex = $this->snmpdevice->walk('ipAddressIfIndex'); if (!empty($ipAddressIfIndex)) { $ipAddressPrefix = $this->snmpdevice->walk('ipAddressPrefix'); $ipAddressType = $this->snmpdevice->walk('ipAddressType'); /* 1 unicast, 2 anycast, 3 braodcast */ reset($ipAddressPrefix); reset($ipAddressType); foreach ($ipAddressIfIndex as $oid => $value) { $prefix = current($ipAddressPrefix); next($ipAddressPrefix); $type = current($ipAddressType); next($ipAddressType); if (!preg_match('/.*\\.(ipv[46]z?)\\.\\"(.*)"$/', $oid, $matches)) { continue; } /* ipv4 or ipv6 address */ $ifindex = array_search($value, $this->ifTable['ifIndex']); $net = NULL; $maskbits = NULL; $bcast = NULL; if (preg_match('/\\."([\\.0-9a-fA-F:%]+)"\\.([0-9]+)$/', $prefix, $prefixmatches)) { $net = $prefixmatches[1]; $maskbits = $prefixmatches[2]; /* ipv4 */ if ($matches[1] == 'ipv4z' || $matches[1] == 'ipv4') { $intnetmask = (int) (0xffffffff << 32 - $maskbits); $intnet = ip2long($net) & $intnetmask; $intbcast = $intnet | $intnetmask ^ 0xffffffff; $net = long2ip($intnet); $bcast = long2ip($intbcast); } } $this->ifTable['ipaddress'][$ifindex][$matches[2]] = array('addrtype' => $matches[1], 'maskbits' => $maskbits, 'net' => $net, 'bcast' => $bcast); } unset($oid); unset($value); // sg_var_dump_html($ipAddressIfIndex); // sg_var_dump_html($ipAddressPrefix); } /* ipaddressifindex */ /* ipv6 MIB */ /* overwrites ipv6 from ipaddresstable */ $ipv6interfaces = $this->snmpdevice->get('ipv6Interfaces.0'); if ($ipv6interfaces) { echo "Found {$ipv6interfaces} ipv6 interfaces<br>"; $ipv6addraddress = $this->snmpdevice->walk('ipv6AddrAddress'); if (!empty($ipv6addraddress)) { $ipv6addrpfxlength = $this->snmpdevice->walk('ipv6AddrPfxLength'); // $ipv6addrtype = $this->snmpdevice->walk('ipv6AddrType'); /* 1 stateless, 2 stateful, 3 unknown */ reset($ipv6addrpfxlength); //reset($ipv6addrtype); foreach ($ipv6addraddress as $oid => $addr_bin) { $addr = ip_format($addr_bin); //$type = current($ipv6addrtype); //next($ipv6addrtype); if (!preg_match('/.*(ipv6).*\\.([0-9]+)\\..*$/', $oid, $matches)) { continue; } $ifindex = array_search($matches[2], $this->ifTable['ifIndex']); if ($ifindex === false) { continue; } $maskbits = current($ipv6addrpfxlength); next($ipv6addrpfxlength); $range = constructIPRange($addr_bin, $maskbits); $net = ip_format($range['ip_bin']); $bcast = NULL; $this->ifTable['ipaddress'][$ifindex][$addr] = array('addrtype' => $matches[1], 'maskbits' => $maskbits, 'net' => $net, 'bcast' => $bcast); } unset($oid); unset($value); } /* ipv6addraddress */ } /* ipv6interfaces */ }
function spotEntity($realm, $id, $ignore_cache = FALSE) { global $entityCache; if (!$ignore_cache) { if (isset($entityCache['complete'][$realm])) { // Emphasize the absence of record, if listCells() has already been called. if (isset($entityCache['complete'][$realm][$id])) { return $entityCache['complete'][$realm][$id]; } else { throw new EntityNotFoundException($realm, $id); } } elseif (isset($entityCache['partial'][$realm][$id])) { return $entityCache['partial'][$realm][$id]; } } global $SQLSchema; if (!isset($SQLSchema[$realm])) { throw new InvalidArgException('realm', $realm); } $SQLinfo = $SQLSchema[$realm]; $query = 'SELECT tag_id, TagStorage.user as tag_user, UNIX_TIMESTAMP(TagStorage.date) AS tag_time'; foreach ($SQLinfo['columns'] as $alias => $expression) { // Automatically prepend table name to each single column, but leave all others intact. $query .= ', ' . ($alias == $expression ? "{$SQLinfo['table']}.{$alias}" : "{$expression} as {$alias}"); } $query .= " FROM {$SQLinfo['table']} LEFT JOIN TagStorage on entity_realm = ? and entity_id = {$SQLinfo['table']}.{$SQLinfo['keycolumn']}"; $query .= " WHERE {$SQLinfo['table']}.{$SQLinfo['keycolumn']} = ?"; $query .= " ORDER BY tag_id"; $result = usePreparedSelectBlade($query, array($realm, $id)); $ret = array(); global $taglist; while ($row = $result->fetch(PDO::FETCH_ASSOC)) { if (!isset($ret['realm'])) { $ret = array('realm' => $realm); foreach (array_keys($SQLinfo['columns']) as $alias) { $ret[$alias] = $row[$alias]; } // use the temporary rack_id_2 key and remove this key from the result array if ($realm == 'object') { if (!isset($ret['rack_id'])) { $ret['rack_id'] = $ret['rack_id_2']; } unset($ret['rack_id_2']); } $ret['etags'] = array(); if ($row['tag_id'] != NULL && isset($taglist[$row['tag_id']])) { $ret['etags'][$row['tag_id']] = array('id' => $row['tag_id'], 'tag' => $taglist[$row['tag_id']]['tag'], 'parent_id' => $taglist[$row['tag_id']]['parent_id'], 'user' => $row['tag_user'], 'time' => $row['tag_time']); } } elseif (isset($taglist[$row['tag_id']])) { $ret['etags'][$row['tag_id']] = array('id' => $row['tag_id'], 'tag' => $taglist[$row['tag_id']]['tag'], 'parent_id' => $taglist[$row['tag_id']]['parent_id'], 'user' => $row['tag_user'], 'time' => $row['tag_time']); } } unset($result); if (!isset($ret['realm'])) { // no rows were returned throw new EntityNotFoundException($realm, $id); } sortEntityTags($ret); // changes ['etags'] and ['itags'] switch ($realm) { case 'object': setDisplayedName($ret); // set $ret['dname'] break; case 'ipv4net': processIPNetVlans($ret); $ret = array_merge($ret, constructIPRange(ip4_int2bin($ret['ip_bin']), $ret['mask'])); $ret['spare_ranges'] = array(); $ret['kidc'] = 0; break; case 'ipv6net': processIPNetVlans($ret); $ret = array_merge($ret, constructIPRange($ret['ip_bin'], $ret['mask'])); $ret['spare_ranges'] = array(); $ret['kidc'] = 0; break; case 'ipv4vs': $ret['vip'] = ip_format($ret['vip_bin']); setDisplayedName($ret); // set $ret['dname'] break; default: break; } if ($realm == 'ipv4net') { $result = usePreparedSelectBlade("\nSELECT n2.id, n2.ip as ip_bin, n2.mask FROM\n\tIPv4Network n1,\n\tIPv4Network n2\nWHERE\n\tn1.id = ?\n\tAND n2.ip BETWEEN n1.ip AND (n1.ip + (1 << (32 - n1.mask)) - 1)\n\tAND n2.mask >= n1.mask\nORDER BY n2.ip, n2.mask\n", array($id)); $nets = $result->fetchAll(PDO::FETCH_ASSOC); foreach ($nets as &$net_row) { $net_row = array_merge($net_row, constructIPRange(ip4_int2bin($net_row['ip_bin']), $net_row['mask'])); $net_row['spare_ranges'] = array(); $net_row['kidc'] = 0; } fillIPNetsCorrelation($nets); if (is_array($nets[0]) and $nets[0]['id'] == $id) { $ret['spare_ranges'] = $nets[0]['spare_ranges']; $ret['kidc'] = $nets[0]['kidc']; } unset($result); } elseif ($realm == 'ipv6net') { $result = usePreparedSelectBlade("\nSELECT n2.id, n2.ip as ip_bin, n2.mask FROM\n\tIPv6Network n1,\n\tIPv6Network n2\nWHERE\n\tn1.id = ?\n\tAND n2.ip BETWEEN n1.ip AND n1.last_ip\n\tAND n2.mask >= n1.mask\nORDER BY n2.ip, n2.mask\n", array($id)); $nets = $result->fetchAll(PDO::FETCH_ASSOC); foreach ($nets as &$net_row) { $net_row = array_merge($net_row, constructIPRange($net_row['ip_bin'], $net_row['mask'])); $net_row['spare_ranges'] = array(); $net_row['kidc'] = 0; } fillIPNetsCorrelation($nets); if (is_array($nets[0]) and $nets[0]['id'] == $id) { $ret['spare_ranges'] = $nets[0]['spare_ranges']; $ret['kidc'] = $nets[0]['kidc']; } unset($result); } $ret['atags'] = generateEntityAutoTags($ret); $entityCache['partial'][$realm][$id] = $ret; return $ret; }
function getRSListInPool($rspool_id) { $ret = array(); $query = "select id, inservice, rsip as rsip_bin, rsport, rsconfig, comment from " . "IPv4RS where rspool_id = ? order by IPv4RS.rsip, rsport"; $result = usePreparedSelectBlade($query, array($rspool_id)); while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $row['rsip'] = ip_format($row['rsip_bin']); $ret[$row['id']] = $row; } return $ret; }
function snmpgeneric_list($object_id) { global $sg_create_noconnector_ports, $sg_known_sysObjectIDs, $sg_portoifoptions, $sg_ifType_ignore; if (isset($_POST['snmpconfig'])) { $snmpconfig = $_POST; } else { showError("Missing SNMP Config"); return; } // sg_var_dump_html($snmpconfig); echo '<body onload="document.getElementById(\'createbutton\').focus();">'; addJS('function setchecked(classname) { var boxes = document.getElementsByClassName(classname); var value = document.getElementById(classname).checked; for(i=0;i<boxes.length;i++) { if(boxes[i].disabled == false) boxes[i].checked=value; } };', TRUE); $object = spotEntity('object', $object_id); $object['attr'] = getAttrValues($object_id); $snmpdev = new mySNMP($snmpconfig['version'], $snmpconfig['host'], $snmpconfig['community']); if ($snmpconfig['version'] == "v3") { $snmpdev->setSecurity($snmpconfig['sec_level'], $snmpconfig['auth_protocol'], $snmpconfig['auth_passphrase'], $snmpconfig['priv_protocol'], $snmpconfig['priv_passphrase']); } $snmpdev->init(); if ($snmpdev->getErrno()) { showError($snmpdev->getError()); return; } /* SNMP connect successfull */ showSuccess("SNMP " . $snmpconfig['version'] . " connect to {$snmpconfig['host']} successfull"); echo '<form name=CreatePorts method=post action=' . $_SERVER['REQUEST_URI'] . '&module=redirect&op=create>'; echo "<strong>System Informations</strong>"; echo "<table>"; // echo "<tr><th>OID</th><th>Value</th></tr>"; $systemoids = array('sysDescr', 'sysObjectID', 'sysUpTime', 'sysContact', 'sysName', 'sysLocation'); foreach ($systemoids as $shortoid) { $value = $snmpdev->{$shortoid}; if ($shortoid == 'sysUpTime') { /* in hundredths of a second */ $secs = (int) ($value / 100); $days = (int) ($secs / (60 * 60 * 24)); $secs -= $days * 60 * 60 * 24; $hours = (int) ($secs / (60 * 60)); $secs -= $hours * 60 * 60; $mins = (int) ($secs / 60); $secs -= $mins * 60; $value = "{$value} ({$days} {$hours}:{$mins}:{$secs})"; } echo "<tr><td title=\"" . $snmpdev->lastgetoid . "\" align=\"right\">{$shortoid}: </td><td>{$value}</td></tr>"; } unset($shortoid); echo "</table>"; /* sysObjectID Attributes and Ports */ $sysObjectID['object'] =& $object; /* get sysObjectID */ $sysObjectID['raw_value'] = $snmpdev->sysObjectID; //$sysObjectID['raw_value'] = 'NET-SNMP-MIB::netSnmpAgentOIDs.10'; $sysObjectID['value'] = preg_replace('/^.*enterprises\\.([\\.[:digit:]]+)$/', '\\1', $sysObjectID['raw_value']); /* try snmptranslate to numeric */ if (preg_match('/[^\\.0-9]+/', $sysObjectID['value'])) { $numeric_value = $snmpdev->translatetonumeric($sysObjectID['value']); if (!empty($numeric_value)) { showSuccess("sysObjectID: " . $sysObjectID['value'] . " translated to {$numeric_value}"); $sysObjectID['value'] = preg_replace('/^.1.3.6.1.4.1.([\\.[:digit:]]+)$/', '\\1', $numeric_value); } } /* array_merge doesn't work with numeric keys !! */ $sysObjectID['attr'] = array(); $sysObjectID['port'] = array(); $sysobjid = $sysObjectID['value']; $count = 1; while ($count) { if (isset($sg_known_sysObjectIDs[$sysobjid])) { $sysObjectID = $sysObjectID + $sg_known_sysObjectIDs[$sysobjid]; if (isset($sg_known_sysObjectIDs[$sysobjid]['attr'])) { $sysObjectID['attr'] = $sysObjectID['attr'] + $sg_known_sysObjectIDs[$sysobjid]['attr']; } if (isset($sg_known_sysObjectIDs[$sysobjid]['port'])) { $sysObjectID['port'] = $sysObjectID['port'] + $sg_known_sysObjectIDs[$sysobjid]['port']; } if (isset($sg_known_sysObjectIDs[$sysobjid]['text'])) { showSuccess("found sysObjectID ({$sysobjid}) " . $sg_known_sysObjectIDs[$sysobjid]['text']); } } $sysobjid = preg_replace('/\\.[[:digit:]]+$/', '', $sysobjid, 1, $count); /* add default sysobjectid */ if ($count == 0 && $sysobjid != 'default') { $sysobjid = 'default'; $count = 1; } } $sysObjectID['vendor_number'] = $sysobjid; /* device pf */ if (isset($sysObjectID['pf'])) { foreach ($sysObjectID['pf'] as $function) { if (function_exists($function)) { /* call device pf */ $function($snmpdev, $sysObjectID, NULL); } else { showWarning("Missing processor function " . $function . " for device {$sysobjid}"); } } } /* sort attributes maintain numeric keys */ ksort($sysObjectID['attr']); /* DEBUG */ //sg_var_dump_html($sysObjectID['attr'], "Before processing"); /* needs PHP >= 5 foreach call by reference */ /* php 5.1.6 doesn't seem to work */ //foreach($sysObjectID['attr'] as $attr_id => &$attr) foreach ($sysObjectID['attr'] as $attr_id => $value) { $attr =& $sysObjectID['attr'][$attr_id]; if (isset($object['attr'][$attr_id])) { if (array_key_exists('key', $object['attr'][$attr_id])) { $attr['key'] = $object['attr'][$attr_id]['key']; } switch (TRUE) { case isset($attr['pf']): if (function_exists($attr['pf'])) { $attr['pf']($snmpdev, $sysObjectID, $attr_id); } else { showWarning("Missing processor function " . $attr['pf'] . " for attribute {$attr_id}"); } break; case isset($attr['oid']): $attrvalue = $snmpdev->get($attr['oid']); if (isset($attr['regex'])) { $regex = $attr['regex']; if (isset($attr['replacement'])) { $replacement = $attr['replacement']; $attrvalue = preg_replace($regex, $replacement, $attrvalue); } else { if (!preg_match($regex, $attrvalue)) { if (!isset($attr['uncheck'])) { $attr['uncheck'] = "regex doesn't match"; } } else { unset($attr['uncheck']); } } } $attr['value'] = $attrvalue; break; case isset($attr['value']): break; default: showError("Error handling attribute id: {$attr_id}"); } } else { showWarning("Object has no attribute id: {$attr_id}"); unset($sysObjectID['attr'][$attr_id]); } } unset($attr_id); /* sort again in case there where attribs added ,maintain numeric keys */ ksort($sysObjectID['attr']); /* print attributes */ echo '<br>Attributes<br><table>'; echo '<tr><th><input type="checkbox" id="attribute" checked="checked" onclick="setchecked(this.id)"></td>'; echo '<th>Name</th><th>Current Value</th><th>new value</th></tr>'; /* DEBUG */ //sg_var_dump_html($sysObjectID['attr'], "After processing"); foreach ($sysObjectID['attr'] as $attr_id => &$attr) { $attr['id'] = $attr_id; if (isset($object['attr'][$attr_id]) && isset($attr['value'])) { if ($attr['value'] == $object['attr'][$attr_id]['value']) { $attr['uncheck'] = 'Current = new value'; } if (isset($attr['key']) && isset($object['attr'][$attr_id]['key'])) { if ($attr['key'] == $object['attr'][$attr_id]['key']) { $attr['uncheck'] = 'Current = new key'; } } $value = $attr['value']; $val_key = isset($object['attr'][$attr_id]['key']) ? ' (' . $object['attr'][$attr_id]['key'] . ')' : ''; $comment = ''; if (isset($attr['comment'])) { if (!empty($attr['comment'])) { $comment = $attr['comment']; } } if (isset($attr['uncheck'])) { $checked = ''; $comment .= ', ' . $attr['uncheck']; } else { $checked = ' checked="checked"'; } $updateattrcheckbox = '<b style="background-color:#00ff00">' . '<input style="background-color:#00ff00" class="attribute" type="checkbox" name="updateattr[' . $attr_id . ']" value="' . $value . '"' . $checked . '></b>'; $comment = trim($comment, ', '); echo "<tr><td>{$updateattrcheckbox}</td><td title=\"id: {$attr_id}\">" . $object['attr'][$attr_id]['name'] . '</td><td style="background-color:#d8d8d8">' . $object['attr'][$attr_id]['value'] . $val_key . '</td><td>' . $value . '</td>' . '<td style="color:#888888">' . $comment . '</td></tr>'; } } unset($attr_id); echo '</table>'; $object['breed'] = sg_detectDeviceBreedByObject($sysObjectID); if (!empty($object['breed'])) { echo "Found Breed: " . $object['breed'] . "<br>"; } /* ports */ /* get ports */ amplifyCell($object); /* set array key to lowercase port name */ foreach ($object['ports'] as $key => $values) { $object['ports'][strtolower(shortenIfName($values['name'], $object['breed']))] = $values; unset($object['ports'][$key]); } $newporttypeoptions = getNewPortTypeOptions(); // sg_var_dump_html($sysObjectID['port']); if (!empty($sysObjectID['port'])) { echo '<br>Vendor / Device specific ports<br>'; echo '<table><tr><th><input type="checkbox" id="moreport" checked="checked" onclick="setchecked(this.id)"></th><th>ifName</th><th>porttypeid</th></tr>'; foreach ($sysObjectID['port'] as $name => $port) { if (array_key_exists(strtolower($name), $object['ports'])) { $disableport = TRUE; } else { $disableport = FALSE; } $comment = ''; if (isset($port['comment'])) { if (!empty($port['comment'])) { $comment = $port['comment']; } } if (isset($port['uncheck'])) { $checked = ''; $comment .= ', ' . $port['uncheck']; } else { $checked = ' checked="checked"'; } $portcreatecheckbox = '<b style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '"><input style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '" class="moreport" type="checkbox" name="portcreate[' . $name . ']" value="' . $name . '"' . ($disableport ? ' disabled="disbaled"' : $checked) . '></b>'; $formfield = '<input type="hidden" name="ifName[' . $name . ']" value="' . $name . '">'; echo "<tr>{$formfield}<td>{$portcreatecheckbox}</td><td>{$name}</td>"; if (isset($port['disabled'])) { $disabledselect = array('disabled' => "disabled"); } else { $disabledselect = array(); } foreach ($port as $key => $value) { if ($key == 'uncheck' || $key == 'comment') { continue; } /* TODO iif_name */ if ($key == 'porttypeid') { $displayvalue = getNiftySelect($newporttypeoptions, array('name' => "porttypeid[{$name}]") + $disabledselect, $value); } else { $displayvalue = $value; } $formfield = '<input type="hidden" name="' . $key . '[' . $name . ']" value="' . $value . '">'; echo "{$formfield}<td>{$displayvalue}</td>"; } $comment = trim($comment, ', '); echo "<td style=\"color:#888888\">{$comment}</td></tr>"; } unset($name); unset($port); echo '</table>'; } /* snmp ports */ $ifsnmp = new ifSNMP($snmpdev); // needed for shortenIfName() $ifsnmp->object_breed = $object['breed']; /* ip spaces */ $ipspace = NULL; foreach ($ifsnmp->ipaddress as $ifindex => $ipaddresses) { foreach ($ipaddresses as $ipaddr => $value) { $addrtype = $value['addrtype']; $netaddr = $value['net']; $maskbits = $value['maskbits']; $netid = NULL; $linklocal = FALSE; //echo "<br> - DEBUG: ipspace $ipaddr - $netaddr - $addrtype - $maskbits<br>"; /* check for ip space */ switch ($addrtype) { case 'ipv4': case 'ipv4z': if ($maskbits == 32) { $netid = 'host'; } else { $netid = getIPv4AddressNetworkId(ip_parse($ipaddr)); } break; case 'ipv6': /* format ipaddr for ip6_parse */ $ipaddr = preg_replace('/((..):(..))/', '\\2\\3', $ipaddr); $ipaddr = preg_replace('/%.*$/', '', $ipaddr); if (ip_checkparse($ipaddr) === false) { continue 2; } // 2 because of switch $ip6_bin = ip6_parse($ipaddr); $ip6_addr = ip_format($ip6_bin); $netid = getIPv6AddressNetworkId($ip6_bin); $node = constructIPRange($ip6_bin, $maskbits); $netaddr = $node['ip']; $linklocal = substr($ip6_addr, 0, 5) == "fe80:"; //echo "<br> - DEBUG: ipspace $ipaddr - $addrtype - $maskbits - $netaddr - >$linklocal<<br>"; break; case 'ipv6z': /* link local */ $netid = 'ignore'; break; default: } if (empty($netid) && $netaddr != '::1' && $netaddr != '127.0.0.1' && $netaddr != '127.0.0.0' && $netaddr != '0.0.0.0' && !$linklocal) { $netaddr .= "/{$maskbits}"; $ipspace[$netaddr] = array('addrtype' => $addrtype, 'checked' => $maskbits > 0 ? true : false); } } unset($ipaddr); unset($value); unset($addrtype); } unset($ifindex); unset($ipaddresses); /* print ip spaces table */ if (!empty($ipspace)) { echo '<br><br>Create IP Spaces'; echo '<table><tr><th><input type="checkbox" id="ipspace" onclick="setchecked(this.id)" checked=\\"checked\\"></th>'; echo '<th>Type</th><th>prefix</th><th>name</th><th width=150 title="reserve network and router addresses">reserve network / router addresses</th></tr>'; $i = 1; foreach ($ipspace as $prefix => $ipspace) { $netcreatecheckbox = '<b style="background-color:#00ff00">' . '<input class="ipspace" style="background-color:#00ff00" type="checkbox" name="netcreate[' . $i . ']" value="' . $ipspace['addrtype'] . '"' . ($ipspace['checked'] ? ' checked=\\"checked\\"' : '') . '></b>'; $netprefixfield = '<input type="text" size=50 name="netprefix[' . $i . ']" value="' . $prefix . '">'; $netnamefield = '<input type="text" name="netname[' . $i . ']">'; $netreservecheckbox = '<input type="checkbox" name="netreserve[' . $i . ']" checked="checked">'; echo "<tr><td>{$netcreatecheckbox}</td><td style=\"color:#888888\">{$ipspace['addrtype']}</td><td>{$netprefixfield}</td><td>{$netnamefield}</td><td>{$netreservecheckbox}</td></tr>"; $i++; } unset($prefix); unset($addrtype); unset($i); echo '</table>'; } echo "<br><br>ifNumber: " . $ifsnmp->ifNumber . "<br>indexcount: " . $ifsnmp->indexcount . "<br><table><tbody valign=\"top\">"; $portcompat = getPortInterfaceCompat(); $ipnets = array(); $ifsnmp->printifInfoTableHeader("<th>add ip</th><th>add port</th><th>upd label</th><th title=\"update mac\">upd mac</th><td>upd port type</th><th>porttypeid</th><th>comment</th></tr>"); echo '<tr><td colspan="11"></td> <td><input type="checkbox" id="ipaddr" onclick="setchecked(this.id)">IPv4<br> <input type="checkbox" id="ipv6addr" onclick="setchecked(this.id)">IPv6</td> <td><input type="checkbox" id="ports" onclick="setchecked(this.id)"></td> <td><input type="checkbox" id="label" onclick="setchecked(this.id)" checked="checked"></td> <td><input type="checkbox" id="mac" onclick="setchecked(this.id)" checked="checked"></td> <td><input type="checkbox" id="porttype" onclick="setchecked(this.id);"></td></tr>'; foreach ($ifsnmp as $if) { $createport = TRUE; $disableport = FALSE; $ignoreport = FALSE; $port_info = NULL; $updatelabel = false; $updateporttype = false; $updatemaccheckbox = ''; $hrefs = array(); $comment = ""; if (trim($ifsnmp->ifName($if)) == '') { $comment .= "no ifName"; $createport = FALSE; } else { if (array_key_exists($ifsnmp->ifName($if), $object['ports'])) { $port_info =& $object['ports'][$ifsnmp->ifName($if)]; $comment .= "Name exists"; /* ifalias change */ if ($port_info['label'] != $ifsnmp->ifAlias($if)) { $updatelabel = true; } $createport = FALSE; $disableport = TRUE; } } if ($ifsnmp->ifPhysAddress($if) != '') { $ifPhysAddress = $ifsnmp->ifPhysAddress($if); $l2port = sg_checkL2Address($ifPhysAddress); if (!empty($l2port)) { $l2object_id = key($l2port); $porthref = makeHref(array('page' => 'object', 'tab' => 'ports', 'object_id' => $l2object_id, 'hl_port_id' => $l2port[$l2object_id])); $comment .= ", L2Address exists"; $hrefs['ifPhysAddress'] = $porthref; $createport = FALSE; // $disableport = TRUE; $updatemaccheckbox = ''; } $disablemac = true; if ($disableport) { if ($port_info !== NULL) { if (str_replace(':', '', $port_info['l2address']) != $ifPhysAddress) { $disablemac = false; } else { $disablemac = true; } } } else { /* port create always updates mac */ $updatemaccheckbox = '<b style="background-color:#00ff00">' . '<input style="background-color:' . '#00ff00" type="checkbox"' . ' checked="checked"' . ' disabled=\\"disabled\\"></b>'; } if (!$disablemac) { $updatemaccheckbox = '<b style="background-color:' . ($disablemac ? '#ff0000' : '#00ff00') . '">' . '<input class="mac" style="background-color:' . ($disablemac ? '#ff0000' : '#00ff00') . '" type="checkbox" name="updatemac[' . $if . ']" value="' . $object['ports'][$ifsnmp->ifName($if)]['id'] . '" checked="checked"' . ($disablemac ? ' disabled=\\"disabled\\"' : '') . '></b>'; } } $porttypeid = guessRToif_id($ifsnmp->ifType($if), $ifsnmp->ifDescr($if)); if (in_array($ifsnmp->ifType($if), $sg_ifType_ignore)) { $comment .= ", ignore if type"; $createport = FALSE; $ignoreport = TRUE; } else { if ($port_info) { $ptid = $port_info['iif_id'] . "-" . $port_info['oif_id']; if ($porttypeid != $ptid) { $comment .= ", Update Type {$ptid} -> {$porttypeid}"; $updateporttype = true; } } } /* ignore ports without an Connector */ if (!$sg_create_noconnector_ports && $ifsnmp->ifConnectorPresent($if) == 2) { $comment .= ", no Connector"; $createport = FALSE; } /* Allocate IPs ipv4 and ipv6 */ $ipaddresses = $ifsnmp->ipaddress($if); if (!empty($ipaddresses)) { $ipaddrcell = '<table>'; foreach ($ipaddresses as $ipaddr => $value) { $createipaddr = FALSE; $disableipaddr = FALSE; $ipaddrhref = ''; $linklocal = FALSE; $addrtype = $value['addrtype']; $maskbits = $value['maskbits']; $bcast = $value['bcast']; //echo "<br> - DEBUG: ip $ipaddr - $addrtype - $maskbits - $bcast<br>"; switch ($addrtype) { case 'ipv4z': case 'ipv4': if ($maskbits == 32) { $bcast = "host"; } $inputname = 'ip'; break; case 'ipv6z': $disableipaddr = TRUE; case 'ipv6': $inputname = 'ipv6'; /* format ipaddr for ip6_parse */ $ipaddr = preg_replace('/((..):(..))/', '\\2\\3', $ipaddr); $ipaddr = preg_replace('/%.*$/', '', $ipaddr); if (ip_checkparse($ipaddr) === false) { continue 2; } // 2 because of switch /* ip_parse throws exception on parse errors */ $ip6_bin = ip_parse($ipaddr); $ipaddr = ip_format($ip6_bin); $node = constructIPRange($ip6_bin, $maskbits); $linklocal = $node['ip'] == 'fe80::'; $createipaddr = FALSE; break; } //switch $address = getIPAddress(ip_parse($ipaddr)); /* only if ip not already allocated */ if (empty($address['allocs'])) { if (!$ignoreport) { $createipaddr = TRUE; } } else { $disableipaddr = TRUE; $ipobject_id = $address['allocs'][0]['object_id']; $ipaddrhref = makeHref(array('page' => 'object', 'object_id' => $ipobject_id, 'hl_ipv4_addr' => $ipaddr)); } /* reserved addresses */ if ($address['reserved'] == 'yes') { $comment .= ', ' . $address['ip'] . ' reserved ' . $address['name']; $createipaddr = FALSE; // $disableipaddr = TRUE; } if ($ipaddr == '127.0.0.1' || $ipaddr == '0.0.0.0' || $ipaddr == '::1' || $ipaddr == '::' || $linklocal) { $createipaddr = FALSE; $disableipaddr = TRUE; } if ($ipaddr === $bcast) { $comment .= ", {$ipaddr} broadcast"; $createipaddr = FALSE; $disableipaddr = TRUE; } if (!$disableipaddr) { $ipaddrcheckbox = '<b style="background-color:' . ($disableipaddr ? '#ff0000' : '#00ff00') . '"><input class="' . $inputname . 'addr" style="background-color:' . ($disableipaddr ? '#ff0000' : '#00ff00') . '" type="checkbox" name="' . $inputname . 'addrcreate[' . $ipaddr . ']" value="' . $if . '"' . ($disableipaddr ? ' disabled="disabled"' : '') . ($createipaddr ? ' checked="checked"' : '') . '></b>'; } else { $ipaddrcheckbox = ''; } $ipaddrcell .= "<tr><td>{$ipaddrcheckbox}</td>"; if (!empty($ipaddrhref)) { $ipaddrcell .= "<td><a href={$ipaddrhref}>{$ipaddr}/{$maskbits}</a></td></tr>"; } else { $ipaddrcell .= "<td>{$ipaddr}/{$maskbits}</td></tr>"; } } // foreach unset($ipaddr); unset($value); $ipaddrcell .= '</table>'; // if(!empty($ipaddresses)) } else { $ipaddrcreatecheckbox = ''; $ipaddrcell = ''; } /* checkboxes for add port and add ip */ /* FireFox needs <b style=..>, IE and Opera work with <td style=..> */ if (!$disableport) { $portcreatecheckbox = '<b style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '"><input class="ports" style="background-color:' . ($disableport ? '#ff0000' : '#00ff00') . '" type="checkbox" name="portcreate[' . $if . ']" value="' . $if . '"' . ($disableport ? ' disabled="disbaled"' : '') . ($createport ? ' checked="checked"' : '') . '></b>'; } else { $portcreatecheckbox = ''; } /* port type id */ /* add port type to newporttypeoptions if missing */ if (strpos(serialize($newporttypeoptions), $porttypeid) === FALSE) { $portids = explode('-', $porttypeid); $oif_name = $sg_portoifoptions[$portids[1]]; $newporttypeoptions['auto'] = array($porttypeid => "*{$oif_name}"); } $selectoptions = array('name' => "porttypeid[{$if}]"); if ($disableport && !$updateporttype) { $selectoptions['disabled'] = "disabled"; } $updateporttypecheckbox = ""; if ($updateporttype) { $updateporttypecheckbox = '<b style="background-color:#00ff00;">' . '<input class="porttype" style="background-color:#00ff00;" type="checkbox" name="updateporttype[' . $if . ']" value="' . $port_info['id'] . '"></b>'; } $porttypeidselect = getNiftySelect($newporttypeoptions, $selectoptions, $porttypeid); $updatelabelcheckbox = ""; if ($updatelabel) { $updatelabelcheckbox = '<b style="background-color:#00ff00;">' . '<input class="label" style="background-color:#00ff00;" type="checkbox" name="updatelabel[' . $if . ']" value="' . $port_info['id'] . ($updatelabel ? '" checked="checked"' : '') . '></b>'; } $comment = trim($comment, ', '); $ifsnmp->printifInfoTableRow($if, "<td>{$ipaddrcell}</td><td>{$portcreatecheckbox}</td><td>{$updatelabelcheckbox}</td><td>{$updatemaccheckbox}</td><td>{$updateporttypecheckbox}</td><td>{$porttypeidselect}</td><td nowrap=\"nowrap\">{$comment}</td>", $hrefs); } unset($if); /* preserve snmpconfig */ foreach ($_POST as $key => $value) { echo '<input type=hidden name=' . $key . ' value=' . $value . ' />'; } unset($key); unset($value); echo '<tr><td colspan=15 align="right"><p><input id="createbutton" type=submit value="Create Ports and IPs" onclick="return confirm(\'Create selected items?\')"></p></td></tr></tbody></table></form>'; }
function renderSLBFormAJAX() { global $pageno, $tabno; parse_str(assertStringArg('form'), $orig_request); parse_str(ltrim(assertStringArg('action'), '?'), $action); $pageno = $action['page']; $tabno = $action['tab']; printOpFormIntro($action['op'], $orig_request); $realm_list = array_diff(array('ipvs', 'object', 'ipv4rspool'), array($pageno)); echo '<table align=center><tr class="tdleft">'; foreach ($realm_list as $realm) { switch ($realm) { case 'object': $slb_cell = spotEntity('object', $orig_request['object_id']); break; case 'ipv4rspool': $slb_cell = spotEntity('ipv4rspool', $orig_request['rspool_id']); break; case 'ipvs': $slb_cell = spotEntity('ipvs', $orig_request['vs_id']); break; } echo '<td>'; renderSLBEntityCell($slb_cell); echo '</td>'; } $vsinfo = spotEntity('ipvs', $orig_request['vs_id']); amplifyCell($vsinfo); echo '<td><ul style="list-style: none">'; foreach ($vsinfo['ports'] as $port) { $key = $port['proto'] . '-' . $port['vport']; echo '<li><label><input type=checkbox name="enabled_ports[]" value="' . htmlspecialchars($key, ENT_QUOTES) . '" checked>' . formatVSPort($port) . '</label></li>'; } echo '</ul></td>'; echo '<td><ul style="list-style: none">'; foreach ($vsinfo['vips'] as $vip) { $key = ip_format($vip['vip']); echo '<li><label><input type=checkbox name="enabled_vips[]" value="' . htmlspecialchars($key, ENT_QUOTES) . '" checked>' . $key . '</label></li>'; } echo '</ul></td>'; echo '<td>'; printImageHREF('ADD', 'Configure LB', TRUE); echo '</td>'; echo '</tr></table>'; echo '</form>'; }
function renderRouterCell($ip_bin, $ifname, $cell) { $dottedquad = ip_format($ip_bin); echo "<table class=slbcell><tr><td rowspan=3>{$dottedquad}"; if (strlen($ifname)) { echo '@' . $ifname; } echo "</td>"; echo "<td><a href='index.php?page=object&object_id={$cell['id']}&hl_ip={$dottedquad}'><strong>{$cell['dname']}</strong></a></td>"; echo "</td></tr><tr><td>"; printImageHREF('router'); echo "</td></tr><tr><td>"; if (count($cell['etags'])) { echo '<small>' . serializeTags($cell['etags']) . '</small>'; } echo "</td></tr></table>"; }
function renderNetCellForAlloc($cell, $needed_mask = NULL) { if (empty($cell['spare_ranges']) and $cell['kidc'] == 0 and $cell['mask'] < 31) { $cell['spare_ranges'][$cell['mask'] + 1][] = $cell['ip_bin']; $cell['spare_ranges'][$cell['mask'] + 1][] = ip_last($cell) & ip4_mask($cell['mask'] + 1); } $ranges = array_keys($cell['spare_ranges']); sort($ranges, SORT_NUMERIC); foreach ($ranges as &$range) { $suffix = count($cell['spare_ranges'][$range]) <= 1 ? '' : '<small> x ' . count($cell['spare_ranges'][$range]) . '</small>'; $range = '<a href="' . makeHref(array('page' => 'ipv4space', 'tab' => 'newrange', 'set-prefix' => ip_format($cell['spare_ranges'][$range][0]) . '/' . $range)) . '">/' . $range . '</a>' . $suffix; } $spare_cidr = NULL; if (isset($needed_mask)) { for ($i = $needed_mask; $i > 0; $i--) { if (!empty($cell['spare_ranges'][$i])) { $spare_cidr = ip_format($cell['spare_ranges'][$i][0]) . '/' . $needed_mask; break; } } } echo "<table class='slbcell vscell'><tr><td rowspan=3 width='5%'>"; printImageHREF('NET'); echo '</td>'; echo "<td><a href='index.php?page={$cell['realm']}&id={$cell['id']}'>{$cell['ip']}/{$cell['mask']}</a>"; echo getRenderedIPNetCapacity($cell); echo '</td></tr>'; echo "<tr><td>"; if (strlen($cell['name'])) { echo "<strong>" . niftyString($cell['name']) . "</strong>"; } else { echo "<span class=sparenetwork>no name</span>"; } // render VLAN echo '<div class="vlan">' . implode(', ', $ranges) . '</div>'; renderNetVLAN($cell); echo "</td></tr>"; echo '<tr><td>'; echo count($cell['etags']) ? "<small>" . serializeTags($cell['etags']) . "</small>" : ' '; if (isset($spare_cidr)) { echo "<div class='vlan'><a href=\"" . makeHref(array('page' => 'ipv4space', 'tab' => 'newrange', 'set-prefix' => $spare_cidr)) . "\">Allocate /{$needed_mask}</a></div>"; } echo "</td></tr></table>"; }
function addIPAllocation() { $ip_bin = assertIPArg('ip'); assertUIntArg('object_id'); assertStringArg('bond_name', TRUE); genericAssertion('bond_type', 'enum/alloc_type'); if (getConfigVar('IPV4_JAYWALK') != 'yes' and NULL === getIPAddressNetworkId($ip_bin)) { return showFuncMessage(__FUNCTION__, 'ERR1', array(ip_format($ip_bin))); } bindIPToObject($ip_bin, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']); showFuncMessage(__FUNCTION__, 'OK'); return buildRedirectURL(NULL, NULL, array('hl_ip' => ip_format($ip_bin))); }
function constructIPAddress($ip_bin) { // common v4/v6 part $ret = array('ip' => ip_format($ip_bin), 'ip_bin' => $ip_bin, 'name' => '', 'comment' => '', 'reserved' => 'no', 'allocs' => array(), 'vslist' => array(), 'rsplist' => array()); // specific v4 part if (strlen($ip_bin) == 4) { $ret = array_merge($ret, array('outpf' => array(), 'inpf' => array())); } return $ret; }