| 1 |
#!/usr/bin/perl -T |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
#------------------------------------------------------------------------------ |
| 5 |
# This is amavisd-agent, a demo program to display |
| 6 |
# SNMP-like counters updated by amavis. |
| 7 |
# |
| 8 |
# Author: Mark Martinec <[email protected]> |
| 9 |
# |
| 10 |
# Copyright (c) 2004-2014, Mark Martinec |
| 11 |
# All rights reserved. |
| 12 |
# |
| 13 |
# Redistribution and use in source and binary forms, with or without |
| 14 |
# modification, are permitted provided that the following conditions |
| 15 |
# are met: |
| 16 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 17 |
# this list of conditions and the following disclaimer. |
| 18 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 |
# this list of conditions and the following disclaimer in the documentation |
| 20 |
# and/or other materials provided with the distribution. |
| 21 |
# |
| 22 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 26 |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 |
# POSSIBILITY OF SUCH DAMAGE. |
| 33 |
# |
| 34 |
# The views and conclusions contained in the software and documentation are |
| 35 |
# those of the authors and should not be interpreted as representing official |
| 36 |
# policies, either expressed or implied, of the Jozef Stefan Institute. |
| 37 |
|
| 38 |
# (the above license is the 2-clause BSD license, also known as |
| 39 |
# a "Simplified BSD License", and pertains to this program only) |
| 40 |
# |
| 41 |
# Patches and problem reports are welcome. |
| 42 |
# The latest version of this program is available at: |
| 43 |
# http://www.ijs.si/software/amavisd/ |
| 44 |
#------------------------------------------------------------------------------ |
| 45 |
|
| 46 |
use strict; |
| 47 |
use re 'taint'; |
| 48 |
use warnings; |
| 49 |
no warnings 'uninitialized'; |
| 50 |
|
| 51 |
use Errno qw(ENOENT); |
| 52 |
use Time::HiRes (); |
| 53 |
use BerkeleyDB; |
| 54 |
|
| 55 |
my($dbfile) = 'snmp.db'; |
| 56 |
my($db_home) = # DB databases directory |
| 57 |
defined $ENV{'AMAVISD_DB_HOME'} ? $ENV{'AMAVISD_DB_HOME'} : '/var/amavis/db'; |
| 58 |
|
| 59 |
my($wakeuptime) = 10; # -w, sleep time in seconds, may be fractional |
| 60 |
my($repeatcount); # -c, repeat count (when defined) |
| 61 |
|
| 62 |
use vars qw($VERSION); $VERSION = 2.701; |
| 63 |
use vars qw(%values %virus_by_name); |
| 64 |
use vars qw(%virus_by_os %spam_by_os %ham_by_os); |
| 65 |
use vars qw(%history $avg_int $uptime); |
| 66 |
$avg_int = 5*60; # 5 minute interval |
| 67 |
|
| 68 |
sub p1($$@) { |
| 69 |
my($k,$avg,@tot_k) = @_; |
| 70 |
printf("%-35s %6d %6.0f/h", $k, $values{$k}, $avg*3600); |
| 71 |
for my $tot_k (@tot_k) { |
| 72 |
if ($values{$tot_k} <= 0) { |
| 73 |
printf(" --- %%") |
| 74 |
} else { |
| 75 |
printf(" %7.1f %%", 100*$values{$k}/$values{$tot_k}) |
| 76 |
} |
| 77 |
print " ($tot_k)"; |
| 78 |
} |
| 79 |
print "\n"; |
| 80 |
} |
| 81 |
|
| 82 |
sub p1_size($$@) { |
| 83 |
my($k,$avg,@tot_k) = @_; |
| 84 |
my($scale) = 1024*1024; |
| 85 |
printf("%-35s %6.0fMB %4.0fMB/h", $k, $values{$k}/$scale, $avg*3600/$scale); |
| 86 |
for my $tot_k (@tot_k) { |
| 87 |
if ($values{$tot_k} <= 0) { |
| 88 |
printf(" --- %%") |
| 89 |
} else { |
| 90 |
printf(" %5.1f %%", 100*$values{$k}/$values{$tot_k}) |
| 91 |
} |
| 92 |
print " ($tot_k)"; |
| 93 |
} |
| 94 |
print "\n"; |
| 95 |
} |
| 96 |
|
| 97 |
sub p1_time($$$$) { |
| 98 |
my($k,$dv,$dcnt,$tot_k) = @_; |
| 99 |
printf("%-35s %6.0f s %8s s/msg (%s)\n", |
| 100 |
$k, $values{$k}/1000, |
| 101 |
$dcnt < 1 ? "---" : sprintf("%7.3f",$dv/1000/$dcnt), |
| 102 |
$tot_k); |
| 103 |
} |
| 104 |
|
| 105 |
sub p2($$$$) { |
| 106 |
my($k,$avg,$tot_k,$href) = @_; |
| 107 |
if ($values{$tot_k} > 0) { |
| 108 |
printf("%-35s %6d %6.0f/h %6.1f %% (%s)\n", |
| 109 |
$k, $href->{$k}, $avg*3600, 100*$href->{$k}/$values{$tot_k}, $tot_k); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
sub enqueue($$$$$) { |
| 114 |
my($name,$now,$val,$msgcnt,$hold_time) = @_; |
| 115 |
if (ref $history{$name} ne 'ARRAY') { $history{$name} = [] } |
| 116 |
my($oldest_useful); |
| 117 |
for my $j (0..$#{$history{$name}}) { |
| 118 |
if ($history{$name}->[$j][0] + $hold_time >= $now) |
| 119 |
{ $oldest_useful = $j; last } |
| 120 |
} |
| 121 |
if (defined $oldest_useful) { |
| 122 |
@{$history{$name}} = |
| 123 |
@{$history{$name}}[$oldest_useful..$#{$history{$name}}]; |
| 124 |
} |
| 125 |
push(@{$history{$name}}, [$now,$val,$msgcnt]); |
| 126 |
my($average,$dv,$dt,$dcnt); my($n) = scalar(@{$history{$name}}); |
| 127 |
my($oldest) = $history{$name}->[0]; |
| 128 |
my($latest) = $history{$name}->[$n-1]; |
| 129 |
$dt = $latest->[0] - $oldest->[0]; |
| 130 |
$dv = $latest->[1] - $oldest->[1]; |
| 131 |
$dcnt = $latest->[2] - $oldest->[2]; |
| 132 |
if ($n < 2 || $dt < $hold_time/2) { |
| 133 |
$dt = $uptime; $dv = $val; $dcnt = $msgcnt; # average since the start time |
| 134 |
} |
| 135 |
if ($dt > 0) { $average = $dv/$dt } |
| 136 |
($average, $dv, $dt, $dcnt, $n); |
| 137 |
} |
| 138 |
|
| 139 |
sub fmt_ticks($) { |
| 140 |
my($t) = @_; |
| 141 |
my($hh)= $t % 100; $t = int($t/100); |
| 142 |
my($s) = $t % 60; $t = int($t/60); |
| 143 |
my($m) = $t % 60; $t = int($t/60); |
| 144 |
my($h) = $t % 24; $t = int($t/24); |
| 145 |
my($d) = $t; |
| 146 |
sprintf("%d days, %d:%02d:%02d.%02d", $d,$h,$m,$s,$hh); |
| 147 |
}; |
| 148 |
|
| 149 |
# main program starts here |
| 150 |
my($normal_termination) = 0; |
| 151 |
$SIG{INT} = sub { die "\n" }; # do the END code block |
| 152 |
while (@ARGV) { |
| 153 |
my($opt) = shift @ARGV; |
| 154 |
my($val) = shift @ARGV; |
| 155 |
if ($opt eq '-w' && $val =~ /^\+?\d+(?:\.\d*)?\z/) { $wakeuptime = $val } |
| 156 |
elsif ($opt eq '-c' && $val =~ /^[+-]?\d+\z/) { $repeatcount = $val } |
| 157 |
else |
| 158 |
{ die "Usage: $0 [-c <count>] [-w <wait-interval>]\n" } |
| 159 |
} |
| 160 |
my($stat,$key,$val); |
| 161 |
my($env,$db,$old_db_inode,@dbstat,$cursor); |
| 162 |
for (;;) { |
| 163 |
last if defined $repeatcount && $repeatcount <= 0; |
| 164 |
@dbstat = stat("$db_home/$dbfile"); |
| 165 |
my($errn) = @dbstat ? 0 : 0+$!; |
| 166 |
$errn==0 || $errn==ENOENT or die "stat $db_home/$dbfile: $!"; |
| 167 |
if (defined $db && $old_db_inode != $dbstat[1]) { |
| 168 |
$db->db_close==0 or die "BDB db_close error: $BerkeleyDB::Error $!"; |
| 169 |
undef $db; |
| 170 |
printf STDERR ("Reopening snmp database %s/%s\n", $db_home,$dbfile); |
| 171 |
} |
| 172 |
if (!defined $db && $errn==0) { |
| 173 |
$old_db_inode = $dbstat[1]; |
| 174 |
$env = BerkeleyDB::Env->new( |
| 175 |
-Home => $db_home, -Flags => DB_INIT_CDB | DB_INIT_MPOOL, |
| 176 |
-ErrFile => \*STDOUT, -Verbose => 1); |
| 177 |
defined $env or die "BDB no env: $BerkeleyDB::Error $!"; |
| 178 |
$db = BerkeleyDB::Hash->new(-Filename => $dbfile, -Env => $env); |
| 179 |
defined $db or die "BDB no dbS 1: $BerkeleyDB::Error $!"; |
| 180 |
} |
| 181 |
$| = 0; |
| 182 |
%values = (); %virus_by_name = (); |
| 183 |
%virus_by_os = (); %spam_by_os = (); %ham_by_os = (); |
| 184 |
my($now); my($eval_stat,$interrupt); $interrupt = ''; |
| 185 |
if (!defined $db) { |
| 186 |
printf STDERR ("No snmp database %s/%s; waiting...\n", $db_home,$dbfile); |
| 187 |
} else { |
| 188 |
$repeatcount-- if defined $repeatcount && $repeatcount > 0; |
| 189 |
print "\n\n"; |
| 190 |
my($h1) = sub { $interrupt = $_[0] }; |
| 191 |
local(@SIG{qw(INT HUP TERM TSTP QUIT ALRM USR1 USR2)}) = ($h1) x 8; |
| 192 |
eval { |
| 193 |
$cursor = $db->db_cursor; # obtain read lock |
| 194 |
defined $cursor or die "db_cursor error: $BerkeleyDB::Error"; |
| 195 |
$now = Time::HiRes::time; |
| 196 |
while ( ($stat=$cursor->c_get($key,$val,DB_NEXT)) == 0 ) { |
| 197 |
if ($key =~ /^(virus\.byname\..*)\z/s) { $virus_by_name{$1} = $val } |
| 198 |
elsif ($key =~ /^(virus\.byOS\..*)\z/s) { $virus_by_os{$1} = $val } |
| 199 |
elsif ($key =~ /^(ham\.byOS\..*)\z/s) { $ham_by_os{$1} = $val } |
| 200 |
elsif ($key =~ /^(?:spam|spammy)\.byOS\.(.*)\z/s) |
| 201 |
{ $spam_by_os{"spam.byOS.$1"} = $val } |
| 202 |
else { $values{$key} = $val } |
| 203 |
} |
| 204 |
$stat==DB_NOTFOUND or die "c_get: $BerkeleyDB::Error $!"; |
| 205 |
$cursor->c_close==0 or die "c_close error: $BerkeleyDB::Error"; |
| 206 |
$cursor = undef; |
| 207 |
}; |
| 208 |
$eval_stat = $@; |
| 209 |
if (defined $db) { |
| 210 |
$cursor->c_close if defined $cursor; # unlock, ignoring status |
| 211 |
$cursor = undef; |
| 212 |
} |
| 213 |
} |
| 214 |
if ($interrupt ne '') { kill($interrupt,$$) } # resignal |
| 215 |
elsif ($eval_stat ne '') { chomp($eval_stat); die "BDB $eval_stat\n" } |
| 216 |
for my $k (sort keys %values) { |
| 217 |
if ($values{$k} =~ /^(?:C32|C64) (.*)\z/) { |
| 218 |
$values{$k} = $1; |
| 219 |
} elsif ($k eq 'sysUpTime' && $values{$k} =~ /^INT (.*)\z/) { |
| 220 |
$uptime = $now - $1; my($ticks) = int($uptime*100); |
| 221 |
printf("%-15s %s %s (%s)\n", |
| 222 |
$k,'TimeTicks', $ticks, fmt_ticks($ticks)); |
| 223 |
delete($values{$k}); |
| 224 |
} elsif ($values{$k} =~ /^(?:INT|TIM) (.*)\z/) { |
| 225 |
$values{$k} = $1; |
| 226 |
} else { |
| 227 |
printf("%-15s %s\n", $k,$values{$k}); |
| 228 |
delete($values{$k}); |
| 229 |
} |
| 230 |
} |
| 231 |
my($msgcnt) = $values{'InMsgs'}; |
| 232 |
for (sort keys %values) { |
| 233 |
my($avg,$dv,$dt,$dcnt,$n) = |
| 234 |
enqueue($_, $now, $values{$_}, $msgcnt, $avg_int); |
| 235 |
if (/^OpsDecTyp/) {} # later |
| 236 |
elsif (/^CacheHitsVirusMsgs$/) { p1($_,$avg,'ContentVirusMsgs') } |
| 237 |
elsif (/^CacheHitsBannedMsgs$/) { p1($_,$avg,'ContentBannedMsgs') } |
| 238 |
elsif (/^CacheHitsSpamMsgs$/) { p1($_,$avg,'ContentSpamMsgs') } |
| 239 |
elsif (/^Cache/) { p1($_,$avg,'CacheAttempts') } |
| 240 |
# elsif (/^Content(.*?)Msgs/) { p1($_,$avg,'Content'.$1.'Msgs') } |
| 241 |
elsif (/^Content(.*?)Msgs(.*)\z/) { p1($_,$avg,'InMsgs'.$2) } |
| 242 |
elsif (/^Content/) { p1($_,$avg,'InMsgs') } |
| 243 |
elsif (/^OpsSql/) { p1($_,$avg,'InMsgsRecips') } |
| 244 |
elsif (/^InMsgsSize/) { p1_size($_,$avg,'InMsgsSize') } |
| 245 |
elsif (/^InMsgsRecipsLocal\z/) { p1($_,$avg,'InMsgsRecips') } |
| 246 |
elsif (/^InMsgsRecips(.*)\z/) { p1($_,$avg,'InMsgs'.$1) } |
| 247 |
elsif (/^InMsgsBounce./) { p1($_,$avg,'InMsgsBounce') } |
| 248 |
elsif (/^(InMsgs|Ops)/) { p1($_,$avg,'InMsgs') } |
| 249 |
elsif (/^OutMsgsSize\z/) { p1_size($_,$avg,'InMsgsSize') } |
| 250 |
elsif (/^OutMsgsSize/) { p1_size($_,$avg,'OutMsgsSize') } |
| 251 |
elsif (/^OutMsgs\z/) { p1($_,$avg,'InMsgs') } |
| 252 |
elsif (/^Out/) { p1($_,$avg,'OutMsgs') } |
| 253 |
elsif (/^QuarMsgsSize\z/) { p1_size($_,$avg,'InMsgsSize') } |
| 254 |
elsif (/^QuarMsgsSize/) { p1_size($_,$avg,'QuarMsgsSize') } |
| 255 |
elsif (/^Quar/) { p1($_,$avg,'QuarMsgs') } |
| 256 |
elsif (/^LogEntries\z/) { p1($_,$avg,'InMsgs') } |
| 257 |
elsif (/^Log/) { p1($_,$avg,'LogEntries') } |
| 258 |
elsif (/^GenMailIdRetries/) { p1($_,$avg,'InMsgs') } |
| 259 |
elsif (/^PenPalsAttempts\z/) { p1($_,$avg,'InMsgsRecipsLocal') } |
| 260 |
elsif (/^PenPalsHits\z/) { p1($_,$avg,'PenPalsAttempts')} |
| 261 |
elsif (/^PenPalsHits./) { p1($_,$avg,'PenPalsHits') } |
| 262 |
elsif (/^PenPals/) { p1($_,$avg,'PenPalsAttempts') } |
| 263 |
elsif (/^SqlAddrSenderAttempts\z/) { p1($_,$avg,'InMsgs') } |
| 264 |
elsif (/^SqlAddrSender/) { p1($_,$avg,'SqlAddrSenderAttempts') } |
| 265 |
elsif (/^SqlAddrRecipAttempts\z/) { p1($_,$avg,'InMsgsRecips') } |
| 266 |
elsif (/^SqlAddrRecip/) { p1($_,$avg,'SqlAddrRecipAttempts') } |
| 267 |
elsif (/^banned\.byOS/) { p1($_,$avg,'InMsgs') } |
| 268 |
elsif (/^TimeElapsed/i) { p1_time($_,$dv,$dcnt,'InMsgs') } |
| 269 |
else { p1($_,$avg,undef) } |
| 270 |
} |
| 271 |
for (sort { $values{$b}<=>$values{$a} } grep {/^OpsDecTyp/} keys %values) { |
| 272 |
my($avg,$dv,$dt,$dcnt,$n) = |
| 273 |
enqueue($_, $now, $values{$_}, $msgcnt, $avg_int); |
| 274 |
p1($_,$avg,'InMsgs'); |
| 275 |
} |
| 276 |
for my $href (\%virus_by_name,\%virus_by_os,\%spam_by_os,\%ham_by_os) { |
| 277 |
for (keys %$href) |
| 278 |
{ $href->{$_} = $1 if $href->{$_} =~ /^(?:C32|C64) (.*)\z/ } |
| 279 |
} |
| 280 |
for my $href (\%virus_by_os,\%spam_by_os,\%ham_by_os) { |
| 281 |
for (keys %$href) { |
| 282 |
/^[a-zA-Z]+\.byOS\.(.*)\z/; my($os) = $1; |
| 283 |
$values{"all.byOS.$os"} += $href->{$_}; |
| 284 |
} |
| 285 |
} |
| 286 |
my($separated) = 0; |
| 287 |
for my $pair ([\%virus_by_name, 'ContentVirusMsgs',], |
| 288 |
[\%virus_by_os, 'ContentVirusMsgs',], |
| 289 |
[\%spam_by_os, 'ContentSpamMsgs', ], |
| 290 |
[\%ham_by_os, 'ContentCleanMsgs' ] ) { |
| 291 |
my($href,$tot_k) = @$pair; |
| 292 |
for (sort {$href->{$b} <=> $href->{$a}} keys %$href) { |
| 293 |
if (!$separated) { print "\n"; $separated = 1 } |
| 294 |
my($avg,$dv,$dt,$dcnt,$n) = |
| 295 |
enqueue($_, $now, $href->{$_}, $msgcnt, $avg_int); |
| 296 |
p2($_,$avg,$tot_k,$href); |
| 297 |
} |
| 298 |
} |
| 299 |
if (0) { # disabled |
| 300 |
$separated = 0; |
| 301 |
for my $href (\%virus_by_os, \%spam_by_os, \%ham_by_os) { |
| 302 |
for (sort {$href->{$b} <=> $href->{$a}} keys %$href) { |
| 303 |
if (!$separated) { print "\n"; $separated = 1 } |
| 304 |
my($avg,$dv,$dt,$dcnt,$n) = |
| 305 |
enqueue($_, $now, $href->{$_}, $msgcnt, $avg_int); |
| 306 |
/^[a-zA-Z]+\.byOS\.(.*)\z/; my($os) = $1; |
| 307 |
p2($_,$avg,"all.byOS.$os",$href); |
| 308 |
} |
| 309 |
} |
| 310 |
$separated = 0; |
| 311 |
for (sort { $values{$b}<=>$values{$a} } |
| 312 |
grep {/^all\.byOS\./} keys %values) { |
| 313 |
if (!$separated) { print "\n"; $separated = 1 } |
| 314 |
my($avg,$dv,$dt,$dcnt,$n) = |
| 315 |
enqueue($_, $now, $values{$_}, $msgcnt, $avg_int); |
| 316 |
p1($_,$avg,'InMsgs'); |
| 317 |
} |
| 318 |
} |
| 319 |
$| = 1; |
| 320 |
last if defined $repeatcount && $repeatcount <= 0; |
| 321 |
Time::HiRes::sleep($wakeuptime) if $wakeuptime > 0; |
| 322 |
} # forever |
| 323 |
$normal_termination = 1; |
| 324 |
|
| 325 |
END { |
| 326 |
if (defined $db) { |
| 327 |
$cursor->c_close if defined $cursor; # ignoring status |
| 328 |
$db->db_close==0 or die "BDB db_close error: $BerkeleyDB::Error $!"; |
| 329 |
} |
| 330 |
print STDERR "exited\n" if !$normal_termination; |
| 331 |
} |