aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring/plugins/check_imap_connection
blob: c1ab0dd469e41f3a867adee8ab8fff4bd0b04aa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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
}