| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
my $VERSION = '0.2'; |
| 4 |
my $COPYRIGHT = 'Copyright (C) 2005-2011 Jonathan Buhacoff <[email protected]>'; |
| 5 |
my $LICENSE = 'http://www.gnu.org/licenses/gpl.txt'; |
| 6 |
my %status = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3 ); |
| 7 |
|
| 8 |
#### IDEA AND INITIAL IMPLEMENTATION BASED ON CHECK_IMAP_RECEIVE WAS CONTRIBUTED BY JOHAN ROMME from THE NETHERLANDS 14 Oct 2011 |
| 9 |
|
| 10 |
# look for required modules |
| 11 |
exit $status{UNKNOWN} unless load_modules(qw/Getopt::Long Mail::IMAPClient/); |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
if( grep { /^--hires$/ } @ARGV ) { |
| 15 |
eval "use Time::HiRes qw(time);"; |
| 16 |
warn "Time::HiRes not installed\n" if $@; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
# get options from command line |
| 21 |
Getopt::Long::Configure("bundling"); |
| 22 |
my $verbose = 0; |
| 23 |
my $help = ""; |
| 24 |
my $help_usage = ""; |
| 25 |
my $show_version = ""; |
| 26 |
my $imap_server = ""; |
| 27 |
my $default_imap_port = "143"; |
| 28 |
my $default_imap_ssl_port = "993"; |
| 29 |
my $imap_port = ""; |
| 30 |
my $username = ""; |
| 31 |
my $password = ""; |
| 32 |
my $mailbox = "INBOX"; |
| 33 |
my $warntime = 15; |
| 34 |
my $criticaltime = 30; |
| 35 |
my $timeout = 60; |
| 36 |
my $peek = ""; |
| 37 |
my $ssl = 0; |
| 38 |
my $tls = 0; |
| 39 |
my $time_hires = ""; |
| 40 |
my $ok; |
| 41 |
$ok = Getopt::Long::GetOptions( |
| 42 |
"V|version"=>\$show_version, |
| 43 |
"v|verbose+"=>\$verbose,"h|help"=>\$help,"usage"=>\$help_usage, |
| 44 |
"w|warning=i"=>\$warntime,"c|critical=i"=>\$criticaltime,"t|timeout=i"=>\$timeout, |
| 45 |
# imap settings |
| 46 |
"H|hostname=s"=>\$imap_server,"p|port=i"=>\$imap_port, |
| 47 |
"U|username=s"=>\$username,"P|password=s"=>\$password, "m|mailbox=s"=>\$mailbox, |
| 48 |
"ssl!"=>\$ssl, "tls!"=>\$tls, |
| 49 |
# search settings |
| 50 |
"peek!"=>\$peek, |
| 51 |
# Time |
| 52 |
"hires"=>\$time_hires, |
| 53 |
); |
| 54 |
|
| 55 |
if( $show_version ) { |
| 56 |
print "$VERSION\n"; |
| 57 |
if( $verbose ) { |
| 58 |
print "Default warning threshold: $warntime seconds\n"; |
| 59 |
print "Default critical threshold: $criticaltime seconds\n"; |
| 60 |
print "Default timeout: $timeout seconds\n"; |
| 61 |
} |
| 62 |
exit $status{UNKNOWN}; |
| 63 |
} |
| 64 |
|
| 65 |
if( $help ) { |
| 66 |
exec "perldoc", $0 or print "Try `perldoc $0`\n"; |
| 67 |
exit $status{UNKNOWN}; |
| 68 |
} |
| 69 |
|
| 70 |
my @required_module = (); |
| 71 |
push @required_module, 'IO::Socket::SSL' if $ssl || $tls; |
| 72 |
exit $status{UNKNOWN} unless load_modules(@required_module); |
| 73 |
|
| 74 |
if( $help_usage |
| 75 |
|| |
| 76 |
( $imap_server eq "" || $username eq "" || $password eq "" ) |
| 77 |
) { |
| 78 |
print "Usage: $0 -H host [-p port] -U username -P password [--imap-retries <tries>]\n"; |
| 79 |
exit $status{UNKNOWN}; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
# initialize |
| 84 |
my $report = new PluginReport; |
| 85 |
my $time_start = time; |
| 86 |
|
| 87 |
# connect to IMAP server |
| 88 |
print "connecting to server $imap_server\n" if $verbose > 2; |
| 89 |
my $imap; |
| 90 |
eval { |
| 91 |
local $SIG{ALRM} = sub { die "exceeded timeout $timeout seconds\n" }; # NB: \n required, see `perldoc -f alarm` |
| 92 |
alarm $timeout; |
| 93 |
|
| 94 |
if( $ssl || $tls ) { |
| 95 |
$imap_port = $default_imap_ssl_port unless $imap_port; |
| 96 |
my $socket = IO::Socket::SSL->new("$imap_server:$imap_port"); |
| 97 |
die IO::Socket::SSL::errstr() unless $socket; |
| 98 |
$socket->autoflush(1); |
| 99 |
$imap = Mail::IMAPClient->new(Socket=>$socket, Debug => 0 ); |
| 100 |
$imap->State(Mail::IMAPClient->Connected); |
| 101 |
$imap->_read_line() if "$Mail::IMAPClient::VERSION" le "2.2.9"; # necessary to remove the server's "ready" line from the input buffer for old versions of Mail::IMAPClient. Using string comparison for the version check because the numeric didn't work on Darwin and for Mail::IMAPClient the next version is 2.3.0 and then 3.00 so string comparison works |
| 102 |
$imap->User($username); |
| 103 |
$imap->Password($password); |
| 104 |
$imap->login() or die "$@"; |
| 105 |
} |
| 106 |
else { |
| 107 |
$imap_port = $default_imap_port unless $imap_port; |
| 108 |
$imap = Mail::IMAPClient->new(Debug => 0 ); |
| 109 |
$imap->Server("$imap_server:$imap_port"); |
| 110 |
$imap->User($username); |
| 111 |
$imap->Password($password); |
| 112 |
$imap->connect() or die "$@"; |
| 113 |
} |
| 114 |
|
| 115 |
$imap->Peek(1) if $peek; |
| 116 |
$imap->Ignoresizeerrors(1); |
| 117 |
|
| 118 |
alarm 0; |
| 119 |
}; |
| 120 |
if( $@ ) { |
| 121 |
chomp $@; |
| 122 |
print "IMAP QUOTA CRITICAL - Could not connect to $imap_server port $imap_port: $@\n"; |
| 123 |
exit $status{CRITICAL}; |
| 124 |
} |
| 125 |
unless( $imap ) { |
| 126 |
print "IMAP QUOTA CRITICAL - Could not connect to $imap_server port $imap_port: $@\n"; |
| 127 |
exit $status{CRITICAL}; |
| 128 |
} |
| 129 |
my $time_connected = time; |
| 130 |
|
| 131 |
my $quotaUsed; |
| 132 |
my $quotaLimit; |
| 133 |
my $quotaPercentage; |
| 134 |
my $quotaPercentageWarning = 80; |
| 135 |
my $quotaPercentageCritical = 90; |
| 136 |
my $quotaMessage; |
| 137 |
|
| 138 |
# look for the quota limits |
| 139 |
my $tries = 0; |
| 140 |
my @msgs; |
| 141 |
|
| 142 |
eval { |
| 143 |
my $k; |
| 144 |
my @l = $imap->getquotaroot(); |
| 145 |
foreach $k (@l) { |
| 146 |
print "$k\n" if $verbose > 2; |
| 147 |
if ($k =~ /STORAGE +(\d+) +(\d+)/) { |
| 148 |
$quotaUsed = $1; |
| 149 |
$quotaLimit = $2; |
| 150 |
} |
| 151 |
} |
| 152 |
if (!length($quotaUsed) && !length($quotaLimit)) { |
| 153 |
print "no answer from imap host\n" if $verbose > 2; |
| 154 |
} elsif (!length($quotaUsed) || !length($quotaLimit) { |
| 155 |
print "incorrect answer from imap host\n"; |
| 156 |
$imap->close(); |
| 157 |
exit $status{UNKNOWN}; |
| 158 |
} else { |
| 159 |
$quotaPercentage = sprintf("%.1f", (100 * $quotaUsed) / $quotaLimit); |
| 160 |
$quotaMessage = "$quotaUsed $quotaLimit - $quotaPercentage%"; |
| 161 |
} |
| 162 |
}; |
| 163 |
if( $@ ) { |
| 164 |
chomp $@; |
| 165 |
print "IMAP QUOTA CRITICAL - Could not check quota at $imap_server port $imap_port: $@\n"; |
| 166 |
exit $status{CRITICAL}; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
# disconnect from IMAP server |
| 171 |
print "disconnecting from server\n" if $verbose > 2; |
| 172 |
$imap->logout(); |
| 173 |
|
| 174 |
# calculate elapsed time and issue warnings |
| 175 |
my $time_end = time; |
| 176 |
my $elapsedtime = $time_end - $time_start; |
| 177 |
$report->{seconds} = $elapsedtime; |
| 178 |
|
| 179 |
# print report and exit with known status |
| 180 |
|
| 181 |
if($quotaPercentage >= $quotaPercentageCritical) { |
| 182 |
print "IMAP QUOTA CRITICAL - $quotaMessage\n"; |
| 183 |
exit $status{CRITICAL}; |
| 184 |
} |
| 185 |
if($quotaPercentage >= $quotaPercentageWarning) { |
| 186 |
print "IMAP QUOTA WARNING - $quotaMessage\n"; |
| 187 |
exit $status{WARNING}; |
| 188 |
} |
| 189 |
print "IMAP QUOTA OK - $quotaMessage\n"; |
| 190 |
exit $status{OK}; |
| 191 |
|
| 192 |
|
| 193 |
# utility to load required modules. exits if unable to load one or more of the modules. |
| 194 |
sub load_modules { |
| 195 |
my @missing_modules = (); |
| 196 |
foreach( @_ ) { |
| 197 |
eval "require $_"; |
| 198 |
push @missing_modules, $_ if $@; |
| 199 |
} |
| 200 |
if( @missing_modules ) { |
| 201 |
print "Missing perl modules: @missing_modules\n"; |
| 202 |
return 0; |
| 203 |
} |
| 204 |
return 1; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
# NAME |
| 209 |
# PluginReport |
| 210 |
# SYNOPSIS |
| 211 |
# $report = new PluginReport; |
| 212 |
# $report->{label1} = "value1"; |
| 213 |
# $report->{label2} = "value2"; |
| 214 |
# print $report->text(qw/label1 label2/); |
| 215 |
package PluginReport; |
| 216 |
|
| 217 |
sub new { |
| 218 |
my ($proto,%p) = @_; |
| 219 |
my $class = ref($proto) || $proto; |
| 220 |
my $self = bless {}, $class; |
| 221 |
$self->{$_} = $p{$_} foreach keys %p; |
| 222 |
return $self; |
| 223 |
} |
| 224 |
|
| 225 |
sub text { |
| 226 |
my ($self,@labels) = @_; |
| 227 |
my @report = map { "$self->{$_} $_" } grep { defined $self->{$_} } @labels; |
| 228 |
my $text = join(", ", @report); |
| 229 |
return $text; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
package main; |
| 234 |
1; |
| 235 |
|