]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/monitoring/plugins/check_emails
Add status engine website
[perso/Immae/Config/Nix.git] / modules / private / monitoring / plugins / check_emails
CommitLineData
71a2425e
IB
1#!/usr/bin/env perl
2
3use strict;
4use Getopt::Std;
5use File::Basename;
6use Date::Parse;
7use POSIX qw(strftime);
8
9$| = 1;
10
11my %opts;
12getopts('hH:l:s:p:f:i:n:r:', \%opts);
13
14my $STATE_OK = 0;
15my $STATE_WARNING = 1;
16my $STATE_CRITICAL = 2;
17my $STATE_UNKNOWN = 3;
18
19if ($opts{'h'} || scalar(%opts) == 0) {
20 &print_help();
21 exit($STATE_OK);
22}
23
24my $port = $opts{'p'};
25my $host = $opts{'H'};
26my $login = $opts{'l'};
27if ($login ne '') {
28 $login = "$login@";
29}
30
31my $identity = $opts{'i'};
32my $local_directory = $opts{'n'};
33my $return_path = $opts{'r'};
34
35my @emails_to_send = split(/,/, $opts{'s'});
36my @emails_to_expect = split(/,/, $opts{'f'});
37
38my $cmd_result;
39if ($local_directory ne '') {
ef0a9217 40 if (@emails_to_expect and ! -d $local_directory) {
71a2425e
IB
41 print "Emails $host UNKNOWN - Could not find local directory";
42 exit($STATE_UNKNOWN);
43 }
44 $cmd_result = `send_mails $local_directory $return_path @emails_to_send 2>&1`;
45} else {
46 $cmd_result = `ssh -o BatchMode=yes -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -p $port -i $identity $login$host send_mails @emails_to_send 2>&1`;
47
48 if ($cmd_result =~ /Host key verification failed./) {
49 print "Emails $host UNKNOWN - Could not connect to host with ssh key\n";
50 exit($STATE_UNKNOWN);
51 }
52}
53
54my @lines = split(/\n/, $cmd_result);
55
56my %found_emails;
57
58foreach my $line (@lines) {
59 my @split_line = split(/;/, $line, 2);
60 $found_emails{$split_line[0]} = $split_line[1];
61}
62
63my $output = "";
a97118c4 64my $old = 0;
71a2425e
IB
65foreach my $email_from (@emails_to_expect) {
66 my @email_split = split(/:/, $email_from);
67 my $email = $email_split[0];
68 my $from = $email_split[1];
69
70 if ( exists $found_emails{$email} ) {
71 my $email_date = str2time($found_emails{$email});
72 my $current_date = strftime "%s", localtime;
73
74 if ($current_date - $email_date > 60*30) {
75 $output = "$output$email ($found_emails{$email} from $from) ";
76 }
a97118c4 77 $old = ($current_date - $email_date) > $old ? ($current_date - $email_date) : $old;
71a2425e
IB
78 } else {
79 $output = "$output$email (missing) "
80 }
81}
82
83if ($output ne '') {
a97118c4 84 print "Emails $host CRITICAL - expecting emails: $output | timestamp=${old}s;;;;\n";
71a2425e
IB
85 exit($STATE_CRITICAL);
86} else {
a97118c4 87 print "Emails $host OK | timestamp=${old}s;;;;\n";
71a2425e
IB
88 exit($STATE_OK);
89}
90
91sub print_help() {
92 print << "EOF";
93Check sent emails
94
95Options:
96-h
97 Print detailed help screen
98
99-H
100 Host to check
101
102-l
103 Login
104
105-i
106 Identity file
107
108-n
109 Don’t use ssh, pass that directory to script
110
111-r
112 Return path for local e-mails
113
114-s
115 Comma separated list of emails to send from the host.
116
117-f
118 Comma separated list of emails to expect on the host.
119EOF
120}
121