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 #define 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 #define SMTP_DUMMYCMD "MAIL FROM:<>\r\n"
57 #define SMTP_USE_DUMMYCMD 1
58 #define SMTP_QUIT "QUIT\r\n"
60 int process_arguments (int, char **);
61 int call_getopt (int, char **);
62 int validate_arguments (void);
63 int check_disk (int usp, int free_disk);
64 void print_help (void);
65 void print_usage (void);
67 int server_port = SMTP_PORT;
68 char *server_address = NULL;
69 char *server_expect = NULL;
70 int warning_time = 0;
71 int check_warning_time = FALSE;
72 int critical_time = 0;
73 int check_critical_time = FALSE;
74 int verbose = FALSE;
76 int
77 main (int argc, char **argv)
78 {
79 int sd;
80 int result;
81 char buffer[MAX_INPUT_BUFFER] = "";
82 char helocmd[255] = SMTP_HELO ;
83 char myhostname[248];
86 if (process_arguments (argc, argv) != OK)
87 usage ("Invalid command arguments supplied\n");
89 /* initalize the HELO command with the localhostname */
90 gethostname(myhostname, sizeof(myhostname));
91 strcat(helocmd, myhostname);
92 strcat(helocmd, "\r\n");
94 /* initialize alarm signal handling */
95 signal (SIGALRM, socket_timeout_alarm_handler);
97 /* set socket timeout */
98 alarm (socket_timeout);
100 /* try to connect to the host at the given port number */
101 time (&start_time);
102 result = my_tcp_connect (server_address, server_port, &sd);
104 /* we connected, so close connection before exiting */
105 if (result == STATE_OK) {
107 /* watch for the SMTP connection string */
108 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
110 /* strip the buffer of carriage returns */
111 strip (buffer);
113 /* return a WARNING status if we couldn't read any data */
114 if (result == -1) {
115 printf ("recv() failed\n");
116 result = STATE_WARNING;
117 }
119 else {
121 /* make sure we find the response we are looking for */
122 if (!strstr (buffer, server_expect)) {
123 if (server_port == SMTP_PORT)
124 printf ("Invalid SMTP response received from host\n");
125 else
126 printf ("Invalid SMTP response received from host on port %d\n",
127 server_port);
128 result = STATE_WARNING;
129 }
131 else {
133 time (&end_time);
135 result = STATE_OK;
137 if (check_critical_time == TRUE
138 && (end_time - start_time) > critical_time) result =
139 STATE_CRITICAL;
140 else if (check_warning_time == TRUE
141 && (end_time - start_time) > warning_time) result =
142 STATE_WARNING;
144 if (verbose == TRUE)
145 printf ("SMTP %s - %d sec. response time, %s\n",
146 state_text (result), (int) (end_time - start_time), buffer);
147 else
148 printf ("SMTP %s - %d second response time\n", state_text (result),
149 (int) (end_time - start_time));
150 }
151 }
153 /* close the connection */
154 /* first send the HELO command */
155 send(sd,helocmd,strlen(helocmd),0);
156 /* allow for response to helo command to reach us */
157 recv(sd,buffer,MAX_INPUT_BUFFER-1,0);
159 #ifdef SMTP_USE_DUMMYCMD
160 send(sd,SMTP_DUMMYCMD,strlen(SMTP_DUMMYCMD),0);
161 /* allow for response to DUMMYCMD to reach us */
162 recv(sd,buffer,MAX_INPUT_BUFFER-1,0);
163 #endif /* SMTP_USE_DUMMYCMD */
165 /* finally close the connection */
166 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
167 close (sd);
168 }
170 /* reset the alarm */
171 alarm (0);
173 return result;
174 }
181 /* process command-line arguments */
182 int
183 process_arguments (int argc, char **argv)
184 {
185 int c;
187 if (argc < 2)
188 return ERROR;
190 for (c = 1; c < argc; c++) {
191 if (strcmp ("-to", argv[c]) == 0)
192 strcpy (argv[c], "-t");
193 else if (strcmp ("-wt", argv[c]) == 0)
194 strcpy (argv[c], "-w");
195 else if (strcmp ("-ct", argv[c]) == 0)
196 strcpy (argv[c], "-c");
197 }
201 c = 0;
202 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
204 if (is_option (argv[c]))
205 continue;
207 if (server_address == NULL) {
208 if (is_host (argv[c])) {
209 server_address = argv[c];
210 }
211 else {
212 usage ("Invalid host name");
213 }
214 }
215 }
217 if (server_address == NULL)
218 server_address = strscpy (NULL, "127.0.0.1");
220 if (server_expect == NULL)
221 server_expect = strscpy (NULL, SMTP_EXPECT);
223 return validate_arguments ();
224 }
231 int
232 call_getopt (int argc, char **argv)
233 {
234 int c, i = 0;
236 #ifdef HAVE_GETOPT_H
237 int option_index = 0;
238 static struct option long_options[] = {
239 {"hostname", required_argument, 0, 'H'},
240 {"expect", required_argument, 0, 'e'},
241 {"critical", required_argument, 0, 'c'},
242 {"warning", required_argument, 0, 'w'},
243 {"port", required_argument, 0, 'P'},
244 {"verbose", no_argument, 0, 'v'},
245 {"version", no_argument, 0, 'V'},
246 {"help", no_argument, 0, 'h'},
247 {0, 0, 0, 0}
248 };
249 #endif
251 while (1) {
252 #ifdef HAVE_GETOPT_H
253 c =
254 getopt_long (argc, argv, "+hVvt:p:e:c:w:H:", long_options,
255 &option_index);
256 #else
257 c = getopt (argc, argv, "+?hVvt:p:e:c:w:H:");
258 #endif
260 i++;
262 if (c == -1 || c == EOF || c == 1)
263 break;
265 switch (c) {
266 case 't':
267 case 'p':
268 case 'e':
269 case 'c':
270 case 'w':
271 case 'H':
272 i++;
273 }
275 switch (c) {
276 case 'H': /* hostname */
277 if (is_host (optarg)) {
278 server_address = optarg;
279 }
280 else {
281 usage ("Invalid host name\n");
282 }
283 break;
284 case 'p': /* port */
285 if (is_intpos (optarg)) {
286 server_port = atoi (optarg);
287 }
288 else {
289 usage ("Server port must be a positive integer\n");
290 }
291 break;
292 case 'e': /* username */
293 server_expect = optarg;
294 break;
295 case 'c': /* critical time threshold */
296 if (is_intnonneg (optarg)) {
297 critical_time = atoi (optarg);
298 check_critical_time = TRUE;
299 }
300 else {
301 usage ("Critical time must be a nonnegative integer\n");
302 }
303 break;
304 case 'w': /* warning time threshold */
305 if (is_intnonneg (optarg)) {
306 warning_time = atoi (optarg);
307 check_warning_time = TRUE;
308 }
309 else {
310 usage ("Warning time must be a nonnegative integer\n");
311 }
312 break;
313 case 'v': /* verbose */
314 verbose = TRUE;
315 break;
316 case 't': /* timeout */
317 if (is_intnonneg (optarg)) {
318 socket_timeout = atoi (optarg);
319 }
320 else {
321 usage ("Time interval must be a nonnegative integer\n");
322 }
323 break;
324 case 'V': /* version */
325 print_revision (PROGNAME, "$Revision$");
326 exit (STATE_OK);
327 case 'h': /* help */
328 print_help ();
329 exit (STATE_OK);
330 case '?': /* help */
331 usage ("Invalid argument\n");
332 }
333 }
334 return i;
335 }
341 int
342 validate_arguments (void)
343 {
344 return OK;
345 }
351 void
352 print_help (void)
353 {
354 print_revision (PROGNAME, "$Revision$");
355 printf
356 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
357 "This plugin test the SMTP service on the specified host.\n\n");
358 print_usage ();
359 printf
360 ("\nOptions:\n"
361 " -H, --hostname=STRING or IPADDRESS\n"
362 " Check server on the indicated host\n"
363 " -p, --port=INTEGER\n"
364 " Make connection on the indicated port (default: %d)\n"
365 " -e, --expect=STRING\n"
366 " String to expect in first line of server response (default: %s)\n"
367 " -w, --warning=INTEGER\n"
368 " Seconds necessary to result in a warning status\n"
369 " -c, --critical=INTEGER\n"
370 " Seconds necessary to result in a critical status\n"
371 " -t, --timeout=INTEGER\n"
372 " Seconds before connection attempt times out (default: %d)\n"
373 " -v, --verbose\n"
374 " Print extra information (command-line use only)\n"
375 " -h, --help\n"
376 " Print detailed help screen\n"
377 " -V, --version\n"
378 " Print version information\n\n",
379 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
380 support ();
381 }
387 void
388 print_usage (void)
389 {
390 printf
391 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n"
392 " %s --help\n"
393 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
394 }