1
/******************************************************************************
5
* Program: SMTP plugin for Nagios
7
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
9
* $Id: check_smtp.c,v 1.9.2.2 2003/03/07 07:15:40 kdebisschop Exp $
13
* This plugin will attempt to open an SMTP connection with the host.
14
* Successul connects return STATE_OK, refusals and timeouts return
15
* STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful
16
* connects, but incorrect reponse messages from the host result in
17
* STATE_WARNING return values.
19
* License Information:
21
* This program is free software; you can redistribute it and/or modify
22
* it under the terms of the GNU General Public License as published by
23
* the Free Software Foundation; either version 2 of the License, or
24
* (at your option) any later version.
26
* This program is distributed in the hope that it will be useful,
27
* but WITHOUT ANY WARRANTY; without even the implied warranty of
28
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
* GNU General Public License for more details.
31
* You should have received a copy of the GNU General Public License
32
* along with this program; if not, write to the Free Software
33
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35
*****************************************************************************/
42
const char *progname = "check_smtp";
45
#define SMTP_EXPECT "220"
46
#define SMTP_HELO "HELO "
48
/* sendmail will syslog a "NOQUEUE" error if session does not attempt
49
* to do something useful. This can be prevented by giving a command
50
* even if syntax is illegal (MAIL requires a FROM:<...> argument)
51
* You can disable sending DUMMYCMD by undefining SMTP_USE_DUMMYCMD.
53
* According to rfc821 you can include a null reversepath in the from command
54
* - but a log message is generated on the smtp server.
56
* Use the -f option to provide a FROM address
59
#define SMTP_DUMMYCMD "MAIL "
60
#define SMTP_USE_DUMMYCMD 1
61
#define SMTP_QUIT "QUIT\r\n"
63
int process_arguments (int, char **);
64
int validate_arguments (void);
65
void print_help (void);
66
void print_usage (void);
68
int server_port = SMTP_PORT;
69
char *server_address = NULL;
70
char *server_expect = NULL;
73
int check_warning_time = FALSE;
74
int critical_time = 0;
75
int check_critical_time = FALSE;
79
main (int argc, char **argv)
82
int result = STATE_UNKNOWN;
83
char buffer[MAX_INPUT_BUFFER] = "";
84
char *from_str = NULL;
87
if (process_arguments (argc, argv) != OK)
88
usage ("Invalid command arguments supplied\n");
90
/* initialize the HELO command with the localhostname */
91
#ifndef HOST_MAX_BYTES
92
#define HOST_MAX_BYTES 255
94
helocmd = malloc (HOST_MAX_BYTES);
95
gethostname(helocmd, HOST_MAX_BYTES);
96
asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
98
/* initialize the MAIL command with optional FROM command */
99
asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n");
102
printf ("FROMCMD: %s\n", from_str);
104
/* initialize alarm signal handling */
105
signal (SIGALRM, socket_timeout_alarm_handler);
107
/* set socket timeout */
108
alarm (socket_timeout);
110
/* try to connect to the host at the given port number */
112
result = my_tcp_connect (server_address, server_port, &sd);
114
/* we connected, so close connection before exiting */
115
if (result == STATE_OK) {
117
/* watch for the SMTP connection string and */
118
/* return a WARNING status if we couldn't read any data */
119
if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
120
printf ("recv() failed\n");
121
result = STATE_WARNING;
124
/* strip the buffer of carriage returns */
126
/* make sure we find the response we are looking for */
127
if (!strstr (buffer, server_expect)) {
128
if (server_port == SMTP_PORT)
129
printf ("Invalid SMTP response received from host\n");
131
printf ("Invalid SMTP response received from host on port %d\n",
133
result = STATE_WARNING;
137
/* send the HELO command */
138
send(sd, helocmd, strlen(helocmd), 0);
140
/* allow for response to helo command to reach us */
141
recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
143
#ifdef SMTP_USE_DUMMYCMD
144
send(sd, from_str, strlen(from_str), 0);
146
/* allow for response to DUMMYCMD to reach us */
147
recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
150
printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
151
#endif /* SMTP_USE_DUMMYCMD */
153
/* tell the server we're done */
154
send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
156
/* finally close the connection */
160
/* reset the alarm */
165
if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
166
result = STATE_CRITICAL;
167
else if (check_warning_time == TRUE && (end_time - start_time) > warning_time)
168
result = STATE_WARNING;
171
printf ("SMTP %s - %d sec. response time, %s\n",
172
state_text (result), (int) (end_time - start_time), buffer);
174
printf ("SMTP %s - %d second response time\n", state_text (result), (int) (end_time - start_time));
184
/* process command-line arguments */
186
process_arguments (int argc, char **argv)
191
int option_index = 0;
192
static struct option long_options[] = {
193
{"hostname", required_argument, 0, 'H'},
194
{"expect", required_argument, 0, 'e'},
195
{"critical", required_argument, 0, 'c'},
196
{"warning", required_argument, 0, 'w'},
197
{"port", required_argument, 0, 'p'},
198
{"from", required_argument, 0, 'f'},
199
{"verbose", no_argument, 0, 'v'},
200
{"version", no_argument, 0, 'V'},
201
{"help", no_argument, 0, 'h'},
209
for (c = 1; c < argc; c++) {
210
if (strcmp ("-to", argv[c]) == 0)
211
strcpy (argv[c], "-t");
212
else if (strcmp ("-wt", argv[c]) == 0)
213
strcpy (argv[c], "-w");
214
else if (strcmp ("-ct", argv[c]) == 0)
215
strcpy (argv[c], "-c");
221
getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
224
c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:");
226
if (c == -1 || c == EOF)
230
case 'H': /* hostname */
231
if (is_host (optarg)) {
232
server_address = optarg;
235
usage ("Invalid host name\n");
239
if (is_intpos (optarg)) {
240
server_port = atoi (optarg);
243
usage ("Server port must be a positive integer\n");
246
case 'f': /* from argument */
249
case 'e': /* server expect string on 220 */
250
server_expect = optarg;
252
case 'c': /* critical time threshold */
253
if (is_intnonneg (optarg)) {
254
critical_time = atoi (optarg);
255
check_critical_time = TRUE;
258
usage ("Critical time must be a nonnegative integer\n");
261
case 'w': /* warning time threshold */
262
if (is_intnonneg (optarg)) {
263
warning_time = atoi (optarg);
264
check_warning_time = TRUE;
267
usage ("Warning time must be a nonnegative integer\n");
270
case 'v': /* verbose */
273
case 't': /* timeout */
274
if (is_intnonneg (optarg)) {
275
socket_timeout = atoi (optarg);
278
usage ("Time interval must be a nonnegative integer\n");
281
case 'V': /* version */
282
print_revision (progname, "$Revision: 1.9.2.2 $");
288
usage ("Invalid argument\n");
293
if (server_address == NULL) {
295
if (is_host (argv[c]))
296
server_address = argv[c];
298
usage ("Invalid host name");
301
asprintf (&server_address, "127.0.0.1");
305
if (server_expect == NULL)
306
asprintf (&server_expect, SMTP_EXPECT);
308
return validate_arguments ();
316
validate_arguments (void)
328
print_revision (progname, "$Revision: 1.9.2.2 $");
330
("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
331
"This plugin test the SMTP service on the specified host.\n\n");
335
" -H, --hostname=STRING or IPADDRESS\n"
336
" Check server on the indicated host\n"
337
" -p, --port=INTEGER\n"
338
" Make connection on the indicated port (default: %d)\n"
339
" -e, --expect=STRING\n"
340
" String to expect in first line of server response (default: %s)\n"
341
" -f, --from=STRING\n"
342
" from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
343
" -w, --warning=INTEGER\n"
344
" Seconds necessary to result in a warning status\n"
345
" -c, --critical=INTEGER\n"
346
" Seconds necessary to result in a critical status\n"
347
" -t, --timeout=INTEGER\n"
348
" Seconds before connection attempt times out (default: %d)\n"
350
" Print extra information (command-line use only)\n"
352
" Print detailed help screen\n"
354
" Print version information\n\n",
355
SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
367
("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
369
" %s --version\n", progname, progname, progname);