aboutsummaryrefslogblamecommitdiff
path: root/modules/private/monitoring/plugins/check_imap_connection
blob: c1ab0dd469e41f3a867adee8ab8fff4bd0b04aa0 (plain) (tree)



























                                                                                                                                           
                                                                     

                        
                                                                



















                                                                     
#!/usr/bin/env perl

use strict;
use Getopt::Std;
$| = 1;

my %opts;
getopts('h:u:p:H:', \%opts);

my $STATE_OK = 0;
my $STATE_WARNING = 1;
my $STATE_CRITICAL = 2;
my $STATE_UNKNOWN = 3;

if ($opts{'h'} || !$opts{'u'} || !$opts{'p'} || !$opts{'H'}) {
  &print_help();
  exit($STATE_UNKNOWN);
}

my $user = $opts{'u'};
my $password = $opts{'p'};
my $host = $opts{'H'};

my $cmd_result = `(echo "a login $user $password"; echo "b logout") | openssl s_client -quiet -ign_eof -connect $host -starttls imap 2>&1`;
my $expected_result = "a OK Logged in";

chomp($cmd_result);
if ($cmd_result !~ /$expected_result/) {
  print "IMAP CRITICAL - Unable to connect via imaps | imap=0;;;;\n";
  exit($STATE_CRITICAL);
} else {
  print "IMAP OK - imaps connected successfully | imap=1;;;;\n";
  exit($STATE_OK);
}

sub print_help() {
  print << "EOF";
Check whether imap works via ssl and is able to connect its database.

Options:
-h
    Print detailed help screen
-u
    User to log in as
-p
    Password to log in
-H
    Host to log in to

EOF
}