Code

Perf data without leading spaces and fix ANSI C complaint about \n
[nagiosplug.git] / plugins / check_smtp.c
1 /******************************************************************************
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 *****************************************************************************/
19 const char *progname = "check_smtp";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *authors = "Nagios Plugin Development Team";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 const char *summary = "\
26 This plugin will attempt to open an SMTP connection with the host.\n";
28 const char *description = "\
29 Successul connects return STATE_OK, refusals and timeouts return\n\
30 STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful\n\
31 connects, but incorrect reponse messages from the host result in\n\
32 STATE_WARNING return values.\n";
34 const char *option_summary = "\
35 -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
36          [-w warn] [-c crit] [-t timeout] [-n] [-v]";
38 const char *options = "\
39  -H, --hostname=STRING or IPADDRESS\n\
40    Check server on the indicated host\n\
41  -p, --port=INTEGER\n\
42    Make connection on the indicated port (default: %d)\n\
43  -e, --expect=STRING\n\
44    String to expect in first line of server response (default: '%s')\n\
45  -n, nocommand\n\
46    Suppress SMTP command\n\
47  -C, --command=STRING\n\
48    SMTP command (default: '%s')\n\
49  -f, --from=STRING\n\
50    FROM-address to include in MAIL command, required by Exchange 2000\n\
51    (default: '%s')\n\
52  -w, --warning=INTEGER\n\
53    Seconds necessary to result in a warning status\n\
54  -c, --critical=INTEGER\n\
55    Seconds necessary to result in a critical status\n\
56  -t, --timeout=INTEGER\n\
57    Seconds before connection attempt times out (default: %d)\n\
58  -v, --verbose\n\
59    Print extra information (command-line use only)\n\
60  -h, --help\n\
61    Print detailed help screen\n\
62  -V, --version\n\
63    Print version information\n\n";
65 enum {
66         SMTP_PORT       = 25
67 };
68 const char *SMTP_EXPECT = "220";
69 const char *SMTP_HELO = "HELO ";
70 const char *SMTP_QUIT   = "QUIT\r\n";
72 #include "config.h"
73 #include "common.h"
74 #include "netutils.h"
75 #include "utils.h"
77 int process_arguments (int, char **);
78 int validate_arguments (void);
79 void print_help (void);
80 void print_usage (void);
82 int server_port = SMTP_PORT;
83 char *server_address = NULL;
84 char *server_expect = NULL;
85 int smtp_use_dummycmd = 1;
86 char *mail_command = "MAIL ";
87 char *from_arg = " ";
88 int warning_time = 0;
89 int check_warning_time = FALSE;
90 int critical_time = 0;
91 int check_critical_time = FALSE;
92 int verbose = 0;
94 int
95 main (int argc, char **argv)
96 {
97         int sd;
98         double elapsed_time;
99         int result = STATE_UNKNOWN;
100         char buffer[MAX_INPUT_BUFFER] = "";
101         char *from_str = NULL;
102         char *helocmd = NULL;
103         struct timeval tv;
105         if (process_arguments (argc, argv) != OK)
106                 usage ("Invalid command arguments supplied\n");
108         /* initialize the HELO command with the localhostname */
109 #ifndef HOST_MAX_BYTES
110 #define HOST_MAX_BYTES 255
111 #endif
112         helocmd = malloc (HOST_MAX_BYTES);
113         gethostname(helocmd, HOST_MAX_BYTES);
114         asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
116         /* initialize the MAIL command with optional FROM command  */
117         asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
119         if (verbose)
120                 printf ("FROMCMD: %s\n", from_str);
121         
122         /* initialize alarm signal handling */
123         (void) signal (SIGALRM, socket_timeout_alarm_handler);
125         /* set socket timeout */
126         (void) alarm (socket_timeout);
128         /* start timer */
129         gettimeofday (&tv, NULL);
131         /* try to connect to the host at the given port number */
132         result = my_tcp_connect (server_address, server_port, &sd);
134         /* we connected, so close connection before exiting */
135         if (result == STATE_OK) {
137                 /* watch for the SMTP connection string and */
138                 /* return a WARNING status if we couldn't read any data */
139                 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
140                         printf ("recv() failed\n");
141                         result = STATE_WARNING;
142                 }
143                 else {
144                         /* strip the buffer of carriage returns */
145                         strip (buffer);
146                         /* make sure we find the response we are looking for */
147                         if (!strstr (buffer, server_expect)) {
148                                 if (server_port == SMTP_PORT)
149                                         printf ("Invalid SMTP response received from host\n");
150                                 else
151                                         printf ("Invalid SMTP response received from host on port %d\n",
152                                                                         server_port);
153                                 result = STATE_WARNING;
154                         }
155                 }
157                 /* send the HELO command */
158                 send(sd, helocmd, strlen(helocmd), 0);
160                 /* allow for response to helo command to reach us */
161                 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
162                                 
163                 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
164                  * to do something useful. This can be prevented by giving a command
165                  * even if syntax is illegal (MAIL requires a FROM:<...> argument)
166                  *
167                  * According to rfc821 you can include a null reversepath in the from command
168                  * - but a log message is generated on the smtp server.
169                  *
170                  * You can disable sending mail_command with '--nocommand'
171                  * Use the -f option to provide a FROM address
172                  */
173                 if (smtp_use_dummycmd) {
175                         send(sd, from_str, strlen(from_str), 0);
177                         /* allow for response to mail_command to reach us */
178                         recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
180                         if (verbose) 
181                                 printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
183                 } /* smtp_use_dummycmd */
185                 /* tell the server we're done */
186                 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
188                 /* finally close the connection */
189                 close (sd);
190         }
192         /* reset the alarm */
193         alarm (0);
195         elapsed_time = delta_time (tv);
197         if (check_critical_time && elapsed_time > (double) critical_time)
198                 result = STATE_CRITICAL;
199         else if (check_warning_time && elapsed_time > (double) warning_time)
200                 result = STATE_WARNING;
202         if (verbose)
203                 printf ("SMTP %s - %.3f sec. response time, %s|time=%.3f\n",
204                         state_text (result), elapsed_time, buffer, elapsed_time);
205         else
206                 printf ("SMTP %s - %.3f second response time|time=%.3f\n",
207                         state_text (result), elapsed_time, elapsed_time);
209         return result;
217 /* process command-line arguments */
218 int
219 process_arguments (int argc, char **argv)
221         int c;
223         int option_index = 0;
224         static struct option long_options[] = {
225                 {"hostname", required_argument, 0, 'H'},
226                 {"expect", required_argument, 0, 'e'},
227                 {"critical", required_argument, 0, 'c'},
228                 {"warning", required_argument, 0, 'w'},
229                 {"timeout", required_argument, 0, 't'},
230                 {"port", required_argument, 0, 'p'},
231                 {"from", required_argument, 0, 'f'},
232                 {"command", required_argument, 0, 'C'},
233                 {"nocommand", required_argument, 0, 'n'},
234                 {"verbose", no_argument, 0, 'v'},
235                 {"version", no_argument, 0, 'V'},
236                 {"help", no_argument, 0, 'h'},
237                 {0, 0, 0, 0}
238         };
240         if (argc < 2)
241                 return ERROR;
243         for (c = 1; c < argc; c++) {
244                 if (strcmp ("-to", argv[c]) == 0)
245                         strcpy (argv[c], "-t");
246                 else if (strcmp ("-wt", argv[c]) == 0)
247                         strcpy (argv[c], "-w");
248                 else if (strcmp ("-ct", argv[c]) == 0)
249                         strcpy (argv[c], "-c");
250         }
252         while (1) {
253                 c = getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:C:",
254                                  long_options, &option_index);
256                 if (c == -1 || c == EOF)
257                         break;
259                 switch (c) {
260                 case 'H':                                                                       /* hostname */
261                         if (is_host (optarg)) {
262                                 server_address = optarg;
263                         }
264                         else {
265                                 usage ("Invalid host name\n");
266                         }
267                         break;
268                 case 'p':                                                                       /* port */
269                         if (is_intpos (optarg)) {
270                                 server_port = atoi (optarg);
271                         }
272                         else {
273                                 usage ("Server port must be a positive integer\n");
274                         }
275                         break;
276                 case 'f':                                                                       /* from argument */
277                         from_arg = optarg;
278                         break;
279                 case 'e':                                                                       /* server expect string on 220  */
280                         server_expect = optarg;
281                         break;
282                 case 'C':                                                                       /* server expect string on 220  */
283                         mail_command = optarg;
284                         smtp_use_dummycmd = 1;
285                         break;
286                 case 'n':                                                                       /* server expect string on 220  */
287                         smtp_use_dummycmd = 0;
288                         break;
289                 case 'c':                                                                       /* critical time threshold */
290                         if (is_intnonneg (optarg)) {
291                                 critical_time = atoi (optarg);
292                                 check_critical_time = TRUE;
293                         }
294                         else {
295                                 usage ("Critical time must be a nonnegative integer\n");
296                         }
297                         break;
298                 case 'w':                                                                       /* warning time threshold */
299                         if (is_intnonneg (optarg)) {
300                                 warning_time = atoi (optarg);
301                                 check_warning_time = TRUE;
302                         }
303                         else {
304                                 usage ("Warning time must be a nonnegative integer\n");
305                         }
306                         break;
307                 case 'v':                                                                       /* verbose */
308                         verbose++;
309                         break;
310                 case 't':                                                                       /* timeout */
311                         if (is_intnonneg (optarg)) {
312                                 socket_timeout = atoi (optarg);
313                         }
314                         else {
315                                 usage ("Time interval must be a nonnegative integer\n");
316                         }
317                         break;
318                 case 'V':                                                                       /* version */
319                         print_revision (progname, revision);
320                         exit (STATE_OK);
321                 case 'h':                                                                       /* help */
322                         print_help ();
323                         exit (STATE_OK);
324                 case '?':                                                                       /* help */
325                         usage ("Invalid argument\n");
326                 }
327         }
329         c = optind;
330         if (server_address == NULL) {
331                 if (argv[c]) {
332                         if (is_host (argv[c]))
333                                 server_address = argv[c];
334                         else
335                                 usage ("Invalid host name");
336                 }
337                 else {
338                         asprintf (&server_address, "127.0.0.1");
339                 }
340         }
342         if (server_expect == NULL)
343                 asprintf (&server_expect, SMTP_EXPECT);
345         return validate_arguments ();
352 int
353 validate_arguments (void)
355         return OK;
362 void
363 print_help (void)
365         print_revision (progname, revision);
366         printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
367                  copyright, authors, email, summary);
368         print_usage ();
369         printf ("\nOptions:\n");
370         printf (options, SMTP_PORT, SMTP_EXPECT, mail_command, from_arg,
371                 DEFAULT_SOCKET_TIMEOUT);
372         support ();
379 void
380 print_usage (void)
382         printf ("Usage: %s %s\n"
383                 "       %s --help\n"
384                 "       %s --version\n",
385                 progname, option_summary, progname, progname);