-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
bw.pl
executable file
·278 lines (257 loc) · 7.48 KB
/
bw.pl
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
#!/usr/local/bin/perl
# Work out the bandwidth usage for all virtual servers.
# For those that are over their limit, send a warning.
package virtual_server;
$main::no_acl_check++;
require './virtual-server-lib.pl';
# Are we already running? If so, die
if (&test_lock($bandwidth_dir)) {
exit(0);
}
&lock_file($bandwidth_dir);
# Work out the start of the monitoring period
$now = time();
$oneday = 24*60*60;
$day = int($now / $oneday);
$start_day = &bandwidth_period_start();
if ($ARGV[0]) {
$onedom = &get_domain_by("dom", $ARGV[0]);
$onedom || die "Server $ARGV[0] not found";
@doms = ( $onedom, &get_domain_by("parent", $onedom->{'id'}) );
}
else {
@doms = &list_domains();
}
@bwdoms = grep { &can_monitor_bandwidth($_) } @doms;
# Get bandwidth info map for all domains
foreach $d (@bwdoms) {
$bwinfo = &get_bandwidth($d);
$bwinfomap{$d->{'id'}} = $bwinfo;
}
# For each feature that has a function for doing bandwidth for all domains
# at once, call it
foreach $f (@bandwidth_features) {
local $bwfunc = "bandwidth_all_$f";
if (defined(&$bwfunc)) {
local %starts = map { $_, $bwinfomap{$_}->{"last_$f"} }
(keys %bwinfomap);
local $newstarts = &$bwfunc(\@bwdoms, \%starts, \%bwinfomap);
foreach my $did (keys %$newstarts) {
$bwinfomap{$did}->{"last_$f"} = $newstarts->{$did};
}
}
}
# For each server, scan it's log files for all usage since the last check, and
# update the count for each day.
$maxdays = $config{'bw_maxdays'} || 366;
foreach $d (@bwdoms) {
# Add bandwidth for all features
$bwinfo = $bwinfomap{$d->{'id'}};
foreach $f (@bandwidth_features) {
local $bwfunc = "bandwidth_$f";
if (defined(&$bwfunc)) {
my $l = &$bwfunc($d, $bwinfo->{"last_$f"}, $bwinfo);
if ($l > $now + $oneday) {
$l = $now;
}
$bwinfo->{"last_$f"} = $l;
}
}
# Add bandwidth for all plugins
foreach $f (&list_feature_plugins()) {
if (&plugin_defined($f, "feature_bandwidth")) {
$bwinfo->{"last_$f"} =
&plugin_call($f, "feature_bandwidth", $d,
$bwinfo->{"last_$f"}, $bwinfo);
}
}
# Prune days more than 1 year old
foreach $k (keys %$bwinfo) {
if ($k =~ /^(\S+)_(\d+)$/ && $2 < $day - $maxdays) {
delete($bwinfo->{$k});
}
}
&save_bandwidth($d, $bwinfo);
}
# For each server, sum up usage over the monitoring period to find those
# that are over their limit
foreach $d (@doms) {
next if ($d->{'parent'});
&lock_domain($d);
$d = &get_domain($d->{'id'}, undef, 1); # Force re-read from disk
if (!$d) {
# Deleted!
&unlock_domain($d);
next;
}
# Find domain and sub-domains
@alld = ($d);
@subd = ();
foreach my $dd (&get_domain_by("parent", $d->{'id'})) {
&lock_domain($dd);
$dd = &get_domain($dd->{'id'}, undef, 1); # Force re-read
push(@subd, $dd);
}
push(@alld, @subd);
# Sum up usage for domain and sub-domains
$usage = 0;
%usage = ( );
foreach $dd (@alld) {
$bwinfo = &get_bandwidth($dd);
local $usage_only = 0;
local %usage_only = ( );
foreach $k (keys %$bwinfo) {
if ($k =~ /^(\S+)_(\d+)$/ && $2 >= $start_day) {
$usage += $bwinfo->{$k};
$usage_only += $bwinfo->{$k};
$usage{$1} += $bwinfo->{$k};
$usage_only{$1} += $bwinfo->{$k};
}
}
$dd->{'bw_usage_only'} = $usage_only;
$dd->{'bw_start'} = $start_day;
foreach $f (@bandwidth_features) {
delete($dd->{"bw_usage_only_$f"});
}
foreach $k (keys %usage_only) {
$dd->{'bw_usage_only_'.$k} = $usage_only{$k};
}
if ($d->{'id'} ne $dd->{'id'}) {
&save_domain($dd);
}
}
$d->{'bw_usage'} = $usage;
foreach $f (@bandwidth_features) {
delete($d->{"bw_usage_$f"});
}
foreach $k (keys %usage) {
$d->{'bw_usage_'.$k} = $usage{$k};
}
local $from = &get_global_from_address($d);
if ($d->{'bw_limit'} && $usage > $d->{'bw_limit'}) {
# Over the limit! But check limit on how often to notify
$etime = $now - $d->{'bw_notify'} > $config{'bw_notify'}*60*60;
if ($etime) {
# Time to email ..
$tmpl = $config{'bw_template'} eq 'default' ?
"$module_config_directory/bw-template" :
$config{'bw_template'};
%tkeys = &make_domain_substitions($d, 1);
$tkeys{'bw_percent'} = int(100*$usage/$d->{'bw_limit'});
foreach $k (keys %usage) {
$tkeys{'bw_usage_'.$k} =
&nice_size($tkeys{'bw_usage_'}.$k);
}
local @addrs;
push(@addrs, $d->{'email'} ||
$d->{'user'}.'@'.&get_system_hostname() )
if ($config{'bw_owner'});
push(@addrs, split(/\s+,\s+/, $config{'bw_email'}));
@erv = &send_template_email(
&cat_file($tmpl),
join(", ", @addrs),
\%tkeys,
&text('newbw_subject', $d->{'dom'}),
undef, undef, undef, $from);
if ($erv[0]) {
$d->{'bw_notify'} = $now;
}
else {
print STDERR "Failed to send email : $erv[1]\n";
}
}
if (!$d->{'disabled'} && $etime && $config{'bw_disable'} &&
!$d->{'bw_no_disable'}) {
# Time to disable this domain and all sub-servers
&set_all_null_print();
foreach my $dd (@alld) {
&disable_virtual_server($dd, 'bw',
'Exceeded bandwidth limit');
}
&run_post_actions();
}
&webmin_log("disable", "domain", $d->{'dom'}, $d);
}
elsif ($d->{'bw_limit'} && $config{'bw_warn'} &&
$usage > $d->{'bw_limit'}*$config{'bw_warn'}/100) {
# Reached the warning limit! But check limit on how often warn
if ($now - $d->{'bw_warnnotify'} > $config{'bw_notify'}*60*60) {
# Time to email ..
$tmpl = $config{'warnbw_template'} eq 'default' ?
"$module_config_directory/warnbw-template" :
$config{'warnbw_template'};
%tkeys = &make_domain_substitions($d, 1);
$tkeys{'bw_percent'} = int(100*$usage/$d->{'bw_limit'});
foreach $k (keys %usage) {
$tkeys{'bw_usage_'.$k} =
&nice_size($tkeys{'bw_usage_'.$k});
}
$tkeys{'bw_warn'} = $config{'bw_warn'};
local @addrs;
push(@addrs, $d->{'email'} ||
$d->{'user'}.'@'.&get_system_hostname() )
if ($config{'bw_owner'});
push(@addrs, split(/\s+,\s+/, $config{'bw_email'}));
@erv = &send_template_email(
&cat_file($tmpl),
join(", ", @addrs),
\%tkeys,
&text('newbw_warnsubject', $d->{'dom'}),
undef, undef, undef, $from);
if ($erv[0]) {
$d->{'bw_warnnotify'} = $now;
}
else {
print STDERR "Failed to send email : $erv[1]\n";
}
}
}
if ($config{'bw_enable'} &&
($usage < $d->{'bw_limit'} || !$d->{'bw_limit'}) &&
$d->{'disabled'} && $d->{'disabled_reason'} eq 'bw') {
# Falled below the disable limit .. re-enable
&set_all_null_print();
foreach my $dd (@alld) {
@enable = &get_enable_features($dd);
%enable = map { $_, 1 } @enable;
delete($dd->{'disabled_reason'});
delete($dd->{'disabled_why'});
delete($dd->{'disabled_time'});
# Run the before command
&set_domain_envs($dd, "ENABLE_DOMAIN");
$merr = &making_changes();
&reset_domain_envs($dd);
next if ($merr);
# Enable all disabled features
foreach my $f (@features) {
if ($dd->{$f} && $enable{$f}) {
local $efunc = "enable_$f";
&try_function($f, $efunc, $dd);
}
}
foreach my $f (&list_feature_plugins()) {
if ($dd->{$f} && $enable{$f}) {
&plugin_call($f, "feature_enable", $dd);
}
}
# Disable extra admins
&update_extra_webmin($dd, 0);
# Save new domain details
delete($dd->{'disabled'});
&save_domain($dd);
# Run the after command
&set_domain_envs($dd, "ENABLE_DOMAIN");
&made_changes();
&reset_domain_envs($dd);
}
&run_post_actions();
&webmin_log("enable", "domain", $d->{'dom'}, $d);
}
&save_domain($d);
foreach my $dd (reverse(@subd)) {
&unlock_domain($dd);
}
&unlock_domain($d);
}
# Release running lock
&unlock_file($bandwidth_dir);