Rework PMG monitoring scripts

tags/zabbix-agent-addons-0.2.53-1.beta1
Daniel Berteaud 6 years ago
parent 6dc3060f97
commit f3be79e879
  1. 2
      zabbix_conf/pmg.conf
  2. 88
      zabbix_scripts/check_pmg_sudo

@ -6,4 +6,4 @@ UserParameter=pmg.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_pmg_sudo
# pmg.check.all[type,id] # pmg.check.all[type,id]
# Where type is what to monitor (domain) # Where type is what to monitor (domain)
# id is the id of the item to monitor. Eg, the domain name # id is the id of the item to monitor. Eg, the domain name
UserParameter=pmg.check.all[*],/usr/bin/sudo /var/lib/zabbix/bin/check_pmg_sudo --$1 $2 UserParameter=pmg.check.all[*],/usr/bin/sudo /var/lib/zabbix/bin/check_pmg_sudo --timespan=$1 --spamthres=$2 --$3 $4

@ -4,28 +4,90 @@ use strict;
use warnings; use warnings;
use JSON; use JSON;
use Getopt::Long; use Getopt::Long;
use File::Which;
use Data::Dumper; use Data::Dumper;
use PMG::DBTools;
my $pmgsh = which('pmgsh'); my $json = {
my $json = {}; count_in => 0,
count_out => 0,
bytes_in => 0,
bytes_out => 0,
spam_in => 0,
spam_out => 0,
virus_in => 0,
virus_out => 0,
ptime_in => 0,
ptime_out => 0
};
my $pretty = 0; my $pretty = 0;
my ($domain) = undef; my ($domain,$what) = undef;
my $timespan = 900;
my $spamthres = 5;
my $resp = undef;
GetOptions( GetOptions(
'domain=s' => \$domain, 'domain=s' => \$domain,
'pretty' => \$pretty 'what=s' => \$what,
'timespan=i' => \$timespan,
'spamthres=i' => \$spamthres,
'pretty' => \$pretty
); );
if ($domain){ my $dbh = PMG::DBTools::open_ruledb;
my $stats = from_json(qx($pmgsh get /statistics/domains 2>/dev/null));
foreach my $dom (@{$stats}){ my $since = time - $timespan;
next unless ($dom->{domain} eq $domain); my $query = "SELECT cstatistic.direction AS direction, cstatistic.bytes AS bytes, cstatistic.spamlevel AS spamlevel, " .
$json->{$_} = $dom->{$_} foreach (qw/bytes_in bytes_out count_in count_out spamcount_in spamcount_out viruscount_in viruscount_out/); "cstatistic.virusinfo AS virus, cstatistic.ptime AS ptime, cstatistic.sender AS sender, creceivers.receiver " .
"AS receiver FROM cstatistic LEFT JOIN creceivers ON cstatistic.rid = creceivers.cstatistic_rid" .
" WHERE time > ?";
my $sth = $dbh->prepare($query);
$sth->execute($since);
while (my $res = $sth->fetchrow_hashref){
if (not $res->{direction}){
next if (defined $domain and $res->{sender} !~ m/.*\@$domain$/);
$json->{bytes_out} += $res->{bytes};
$json->{count_out} += 1;
$json->{ptime_out} += $res->{ptime};
$json->{spam_out} += 1 if ($res->{spamlevel} ge $spamthres);
$json->{virus_out} += 1 if (defined $res->{virus});
} else {
next if (defined $domain and $res->{receiver} !~ /.*\@$domain$/);
$json->{bytes_in} += $res->{bytes};
$json->{count_in} += 1;
$json->{ptime_in} += $res->{ptime};
$json->{spam_in} += 1 if ($res->{spamlevel} ge $spamthres);
$json->{virus_in} += 1 if (defined $res->{virus});
} }
} else{ }
# Init to 0 if missing
$json->{$_} //= 0 foreach (qw/bytes_out count_out ptime_out spam_out virus_out
bytes_in count_in ptime_in spam_in virus_in/);
# Compute averages
$json->{ptime_in} = $json->{ptime_in} / $json->{count_in} / 1000 if ($json->{count_in} > 0);
$json->{ptime_out} = $json->{ptime_out} / $json->{count_out} / 1000 if ($json->{count_out} > 0);
# Now, only for general stats, count early rejects
if (not defined $domain){
$query = "SELECT SUM(rblcount) AS rbl, SUM(pregreetcount) AS pregreet FROM localstat WHERE mtime > ?";
$sth = $dbh->prepare($query);
$sth->execute($since);
my $res = $sth->fetchrow_hashref;
$json->{$_} = $res->{$_} foreach (qw/rbl pregreet/);
}
$json->{$_} //= 0 foreach (qw/rbl pregreet/);
if (defined $what and not defined $json->{$what}){
print 'ZBX_UNSUPORTED'; print 'ZBX_UNSUPORTED';
exit 0; exit 0;
} elsif (defined $what){
$resp = $json->{$what}
} else {
$resp = $json;
} }
$resp = (ref $resp eq 'HASH' or ref $resp eq 'ARRAY') ? to_json($resp, { pretty => $pretty }) : $resp;
print $resp . "\n";
print to_json($json, { pretty => $pretty }) . "\n";

Loading…
Cancel
Save