Code

conformance to RFC 821 <CRLF>
[nagiosplug.git] / plugins / check_smtp.c
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 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
47  * to do something useful. This can be prevented by giving a command
48  * even if syntax is illegal (MAIL requires a FROM:<...> argument)
49  * You can disable sending DUMMYCMD by undefining SMTP_USE_DUMMYCMD.
50  */
51 #define SMTP_DUMMYCMD  "MAIL\r\n"
52 #define SMTP_USE_DUMMYCMD 1
53 #define SMTP_QUIT       "QUIT\r\n"
55 int process_arguments (int, char **);
56 int call_getopt (int, char **);
57 int validate_arguments (void);
58 int check_disk (int usp, int free_disk);
59 void print_help (void);
60 void print_usage (void);
62 int server_port = SMTP_PORT;
63 char *server_address = NULL;
64 char *server_expect = NULL;
65 int warning_time = 0;
66 int check_warning_time = FALSE;
67 int critical_time = 0;
68 int check_critical_time = FALSE;
69 int verbose = FALSE;
71 int
72 main (int argc, char **argv)
73 {
74         int sd;
75         int result;
76         char buffer[MAX_INPUT_BUFFER] = "";
78         if (process_arguments (argc, argv) != OK)
79                 usage ("Invalid command arguments supplied\n");
81         /* initialize alarm signal handling */
82         signal (SIGALRM, socket_timeout_alarm_handler);
84         /* set socket timeout */
85         alarm (socket_timeout);
87         /* try to connect to the host at the given port number */
88         time (&start_time);
89         result = my_tcp_connect (server_address, server_port, &sd);
91         /* we connected, so close connection before exiting */
92         if (result == STATE_OK) {
94                 /* watch for the SMTP connection string */
95                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
97                 /* strip the buffer of carriage returns */
98                 strip (buffer);
100                 /* return a WARNING status if we couldn't read any data */
101                 if (result == -1) {
102                         printf ("recv() failed\n");
103                         result = STATE_WARNING;
104                 }
106                 else {
108                         /* make sure we find the response we are looking for */
109                         if (!strstr (buffer, server_expect)) {
110                                 if (server_port == SMTP_PORT)
111                                         printf ("Invalid SMTP response received from host\n");
112                                 else
113                                         printf ("Invalid SMTP response received from host on port %d\n",
114                                                                         server_port);
115                                 result = STATE_WARNING;
116                         }
118                         else {
120                                 time (&end_time);
122                                 result = STATE_OK;
124                                 if (check_critical_time == TRUE
125                                                 && (end_time - start_time) > critical_time) result =
126                                                 STATE_CRITICAL;
127                                 else if (check_warning_time == TRUE
128                                                                  && (end_time - start_time) > warning_time) result =
129                                                 STATE_WARNING;
131                                 if (verbose == TRUE)
132                                         printf ("SMTP %s - %d sec. response time, %s\n",
133                                                                         state_text (result), (int) (end_time - start_time), buffer);
134                                 else
135                                         printf ("SMTP %s - %d second response time\n", state_text (result),
136                                                                         (int) (end_time - start_time));
137                         }
138                 }
140                 /* close the connection */
141 #ifdef SMTP_USE_DUMMYCMD
142                send(sd,SMTP_DUMMYCMD,strlen(SMTP_DUMMYCMD),0);
143                /* allow for response to DUMMYCMD to reach us */
144                recv(sd,buffer,MAX_INPUT_BUFFER-1,0);
145 #endif /* SMTP_USE_DUMMYCMD */
147                 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
148                 close (sd);
149         }
151         /* reset the alarm */
152         alarm (0);
154         return result;
162 /* process command-line arguments */
163 int
164 process_arguments (int argc, char **argv)
166         int c;
168         if (argc < 2)
169                 return ERROR;
171         for (c = 1; c < argc; c++) {
172                 if (strcmp ("-to", argv[c]) == 0)
173                         strcpy (argv[c], "-t");
174                 else if (strcmp ("-wt", argv[c]) == 0)
175                         strcpy (argv[c], "-w");
176                 else if (strcmp ("-ct", argv[c]) == 0)
177                         strcpy (argv[c], "-c");
178         }
182         c = 0;
183         while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
185                 if (is_option (argv[c]))
186                         continue;
188                 if (server_address == NULL) {
189                         if (is_host (argv[c])) {
190                                 server_address = argv[c];
191                         }
192                         else {
193                                 usage ("Invalid host name");
194                         }
195                 }
196         }
198         if (server_address == NULL)
199                 server_address = strscpy (NULL, "127.0.0.1");
201         if (server_expect == NULL)
202                 server_expect = strscpy (NULL, SMTP_EXPECT);
204         return validate_arguments ();
212 int
213 call_getopt (int argc, char **argv)
215         int c, i = 0;
217 #ifdef HAVE_GETOPT_H
218         int option_index = 0;
219         static struct option long_options[] = {
220                 {"hostname", required_argument, 0, 'H'},
221                 {"expect", required_argument, 0, 'e'},
222                 {"critical", required_argument, 0, 'c'},
223                 {"warning", required_argument, 0, 'w'},
224                 {"port", required_argument, 0, 'P'},
225                 {"verbose", no_argument, 0, 'v'},
226                 {"version", no_argument, 0, 'V'},
227                 {"help", no_argument, 0, 'h'},
228                 {0, 0, 0, 0}
229         };
230 #endif
232         while (1) {
233 #ifdef HAVE_GETOPT_H
234                 c =
235                         getopt_long (argc, argv, "+hVvt:p:e:c:w:H:", long_options,
236                                                                          &option_index);
237 #else
238                 c = getopt (argc, argv, "+?hVvt:p:e:c:w:H:");
239 #endif
241                 i++;
243                 if (c == -1 || c == EOF || c == 1)
244                         break;
246                 switch (c) {
247                 case 't':
248                 case 'p':
249                 case 'e':
250                 case 'c':
251                 case 'w':
252                 case 'H':
253                         i++;
254                 }
256                 switch (c) {
257                 case 'H':                                                                       /* hostname */
258                         if (is_host (optarg)) {
259                                 server_address = optarg;
260                         }
261                         else {
262                                 usage ("Invalid host name\n");
263                         }
264                         break;
265                 case 'p':                                                                       /* port */
266                         if (is_intpos (optarg)) {
267                                 server_port = atoi (optarg);
268                         }
269                         else {
270                                 usage ("Server port must be a positive integer\n");
271                         }
272                         break;
273                 case 'e':                                                                       /* username */
274                         server_expect = optarg;
275                         break;
276                 case 'c':                                                                       /* critical time threshold */
277                         if (is_intnonneg (optarg)) {
278                                 critical_time = atoi (optarg);
279                                 check_critical_time = TRUE;
280                         }
281                         else {
282                                 usage ("Critical time must be a nonnegative integer\n");
283                         }
284                         break;
285                 case 'w':                                                                       /* warning time threshold */
286                         if (is_intnonneg (optarg)) {
287                                 warning_time = atoi (optarg);
288                                 check_warning_time = TRUE;
289                         }
290                         else {
291                                 usage ("Warning time must be a nonnegative integer\n");
292                         }
293                         break;
294                 case 'v':                                                                       /* verbose */
295                         verbose = TRUE;
296                         break;
297                 case 't':                                                                       /* timeout */
298                         if (is_intnonneg (optarg)) {
299                                 socket_timeout = atoi (optarg);
300                         }
301                         else {
302                                 usage ("Time interval must be a nonnegative integer\n");
303                         }
304                         break;
305                 case 'V':                                                                       /* version */
306                         print_revision (PROGNAME, "$Revision$");
307                         exit (STATE_OK);
308                 case 'h':                                                                       /* help */
309                         print_help ();
310                         exit (STATE_OK);
311                 case '?':                                                                       /* help */
312                         usage ("Invalid argument\n");
313                 }
314         }
315         return i;
322 int
323 validate_arguments (void)
325         return OK;
332 void
333 print_help (void)
335         print_revision (PROGNAME, "$Revision$");
336         printf
337                 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
338                  "This plugin test the SMTP service on the specified host.\n\n");
339         print_usage ();
340         printf
341                 ("\nOptions:\n"
342                  " -H, --hostname=STRING or IPADDRESS\n"
343                  "   Check server on the indicated host\n"
344                  " -p, --port=INTEGER\n"
345                  "   Make connection on the indicated port (default: %d)\n"
346                  " -e, --expect=STRING\n"
347                  "   String to expect in first line of server response (default: %s)\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 ();
368 void
369 print_usage (void)
371         printf
372                 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n"
373                  "       %s --help\n"
374                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);