diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2020-01-17 01:15:04 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2020-01-17 01:15:04 +0100 |
commit | 71a2425ed95120a6de3a41bb233b1066779d4c26 (patch) | |
tree | 007f2756abab644604577352da28b1fd4c20df44 /modules/private/monitoring/plugins | |
parent | 981fa80354fd6f00f49446777c38f77bd8a65f65 (diff) | |
download | Nix-71a2425ed95120a6de3a41bb233b1066779d4c26.tar.gz Nix-71a2425ed95120a6de3a41bb233b1066779d4c26.tar.zst Nix-71a2425ed95120a6de3a41bb233b1066779d4c26.zip |
Add e-mail checks monitoring
Diffstat (limited to 'modules/private/monitoring/plugins')
-rwxr-xr-x | modules/private/monitoring/plugins/check_emails | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/modules/private/monitoring/plugins/check_emails b/modules/private/monitoring/plugins/check_emails new file mode 100755 index 0000000..0ee3e4e --- /dev/null +++ b/modules/private/monitoring/plugins/check_emails | |||
@@ -0,0 +1,119 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | use strict; | ||
4 | use Getopt::Std; | ||
5 | use File::Basename; | ||
6 | use Date::Parse; | ||
7 | use POSIX qw(strftime); | ||
8 | |||
9 | $| = 1; | ||
10 | |||
11 | my %opts; | ||
12 | getopts('hH:l:s:p:f:i:n:r:', \%opts); | ||
13 | |||
14 | my $STATE_OK = 0; | ||
15 | my $STATE_WARNING = 1; | ||
16 | my $STATE_CRITICAL = 2; | ||
17 | my $STATE_UNKNOWN = 3; | ||
18 | |||
19 | if ($opts{'h'} || scalar(%opts) == 0) { | ||
20 | &print_help(); | ||
21 | exit($STATE_OK); | ||
22 | } | ||
23 | |||
24 | my $port = $opts{'p'}; | ||
25 | my $host = $opts{'H'}; | ||
26 | my $login = $opts{'l'}; | ||
27 | if ($login ne '') { | ||
28 | $login = "$login@"; | ||
29 | } | ||
30 | |||
31 | my $identity = $opts{'i'}; | ||
32 | my $local_directory = $opts{'n'}; | ||
33 | my $return_path = $opts{'r'}; | ||
34 | |||
35 | my @emails_to_send = split(/,/, $opts{'s'}); | ||
36 | my @emails_to_expect = split(/,/, $opts{'f'}); | ||
37 | |||
38 | my $cmd_result; | ||
39 | if ($local_directory ne '') { | ||
40 | if (! -d $local_directory) { | ||
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 | |||
54 | my @lines = split(/\n/, $cmd_result); | ||
55 | |||
56 | my %found_emails; | ||
57 | |||
58 | foreach my $line (@lines) { | ||
59 | my @split_line = split(/;/, $line, 2); | ||
60 | $found_emails{$split_line[0]} = $split_line[1]; | ||
61 | } | ||
62 | |||
63 | my $output = ""; | ||
64 | foreach my $email_from (@emails_to_expect) { | ||
65 | my @email_split = split(/:/, $email_from); | ||
66 | my $email = $email_split[0]; | ||
67 | my $from = $email_split[1]; | ||
68 | |||
69 | if ( exists $found_emails{$email} ) { | ||
70 | my $email_date = str2time($found_emails{$email}); | ||
71 | my $current_date = strftime "%s", localtime; | ||
72 | |||
73 | if ($current_date - $email_date > 60*30) { | ||
74 | $output = "$output$email ($found_emails{$email} from $from) "; | ||
75 | } | ||
76 | } else { | ||
77 | $output = "$output$email (missing) " | ||
78 | } | ||
79 | } | ||
80 | |||
81 | if ($output ne '') { | ||
82 | print "Emails $host CRITICAL - expecting emails: $output\n"; | ||
83 | exit($STATE_CRITICAL); | ||
84 | } else { | ||
85 | print "Emails $host OK\n"; | ||
86 | exit($STATE_OK); | ||
87 | } | ||
88 | |||
89 | sub print_help() { | ||
90 | print << "EOF"; | ||
91 | Check sent emails | ||
92 | |||
93 | Options: | ||
94 | -h | ||
95 | Print detailed help screen | ||
96 | |||
97 | -H | ||
98 | Host to check | ||
99 | |||
100 | -l | ||
101 | Login | ||
102 | |||
103 | -i | ||
104 | Identity file | ||
105 | |||
106 | -n | ||
107 | Don’t use ssh, pass that directory to script | ||
108 | |||
109 | -r | ||
110 | Return path for local e-mails | ||
111 | |||
112 | -s | ||
113 | Comma separated list of emails to send from the host. | ||
114 | |||
115 | -f | ||
116 | Comma separated list of emails to expect on the host. | ||
117 | EOF | ||
118 | } | ||
119 | |||