1
0
Fork 0
scripts-admin-quickndirty-p.../nagios/check_quota.pl

91 lines
2.4 KiB
Perl

#!/usr/bin/perl
# TODO : intégrer avec utils.pm / Nagios::Plugin du CPAN
# TODO : gérer les niveaux via des arguments en ligne de commande
use strict;
use Cyrus::IMAP::Shell;
use Data::Dumper;
my $configFile = '/root/.cyrus_auth';
my $configWarningLevel = 90;
my $configCriticalLevel = 97;
my %cyrusConfig;
my $var;
my $value;
my $username;
my $taux_occupation;
my @donnees;
my @quota;
my @listingMailboxes;
my @listingWarning;
my @listingCritical;
# Lecture du fichier de configuration
open CONFIG, $configFile;
while (<CONFIG>) {
chomp; # no newline
s/^#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
$cyrusConfig{$var} = $value;
}
# Connexion
my $conn = Cyrus::IMAP::Admin->new($cyrusConfig{'host'}) or die "Echec de connexion.";
# Authentification
$conn->authenticate(
"-user" => $cyrusConfig{'user'},
"-password" => $cyrusConfig{'password'},
"-mechanism" => 'LOGIN',
) or die "Echec à l'authentification.";
# Boucle sur chacune des boîtes
@listingMailboxes = $conn->listmailbox('user.%');
if ($conn->error) {
print $conn->error;
exit;
}
for (@listingMailboxes) {
# listingMailboxes est un tableau multi-dimensionnel
# On essaie de récupérer proprement les tableaux internes
@donnees = @$_;
$username = @donnees[0];
# Récupération du quota
@quota = $conn->listquota($username);
# fin de boucle si vide (= pas de quota) (ou erreur peut-être...)
next unless @quota;
if (shift(@quota) != 'STORAGE') {
die "Type de quota inconnu.";
}
# Calcul du taux d'occupation et éventuel ajout aux
# listes warning/critical
$taux_occupation = 100 * $quota[0][0] / $quota[0][1];
if ( $taux_occupation > $configCriticalLevel ) {
push (@listingCritical, $username . sprintf("(%.4f)", $taux_occupation));
} else {
if ( $taux_occupation > $configWarningLevel ) {
push (@listingWarning, $username . sprintf("(%.4f)", $taux_occupation));
}
}
}
if (@listingCritical) {
print "CRITICAL : " . scalar(@listingCritical) . " au dessus de " . $configCriticalLevel . "% : " . join ", ", @listingCritical;
exit(2);
}
if (@listingWarning) {
print "WARNING : " . scalar(@listingWarning) . " au dessus de " . $configWarningLevel . "% : " . join ", ", @listingWarning;
exit(1);
}
print "OK";
exit(0);