| 1 |
<? |
| 2 |
include("include.php"); |
| 3 |
include("header.php"); |
| 4 |
|
| 5 |
// Get variables from url |
| 6 |
|
| 7 |
if (isset($_GET['sensor_id']) && $_GET['sensor_id'] != "none") |
| 8 |
$sensor_id = $_GET['sensor_id']; |
| 9 |
|
| 10 |
if (isset($_GET['interval']) && $_GET['interval'] != "none") |
| 11 |
$interval = $_GET['interval']; |
| 12 |
|
| 13 |
if (isset($_GET['timestamp']) && $_GET['timestamp'] != "none") |
| 14 |
$timestamp = $_GET['timestamp']; |
| 15 |
|
| 16 |
if (isset($_GET['subnet']) && $_GET['subnet'] != "none") |
| 17 |
$subnet = $_GET['subnet']; |
| 18 |
|
| 19 |
if (isset($_GET['limit']) && $_GET['limit'] != "none") |
| 20 |
$limit = $_GET['limit']; |
| 21 |
|
| 22 |
if (isset($_GET['graphs'])) |
| 23 |
$graphs = $_GET['graphs']; |
| 24 |
|
| 25 |
$db = ConnectDb(); |
| 26 |
?> |
| 27 |
<HEAD> |
| 28 |
<link href="bandwidthd.css" rel="stylesheet" type="text/css"> |
| 29 |
</head> |
| 30 |
<FORM name="navigation" method=get action=<?=$PHP_SELF?>> |
| 31 |
<table width=100% cellspacing=0 cellpadding=5 border=1> |
| 32 |
<tr> |
| 33 |
<td> |
| 34 |
<? |
| 35 |
$sql = "SELECT sensor_name, interface, sensor_id from sensors order by sensor_name, interface;"; |
| 36 |
$result = @pg_query($sql); |
| 37 |
if (!$result) |
| 38 |
{ |
| 39 |
echo "<center>Collecting data...</center>"; |
| 40 |
exit; |
| 41 |
} |
| 42 |
?> |
| 43 |
<SELECT name="sensor_id"> |
| 44 |
<OPTION value="none">--Select A Sensor-- |
| 45 |
<? |
| 46 |
while ($r = pg_fetch_array($result)) |
| 47 |
echo "<option value=\"".$r['sensor_id']."\" ".($sensor_id==$r['sensor_id']?"SELECTED":"").">".$r['sensor_name']." - ".$r['interface']."\n"; |
| 48 |
?> |
| 49 |
</SELECT> |
| 50 |
<td><SELECT name="interval"> |
| 51 |
<OPTION value="none">--Select An Interval-- |
| 52 |
<OPTION value=<?=INT_DAILY?> <?=$interval==INT_DAILY?"SELECTED":""?>>Daily |
| 53 |
<OPTION value=<?=INT_WEEKLY?> <?=$interval==INT_WEEKLY?"SELECTED":""?>>Weekly |
| 54 |
<OPTION value=<?=INT_MONTHLY?> <?=$interval==INT_MONTHLY?"SELECTED":""?>>Monthly |
| 55 |
<OPTION value=<?=INT_YEARLY?> <?=$interval==INT_YEARLY?"SELECTED":""?>>Yearly |
| 56 |
<OPTION value=<?=24*60*60?> <?=$interval==24*60*60?"SELECTED":""?>>24hrs |
| 57 |
<OPTION value=<?=30*24*60*60?> <?=$interval==30*24*60*60?"SELECTED":""?>>30days |
| 58 |
</select> |
| 59 |
|
| 60 |
<td><SELECT name="limit"> |
| 61 |
<OPTION value="none">--How Many Results-- |
| 62 |
<OPTION value=20 <?=$limit==20?"SELECTED":""?>>20 |
| 63 |
<OPTION value=50 <?=$limit==50?"SELECTED":""?>>50 |
| 64 |
<OPTION value=100 <?=$limit==100?"SELECTED":""?>>100 |
| 65 |
<OPTION value=all <?=$limit=="all"?"SELECTED":""?>>All |
| 66 |
</select> |
| 67 |
|
| 68 |
<? if ($graphs != "") $GraphsChecked = "Checked"; else $GraphsChecked = ""; ?> |
| 69 |
<td><input type="checkbox" name="graphs" <?=$GraphsChecked?>>Display Graphs</a> |
| 70 |
|
| 71 |
<td>Subnet Filter:<input name=subnet value="<?=isset($subnet)?$subnet:"0.0.0.0/0"?>"> |
| 72 |
<input type=submit value="Go"> |
| 73 |
</table> |
| 74 |
</FORM> |
| 75 |
<? |
| 76 |
// Set defaults |
| 77 |
if (!isset($interval)) |
| 78 |
$interval = DFLT_INTERVAL; |
| 79 |
|
| 80 |
if (!isset($timestamp)) |
| 81 |
$timestamp = time() - $interval + (0.05*$interval); |
| 82 |
|
| 83 |
if (!isset($limit)) |
| 84 |
$limit = 20; |
| 85 |
|
| 86 |
// Validation |
| 87 |
if (!isset($sensor_id)) |
| 88 |
exit(0); |
| 89 |
|
| 90 |
$sql = "SELECT sensor_name, interface, sensor_id from sensors where sensor_id = $sensor_id;"; |
| 91 |
$result = @pg_query($sql); |
| 92 |
$r = pg_fetch_array($result); |
| 93 |
$sensor_name = $r['sensor_name']; |
| 94 |
$interface = $r['interface']; |
| 95 |
|
| 96 |
// Print Title |
| 97 |
|
| 98 |
if (isset($limit)) |
| 99 |
echo "<h2>Top $limit - $sensor_name - $interface</h2>"; |
| 100 |
else |
| 101 |
echo "<h2>All Records - $sensor_name - $interface</h2>"; |
| 102 |
|
| 103 |
// Sqlize the incomming variables |
| 104 |
if (isset($subnet)) |
| 105 |
$sql_subnet = "and ip <<= '$subnet'"; |
| 106 |
|
| 107 |
// Sql Statement |
| 108 |
$sql = "select tx.ip, rx.scale as rxscale, tx.scale as txscale, tx.total+rx.total as total, tx.total as sent, |
| 109 |
rx.total as received, tx.tcp+rx.tcp as tcp, tx.udp+rx.udp as udp, |
| 110 |
tx.icmp+rx.icmp as icmp, tx.http+rx.http as http, |
| 111 |
tx.mail+rx.mail as mail, |
| 112 |
tx.p2p+rx.p2p as p2p, tx.ftp+rx.ftp as ftp |
| 113 |
from |
| 114 |
|
| 115 |
(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp, |
| 116 |
sum(http) as http, sum(mail) as mail, sum(p2p) as p2p, sum(ftp) as ftp |
| 117 |
from sensors, bd_tx_log |
| 118 |
where sensors.sensor_id = '$sensor_id' |
| 119 |
and sensors.sensor_id = bd_tx_log.sensor_id |
| 120 |
$sql_subnet |
| 121 |
and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime |
| 122 |
group by ip) as tx, |
| 123 |
|
| 124 |
(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp, |
| 125 |
sum(http) as http, sum(mail) as mail, sum(p2p) as p2p, sum(ftp) as ftp |
| 126 |
from sensors, bd_rx_log |
| 127 |
where sensors.sensor_id = '$sensor_id' |
| 128 |
and sensors.sensor_id = bd_rx_log.sensor_id |
| 129 |
$sql_subnet |
| 130 |
and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime |
| 131 |
group by ip) as rx |
| 132 |
|
| 133 |
where tx.ip = rx.ip |
| 134 |
order by total desc;"; |
| 135 |
|
| 136 |
//echo "</center><pre>$sql</pre><center>"; exit(0); |
| 137 |
pg_query("SET sort_mem TO 30000;"); |
| 138 |
|
| 139 |
pg_send_query($db, $sql); |
| 140 |
|
| 141 |
$query_start = time(); |
| 142 |
Echo "<strong>Query In Progress."; |
| 143 |
flush(); |
| 144 |
while (pg_connection_busy($db)) { |
| 145 |
sleep(2); |
| 146 |
Echo "."; |
| 147 |
flush(); |
| 148 |
} |
| 149 |
|
| 150 |
echo((time()-$query_start)." seconds </strong>"); |
| 151 |
|
| 152 |
$result = pg_get_result($db); |
| 153 |
|
| 154 |
pg_query("set sort_mem to default;"); |
| 155 |
|
| 156 |
if ($limit == "all") |
| 157 |
$limit = pg_num_rows($result); |
| 158 |
|
| 159 |
echo "<a name=top><table width=100% border=1 cellspacing=0><tr><td>Ip<td>Name<td>Total<td>Sent<td>Received<td>tcp<td>udp<td>icmp<td>http<td>mail<td>p2p<td>ftp"; |
| 160 |
|
| 161 |
if (!isset($subnet)) // Set this now for total graphs |
| 162 |
$subnet = "0.0.0.0/0"; |
| 163 |
|
| 164 |
// Output Total Line |
| 165 |
if ($graphs == "") |
| 166 |
$url = "<a href=# onclick=\"window.open('details.php?sensor_id=$sensor_id&ip=$subnet','_blank', 'scrollbars=yes,width=930,height=768,resizable=yes,left=20,top=20')\">"; |
| 167 |
else |
| 168 |
$url = "<a href=#Total>"; |
| 169 |
|
| 170 |
echo "\n<TR><TD>".$url."Total</a><TD>$subnet"; |
| 171 |
foreach (array("total", "sent", "received", "tcp", "udp", "icmp", "http", "mail", "p2p", "ftp") as $key) |
| 172 |
{ |
| 173 |
for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++) |
| 174 |
{ |
| 175 |
$r = pg_fetch_array($result, $Counter); |
| 176 |
$Total += $r[$key]; |
| 177 |
} |
| 178 |
echo fmtb($Total); |
| 179 |
} |
| 180 |
echo "\n"; |
| 181 |
|
| 182 |
// Output Other Lines |
| 183 |
for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++) |
| 184 |
{ |
| 185 |
$r = pg_fetch_array($result, $Counter); |
| 186 |
if ($graphs == "") |
| 187 |
$url = "<a href=# onclick=\"window.open('details.php?sensor_id=$sensor_id&ip=".$r['ip']."','_blank', 'scrollbars=yes,width=930,height=768,resizable=yes,left=20,top=20')\">"; |
| 188 |
else |
| 189 |
$url = "<a href=#".$r['ip'].">"; |
| 190 |
echo "<tr><td>".$url; |
| 191 |
echo $r['ip']."<td>".gethostbyaddr($r['ip']); |
| 192 |
echo "</a>"; |
| 193 |
echo fmtb($r['total']).fmtb($r['sent']).fmtb($r['received']). |
| 194 |
fmtb($r['tcp']).fmtb($r['udp']).fmtb($r['icmp']).fmtb($r['http']).fmtb($r['mail']). |
| 195 |
fmtb($r['p2p']).fmtb($r['ftp'])."\n"; |
| 196 |
} |
| 197 |
echo "</table></center>"; |
| 198 |
|
| 199 |
// Stop here |
| 200 |
if ($graphs == "") |
| 201 |
exit(); |
| 202 |
|
| 203 |
// Output Total Graph |
| 204 |
for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++) |
| 205 |
{ |
| 206 |
$r = pg_fetch_array($result, $Counter); |
| 207 |
$scale = max($r['txscale'], $scale); |
| 208 |
$scale = max($r['rxscale'], $scale); |
| 209 |
} |
| 210 |
|
| 211 |
if ($subnet == "0.0.0.0/0") |
| 212 |
$total_table = "bd_tx_total_log"; |
| 213 |
else |
| 214 |
$total_table = "bd_tx_log"; |
| 215 |
|
| 216 |
$sn = str_replace("/", "_", $subnet); |
| 217 |
|
| 218 |
echo "<a name=Total><h3><a href=details.php?sensor_id=$sensor_id&ip=$subnet>"; |
| 219 |
echo "Total - Total of $subnet</h3>"; |
| 220 |
echo "</a>"; |
| 221 |
echo "Send:<br><img src=graph.php?ip=$sn&interval=$interval&sensor_id=".$sensor_id."&table=$total_table><br>"; |
| 222 |
echo "<img src=legend.gif><br>\n"; |
| 223 |
if ($subnet == "0.0.0.0/0") |
| 224 |
$total_table = "bd_rx_total_log"; |
| 225 |
else |
| 226 |
$total_table = "bd_rx_log"; |
| 227 |
echo "Receive:<br><img src=graph.php?ip=$sn&interval=$interval&sensor_id=".$sensor_id."&table=$total_table><br>"; |
| 228 |
echo "<img src=legend.gif><br>\n"; |
| 229 |
echo "<a href=#top>[Return to Top]</a>"; |
| 230 |
|
| 231 |
// Output Other Graphs |
| 232 |
for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++) |
| 233 |
{ |
| 234 |
$r = pg_fetch_array($result, $Counter); |
| 235 |
echo "<a name=".$r['ip']."><h3><a href=details.php?sensor_id=$sensor_id&ip=".$r['ip'].">"; |
| 236 |
if ($r['ip'] == "0.0.0.0") |
| 237 |
echo "Total - Total of all subnets</h3>"; |
| 238 |
else |
| 239 |
echo $r['ip']." - ".gethostbyaddr($r['ip'])."</h3>"; |
| 240 |
echo "</a>"; |
| 241 |
echo "Send:<br><img src=graph.php?ip=".$r['ip']."&interval=$interval&sensor_id=".$sensor_id."&table=bd_tx_log&yscale=".(max($r['txscale'], $r['rxscale']))."><br>"; |
| 242 |
echo "<img src=legend.gif><br>\n"; |
| 243 |
echo "Receive:<br><img src=graph.php?ip=".$r['ip']."&interval=$interval&sensor_id=".$sensor_id."&table=bd_rx_log&yscale=".(max($r['txscale'], $r['rxscale']))."><br>"; |
| 244 |
echo "<img src=legend.gif><br>\n"; |
| 245 |
echo "<a href=#top>[Return to Top]</a>"; |
| 246 |
} |
| 247 |
|
| 248 |
include('footer.php'); |