-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_mail_delivery
executable file
·231 lines (186 loc) · 7.14 KB
/
check_mail_delivery
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl
use strict;
use lib "/usr/lib64/nagios/plugins";
use utils qw(%ERRORS);
# die with an error if we're not on Linux
if ($^O ne 'linux') {
print "This plugin only applicable on Linux.\n";
exit $ERRORS{'UNKNOWN'};
}
###############################################################
# NAME: check_mail_delivery
# DESCRIPTION: A Nagios/Icinga plugin that sends an email to a mail server
# to check via Pop3 if it has been received afterwards.
# Developed and tested for outlook.com, gmx, t-online and gmail
# but should work with any other POP3 capable ESP, too.
# (see bottom of file for hints)
#
# AUTHOR: Martin Emberger, https://sporch.de/
# inspired by https://forum.hetzner.de/thread/20761-von-hotmail-blockiert
#
# CO-AUTHORS: Michael Seevogel, Postfix postmulti support and minor enhancements
#
# REQUIRES: Mail::POP3Client, String::Random
# USAGE: ./check_mail_delivery profile_name
# Adapt %CONFIG and %PROFILES here,
# configure Nagios/Icinga accordingly.
#
# VERSION: 0.3.2
#
# CHANGELOG: 0.1 Useable version
# 0.2 Change license to CC BY-SA 4.0
# 0.3 Bugfix for Inbox overflow, deletion of emails via POP3
# 0.3.1 Added Postfix postmulti support
# 0.3.2 Additionaly set MAIL FROM: via sendmail command so that return-path will definitely match From: and not the local NRPE user as email sender address.
#
# This script comes with ABSOLUTELY NO WARRANTY.
# This is free software, and you are welcome to redistribute it
# under certain conditions. See CC BY-SA 4.0 for details.
# https://creativecommons.org/licenses/by-sa/4.0/
#
###############################################################
###############################################################
# SETTINGS
###############################################################
#my %PROFILES = (
# gmail => [ '[email protected]', 'myPOP3Passw0rd', 'pop.gmail.com' ],
# gmx => [ '[email protected]', 'myPOP3Passw0rd', 'pop.gmx.net' ],
# tonline => [ '[email protected]', 'myPOP3Passw0rd', 'securepop.t-online.de' ],
# hotmail => [ '[email protected]', 'myPOP3Passw0rd', 'pop-mail.outlook.com' ],
# aol => [ '[email protected]', 'myPOP3Passw0rd', 'pop.aol.com' ],
# yahoo => [ '[email protected]', 'myPOP3Passw0rd', 'pop.mail.yahoo.com' ],
#
#);
my %CONFIG;
$CONFIG{'TESTMAIL_SUBJECT'} = 'Automatische Icinga Test Nachricht'; # unique ID will be appended later on
$CONFIG{'TESTMAIL_FROM'} = '[email protected]';
$CONFIG{'CHECK_WAITTIME'} = '30'; # recommended >=20 secs (consider Nagios' plugin execution time settings)
$CONFIG{'OTHER_DEBUG'} = 0; # print debug information to STDOUT (Nagios/Icinga will see them, too)
$CONFIG{'OTHER_DELETE_TESTMAIL'} = 1; # delete the test mail after it was detected (definitely recommended!)
#// END SETTINGS
###############################################################
# Let's go
###############################################################
my %WORK;
get_profile();
send_test_mail();
wait_before_check();
check_remote_server();
handle_return();
exit();
###
sub get_profile() {
if ($#ARGV < 0) {
die error ("Configuration", "no profile provided");
}
my $profile_param = $ARGV[0];
debug("Provided profile '$profile_param'");
my $postmulti_param = $ARGV[1];
debug("Postfix instance '$postmulti_param'");
if (!$PROFILES{$profile_param}) {
die error ("Configuration", "unknown profile provided");
}
else {
debug("Profile OK");
$CONFIG{'TESTMAIL_TO'} = $PROFILES{$profile_param}[0];
$CONFIG{'POP3_USER'} = $CONFIG{TESTMAIL_TO};
$CONFIG{'POP3_PASS'} = $PROFILES{$profile_param}[1];
$CONFIG{'POP3_SERVER'} = $PROFILES{$profile_param}[2];
$CONFIG{'POSTMULTI_INSTANCE'} = $postmulti_param;
}
}
sub send_test_mail() {
debug("Sending mail to $CONFIG{TESTMAIL_TO}");
use String::Random;
$WORK{TMP_ID} = new String::Random->randpattern("CncCccCnnnc");
my $tmp_subject = $CONFIG{TESTMAIL_SUBJECT} . " ($WORK{TMP_ID})";
if ($CONFIG{'POSTMULTI_INSTANCE'})
{
debug("Using $CONFIG{'POSTMULTI_INSTANCE'} as Postfix Instance\n");
open (MAIL, "|postmulti -i $CONFIG{'POSTMULTI_INSTANCE'} -x sendmail -t -f $CONFIG{TESTMAIL_FROM}") or die error('Sendmail', 'did not work');
}
else
{
debug("POSTMULTI_INSTANCE is NOT defined, gonna send an email from local/regular sendmail client\n");
open (MAIL, "|/usr/sbin/sendmail -t -f $CONFIG{TESTMAIL_FROM}") or die error('Sendmail', 'did not work');
}
print MAIL "To: $CONFIG{TESTMAIL_TO}\n";
print MAIL "From: $CONFIG{TESTMAIL_FROM}\n";
print MAIL "Subject: $tmp_subject\n\n";
print MAIL "Wenn Du meinst es geht nicht mehr, kommt von irgendwo ein Linux her! :-)";
close (MAIL);
debug("Send succeeded!");
}
sub wait_before_check() {
debug("Waiting $CONFIG{CHECK_WAITTIME} seconds...");
sleep ($CONFIG{CHECK_WAITTIME});
}
sub check_remote_server() {
debug("Checking $CONFIG{POP3_SERVER} for our mail with ID $WORK{TMP_ID}");
my $mail_was_found = 0;
my $mail_id = -1;
use Mail::POP3Client;
use IO::Socket::SSL;
my $conn = Mail::POP3Client->new(HOST => $CONFIG{POP3_SERVER}, USESSL => 1, USER => $CONFIG{POP3_USER}, PASSWORD => $CONFIG{POP3_PASS}) or die error("Connect", "unable to connect");
my $numMsg = $conn->Count;
debug("Number of messages: " . $numMsg);
if ($numMsg eq -1) { # see documention of Mail::POP3Client
die error ("Connect", "Invalid credentials");
}
for (my $msgNr = 1; $msgNr <= $numMsg; $msgNr++) {
foreach ($conn->Head($msgNr)) {
if (/^(Subject):/i) {
debug("Message $msgNr: '$_'");
if ($_ =~ m/\($WORK{TMP_ID}\)/) {
debug("Yeah, we found our message!");
$mail_was_found = 1;
$mail_id = $msgNr;
}
if (($_ =~ m/$CONFIG{TESTMAIL_SUBJECT}/) && (!($_ =~ m/\($WORK{TMP_ID}\)/)) && ($CONFIG{OTHER_DELETE_TESTMAIL})) {
debug("Found an old test mail, deleting it");
$conn->Delete($msgNr);
}
}
}
}
debug("New message count: " . $conn->Count());
if (($CONFIG{OTHER_DELETE_TESTMAIL}) && ($mail_was_found)) {
$conn->Delete($mail_id);
debug("Message deleted");
}
if ($mail_was_found) {
$WORK{'RETURN_MSG'} = "Test Mail $WORK{TMP_ID} was found in $CONFIG{POP3_USER}'s Inbox";
$WORK{'RETURN_CODE'} = 0; # State OK for Nagios/Icinga
} else {
$WORK{'RETURN_MSG'} = "Did NOT found Test Mail $WORK{TMP_ID} in $CONFIG{POP3_USER}'s Inbox. Maybe you've been blocked or the timeout ($CONFIG{CHECK_WAITTIME}s) is too low.";
$WORK{'RETURN_CODE'} = 2; # State CRITICAL for Nagios/Icinga
}
$conn->Close();
}
sub handle_return() {
print $WORK{RETURN_MSG}, "\n"; # STDOUT
exit $WORK{RETURN_CODE}; # corresponding Nagios/Icinga code
# exit ($ERRORS{$WORK{RETURN_CODE}});
}
sub debug() {
my $msg = shift;
if ($CONFIG{OTHER_DEBUG}) {
print $msg . "\n";
}
}
sub error() {
my $source = shift;
my $msg = shift;
print "\n\n" . "ERROR: $source reports \"$msg\"!" . "\n\n";
exit 2;
}
###############################################################
# Hints for setting up email accounts (had to learn by myself)
# GMAIL
# Option "Weniger sichere Apps zulassen:" has to be enabled
# via Kontoeinstellungen
#
# T-ONLINE
# Most stupid ESP ever. You have to set the
# "Email Passwort", as it's not the same as for web
###############################################################