]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/monitoring/plugins/check_imap_connection
Add monitoring host
[perso/Immae/Config/Nix.git] / modules / private / monitoring / plugins / check_imap_connection
diff --git a/modules/private/monitoring/plugins/check_imap_connection b/modules/private/monitoring/plugins/check_imap_connection
new file mode 100755 (executable)
index 0000000..304eae6
--- /dev/null
@@ -0,0 +1,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\n";
+  exit($STATE_CRITICAL);
+} else {
+  print "IMAP OK - imaps connected successfully\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
+}
+