1 /******************************************************************************
2 *
3 * CHECK_SMTP.C
4 *
5 * Program: SMTP plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 *
9 * $Id$
10 *
11 * Description:
12 *
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.
18 *
19 * License Information:
20 *
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.
25 *
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.
30 *
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.
34 *
35 *****************************************************************************/
37 #include "config.h"
38 #include "common.h"
39 #include "netutils.h"
40 #include "utils.h"
42 const char *progname = "check_smtp";
44 #define SMTP_PORT 25
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.
52 *
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.
55 *
56 * Use the -f option to provide a FROM address
57 */
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;
71 char *from_arg = " ";
72 int warning_time = 0;
73 int check_warning_time = FALSE;
74 int critical_time = 0;
75 int check_critical_time = FALSE;
76 int verbose = 0;
78 int
79 main (int argc, char **argv)
80 {
81 int sd;
82 double elapsed_time;
83 int result = STATE_UNKNOWN;
84 char buffer[MAX_INPUT_BUFFER] = "";
85 char *from_str = NULL;
86 char *helocmd = NULL;
87 struct timeval tv;
89 if (process_arguments (argc, argv) != OK)
90 usage ("Invalid command arguments supplied\n");
92 /* initialize the HELO command with the localhostname */
93 #ifndef HOST_MAX_BYTES
94 #define HOST_MAX_BYTES 255
95 #endif
96 helocmd = malloc (HOST_MAX_BYTES);
97 gethostname(helocmd, HOST_MAX_BYTES);
98 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
100 /* initialize the MAIL command with optional FROM command */
101 asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n");
103 if (verbose)
104 printf ("FROMCMD: %s\n", from_str);
106 /* initialize alarm signal handling */
107 (void) signal (SIGALRM, socket_timeout_alarm_handler);
109 /* set socket timeout */
110 (void) alarm (socket_timeout);
112 /* start timer */
113 gettimeofday (&tv, NULL);
115 /* try to connect to the host at the given port number */
116 result = my_tcp_connect (server_address, server_port, &sd);
118 /* we connected, so close connection before exiting */
119 if (result == STATE_OK) {
121 /* watch for the SMTP connection string and */
122 /* return a WARNING status if we couldn't read any data */
123 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
124 printf ("recv() failed\n");
125 result = STATE_WARNING;
126 }
127 else {
128 /* strip the buffer of carriage returns */
129 strip (buffer);
130 /* make sure we find the response we are looking for */
131 if (!strstr (buffer, server_expect)) {
132 if (server_port == SMTP_PORT)
133 printf ("Invalid SMTP response received from host\n");
134 else
135 printf ("Invalid SMTP response received from host on port %d\n",
136 server_port);
137 result = STATE_WARNING;
138 }
139 }
141 /* send the HELO command */
142 send(sd, helocmd, strlen(helocmd), 0);
144 /* allow for response to helo command to reach us */
145 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
147 #ifdef SMTP_USE_DUMMYCMD
148 send(sd, from_str, strlen(from_str), 0);
150 /* allow for response to DUMMYCMD to reach us */
151 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
153 if (verbose)
154 printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
155 #endif /* SMTP_USE_DUMMYCMD */
157 /* tell the server we're done */
158 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
160 /* finally close the connection */
161 close (sd);
162 }
164 /* reset the alarm */
165 alarm (0);
167 elapsed_time = delta_time (tv);
169 if (check_critical_time && elapsed_time > (double) critical_time)
170 result = STATE_CRITICAL;
171 else if (check_warning_time && elapsed_time > (double) warning_time)
172 result = STATE_WARNING;
174 if (verbose)
175 printf ("SMTP %s - %7.3f sec. response time, %s|time=%7.3f\n",
176 state_text (result), elapsed_time, buffer, elapsed_time);
177 else
178 printf ("SMTP %s - %7.3f second response time|time=%7.3f\n",
179 state_text (result), elapsed_time, elapsed_time);
181 return result;
182 }
189 /* process command-line arguments */
190 int
191 process_arguments (int argc, char **argv)
192 {
193 int c;
195 #ifdef HAVE_GETOPT_H
196 int option_index = 0;
197 static struct option long_options[] = {
198 {"hostname", required_argument, 0, 'H'},
199 {"expect", required_argument, 0, 'e'},
200 {"critical", required_argument, 0, 'c'},
201 {"warning", required_argument, 0, 'w'},
202 {"port", required_argument, 0, 'p'},
203 {"from", required_argument, 0, 'f'},
204 {"verbose", no_argument, 0, 'v'},
205 {"version", no_argument, 0, 'V'},
206 {"help", no_argument, 0, 'h'},
207 {0, 0, 0, 0}
208 };
209 #endif
211 if (argc < 2)
212 return ERROR;
214 for (c = 1; c < argc; c++) {
215 if (strcmp ("-to", argv[c]) == 0)
216 strcpy (argv[c], "-t");
217 else if (strcmp ("-wt", argv[c]) == 0)
218 strcpy (argv[c], "-w");
219 else if (strcmp ("-ct", argv[c]) == 0)
220 strcpy (argv[c], "-c");
221 }
223 while (1) {
224 #ifdef HAVE_GETOPT_H
225 c =
226 getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
227 &option_index);
228 #else
229 c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:");
230 #endif
231 if (c == -1 || c == EOF)
232 break;
234 switch (c) {
235 case 'H': /* hostname */
236 if (is_host (optarg)) {
237 server_address = optarg;
238 }
239 else {
240 usage ("Invalid host name\n");
241 }
242 break;
243 case 'p': /* port */
244 if (is_intpos (optarg)) {
245 server_port = atoi (optarg);
246 }
247 else {
248 usage ("Server port must be a positive integer\n");
249 }
250 break;
251 case 'f': /* from argument */
252 from_arg = optarg;
253 break;
254 case 'e': /* server expect string on 220 */
255 server_expect = optarg;
256 break;
257 case 'c': /* critical time threshold */
258 if (is_intnonneg (optarg)) {
259 critical_time = atoi (optarg);
260 check_critical_time = TRUE;
261 }
262 else {
263 usage ("Critical time must be a nonnegative integer\n");
264 }
265 break;
266 case 'w': /* warning time threshold */
267 if (is_intnonneg (optarg)) {
268 warning_time = atoi (optarg);
269 check_warning_time = TRUE;
270 }
271 else {
272 usage ("Warning time must be a nonnegative integer\n");
273 }
274 break;
275 case 'v': /* verbose */
276 verbose++;
277 break;
278 case 't': /* timeout */
279 if (is_intnonneg (optarg)) {
280 socket_timeout = atoi (optarg);
281 }
282 else {
283 usage ("Time interval must be a nonnegative integer\n");
284 }
285 break;
286 case 'V': /* version */
287 print_revision (progname, "$Revision$");
288 exit (STATE_OK);
289 case 'h': /* help */
290 print_help ();
291 exit (STATE_OK);
292 case '?': /* help */
293 usage ("Invalid argument\n");
294 }
295 }
297 c = optind;
298 if (server_address == NULL) {
299 if (argv[c]) {
300 if (is_host (argv[c]))
301 server_address = argv[c];
302 else
303 usage ("Invalid host name");
304 }
305 else {
306 asprintf (&server_address, "127.0.0.1");
307 }
308 }
310 if (server_expect == NULL)
311 asprintf (&server_expect, SMTP_EXPECT);
313 return validate_arguments ();
314 }
320 int
321 validate_arguments (void)
322 {
323 return OK;
324 }
330 void
331 print_help (void)
332 {
333 print_revision (progname, "$Revision$");
334 printf
335 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
336 "This plugin test the SMTP service on the specified host.\n\n");
337 print_usage ();
338 printf
339 ("\nOptions:\n"
340 " -H, --hostname=STRING or IPADDRESS\n"
341 " Check server on the indicated host\n"
342 " -p, --port=INTEGER\n"
343 " Make connection on the indicated port (default: %d)\n"
344 " -e, --expect=STRING\n"
345 " String to expect in first line of server response (default: %s)\n"
346 " -f, --from=STRING\n"
347 " from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
348 " -w, --warning=INTEGER\n"
349 " Seconds necessary to result in a warning status\n"
350 " -c, --critical=INTEGER\n"
351 " Seconds necessary to result in a critical status\n"
352 " -t, --timeout=INTEGER\n"
353 " Seconds before connection attempt times out (default: %d)\n"
354 " -v, --verbose\n"
355 " Print extra information (command-line use only)\n"
356 " -h, --help\n"
357 " Print detailed help screen\n"
358 " -V, --version\n"
359 " Print version information\n\n",
360 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
361 support ();
362 }
368 void
369 print_usage (void)
370 {
371 printf
372 ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
373 " %s --help\n"
374 " %s --version\n", progname, progname, progname);
375 }