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 int option_index = 0;
196 static struct option long_options[] = {
197 {"hostname", required_argument, 0, 'H'},
198 {"expect", required_argument, 0, 'e'},
199 {"critical", required_argument, 0, 'c'},
200 {"warning", required_argument, 0, 'w'},
201 {"port", required_argument, 0, 'p'},
202 {"from", required_argument, 0, 'f'},
203 {"verbose", no_argument, 0, 'v'},
204 {"version", no_argument, 0, 'V'},
205 {"help", no_argument, 0, 'h'},
206 {0, 0, 0, 0}
207 };
209 if (argc < 2)
210 return ERROR;
212 for (c = 1; c < argc; c++) {
213 if (strcmp ("-to", argv[c]) == 0)
214 strcpy (argv[c], "-t");
215 else if (strcmp ("-wt", argv[c]) == 0)
216 strcpy (argv[c], "-w");
217 else if (strcmp ("-ct", argv[c]) == 0)
218 strcpy (argv[c], "-c");
219 }
221 while (1) {
222 c = getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
223 &option_index);
225 if (c == -1 || c == EOF)
226 break;
228 switch (c) {
229 case 'H': /* hostname */
230 if (is_host (optarg)) {
231 server_address = optarg;
232 }
233 else {
234 usage ("Invalid host name\n");
235 }
236 break;
237 case 'p': /* port */
238 if (is_intpos (optarg)) {
239 server_port = atoi (optarg);
240 }
241 else {
242 usage ("Server port must be a positive integer\n");
243 }
244 break;
245 case 'f': /* from argument */
246 from_arg = optarg;
247 break;
248 case 'e': /* server expect string on 220 */
249 server_expect = optarg;
250 break;
251 case 'c': /* critical time threshold */
252 if (is_intnonneg (optarg)) {
253 critical_time = atoi (optarg);
254 check_critical_time = TRUE;
255 }
256 else {
257 usage ("Critical time must be a nonnegative integer\n");
258 }
259 break;
260 case 'w': /* warning time threshold */
261 if (is_intnonneg (optarg)) {
262 warning_time = atoi (optarg);
263 check_warning_time = TRUE;
264 }
265 else {
266 usage ("Warning time must be a nonnegative integer\n");
267 }
268 break;
269 case 'v': /* verbose */
270 verbose++;
271 break;
272 case 't': /* timeout */
273 if (is_intnonneg (optarg)) {
274 socket_timeout = atoi (optarg);
275 }
276 else {
277 usage ("Time interval must be a nonnegative integer\n");
278 }
279 break;
280 case 'V': /* version */
281 print_revision (progname, "$Revision$");
282 exit (STATE_OK);
283 case 'h': /* help */
284 print_help ();
285 exit (STATE_OK);
286 case '?': /* help */
287 usage ("Invalid argument\n");
288 }
289 }
291 c = optind;
292 if (server_address == NULL) {
293 if (argv[c]) {
294 if (is_host (argv[c]))
295 server_address = argv[c];
296 else
297 usage ("Invalid host name");
298 }
299 else {
300 asprintf (&server_address, "127.0.0.1");
301 }
302 }
304 if (server_expect == NULL)
305 asprintf (&server_expect, SMTP_EXPECT);
307 return validate_arguments ();
308 }
314 int
315 validate_arguments (void)
316 {
317 return OK;
318 }
324 void
325 print_help (void)
326 {
327 print_revision (progname, "$Revision$");
328 printf
329 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
330 "This plugin test the SMTP service on the specified host.\n\n");
331 print_usage ();
332 printf
333 ("\nOptions:\n"
334 " -H, --hostname=STRING or IPADDRESS\n"
335 " Check server on the indicated host\n"
336 " -p, --port=INTEGER\n"
337 " Make connection on the indicated port (default: %d)\n"
338 " -e, --expect=STRING\n"
339 " String to expect in first line of server response (default: %s)\n"
340 " -f, --from=STRING\n"
341 " from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
342 " -w, --warning=INTEGER\n"
343 " Seconds necessary to result in a warning status\n"
344 " -c, --critical=INTEGER\n"
345 " Seconds necessary to result in a critical status\n"
346 " -t, --timeout=INTEGER\n"
347 " Seconds before connection attempt times out (default: %d)\n"
348 " -v, --verbose\n"
349 " Print extra information (command-line use only)\n"
350 " -h, --help\n"
351 " Print detailed help screen\n"
352 " -V, --version\n"
353 " Print version information\n\n",
354 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
355 support ();
356 }
362 void
363 print_usage (void)
364 {
365 printf
366 ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
367 " %s --help\n"
368 " %s --version\n", progname, progname, progname);
369 }