forked from individual-it/bandwidthd-pSQL-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
256 lines (218 loc) · 9.13 KB
/
index.php
File metadata and controls
256 lines (218 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?include("include.php");?>
<!DOCTYPE HTML>
<html>
<head>
<title>BandWidthd</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="js/jquery.tinysort.min.js"></script>
<script type="text/javascript" src="js/bandwidthd.js"></script>
<link media="screen" rel="stylesheet" type="text/css" href="css/style.css">
<link media="screen" rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container content">
<img id="logo" alt="logo" src="logo.gif">
<?
trim_get();
// Get variables from url and set defaults
if (isset($_GET['sensor_id']) && $_GET['sensor_id'] != "none")
$sensor_id = filter_var($_GET['sensor_id'], FILTER_SANITIZE_NUMBER_INT);
if (isset($_GET['interval']) && $_GET['interval'] != "none")
$interval = filter_var($_GET['interval'], FILTER_SANITIZE_NUMBER_INT);
else
$interval = DFLT_INTERVAL;
if (isset($_GET['timestamp']) && $_GET['timestamp'] != "none")
$timestamp = filter_var($_GET['timestamp'], FILTER_SANITIZE_NUMBER_INT);
else
$timestamp = time() - $interval + (0.05*$interval);
if (isset($_GET['subnet']) && $_GET['subnet'] != "none")
{
$subnet = sanitize_ip ($_GET['subnet']);
}
if (isset($_GET['limit']) && $_GET['limit'] == "all")
$limit = "all";
elseif (isset($_GET['limit']) && $_GET['limit'] != "none")
$limit = filter_var($_GET['limit'], FILTER_SANITIZE_NUMBER_INT);
else
$limit = 20;
$db = ConnectDb();
?>
<form name="navigation" class="form-inline" method=get action=<?=$_SERVER['PHP_SELF']?>>
<div class="form-group">
<select class="form-control" name="sensor_id">
<option value="none">--Select A Sensor--
<? $sql = "SELECT sensor_id,sensor_name from sensors order by sensor_name;";
$result = pg_query($sql);
while ($r = pg_fetch_array($result))
{
echo "<option value=\"".$r['sensor_id']."\" ".(isset($sensor_id) && $sensor_id==$r['sensor_id']?"SELECTED":"").">".$r['sensor_name']."\n";
}
?>
</select>
</div>
<div class="form-group">
<select class="form-control" name="interval">
<option value="none">--Select An Interval--
<option value=<?=INT_DAILY?> <?=isset($interval) && $interval==INT_DAILY?"SELECTED":""?>>Daily
<option value=<?=INT_WEEKLY?> <?=isset($interval) && $interval==INT_WEEKLY?"SELECTED":""?>>Weekly
<option value=<?=INT_MONTHLY?> <?=isset($interval) && $interval==INT_MONTHLY?"SELECTED":""?>>Monthly
<option value=<?=INT_YEARLY?> <?=isset($interval) && $interval==INT_YEARLY?"SELECTED":""?>>Yearly
<option value=<?=24*60*60?> <?=isset($interval) && $interval==24*60*60?"SELECTED":""?>>24hrs
<option value=<?=30*24*60*60?> <?=isset($interval) && $interval==30*24*60*60?"SELECTED":""?>>30days
</select>
</div>
<div class="form-group">
<select class="form-control" name="limit">
<option value="none">--How Many Results--
<option value="20" <?=isset($limit) && $limit==20?"SELECTED":""?>>20
<option value="50" <?=isset($limit) && $limit==50?"SELECTED":""?>>50
<option value="100" <?=isset($limit) && $limit==100?"SELECTED":""?>>100
<option value="all" <?=isset($limit) && $limit=="all"?"SELECTED":""?>>All
</select>
</div>
<div class="form-group">
<input id="subnet" class="form-control" name="subnet" value="<?=isset($subnet)?$subnet:"0.0.0.0/0"?>">
</div>
<input class="btn btn-success" type="submit" value="Go">
</form>
<? if (!isset($sensor_id)) exit(0);
$sql = "SELECT sensor_name from sensors WHERE sensor_id = $sensor_id ;";
$result = pg_query($sql);
$sensor_name = pg_fetch_row($result);
$sensor_name = $sensor_name[0];
// Validation
if (!isset($sensor_name)) exit(0);
// Print Title
if (isset($limit))
echo "<h2>Top $limit - $sensor_name</h2>";
else
echo "<h2>All Records - $sensor_name</h2>";
// Sqlize the incomming variables
if (isset($subnet))
$sql_subnet = "and ip <<= '$subnet'";
// Sql Statement
$sql = "select tx.ip, rx.scale as rxscale, tx.scale as txscale, tx.total+rx.total as total, tx.total as sent,
rx.total as received, tx.tcp+rx.tcp as tcp, tx.udp+rx.udp as udp,
tx.icmp+rx.icmp as icmp, tx.http+rx.http as http,
tx.p2p+rx.p2p as p2p, tx.ftp+rx.ftp as ftp
from
(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,
sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
from sensors, bd_tx_log
where sensor_name = '$sensor_name'
and sensors.sensor_id = bd_tx_log.sensor_id
$sql_subnet
and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime
group by ip) as tx,
(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,
sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
from sensors, bd_rx_log
where sensor_name = '$sensor_name'
and sensors.sensor_id = bd_rx_log.sensor_id
$sql_subnet
and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime
group by ip) as rx
where tx.ip = rx.ip
order by total desc;";
//echo "</center><pre>$sql</pre><center>"; exit(0);
pg_query("SET sort_mem TO 30000;");
$result = pg_query($sql);
pg_query("set sort_mem to default;");
if ($limit == "all")
$limit = pg_num_rows($result);
echo "<div class='table-responsive'><table class='table table-hover table-bordered' id='xtable' style='font-size:13px;'>
<thead>
<tr>
<th title='click for sorting' onclick='sortTable(0);'>IP</th>
<th title='click for sorting' onclick='sortTable(1);'>Name</th>
<th title='click for sorting' onclick='sortTable(2);'>Total</th>
<th title='click for sorting' onclick='sortTable(3);'>Sent</th>
<th title='click for sorting' onclick='sortTable(4);'>Received</th>
<th title='click for sorting' onclick='sortTable(5);'>tcp</th>
<th title='click for sorting' onclick='sortTable(6);'>udp</th>
<th title='click for sorting' onclick='sortTable(7);'>icmp</th>
<th title='click for sorting' onclick='sortTable(8);'>http</th>
<th title='click for sorting' onclick='sortTable(9);'>p2p</th>
<th title='click for sorting' onclick='sortTable(10);'>ftp</th>
</tr>
</thead>";
if (!isset($subnet)) // Set this now for total graphs
$subnet = "0.0.0.0/0";
// Output Total Line
echo "<tbody><tr><td><a href=#Total>Total</a><td>$subnet";
foreach (array("total", "sent", "received", "tcp", "udp", "icmp", "http", "p2p", "ftp") as $key)
{
for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++)
{
$r = pg_fetch_array($result, $Counter);
$Total += $r[$key];
}
echo fmtb($Total);
}
echo "\n";
// Output Other Lines
for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++)
{
$r = pg_fetch_array($result, $Counter);
$hostname=gethostbyaddr($r['ip']);
echo "<tr><td class='".str_replace('.','_',$r['ip'])."'><a href='#".$r['ip']."'>";
echo $r['ip']."</a></td><td class='".$hostname."'>".$hostname;
echo "</td>";
echo fmtb($r['total']).fmtb($r['sent']).fmtb($r['received']).
fmtb($r['tcp']).fmtb($r['udp']).fmtb($r['icmp']).fmtb($r['http']).
fmtb($r['p2p']).fmtb($r['ftp'])."</tr>";
}
echo "</tbody></table></div>";
// Output Total Graph
$scale = 0;
for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++)
{
$r = pg_fetch_array($result, $Counter);
$scale = max($r['txscale'], $scale);
$scale = max($r['rxscale'], $scale);
}
if ($subnet == "0.0.0.0/0") {
$total_table = "bd_tx_total_log";
$total_table2 = "bd_rx_total_log";
}
else {
$total_table = "bd_tx_log";
$total_table2 = "bd_rx_log";
}
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><a href='details.php?sensor_id=$sensor_id&ip=$subnet'>Total - Total of $subnet</a></h3>
</div>
<div class='panel-body'>
<div class='well'>
Send:<br><img alt='graph' src='graph.php?ip=$subnet&interval=$interval&sensor_id=".$sensor_id."&table=$total_table'><br>
<img alt='' src=legend.gif><br>
</div>
<div class='well'>
Receive:<br><img alt='graph' src='graph.php?ip=$subnet&interval=$interval&sensor_id=".$sensor_id."&table=$total_table2'>
<img alt='' src=legend.gif><br>
</div>
</div>
</div>";
// Output Other Graphs
for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++)
{
$r = pg_fetch_array($result, $Counter);
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><a href='details.php?sensor_id=$sensor_id&ip=".$r['ip']."'>";
if ($r['ip'] == "0.0.0.0") {
echo "Total - Total of all subnets";
}
else {
echo $r['ip']." - ".gethostbyaddr($r['ip']);
}
echo "</a></h3></div> <div class='panel-body'>
<div class='well'>Send:<br><img alt='graph' src='graph.php?ip=".$r['ip']."&interval=$interval&sensor_id=".$sensor_id."&table=bd_tx_log&yscale=".(max($r['txscale'], $r['rxscale']))."'/><br>
<img alt='legend' src='legend.gif'/><br></div>
<div class='well'>Receive:<br><img alt='graph' src='graph.php?ip=".$r['ip']."&interval=$interval&sensor_id=".$sensor_id."&table=bd_rx_log&yscale=".(max($r['txscale'], $r['rxscale']))."'/><br>
<img alt='legend' src='legend.gif'/><br></div></div></div>";
}
include('footer.php');