aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring/plugins/check_emails
blob: 534e5a5096bf3943c766ba4c92ac15e529510923 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env perl

use strict;
use Getopt::Std;
use File::Basename;
use Date::Parse;
use POSIX qw(strftime);

$| = 1;

my %opts;
getopts('hH:l:s:p:f:i:n:r:', \%opts);

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

if ($opts{'h'} || scalar(%opts) == 0) {
  &print_help();
  exit($STATE_OK);
}

my $port = $opts{'p'};
my $host = $opts{'H'};
my $login = $opts{'l'};
if ($login ne '') {
  $login = "$login@";
}

my $identity = $opts{'i'};
my $local_directory = $opts{'n'};
my $return_path = $opts{'r'};

my @emails_to_send = split(/,/, $opts{'s'});
my @emails_to_expect = split(/,/, $opts{'f'});

my $cmd_result;
if ($local_directory ne '') {
  if (@emails_to_expect and ! -d $local_directory) {
    print "Emails $host UNKNOWN - Could not find local directory";
    exit($STATE_UNKNOWN);
  }
  $cmd_result = `send_mails $local_directory $return_path @emails_to_send 2>&1`;
} else {
  $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`;

  if ($cmd_result =~ /Host key verification failed./) {
    print "Emails $host UNKNOWN - Could not connect to host with ssh key\n";
    exit($STATE_UNKNOWN);
  }
}

my @lines = split(/\n/, $cmd_result);

my %found_emails;

foreach my $line (@lines) {
  my @split_line = split(/;/, $line, 2);
  $found_emails{$split_line[0]} = $split_line[1];
}

my $output = "";
my $old = 0;
foreach my $email_from (@emails_to_expect) {
  my @email_split = split(/:/, $email_from);
  my $email = $email_split[0];
  my $from = $email_split[1];

  if ( exists $found_emails{$email} ) {
    my $email_date = str2time($found_emails{$email});
    my $current_date = strftime "%s", localtime;

    if ($current_date - $email_date > 60*30) {
      $output = "$output$email ($found_emails{$email} from $from) ";
    }
    $old = ($current_date - $email_date) > $old ? ($current_date - $email_date) : $old;
  } else {
    $output = "$output$email (missing) "
  }
}

if ($output ne '') {
  print "Emails $host CRITICAL - expecting emails: $output | timestamp=${old}s;;;;\n";
  exit($STATE_CRITICAL);
} else {
  print "Emails $host OK | timestamp=${old}s;;;;\n";
  exit($STATE_OK);
}

sub print_help() {
  print << "EOF";
Check sent emails

Options:
-h
    Print detailed help screen

-H
    Host to check

-l
    Login

-i
    Identity file

-n
    Dont use ssh, pass that directory to script

-r
    Return path for local e-mails

-s
    Comma separated list of emails to send from the host.

-f
    Comma separated list of emails to expect on the host.
EOF
}