echo "<tr bgcolor=#{$bg2}><td>{$row} IF changes in total</td></tr></table>\n";
    echo '</td></tr></table>';
    include_once "inc/footer.php";
} elseif ($wol) {
    if (preg_match("/dsk/", $_SESSION['group'])) {
        $link = @DbConnect($dbhost, $dbuser, $dbpass, $dbname);
        $query = GenQuery('nodes', 's', '*', '', '', array('mac'), array('='), array($wol));
        $res = @DbQuery($query, $link);
        $nnod = @DbNumRows($res);
        if ($nnod != 1) {
            echo "<h4>{$wol} {$n1rmsg}</h4>";
            @DbFreeResult($res);
            die;
        } else {
            $n = @DbFetchRow($res);
            @DbFreeResult($res);
            $ip = long2ip($n[1]);
        }
        wake($ip, $wol, 9);
    } else {
        echo $nokmsg;
    }
    ?>
<h5>Magic Packet sent to <?php 
    echo $ip;
    ?>
</h5>
<script language="JavaScript"><!--
setTimeout("history.go(-1)",10000);
//--></script>
<?php 
Example #2
0
function TopoTable($reg = "", $cty = "", $bld = "", $flr = "", $rom = "", $nsd = 0)
{
    global $link, $dev, $noloc, $alarr, $dreg, $dcity, $dbuild, $locsep, $now, $retire;
    if ($nsd) {
        $query = GenQuery('devices', 's', '*', '', '', array('location'), array('like'), array(TopoLoc($reg, $cty, $bld, $flr, $rom)));
    } else {
        $query = GenQuery('devices', 's', '*', '', '', array('snmpversion', 'location'), array('>', 'like'), array('0', TopoLoc($reg, $cty, $bld, $flr, $rom)), array('AND'));
    }
    $res = DbQuery($query, $link);
    if ($res) {
        while ($d = DbFetchRow($res)) {
            $mn = array_key_exists($d[0], $alarr) ? 1 : 0;
            if (preg_match("/.+{$locsep}.+{$locsep}.+/", $d[10])) {
                $l = explode($locsep, $d[10]);
                if ($mn) {
                    $dreg[$l[0]]['mn']++;
                    $dreg[$l[0]]['al'] += $alarr[$d[0]]['st'];
                    $dcity[$l[0]][$l[1]]['mn']++;
                    $dcity[$l[0]][$l[1]]['al'] += $alarr[$d[0]]['st'];
                    $dbuild[$l[0]][$l[1]][$l[2]]['mn']++;
                    $dbuild[$l[0]][$l[1]][$l[2]]['al'] += $alarr[$d[0]]['st'];
                }
                $dreg[$l[0]]['nd']++;
                $dcity[$l[0]][$l[1]]['nd']++;
                $dbuild[$l[0]][$l[1]][$l[2]]['nd']++;
                if ($d[6] > 3) {
                    $dbuild[$l[0]][$l[1]][$l[2]]['nr']++;
                }
                if ($reg and $cty) {
                    $dev[$l[3]][$l[4]][$d[0]]['rk'] = $l[5];
                    $dev[$l[3]][$l[4]][$d[0]]['ru'] = $l[6];
                    $dev[$l[3]][$l[4]][$d[0]]['ip'] = long2ip($d[1]);
                    $dev[$l[3]][$l[4]][$d[0]]['ty'] = $d[3];
                    $dev[$l[3]][$l[4]][$d[0]]['co'] = $d[11];
                    $dev[$l[3]][$l[4]][$d[0]]['po'] = $d[16];
                    $dev[$l[3]][$l[4]][$d[0]]['ic'] = $d[18];
                    $dev[$l[3]][$l[4]][$d[0]]['mn'] = $mn;
                    $dev[$l[3]][$l[4]][$d[0]]['al'] = $alarr[$d[0]]['st'];
                    $dev[$l[3]][$l[4]][$d[0]]['sz'] = $d[28];
                    $dev[$l[3]][$l[4]][$d[0]]['sk'] = $d[29] ? $d[29] : 1;
                }
            } else {
                $noloc[$d[0]]['ip'] = long2ip($d[1]);
                $noloc[$d[0]]['ty'] = $d[3];
                $noloc[$d[0]]['lo'] = $d[10];
                $noloc[$d[0]]['co'] = $d[11];
                $noloc[$d[0]]['po'] = $d[16];
                $noloc[$d[0]]['ic'] = $d[18];
                $noloc[$d[0]]['mn'] = $mn;
                if ($mn) {
                    $noloc[$d[0]]['al'] = $alarr[$d[0]]['st'];
                }
            }
        }
        DbFreeResult($res);
    } else {
        print DbError($link);
    }
}
    @DbFreeResult($radres);
} else {
    print @DbError($radlink);
}
// get online status on these users
for ($i = 0; $i < $nres; $i++) {
    $radquery = Query('radacct', '*', 'UserName', '=', $radusr[$i]["UserName"], 'AND', 'AcctStopTime', '=', '0', $_GET[ord], '');
    $radres = @DbQuery($radquery, $radlink);
    if ($u = @DbFetchArray($radres)) {
        $radusr[$i]["Online"] = true;
        $radusr[$i]["NAS"] = $u['NASIPAddress'];
    } else {
        $radusr[$i]["Online"] = false;
        $radusr[$i]["NAS"] = '';
    }
    @DbFreeResult($radres);
}
// print
for ($i = 0; $i < $nres; $i++) {
    if ($row == "1") {
        $row = "0";
        $bg = $bga;
        $bi = $bia;
    } else {
        $row = "1";
        $bg = $bgb;
        $bi = $bib;
    }
    $si = ord(substr(strtolower($radusr[$i]["UserName"]), 0, 1)) + ord(substr(strtolower($radusr[$i]["UserName"]), 1, 1)) + ord(substr(strtolower($radusr[$i]["UserName"]), 2, 1)) - 291;
    if ($si < 1 or $si > 70) {
        $si = "36";
function Read($ina, $filter)
{
    global $link, $locsep, $resmsg;
    global $lev, $ipi, $net, $dev, $ndev, $bdev, $cdev, $rdev;
    global $devlink, $ctylink, $bldlink;
    global $nctylink, $nbldlink, $actylink, $abldlink;
    $net = array();
    if ($ina == "vlan") {
        $query = GenQuery('vlans', 'g', 'device', 'device', '', array('vlanid'), array('regexp'), array($filter));
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($vl = @DbFetchRow($res)) {
                $devs[] = preg_replace('/([\\^\\$+])/', '\\\\\\\\$1', $vl[0]);
                # \Q...\E Doesn't seem to work in MySQL?
            }
            @DbFreeResult($res);
        } else {
            echo @DbError($link);
        }
        if (!is_array($devs)) {
            echo $resmsg;
            die;
        }
        $query = GenQuery('devices', 's', 'name,ip,location,icon', '', '', array('name'), array('regexp'), array(implode("|", $devs)));
    } elseif ($ina == "network") {
        $query = GenQuery('networks', 'g', 'device', 'device', '', array('ip'), array('='), array($filter));
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($net = @DbFetchRow($res)) {
                $devs[] = preg_replace('/([\\^\\$\\*\\+])/', '\\\\\\\\$1', $net[0]);
            }
            @DbFreeResult($res);
        } else {
            echo @DbError($link);
        }
        if (!is_array($devs)) {
            echo $resmsg;
            die;
        }
        $query = GenQuery('devices', 's', 'name,ip,location,icon', '', '', array('name'), array('regexp'), array(implode("|", $devs)));
    } elseif ($ina == "type") {
        $query = GenQuery('devices', 's', 'name,ip,location,icon', '', '', array('type'), array('regexp'), array($filter));
    } else {
        $query = GenQuery('devices', 's', 'name,ip,location,icon', '', '', array('location'), array('regexp'), array($filter));
    }
    $res = @DbQuery($query, $link);
    if ($res) {
        while ($unit = @DbFetchRow($res)) {
            $l = explode($locsep, $unit[2]);
            $ndev["{$l['0']}{$locsep}{$l['1']}"][$l[2]][$l[3]][] = $unit[0];
            $bdev["{$l['0']}{$locsep}{$l['1']}"][$l[2]]++;
            $cdev["{$l['0']}{$locsep}{$l['1']}"]++;
            $rdev[$l[0]][$l[1]]++;
            $dev[$unit[0]]['ip'] = long2ip($unit[1]);
            $dev[$unit[0]]['ic'] = $unit[3];
            $dev[$unit[0]]['cty'] = "{$l['0']}{$locsep}{$l['1']}";
            $dev[$unit[0]]['bld'] = $l[2];
            $dev[$unit[0]]['rom'] = $l[4];
        }
        @DbFreeResult($res);
    } else {
        echo @DbError($link);
    }
    if ($ipi) {
        $query = GenQuery('networks');
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($n = @DbFetchRow($res)) {
                $net[$n[0]][$n[1]] .= " " . long2ip($n[2]);
            }
        } else {
            echo @DbError($link);
        }
        @DbFreeResult($res);
    }
    $query = GenQuery('links');
    $res = @DbQuery($query, $link);
    if ($res) {
        while ($l = @DbFetchRow($res)) {
            if ($dev[$l[1]]['ic'] and $dev[$l[3]]['ic']) {
                # both ends are ok, if an icon exists
                if ($lev == "f") {
                    if (isset($devlink[$l[3]][$l[1]])) {
                        # opposite link doesn't exist?
                        $devlink[$l[3]][$l[1]]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $devlink[$l[1]][$l[3]]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
                if ($dev[$l[1]]['bld'] != $dev[$l[3]]['bld']) {
                    # is it same bld?
                    $nbldlink[$dev[$l[1]]['bld']]++;
                    $abldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]++;
                    # needed for Arranging.
                    if (isset($bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']])) {
                        # link defined already?
                        $bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $bldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
                if ($dev[$l[1]]['cty'] != $dev[$l[3]]['cty']) {
                    # is it same cty?
                    $nctylink[$dev[$l[1]]['cty']]++;
                    $actylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]++;
                    # needed for Arranging.
                    if (isset($ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']])) {
                        # link defined already?
                        $ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $ctylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
            }
        }
        @DbFreeResult($res);
    } else {
        echo @DbError($link);
    }
}
function Layout() {

	global $link,$locsep,$resmsg;
	global $xm,$ym,$xo,$yo,$len,$geo,$ina,$flt,$lev,$ipi;
	global $mapbg,$rloc,$cloc,$bloc;

	$net       = array();

# Gather Device Info
	if($ina == "vlan"){
		$query	= GenQuery('vlans','g','device','device','',array('vlanid'),array('regexp'),array($flt));
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($vl = @DbFetchRow($res)) ){
				$devs[] = preg_replace('/([\^\$+])/','\\\\\\\\$1',$vl[0]);		# \Q...\E Doesn't seem to work in MySQL?
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		if (! is_array ($devs) ){echo $resmsg;die;}
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('name'),array('regexp'),array(implode("|",$devs)));
	}elseif($ina == "network"){
		$query	= GenQuery('networks','g','device','device','',array('ip'),array('='),array($flt));
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($net = @DbFetchRow($res)) ){
				$devs[] = preg_replace('/([\^\$\*\+])/','\\\\\\\\$1',$net[0]);
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		if (! is_array ($devs) ){echo $resmsg;die;}
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('name'),array('regexp'),array(implode("|",$devs)));
	}elseif($ina == "type"){
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('type'),array('regexp'),array($flt));
	}else{
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('location'),array('regexp'),array($flt));
	}
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($d = @DbFetchRow($res)) ){
			$l = explode($locsep, $d[2]);
			if($lev == "c" or $lev == "C"){
				$locs[$l[0]][$l[1]]++;
			}elseif($lev == "b" or $lev == "B"){
				$locs[$l[0]][$l[1]][$l[2]]++;
			}else{
				$locs[$l[0]][$l[1]][$l[2]][$l[3]][] = $d[0];
			}
			$dev['ip'] [$d[0]] = long2ip($d[1]);
			$dev['ic'] [$d[0]] = $d[3];
			$dev['reg'][$d[0]] = $l[0];
			$dev['cty'][$d[0]] = $l[1];
			$dev['bld'][$d[0]] = $l[2];
			$dev['rom'][$d[0]] = $l[4];
		}
		@DbFreeResult($res);
	}else{
		echo @DbError($link);
	}
	if($ipi){
		$query	= GenQuery('networks');
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($n = @DbFetchRow($res)) ){
				$net[$n[0]][$n[1]] .= " ". long2ip($n[2]);
			}
		}else{
			echo @DbError($link);
		}
		@DbFreeResult($res);
	}

# Gather Link Info
	$query	= GenQuery('links');
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($l = @DbFetchRow($res)) ){
			if($dev[$l[1]]['ic'] and $dev[$l[3]]['ic']){					# both ends are ok, if an icon exists
				if($lev == "f"){
					if( isset($devlink[$l[3]][$l[1]]) ){				# opposite link doesn't exist?
						$devlink[$l[3]][$l[1]]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{
						$devlink[$l[1]][$l[3]]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
				if($dev[$l[1]]['bld'] != $dev[$l[3]]['bld'])			{	# is it same bld?
					$nbldlink[$dev[$l[1]]['bld']] ++;
					$abldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]++;		# needed for Arranging.
					if(isset($bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]) ){	# link defined already?
						$bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{
						$bldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
				if($dev[$l[1]]['cty'] != $dev[$l[3]]['cty']){				# is it same cty?
					$nctylink[$dev[$l[1]]['cty']]++;
					$actylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]++;		# needed for Arranging.
					if(isset($ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]) ){	# link defined already?
						$ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{

						$ctylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
			}
		}
		@DbFreeResult($res);
	}else{
		echo @DbError($link);
	}

# Prepare geographic stuff
	$mapinfo   = "";
	$mapframes = "";
	$maplinks  = "";
	$mapitems  = "";

	if ($geo) {
		if(count($cdev) == 1){
			$rk   = array_keys($cdev);
			if(count($cdev[$rk[0]]) == 1){
				$ck = array_keys($cdev[$rk[0]]);
				$mapbg = TopoMap($rk[0],$ck[0]);
			}else{
				$mapbg = TopoMap($rk[0]);
			}
		}else{
			$mapbg = TopoMap();
		}
		$bg = Imagecreatefrompng("log/$mapbg");
		$xm = Imagesx($bg);
		$ym = Imagesy($bg);
		Imagedestroy($bg);

		$query	= GenQuery('locations');
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($l = @DbFetchRow($res)) ){
				if($l[3]){
					if( $mapbg == TopoMap($l[1],$l[2]) ){
						$bloc[$l[3]] = $l[4];
						$bloc[$l[3]] = $l[5];
					}
				}elseif($l[2]){
					if($mapbg == TopoMap($l[1]) ){
						$cloc[$l[2]] = $l[4];
						$cloc[$l[2]] = $l[5];
					}
				}else{
					$rloc[$l[1]] = $l[4];
					$rloc[$l[1]] = $l[5];
				}
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		
	}
# Build Layout
	Drawlevel($locs,(intval($xm/2) + $xo), (intval($ym/2) + $yo),$len );
}
Example #6
0
function Map()
{
    global $debug, $link, $locsep, $vallbl, $sholbl, $sumlbl, $imas, $fmt, $lit, $fsz, $pos;
    global $xm, $ym, $xo, $yo, $rot, $cro, $bro, $len, $lsf, $mde, $in, $op, $st, $co, $lev, $loo, $loa, $loi, $ipi, $ifa;
    global $mapbg, $mapitems, $maplinks, $mapinfo, $imgmap, $reg, $cty, $bld, $flr, $dev, $nod, $nlnk, $jnod, $cud;
    $rlnk = array();
    $clnk = array();
    $blnk = array();
    $dlnk = array();
    $acol = '';
    $join = '';
    if ($pos == 'a') {
        $acol = ',status';
        $join .= 'LEFT JOIN monitoring USING (device) ';
    }
    if (in_array('vlanid', $in) or in_array('vlanname', $in)) {
        $join .= 'LEFT JOIN vlans USING (device) ';
    }
    if (in_array('mac', $in) or in_array('nodip', $in) or in_array('name', $in) or in_array('oui', $in)) {
        $join .= 'LEFT JOIN nodes USING (device) ';
    }
    if (in_array('ifip', $in) or in_array('vrfname', $in)) {
        $join .= 'LEFT JOIN networks USING (device) ';
    }
    if (in_array('neighbor', $in)) {
        $join .= 'LEFT JOIN links USING (device) ';
    }
    $query .= GenQuery('devices', 's', "distinct device,devip,type,location,contact,devmode,icon,cpu,temp,devopts,size,stack{$acol},snmpversion", '', '', $in, $op, $st, $co, $join);
    # Postgres requires ordercolumn (snmpversion)!
    if ($lev < 5) {
        if (strpos($query, 'WHERE')) {
            $query .= ' AND snmpversion != 0';
        } else {
            $query .= ' WHERE snmpversion != 0';
        }
    }
    $query .= ' order by snmpversion desc';
    $res = DbQuery($query, $link);
    if ($res) {
        while ($d = DbFetchRow($res)) {
            $l = explode($locsep, $d[3]);
            $reg[$l[0]]['ndv']++;
            $cty[$l[0]][$l[1]]['ndv']++;
            $dev[$d[0]]['reg'] = $l[0];
            if ($d[6] and $ipi) {
                # Get IP info for interfaces on snmpdevs
                $nquery = GenQuery('networks', 's', 'ifname,ifip,ifip6,vrfname', '', '', array('device'), array('='), array($d[0]));
                $nres = DbQuery($nquery, $link);
                if ($nres) {
                    while ($n = DbFetchRow($nres)) {
                        if ($n[1]) {
                            $dev[$d[0]]['ifip'][$n[0]] .= " " . long2ip($n[1]) . ($n[3] ? " ({$n['3']})" : "");
                        } else {
                            $dev[$d[0]]['ifip'][$n[0]] .= " " . DbIPv6($n[2]) . ($n[3] ? " ({$n['3']})" : "");
                        }
                    }
                } else {
                    echo DbError($nlink);
                }
                DbFreeResult($nres);
            }
            if ($d[6] and ($ifa or $lit == 'l')) {
                # Get IF alias TODO use iftype to determine links?
                $nquery = GenQuery('interfaces', 's', 'ifname,ifidx,iftype,alias,dinoct,doutoct', '', '', array('device'), array('='), array($d[0]));
                $nres = DbQuery($nquery, $link);
                if ($nres) {
                    while ($n = DbFetchRow($nres)) {
                        $dev[$d[0]]['ifty'][$n[0]] = $n[1];
                        $dev[$d[0]]['ifix'][$n[0]] = $n[2];
                        $dev[$d[0]]['ifal'][$n[0]] = $n[3];
                        $dev[$d[0]]['ifin'][$n[0]] = $n[4];
                        $dev[$d[0]]['ifout'][$n[0]] = $n[5];
                    }
                } else {
                    echo DbError($nlink);
                }
                DbFreeResult($nres);
            }
            if ($lev > 1) {
                $dev[$d[0]]['cty'] = $l[1];
            }
            if ($lev > 2) {
                $bld[$l[0]][$l[1]][$l[2]]['ndv']++;
                $dev[$d[0]]['bld'] = $l[2];
            }
            if ($lev > 3) {
                if ($mde == "r") {
                    $flr[$l[0]][$l[1]][$l[2]][$d[0]]['ndv']++;
                } else {
                    $flr[$l[0]][$l[1]][$l[2]][$l[3]][] = $d[0];
                }
                $dev[$d[0]]['ip'] = long2ip($d[1]);
                $dev[$d[0]]['rom'] = $l[4];
                $dev[$d[0]]['rak'] = $l[5] ? $l[5] : "";
                $dev[$d[0]]['typ'] = $d[2];
                $dev[$d[0]]['con'] = $d[4];
                $dev[$d[0]]['mod'] = Devmode($d[5]);
                $dev[$d[0]]['ico'] = $d[6];
                if (substr($d[9], 1, 1) == "C") {
                    $dev[$d[0]]['cpu'] = $d[7];
                } else {
                    $dev[$d[0]]['cpu'] = "-";
                }
                $dev[$d[0]]['tmp'] = $d[8];
                if ($d[8] != 0) {
                    $dev[$d[0]]['tlb'] = $_SESSION['far'] ? intval($dev[$d[0]]['tmp'] * 1.8 + 32) . "F" : $dev[$d[0]]['tmp'] . "C";
                } else {
                    $dev[$d[0]]['tlb'] = "-";
                }
                $dev[$d[0]]['stk'] = $d[11] > 1 ? $d[11] : 1;
                $dev[$d[0]]['siz'] = $d[10] * $dev[$d[0]]['stk'];
                if ($pos == 'a') {
                    $dev[$d[0]]['sta'] = $d[12];
                }
            }
        }
        DbFreeResult($res);
    } else {
        echo DbError($link);
    }
    # Precalculate Links
    foreach (array_keys($dev) as $d) {
        # Devs sorted by snmpversion creates links with stats first!
        $lquery = GenQuery('links', 's', '*', '', '', array('device'), array('='), array($d));
        $lres = DbQuery($lquery, $link);
        while ($k = DbFetchRow($lres)) {
            if (isset($dev[$k[3]]['reg'])) {
                # Only use, if we have complete devs
                $rlquery = GenQuery('links', 's', '*', '', '', array('device', 'neighbor'), array('=', '='), array($k[3], $k[1]), array('AND'));
                $rlres = DbQuery($rlquery, $link);
                $rlnum = DbNumRows($rlres);
                if ($debug) {
                    echo "<div class=\"textpad good\">LINK:{$k['1']} to {$k['3']} with BW of {$k['5']}</div>\n";
                }
                if (array_key_exists("{$k['3']};;{$k['1']}", $dlnk)) {
                    $dlnk["{$k['3']};;{$k['1']}"]['rbw'] += $k[5];
                    $dlnk["{$k['3']};;{$k['1']}"]['rtr'] += $dev[$k[3]]['ifin'][$k[4]];
                    $dlnk["{$k['3']};;{$k['1']}"]['rif'][] = "{$k['1']};;{$k['2']}";
                    $dlnk["{$k['3']};;{$k['1']}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                } elseif (isset($dev[$k[3]]['ico'])) {
                    if (!$rlnum) {
                        if ($debug) {
                            echo "<div class=\"textpad alrm\">LNK: Fixing missing link from {$k['3']} to {$k['1']}</div>\n";
                        }
                        $dlnk["{$k['1']};;{$k['3']}"]['rbw'] += $k[5];
                        $dlnk["{$k['1']};;{$k['3']}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                        $dlnk["{$k['1']};;{$k['3']}"]['rif'][] = "{$k['3']};;{$k['4']}";
                        $dlnk["{$k['1']};;{$k['3']}"]['fty']["{$k['6']}:" . date('j.M', $k[10])]++;
                    }
                    $dlnk["{$k['1']};;{$k['3']}"]['fbw'] += $k[5];
                    $dlnk["{$k['1']};;{$k['3']}"]['ftr'] += $dev[$k[1]]['ifout'][$k[2]];
                    $dlnk["{$k['1']};;{$k['3']}"]['fif'][] = "{$k['1']};;{$k['2']}";
                    $dlnk["{$k['1']};;{$k['3']}"]['fty']["{$k['6']}:" . date('j.M', $k[10])]++;
                }
                $ra = $dev[$k[1]]['reg'];
                $rb = $dev[$k[3]]['reg'];
                $ca = $dev[$k[1]]['cty'];
                $cb = $dev[$k[3]]['cty'];
                $ba = $dev[$k[1]]['bld'];
                $bb = $dev[$k[3]]['bld'];
                if ($mde != "f" and $ra != $rb) {
                    $reg[$ra]['nlk']++;
                    $reg[$ra]['alk'][$rb]++;
                    # Needed for arranging
                    if (array_key_exists("{$rb};;{$ra}", $rlnk)) {
                        # Reverse link exists?
                        $rlnk["{$rb};;{$ra}"]['rbw'] += $k[5];
                        $rlnk["{$rb};;{$ra}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                        $rlnk["{$rb};;{$ra}"]['rif'][] = "{$k['1']};;{$k['2']}";
                        $rlnk["{$rb};;{$ra}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                    } else {
                        if (!$rlnum) {
                            $reg[$rb]['nlk']++;
                            $reg[$rb]['alk'][$rb]++;
                            $rlnk["{$ra};;{$rb}"]['rbw'] += $k[5];
                            $rlnk["{$ra};;{$rb}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                            $rlnk["{$ra};;{$rb}"]['rif'][] = "{$k['3']};;{$k['4']}";
                            $rlnk["{$ra};;{$rb}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                        }
                        $rlnk["{$ra};;{$rb}"]['fbw'] += $k[5];
                        $rlnk["{$ra};;{$rb}"]['ftr'] += $dev[$k[1]]['ifout'][$k[2]];
                        $rlnk["{$ra};;{$rb}"]['fif'][] = "{$k['1']};;{$k['2']}";
                        $rlnk["{$ra};;{$rb}"]['fty']["{$k['6']}:" . date('j.M', $k[10])]++;
                    }
                }
                if ($mde != "f" and $lev > 1) {
                    if ("{$ra};;{$ca}" != "{$rb};;{$cb}") {
                        $cty[$ra][$ca]['nlk']++;
                        if ($ra == $rb) {
                            $cty[$ra][$ca]['alk'][$cb]++;
                        }
                        #TODO test whether this improves arranging!
                        if (array_key_exists("{$rb};;{$cb};;{$ra};;{$ca}", $clnk)) {
                            $clnk["{$rb};;{$cb};;{$ra};;{$ca}"]['rbw'] += $k[5];
                            $clnk["{$rb};;{$cb};;{$ra};;{$ca}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                            $clnk["{$rb};;{$cb};;{$ra};;{$ca}"]['rif'][] = "{$k['1']};;{$k['2']}";
                            $clnk["{$rb};;{$cb};;{$ra};;{$ca}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                        } else {
                            if (!$rlnum) {
                                $cty[$rb][$cb]['nlk']++;
                                if ($ra == $rb) {
                                    $cty[$rb][$cb]['alk'][$ca]++;
                                }
                                $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['rbw'] += $k[5];
                                $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                                $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['rif'][] = "{$k['3']};;{$k['4']}";
                                $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                            }
                            $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['fbw'] += $k[5];
                            $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['ftr'] += $dev[$k[1]]['ifout'][$k[2]];
                            $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['fif'][] = "{$k['1']};;{$k['2']}";
                            $clnk["{$ra};;{$ca};;{$rb};;{$cb}"]['fty']["{$k['6']}:" . date('j.M', $k[10])]++;
                        }
                    }
                }
                if ($mde != "f" and $lev > 2) {
                    if ("{$ra};;{$ca};;{$ba}" != "{$rb};;{$cb};;{$bb}") {
                        $bld[$ra][$ca][$ba]['nlk']++;
                        if ("{$ra};;{$ca}" == "{$rb};;{$cb}") {
                            $bld[$ra][$ca][$ba]['alk'][$bb]++;
                        }
                        if (array_key_exists("{$rb};;{$cb};;{$bb};;{$ra};;{$ca};;{$ba}", $blnk)) {
                            $blnk["{$rb};;{$cb};;{$bb};;{$ra};;{$ca};;{$ba}"]['rbw'] += $k[5];
                            $blnk["{$rb};;{$cb};;{$bb};;{$ra};;{$ca};;{$ba}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                            $blnk["{$rb};;{$cb};;{$bb};;{$ra};;{$ca};;{$ba}"]['rif'][] = "{$k['1']};;{$k['2']}";
                            $blnk["{$rb};;{$cb};;{$bb};;{$ra};;{$ca};;{$ba}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                        } else {
                            if (!$rlnum) {
                                $bld[$rb][$cb][$bb]['nlk']++;
                                if ("{$ra};;{$ca}" == "{$rb};;{$cb}") {
                                    $bld[$rb][$cb][$bb]['alk'][$ba]++;
                                }
                                $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['rbw'] += $k[5];
                                $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['rtr'] += $dev[$k[1]]['ifin'][$k[2]];
                                $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['rif'][] = "{$k['3']};;{$k['4']}";
                                $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['rty']["{$k['6']}:" . date('j.M', $k[10])]++;
                            }
                            $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['fbw'] += $k[5];
                            $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['ftr'] += $dev[$k[1]]['ifout'][$k[2]];
                            $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['fif'][] = "{$k['1']};;{$k['2']}";
                            $blnk["{$ra};;{$ca};;{$ba};;{$rb};;{$cb};;{$bb}"]['fty']["{$k['6']}:" . date('j.M', $k[10])]++;
                        }
                    }
                }
                if ($lev > 3) {
                    $dev[$k[1]]['nlk']++;
                    # Count devlinks for flatmode
                    $dev[$k[1]]['alk'][$k[3]]++;
                    # Needed for arranging
                    #if ($mde == "r") {# TODO find arrange method for building rings (only links within bld matter!)
                    #	$flr[$l[0]][$l[1]][$l[2]][$k[1]]['alk'][$k[3]]++;
                    #}
                    if (!$rlnum) {
                        $dev[$k[3]]['nlk']++;
                        $dev[$k[3]]['alk'][$k[1]]++;
                    }
                }
            }
        }
        DbFreeResult($lres);
    }
    $rk = array_keys($reg);
    $nr = count($rk);
    # Draw Layout
    $cud = 0;
    if ($mde == "f") {
        $fstnod = 1;
        $nd = count(array_keys($dev));
        foreach (Arrange($dev) as $dv) {
            $jdev[$dv] = $cud;
            list($dev[$dv]['x'], $dev[$dv]['y']) = CircleCoords(intval($xm / 2 + $xo), intval($ym / 2 - $yo), $cud, $nd, $dev[$dv]['nlk'], $len, $rot);
            $mapitems .= DrawItem($dev[$dv]['x'], $dev[$dv]['y'], '0', $dv, 'd');
            if ($lev == 6) {
                DrawNodes($dv);
            }
            $imgmap .= "<area href=\"Devices-Status.php?dev=" . urlencode($dv) . "\" coords=\"" . ($dev[$dv]['x'] - $imas) . "," . ($dev[$dv]['y'] - $imas) . "," . ($dev[$dv]['x'] + $imas) . "," . ($dev[$dv]['y'] + $imas) . "\" shape=\"rect\" title=\"{$dv} " . $dev[$dv]['ip'] . " CPU:" . $dev[$dv]['cpu'] . "% Temp:" . $dev[$dv]['tlb'] . "\">\n";
            if ($loi) {
                $mapinfo .= DrawLabel($dev[$dv]['x'], $dev[$dv]['y'] - 40, Safelabel($dev[$dv]['cty'] . " " . $dev[$dv]['bld']), 1, "cornflowerblue");
            } elseif ($debug) {
                $mapinfo .= DrawLabel($dev[$dv]['x'], $dev[$dv]['y'] - 40, "Pos{$cud}", 1, "cornflowerblue");
            }
            $cud++;
        }
    } else {
        if ($mde == "g") {
            # Prepare geographic stuff
            if (count($rk) == 1) {
                $ck = array_keys($cty[$rk[0]]);
                if (count($ck) == 1) {
                    $mapbg = TopoMap($rk[0], $ck[0]);
                } else {
                    $mapbg = TopoMap($rk[0]);
                }
            } else {
                $mapbg = TopoMap();
            }
            $bg = Imagecreatefromjpeg("../topo/{$mapbg}");
            $xm = Imagesx($bg);
            $ym = Imagesy($bg);
            Imagedestroy($bg);
        }
        $cur = 0;
        $toc = 0;
        $tob = 0;
        foreach (Arrange($reg) as $r) {
            if ($mde == "g") {
                list($reg[$r]['x'], $reg[$r]['y'], $reg[$r]['cmt']) = DbCoords($r);
            }
            if (!$reg[$r]['x']) {
                list($reg[$r]['x'], $reg[$r]['y']) = CircleCoords(intval($xm / 2 + $xo), intval($ym / 2 - $yo), $cur, $nr, $reg[$r]['nlk'], $len, $rot);
            }
            if ($lev == 1) {
                $jreg[$r] = $cur;
                $mapitems .= DrawItem($reg[$r]['x'], $reg[$r]['y'], $reg[$r]['ndv'], $r, 1);
                $imgmap .= "<area href=\"?lev=2&mde={$mde}&fmt=png&loo={$loo}&loa={$loa}&st[]=" . urlencode(TopoLoc($r)) . "\" coords=\"" . ($reg[$r]['x'] - $imas) . "," . ($reg[$r]['y'] - $imas) . "," . ($reg[$r]['x'] + $imas) . "," . ($reg[$r]['y'] + $imas) . "\" shape=\"rect\" title=\"{$sholbl}\">\n";
            } else {
                if ($loi) {
                    if (count($cty[$r]) > 1) {
                        $mapinfo .= DrawItem($reg[$r]['x'], $reg[$r]['y'], '0', $r . " " . $reg[$r]['cmt'], 'ri');
                    } else {
                        $mapinfo .= DrawLabel($reg[$r]['x'], $reg[$r]['y'] - 42, Safelabel($r), 1, "cornflowerblue");
                    }
                }
                $cuc = 0;
                $nc = count(array_keys($cty[$r]));
                foreach (Arrange($cty[$r]) as $c) {
                    if ($mde == "g") {
                        list($cty[$r][$c]['x'], $cty[$r][$c]['y'], $cty[$r][$c]['cmt']) = DbCoords($r, $c);
                    }
                    if (!$cty[$r][$c]['x']) {
                        list($cty[$r][$c]['x'], $cty[$r][$c]['y']) = CircleCoords($reg[$r]['x'], $reg[$r]['y'], $cuc, $nc, $cty[$r][$c]['nlk'], $len * 10 / $lsf, $cro);
                    }
                    if ($lev == 2) {
                        $jcty["{$r};;{$c}"] = $toc;
                        $mapitems .= DrawItem($cty[$r][$c]['x'], $cty[$r][$c]['y'], $cty[$r][$c]['ndv'], $c, 2);
                        $imgmap .= "<area href=\"?lev=3&mde={$mde}&fmt=png&loo={$loo}&loa={$loa}&st[]=" . urlencode(TopoLoc($r, $c)) . "\" coords=\"" . ($cty[$r][$c]['x'] - $imas) . "," . ($cty[$r][$c]['y'] - $imas) . "," . ($cty[$r][$c]['x'] + $imas) . "," . ($cty[$r][$c]['y'] + $imas) . "\" shape=\"rect\" title=\"{$sholbl}\">\n";
                    } else {
                        if ($loi) {
                            if (count($bld[$r][$c]) > 1) {
                                $mapinfo .= DrawItem($cty[$r][$c]['x'], $cty[$r][$c]['y'], '0', $c . " " . $cty[$r][$c]['cmt'], 'ci');
                            } else {
                                $mapinfo .= DrawLabel($cty[$r][$c]['x'], $cty[$r][$c]['y'] - 30, Safelabel($c), 1, "cornflowerblue");
                            }
                        }
                        $cub = 0;
                        $nb = count(array_keys($bld[$r][$c]));
                        foreach (Arrange($bld[$r][$c]) as $b) {
                            if ($mde == "g") {
                                list($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'], $bld[$r][$c][$b]['cmt']) = DbCoords($r, $c, $b);
                            }
                            if (!$bld[$r][$c][$b]['x']) {
                                list($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y']) = CircleCoords($cty[$r][$c]['x'], $cty[$r][$c]['y'], $cub, $nb, $bld[$r][$c][$b]['nlk'] * ($mde == "b" ? $cb % 2 + 0.3 : 1), $len / pow($lsf / 10, 2), $bro);
                            }
                            if ($lev == 3) {
                                $jbld["{$r};;{$c};;{$b}"] = $tob;
                                $mapitems .= DrawItem($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'], $bld[$r][$c][$b]['ndv'], $b, 3);
                                $imgmap .= "<area href=\"?lev=4&mde={$mde}&fmt=png&loo={$loo}&loa={$loa}&st[]=" . urlencode(TopoLoc($r, $c, $b)) . "\" coords=\"" . ($bld[$r][$c][$b]['x'] - $imas) . "," . ($bld[$r][$c][$b]['y'] - $imas) . "," . ($bld[$r][$c][$b]['x'] + $imas) . "," . ($bld[$r][$c][$b]['y'] + $imas) . "\" shape=\"rect\" title=\"{$sholbl}\">\n";
                            } elseif ($mde == "b" or $mde == "g") {
                                DrawBuilding($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'], $r, $c, $b);
                            } else {
                                if ($loi) {
                                    if (count($flr[$r][$c][$b]) > 1) {
                                        $mapinfo .= DrawItem($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'], '0', $b . " " . $bld[$r][$c][$b]['cmt'], 'bi');
                                    } else {
                                        $mapinfo .= DrawLabel($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'] - 38, Safelabel($b), 1, "cornflowerblue");
                                    }
                                }
                                $cd = 0;
                                $nd = count(array_keys($flr[$r][$c][$b]));
                                foreach (Arrange($flr[$r][$c][$b]) as $d) {
                                    $jdev[$d] = $cud;
                                    list($dev[$d]['x'], $dev[$d]['y']) = CircleCoords($bld[$r][$c][$b]['x'], $bld[$r][$c][$b]['y'], $cd, $nd, $dev[$d]['nlk'], $fsz, 0, 0);
                                    $mapitems .= DrawItem($dev[$d]['x'], $dev[$d]['y'], '0', $d, 'd');
                                    $imgmap .= "<area href=\"Devices-Status.php?dev=" . urlencode($d) . "\" coords=\"" . ($dev[$d]['x'] - $imas) . "," . ($dev[$d]['y'] - $imas) . "," . ($dev[$d]['x'] + $imas) . "," . ($dev[$d]['y'] + $imas) . "\" shape=\"rect\" title=\"{$dv} " . $dev[$d]['ip'] . " CPU:" . $dev[$d]['cpu'] . "%  T:" . $dev[$d]['tmp'] . "C\">\n";
                                    if ($lev == 6) {
                                        DrawNodes($d);
                                    }
                                    $cd++;
                                    $cud++;
                                }
                            }
                            $cub++;
                            $tob++;
                        }
                    }
                    $cuc++;
                    $toc++;
                }
            }
            $cur++;
        }
    }
    # Draw Links
    if ($lev == 1) {
        $rlkeys = array_keys($rlnk);
        foreach ($rlkeys as $li) {
            $l = explode(';;', $li);
            if ($fmt == "json") {
                $ls = intval(sqrt($rlnk[$li]['fbw'] / 100000000 / $lsf) + 1);
                $maplinks .= "    {\"source\":" . $jreg[$l[0]] . ",\"target\":" . $jreg[$l[1]] . ",\"value\":{$ls}},\n";
            } else {
                DrawLink($reg[$l[0]]['x'], $reg[$l[0]]['y'], $reg[$l[1]]['x'], $reg[$l[1]]['y'], $rlnk[$li]);
            }
        }
    } elseif ($lev == 2) {
        foreach (array_keys($clnk) as $li) {
            $l = explode(';;', $li);
            if ($fmt == "json") {
                $ls = intval(sqrt($clnk[$li]['fbw'] / 100000000 / $lsf) + 1);
                $maplinks .= "    {\"source\":" . $jcty["{$l['0']};;{$l['1']}"] . ",\"target\":" . $jcty["{$l['2']};;{$l['3']}"] . ",\"value\":{$ls}},\n";
            } else {
                DrawLink($cty[$l[0]][$l[1]]['x'], $cty[$l[0]][$l[1]]['y'], $cty[$l[2]][$l[3]]['x'], $cty[$l[2]][$l[3]]['y'], $clnk[$li]);
            }
        }
    } elseif ($lev == 3) {
        foreach (array_keys($blnk) as $li) {
            $l = explode(';;', $li);
            if ($fmt == "json") {
                $ls = intval(sqrt($blnk[$li]['fbw'] / 100000000 / $lsf) + 1);
                $maplinks .= "    {\"source\":" . $jbld["{$l['0']};;{$l['1']};;{$l['2']}"] . ",\"target\":" . $jbld["{$l['3']};;{$l['4']};;{$l['5']}"] . ",\"value\":{$ls}},\n";
            } else {
                DrawLink($bld[$l[0]][$l[1]][$l[2]]['x'], $bld[$l[0]][$l[1]][$l[2]]['y'], $bld[$l[3]][$l[4]][$l[5]]['x'], $bld[$l[3]][$l[4]][$l[5]]['y'], $blnk[$li]);
            }
        }
    } elseif ($lev > 3) {
        foreach (array_keys($dlnk) as $li) {
            $l = explode(';;', $li);
            if ($fmt == "json") {
                $ls = intval(sqrt($dlnk[$li]['fbw'] / 100000000 / $lsf) + 1);
                $ls = $ls ? $ls : 1;
                $maplinks .= "    {\"source\":" . $jdev[$l[0]] . ",\"target\":" . $jdev[$l[1]] . ",\"value\":{$ls}},\n";
            } else {
                DrawLink($dev[$l[0]]['x'], $dev[$l[0]]['y'], $dev[$l[1]]['x'], $dev[$l[1]]['y'], $dlnk[$li]);
            }
        }
        if ($lev == 6) {
            foreach (array_keys($nlnk) as $li) {
                $l = explode(';;', $li);
                if ($fmt == "json") {
                    $ls = intval(sqrt($nlnk[$li]['fbw'] / 100000000 / $lsf) + 1);
                    $ls = $ls ? $ls : 1;
                    $maplinks .= "    {\"source\":" . $jdev[$l[0]] . ",\"target\":" . $jnod["{$l['0']};;{$l['1']}"] . ",\"value\":{$ls}},\n";
                } else {
                    DrawLink($dev[$l[0]]['x'], $dev[$l[0]]['y'], $nod[$l[1]]['x'], $nod[$l[1]]['y'], $nlnk[$li]);
                }
            }
        }
    }
}
function Read($ina, $filter)
{
    global $link, $locsep, $fpos, $bpos, $cpos, $rpos, $resmsg;
    global $lev, $ipi, $net, $dev, $ndev, $bdev, $fdev;
    global $devlink, $ctylink, $bldlink;
    global $nctylink, $nbldlink, $actylink, $abldlink;
    $net = array();
    if ($ina == "vlan") {
        $query = GenQuery('vlans', 's', '*', '', '', array('vlanid'), array('regexp'), array($filter));
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($vl = @DbFetchRow($res)) {
                $devs[] = preg_replace('/([\\^\\$+])/', '\\\\\\\\$1', $vl[0]);
            }
            @DbFreeResult($res);
        } else {
            print @DbError($link);
        }
        if (!is_array($devs)) {
            echo $resmsg;
            die;
        }
        $query = GenQuery('devices', 's', '*', '', '', array('name'), array('regexp'), array(implode("|", $devs)));
    } elseif ($ina == "network") {
        $query = GenQuery('networks', 's', '*', '', '', array('ip'), array('='), array($filter));
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($vl = @DbFetchRow($res)) {
                $devs[] = preg_replace('/([\\^\\$\\*\\+])/', '\\\\\\\\$1', $vl[0]);
            }
            @DbFreeResult($res);
        } else {
            print @DbError($link);
        }
        if (!is_array($devs)) {
            echo $resmsg;
            die;
        }
        $query = GenQuery('devices', 's', '*', '', '', array('name'), array('regexp'), array(implode("|", $devs)));
    } else {
        $query = GenQuery('devices', 's', '*', '', '', array('location'), array('regexp'), array($filter));
    }
    $res = @DbQuery($query, $link);
    if ($res) {
        while ($unit = @DbFetchRow($res)) {
            $locitems = explode($locsep, $unit[10]);
            if ($cpos === false) {
                $cty = "-";
            } else {
                $cty = $locitems[$cpos];
            }
            if ($bpos === false) {
                $bld = "-";
            } else {
                $bld = $locitems[$bpos];
            }
            if ($fpos === false) {
                $flr = "-";
            } else {
                $flr = $locitems[$fpos];
            }
            if ($rpos === false) {
                $rom = "-";
            } else {
                $rom = $locitems[$rpos];
            }
            $dev[$unit[0]]['ip'] = long2ip($unit[1]);
            $dev[$unit[0]]['ic'] = $unit[18];
            $dev[$unit[0]]['cty'] = $cty;
            $dev[$unit[0]]['bld'] = $bld;
            $dev[$unit[0]]['flr'] = $flr;
            $dev[$unit[0]]['rom'] = $rom;
            $ndev[$cty][$bld][$flr][] = $unit[0];
            $bdev[$cty][$bld]++;
            $fdev[$cty][$bld][$flr]++;
        }
        @DbFreeResult($res);
    } else {
        print @DbError($link);
    }
    if ($ipi) {
        $query = GenQuery('networks');
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($n = @DbFetchRow($res)) {
                $net[$n[0]][$n[1]] .= " " . long2ip($n[2]);
            }
        } else {
            print @DbError($link);
        }
        @DbFreeResult($res);
    }
    $query = GenQuery('links');
    $res = @DbQuery($query, $link);
    if ($res) {
        while ($l = @DbFetchRow($res)) {
            if ($dev[$l[1]]['ic'] and $dev[$l[3]]['ic']) {
                // both ends are ok, if an icon exists
                if ($lev == "f") {
                    if (isset($devlink[$l[3]][$l[1]])) {
                        // opposite link doesn't exist?
                        $devlink[$l[3]][$l[1]]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $devlink[$l[1]][$l[3]]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
                if ($dev[$l[1]]['bld'] != $dev[$l[3]]['bld']) {
                    // is it same bld?
                    $nbldlink[$dev[$l[1]]['bld']]++;
                    $abldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]++;
                    // needed for Arranging.
                    if (isset($bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']])) {
                        // link defined already?
                        $bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $bldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
                if ($dev[$l[1]]['cty'] != $dev[$l[3]]['cty']) {
                    // is it same cty?
                    $nctylink[$dev[$l[1]]['cty']]++;
                    $actylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]++;
                    // needed for Arranging.
                    if (isset($ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']])) {
                        // link defined already?
                        $ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
                    } else {
                        $ctylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
                    }
                }
            }
        }
        @DbFreeResult($res);
    } else {
        print @DbError($link);
    }
}
Example #8
0
function NetPop($ina, $opa, $sta, $lim, $ord)
{
    global $link, $modgroup, $self, $verb1, $netlbl, $dislbl, $adrlbl, $poplbl, $agelbl, $tim, $totlbl, $srtlbl;
    if ($ina == "devip") {
        $ina = "ifip";
    }
    if ($ord) {
        $ocol = "device";
        $srt = "{$srtlbl}: Device";
    } else {
        $ocol = "ifip";
        $srt = "{$srtlbl}: IP {$adrlbl}";
    }
    $query = GenQuery('networks', 's', 'networks.*,lastdis', $ocol, '', array('ifip', $ina), array('>', $opa), array('0', $sta), array('AND'), 'LEFT JOIN devices USING (device)');
    $res = DbQuery($query, $link);
    if ($res) {
        $row = 0;
        $netok = array();
        while ($n = DbFetchRow($res)) {
            $n[2] = ip2long(long2ip($n[2]));
            # Hack to fix signing issue for 32bit vars in PHP!
            $dmsk = 4294967295.0 << 32 - $n[4];
            $dnet = long2ip($n[2] & $dmsk);
            if ($n[4] > 16 and $n[4] < 32) {
                # Only > /16 but not /32 networks
                if (!array_key_exists($dnet, $netok)) {
                    # Only if subnet hasn't been processed
                    $netok[$dnet] = 1;
                    $nod[$dnet] = array();
                    $nquery = GenQuery('nodes', 's', 'name,inet_ntoa(nodip)', 'nodip', '', array("nodip & {$dmsk}"), array('='), array(sprintf("%u", $n[2] & $dmsk)));
                    $nres = DbQuery($nquery, $link);
                    if ($nres) {
                        while ($no = DbFetchRow($nres)) {
                            $nod[$dnet][$no[1]] = $no[0];
                        }
                    }
                    DbFreeResult($nres);
                }
                $n[2] = long2ip($n[2]);
                $dev[$dnet][$n[2]] = $n[0];
                $nets[$dnet] = $n[4];
                if (count(array_keys($nets)) == $lim) {
                    break;
                }
            }
        }
        DbFreeResult($res);
        if ($nets) {
            ?>
<h2><?php 
            echo $netlbl;
            ?>
 <?php 
            echo $poplbl;
            ?>
</h2>

<table class="content"><tr class="<?php 
            echo $modgroup[$self];
            ?>
2">
<th colspan="2"><img src="img/16/net.png"><br>IP <?php 
            echo $adrlbl;
            ?>
</th>
<th><img src="img/16/nods.png"><br><?php 
            echo $poplbl;
            ?>
</th>
</tr>
<?php 
            $row = 0;
            foreach (array_keys($nets) as $net) {
                if ($row % 2) {
                    $bg = "txta";
                    $bi = "imga";
                } else {
                    $bg = "txtb";
                    $bi = "imgb";
                }
                $row++;
                list($ntimg, $ntit) = Nettype($net);
                echo "<tr class=\"{$bg}\">\n";
                echo "<th class=\"{$bi}\" width=\"20\"><img src=\"img/{$ntimg}\" title=\"{$ntit}\"></th>\n";
                echo "<td><a href=\"?in[]=devip&op[]==&st[]={$net}%2F{$nets[$net]}&rep%5B%5D=pop\">{$net}/{$nets[$net]}</a><p>";
                echo "<a href=\"Topology-Networks.php?in[]=ifip&op[]==&st[]={$net}%2F{$nets[$net]}\"><img src=\"img/16/glob.png\" title=\"IF IPs\"> " . count(array_keys($dev[$net])) . "</a><p>";
                echo "<a href=\"Nodes-List.php?in[]=nodip&op[]==&st[]={$net}%2F{$nets[$net]}\"><img src=\"img/16/nods.png\" title=\"Node IPs\"> " . count(array_keys($nod[$net])) . "</a";
                echo "</td>";
                echo "<td><table><tr>";
                $col = 0;
                $dn = ip2long($net);
                $max = $dn + pow(2, 32 - $nets[$net]);
                for ($a = $dn; $a < $max; $a++) {
                    if ($col == 64) {
                        $col = 0;
                        echo "</tr>\n<tr>";
                    }
                    $ip = long2ip($a);
                    if (array_key_exists($ip, $dev[$net]) and array_key_exists($ip, $nod[$net])) {
                        echo "<td title=\"{$ip} Dev:" . $dev[$net][$ip] . " Node:" . $nod[$net][$ip] . "\" class=\"warn\"><a href=\"Topology-Networks.php?in[]=ifip&op[]==&st[]={$ip}\">&nbsp;</a></td>";
                    } elseif (array_key_exists($ip, $nod[$net])) {
                        echo "<td title=\"{$ip} Node:" . $nod[$net][$ip] . "\" class=\"good\"><a href=\"Nodes-List.php?in[]=nodip&op[]==&st[]={$ip}\">&nbsp;</a></td>";
                    } elseif (array_key_exists($ip, $dev[$net])) {
                        echo "<td title=\"{$ip} Dev:" . $dev[$net][$ip] . "\" class=\"noti\"><a href=\"Topology-Networks.php?in[]=ifip&op[]==&st[]={$ip}\">&nbsp;</a></td>";
                    } elseif ($a == $dn or $a == $max - 1) {
                        $netxt = $a == $dn ? $netlbl : "Broadcast";
                        echo "<td title=\"{$netxt}:{$ip}\" class=\"{$bg} part\">&nbsp;</td>";
                    } else {
                        echo "<td title=\"{$ip}\" class=\"{$bi}\">&nbsp;</td>";
                    }
                    $col++;
                }
                echo "</tr></table></td></tr>\n";
            }
            ?>
</table>
<table class="content">
<tr class="<?php 
            echo $modgroup[$self];
            ?>
2"><td><?php 
            echo $row;
            ?>
 <?php 
            echo $netlbl;
            ?>
, <?php 
            echo $srt;
            ?>
</td></tr>
</table>
<p>
<?php 
        }
    }
}
Example #9
0
function TopoTable($reg = "", $cty = "", $bld = "")
{
    global $link, $dev, $deval, $dcity, $dbuild, $locsep, $maxcol, $bg1, $bg2, $tabtag;
    $query = GenQuery('devices', 's', '*', '', '', array('location'), array('regexp'), array(TopoLoc($reg, $cty, $bld)));
    $res = @DbQuery($query, $link);
    if ($res) {
        while ($d = @DbFetchRow($res)) {
            $l = explode($locsep, $d[10]);
            if (isset($deval[$d[0]])) {
                # Device is monitored if set...
                $dcity[$l[0]][$l[1]]['mn']++;
                $dcity[$l[0]][$l[1]]['al'] += $deval[$d[0]];
                $dbuild[$l[0]][$l[1]][$l[2]]['mn']++;
                $dbuild[$l[0]][$l[1]][$l[2]]['al'] += $deval[$d[0]];
                $mn = 1;
            } else {
                $mn = 0;
            }
            if (!$cty) {
                $dcity[$l[0]][$l[1]]['nd']++;
            } elseif (!$bld) {
                $dbuild[$l[0]][$l[1]][$l[2]]['nd']++;
                if ($d[6] > 3) {
                    $dbuild[$l[0]][$l[1]][$l[2]]['nr']++;
                }
            } else {
                $dev[$l[3]][$l[4]][$d[0]]['rk'] = "{$l['5']} <i>{$l['6']}</i>";
                $dev[$l[3]][$l[4]][$d[0]]['ip'] = $d[1];
                $dev[$l[3]][$l[4]][$d[0]]['ty'] = $d[3];
                $dev[$l[3]][$l[4]][$d[0]]['co'] = $d[11];
                $dev[$l[3]][$l[4]][$d[0]]['ic'] = $d[18];
                $dev[$l[3]][$l[4]][$d[0]]['mn'] = $mn;
                $dev[$l[3]][$l[4]][$d[0]]['al'] = $deval[$d[0]];
            }
        }
        @DbFreeResult($res);
    } else {
        print @DbError($link);
    }
}
Example #10
0
function NodPop($in, $op, $st, $co)
{
    global $link, $retire;
    $query = GenQuery('nodes', 's', 'count(mac)', '', '', $in, $op, $st, $co, 'JOIN devices USING (device)');
    $res = DbQuery($query, $link);
    $lpop = DbFetchRow($res);
    DbFreeResult($res);
    return $lpop[0];
}