forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwgraph.cgi
executable file
·451 lines (422 loc) · 12 KB
/
bwgraph.cgi
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/local/bin/perl
# bwgraph.cgi
# Show current bandwidth usage graphs
require './virtual-server-lib.pl';
&ReadParse();
# Work out which domains to show
if ($in{'dom'}) {
# Specific domain
@doms = ( $d=&get_domain($in{'dom'}) );
&can_edit_domain($d) || &error($text{'newbw_ecannot'});
$subh = &domain_in($doms[0]);
if ($d->{'parent'}) {
# Which is a sub-server
$parent = &get_domain($d->{'parent'});
}
}
else {
# All owned by user
@doms = grep { !$_->{'parent'} &&
&can_edit_domain($_) } &list_domains();
}
&ui_print_header($subh, $text{'bwgraph_title'}, "");
# Show current usage and limit for all virtual servers
$max = 0;
foreach $d (@doms) {
$max = $d->{'bw_usage'} if ($d->{'bw_usage'} > $max);
$max = $d->{'bw_usage_only'} if ($d->{'bw_usage_only'} > $max);
$max = $d->{'bw_limit'} if ($d->{'bw_limit'} > $max);
}
if ($parent) {
$max = $parent->{'bw_limit'} if ($parent->{'bw_limit'} > $max);
}
if ($max) {
# Show links for mode
print "<b>$text{'bwgraph_mode'}</b>\n";
if ($in{'dom'}) {
@subs = grep { !$_->{'alias'} }
&get_domain_by("parent", $doms[0]->{'id'});
}
@links = ( );
if ($parent && !$in{'mode'}) {
# For sub-servers, default mode is by date
$in{'mode'} = 2;
}
foreach $m (0, 4, 1, 2, 3) {
if ($m == 4 && $in{'dom'}) {
# Just one domain, so no need for over-limit mode
next;
}
if ($m == 0 && $parent) {
# If this is a sub-server, don't show limit
next;
}
if ($m == 1 && $in{'dom'}) {
# Don't show sub-server if none
next if (!@subs);
$t = $text{'bwgraph_mode_'.$m};
}
elsif (($m == 0 || $m == 4) && $in{'dom'}) {
$t = @subs ? $text{'bwgraph_mode_'.$m.'two'}
: $text{'bwgraph_mode_'.$m.'one'};
}
else {
$t = $text{'bwgraph_mode_'.$m};
}
if ($m == $in{'mode'}) {
push(@links, $t);
}
else {
push(@links, "<a href='bwgraph.cgi?mode=$m&".
"dom=$in{'dom'}&mago=$in{'mago'}'>$t</a>\n");
}
}
print &ui_links_row(\@links),"<p>\n";
# Show table
$width = 500;
print "<table width=100%>\n";
if ($in{'mode'} == 0 || $in{'mode'} == 1 || $in{'mode'} == 4) {
# By domain .. start by computing usage for the selected
# period for each domain
%usage = ( );
%usage_only = ( );
%fusage = ( );
%fusage_only = ( );
$start_day = &bandwidth_period_start($in{'mago'});
$end_day = &bandwidth_period_end($in{'mago'});
if ($in{'mago'}) {
foreach $d (grep { !$_->{'parent'} } @doms) {
# Need to re-compute for some past period
foreach $dd ($d, &get_domain_by("parent", $d->{'id'})) {
$bwinfo = &get_bandwidth($dd);
foreach $k (keys %$bwinfo) {
$v = $bwinfo->{$k};
if ($k =~ /^(\S+)_(\d+)$/ &&
$2 >= $start_day &&
$2 <= $end_day) {
$usage_only{$dd->{'id'}} += $v;
$fusage_only{$1}->{$dd->{'id'}} += $v;
$pid = $dd->{'parent'} || $dd->{'id'};
$usage{$pid} += $v;
$fusage{$1}->{$pid} += $v;
}
}
}
}
}
else {
foreach $d (@doms) {
# bw.pl has already given us stats for the
# current period
foreach $dd ($d, &get_domain_by("parent",
$d->{'id'})) {
$pid = $dd->{'parent'} || $dd->{'id'};
$usage{$pid} += $dd->{'bw_usage'};
$usage_only{$dd->{'id'}} = $dd->{'bw_usage_only'};
foreach $f (@bandwidth_features) {
$fusage{$f}->{$pid} +=
$dd->{'bw_usage_'.$f};
$fusage_only{$f}->{$dd->{'id'}} =
$dd->{'bw_usage_only_'.$f};
}
}
}
}
# Only show those that are over the limit
if ($in{'mode'} == 4) {
@doms = grep { $usage{$_->{'id'}} > $_->{'bw_limit'} &&
$_->{'bw_limit'} } @doms;
if (!@doms) {
print "<tr> <td><b>$text{'newbw_allunder'}</b></td> </tr>\n";
}
}
# Show the table of domains
if (@doms) {
print "<tr> <td><b>$text{'newbw_dom'}</b></td>\n";
print "<td><b>",&text('edit_bwpast_'.$config{'bw_past'},
$text{'newbw_graph'}, $config{'bw_period'}),
"</b></td>\n";
print "<td><b>$text{'newbw_glimit'}</b></td>\n";
print "<td><b>$text{'newbw_gusage'}</b></td>\n";
print "</tr>\n";
}
foreach $d (sort { $usage{$b->{'id'}} <=> $usage{$a->{'id'}} }
grep { !$_->{'parent'} }
@doms) {
$usage = $in{'mode'} == 1 ? $usage_only{$d->{'id'}}
: $usage{$d->{'id'}};
$dname = &show_domain_name($d);
if ($in{'dom'}) {
print "<tr> <td>$dname</td> <td nowrap>\n";
}
else {
print "<tr> <td><a href='bwgraph.cgi?",
"dom=$d->{'id'}&mago=$in{'mago'}'>",
"$dname</td> <td nowrap>\n";
}
# Show nothing if this domain is disabled
if (!&can_monitor_bandwidth($d)) {
print "</td> <td colspan=2>",
"$text{'bwgraph_dis'}</td> </tr>\n";
next;
}
# Show limit, or grey box if unlimited
if ($d->{'bw_limit'}) {
printf "<img src=images/red.gif width=%s height=10>\n",
int($width*$d->{'bw_limit'}/$max)+1;
}
else {
printf "<img src=images/grey.gif width=%s height=10>\n",
$width;
}
# Show usage by feature
print "<br>";
&usage_colours($d, $in{'mode'} ? \%fusage_only
: \%fusage);
print "</td>\n";
print "<td>\n";
if ($d->{'bw_limit'}) {
print &nice_size($d->{'bw_limit'});
}
else {
print $text{'newbw_unlim'};
}
print "</td>\n";
print "<td>",&nice_size($usage),"</td>\n";
print "</tr>\n";
next if ($in{'mode'} != 1);
# Show sub-servers
$space = $d->{'bw_usage_only'};
foreach $sd (grep { !$_->{'alias'} }
&get_domain_by("parent", $d->{'id'})) {
$dname = &show_domain_name($sd);
print "<tr> <td> ",
"<a href='bwgraph.cgi?dom=$sd->{'id'}&",
"mago=$in{'mago'}'>$dname</a>",
"</td> <td>\n";
# Show nothing if this domain is disabled
if (!&can_monitor_bandwidth($sd)) {
print "</td> <td colspan=2>$text{'bwgraph_dis'}</td> </tr>\n";
next;
}
printf "<img src=images/white.gif width=%s height=10>",
int($width*$space/$max);
&usage_colours($sd, $in{'mode'} ? \%fusage_only
: \%fusage);
print "</td>\n";
print "<td></td>\n";
print "<td>",&nice_size($usage_only{$sd->{'id'}}),"</td>\n";
print "</tr>\n";
$space += $sd->{'bw_usage_only'};
}
}
}
elsif ($in{'mode'} == 2) {
# By date, for current billing period
print "<tr> <td><b>$text{'newbw_date'}</b></td>\n";
print "<td><b>$text{'newbw_dusage'}</b></td>\n";
print "<td><b>$text{'newbw_gusage'}</b></td>\n";
print "</tr>\n";
# Get bandwidth for relevant domains and sub-domains
foreach $d (@doms) {
foreach $dd ($d, &get_domain_by("parent", $d->{'id'})) {
local $bwinfo = &get_bandwidth($dd);
push(@bands, $bwinfo);
}
}
# Work out the max day
$day = &bandwidth_period_end($in{'mago'});
$start_day = &bandwidth_period_start($in{'mago'});
$max = 0;
for($i=$day; $i>=$start_day; $i--) {
$usage = 0;
foreach $f (@bandwidth_features) {
$usage += &usage_for_days($i, $i, $f, @bands);
}
$max = $usage if ($usage > $max);
}
# Show the day table
$max ||= 1;
for($i=$day; $i>=$start_day; $i--) {
print "<tr>\n";
print "<td>",&make_date($i*24*60*60, 1),"</td>\n";
print "<td>";
local $usage = 0;
foreach $f (@bandwidth_features) {
local $fusage = &usage_for_days($i, $i, $f,
@bands);
$usage += $fusage;
if ($fusage) {
printf "<img src=images/usage-$f.gif width=%s height=10>",
int($width*$fusage/$max)+($f eq "web" ? 1 : 0);
$donecolour{$f} += $fusage;
}
}
print "</td>";
print "<td>",&nice_size($usage),"</td>\n";
print "</tr>\n";
}
}
elsif ($in{'mode'} == 3) {
# By month
print "<tr> <td><b>$text{'newbw_month'}</b></td>\n";
print "<td><b>$text{'newbw_dusage'}</b></td>\n";
print "<td><b>$text{'newbw_gusage'}</b></td>\n";
print "</tr>\n";
# Get bandwidth for relevant domains and sub-domains, and
# work out the earliest time
$start_day = undef;
foreach $d (@doms) {
foreach $dd ($d, &get_domain_by("parent", $d->{'id'})) {
local $bwinfo = &get_bandwidth($dd);
push(@bands, $bwinfo);
local $min = &minimum_day($bwinfo);
if ($min && (!defined($start_day) ||
$min < $start_day)) {
$start_day = $min;
}
}
}
$start_day ||= time()/(24*60*60); # If none
# Work out the start and end months
@start_tm = localtime($start_day * (24*60*60));
@end_tm = localtime(time());
$start_month = $start_tm[5]*12 + $start_tm[4];
$end_month = $end_tm[5]*12 + $end_tm[4];
# Work out the max usage for a month
$max = 0;
for($i=$end_month; $i>=$start_month; $i--) {
@tm = ( 0, 0, 0, 1, $i%12, int($i/12) );
$istart[$i] = int(timelocal(@tm)/(24*60*60));
@endtm = ( 0, 0, 0, 1, $tm[4]+1, $tm[5] );
if ($endtm[4] == 12) { $endtm[4] = 0; $endtm[5]++ };
$iend[$i] = int(timelocal(@endtm)/(24*60*60) - 1);
$usage = 0;
foreach $f (@bandwidth_features) {
$usage += &usage_for_days(
$istart[$i], $iend[$i], $f, @bands);
}
$max = $usage if ($usage > $max);
}
# Show the month table
$max ||= 1;
for($i=$end_month; $i>=$start_month; $i--) {
@tm = ( 0, 0, 0, 1, $i%12, int($i/12) );
print "<tr>\n";
print "<td>",strftime("%m/%Y", @tm),"</td>\n";
print "<td>";
local $usage = 0;
foreach $f (@bandwidth_features) {
local $fusage = &usage_for_days(
$istart[$i], $iend[$i], $f, @bands);
$usage += $fusage;
if ($fusage) {
printf "<img src=images/usage-$f.gif width=%s height=10>",
int($width*$fusage/$max)+($f eq "web" ? 1 : 0);
$donecolour{$f} += $fusage;
}
}
print "</td>";
print "<td>",&nice_size($usage),"</td>\n";
print "</tr>\n";
}
}
print "</table>\n";
# Show colour keys
print "<br>\n";
foreach $f (@bandwidth_features) {
if ($donecolour{$f}) {
print "<img src=images/usage-$f.gif ",
"width=10 height=10>\n";
local $label = $text{'bandwidth_'.$f} ||
$text{'feature_'.$f};
print $label," (",
&nice_size($donecolour{$f}),")\n";
}
}
print "<br>\n";
# Show month selector
if ($in{'mode'} != 3) {
print &ui_form_start("bwgraph.cgi");
print &ui_hidden("mode", $in{'mode'});
print &ui_hidden("dom", $in{'dom'});
print "<b>",$text{'bwgraph_mago_'.$config{'bw_past'}},"</b>\n";
@mago = ( );
@tm = localtime(time());
for($i=0; $i<24; $i++) {
local $sday = &bandwidth_period_start($i);
local $eday = &bandwidth_period_end($i);
push(@mago, [ $i, &make_date($sday*24*60*60, 1)." - ".
&make_date($eday*24*60*60, 1) ]);
}
print &ui_select("mago", $in{'mago'}, \@mago,
1, 0, 0, 0, "onChange='form.submit()'" );
print &ui_submit($text{'bwgraph_mok'});
print &ui_form_end();
}
}
else {
print "<b>$text{'bwgraph_none'}</b><p>\n";
if (&can_edit_templates()) {
my $job = &find_bandwidth_job();
if (!$job || !$config{'bw_active'}) {
print &text('bwgraph_enable',
&ui_link('edit_newbw.cgi', $text{'newbw_title'})),
"<p>\n";
}
}
}
if ($parent) {
push(@rets, "bwgraph.cgi?dom=$parent->{'id'}",
$text{'bwgraph_returnparent'});
}
if ($in{'dom'}) {
push(@rets, &domain_footer_link($d));
}
if (&can_edit_templates() && $in{'dom'}) {
push(@rets, "bwgraph.cgi", $text{'bwgraph_return'});
}
push(@rets, "", $text{'index_return'});
&ui_print_footer(@rets);
# usage_colours(&domain, &usage)
sub usage_colours
{
local ($d, $usage) = @_;
local ($f, $total);
foreach $f (@bandwidth_features) {
local $fusage = $usage->{$f}->{$d->{'id'}};
if ($fusage) {
printf "<img src=images/usage-$f.gif width=%s height=10>",
int($width*$fusage/$max)+($f eq "web" ? 1 : 0);
$donecolour{$f} += $fusage;
$total += $fusage;
}
}
if (!$total) {
print "<img src=images/usage-web.gif width=1 height=10>";
}
}
# minimum_day(&bandwidth)
sub minimum_day
{
local $min = undef;
foreach $k (keys %{$_[0]}) {
if ($k =~ /^(\S+)_(\d+)$/ && (!defined($min) || $2 < $min)) {
$min = $2;
}
}
return $min;
}
# usage_for_days(start, end, feature, &bandwidth, ...)
sub usage_for_days
{
local ($start, $end, $f, @bands) = @_;
local $usage = 0;
local ($i, $band);
for($i=int($start); $i<=int($end); $i++) {
foreach $band (@bands) {
$usage += $band->{$f.'_'.$i};
}
}
return $usage;
}