8c4f26e3276d77c6e491ff9d294a100747494d13
1 /******************************************************************************
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.
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.
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.
17 ******************************************************************************/
19 const char *progname = "check_smtp";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 enum {
29 SMTP_PORT = 25
30 };
31 const char *SMTP_EXPECT = "220";
32 const char *SMTP_HELO = "HELO ";
33 const char *SMTP_QUIT = "QUIT\r\n";
35 int process_arguments (int, char **);
36 int validate_arguments (void);
37 void print_help (void);
38 void print_usage (void);
40 int server_port = SMTP_PORT;
41 char *server_address = NULL;
42 char *server_expect = NULL;
43 int smtp_use_dummycmd = 1;
44 char *mail_command;
45 char *from_arg;
46 int warning_time = 0;
47 int check_warning_time = FALSE;
48 int critical_time = 0;
49 int check_critical_time = FALSE;
50 int verbose = 0;
56 \f
57 int
58 main (int argc, char **argv)
59 {
60 int sd;
61 double elapsed_time;
62 long microsec;
63 int result = STATE_UNKNOWN;
64 char buffer[MAX_INPUT_BUFFER];
65 char *from_str = NULL;
66 char *helocmd = NULL;
67 struct timeval tv;
69 setlocale (LC_ALL, "");
70 bindtextdomain (PACKAGE, LOCALEDIR);
71 textdomain (PACKAGE);
73 if (process_arguments (argc, argv) != OK)
74 usage (_("Invalid command arguments supplied\n"));
76 /* initialize the HELO command with the localhostname */
77 #ifndef HOST_MAX_BYTES
78 #define HOST_MAX_BYTES 255
79 #endif
80 helocmd = malloc (HOST_MAX_BYTES);
81 gethostname(helocmd, HOST_MAX_BYTES);
82 asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
84 /* initialize the MAIL command with optional FROM command */
85 asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
87 if (verbose)
88 printf ("FROMCMD: %s\n", from_str);
90 /* initialize alarm signal handling */
91 (void) signal (SIGALRM, socket_timeout_alarm_handler);
93 /* set socket timeout */
94 (void) alarm (socket_timeout);
96 /* start timer */
97 gettimeofday (&tv, NULL);
99 /* try to connect to the host at the given port number */
100 result = my_tcp_connect (server_address, server_port, &sd);
102 /* we connected, so close connection before exiting */
103 if (result == STATE_OK) {
105 /* watch for the SMTP connection string and */
106 /* return a WARNING status if we couldn't read any data */
107 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
108 printf (_("recv() failed\n"));
109 result = STATE_WARNING;
110 }
111 else {
112 /* strip the buffer of carriage returns */
113 strip (buffer);
114 /* make sure we find the response we are looking for */
115 if (!strstr (buffer, server_expect)) {
116 if (server_port == SMTP_PORT)
117 printf (_("Invalid SMTP response received from host\n"));
118 else
119 printf (_("Invalid SMTP response received from host on port %d\n"),
120 server_port);
121 result = STATE_WARNING;
122 }
123 }
125 /* send the HELO command */
126 send(sd, helocmd, strlen(helocmd), 0);
128 /* allow for response to helo command to reach us */
129 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
131 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
132 * to do something useful. This can be prevented by giving a command
133 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
134 *
135 * According to rfc821 you can include a null reversepath in the from command
136 * - but a log message is generated on the smtp server.
137 *
138 * You can disable sending mail_command with '--nocommand'
139 * Use the -f option to provide a FROM address
140 */
141 if (smtp_use_dummycmd) {
143 send(sd, from_str, strlen(from_str), 0);
145 /* allow for response to mail_command to reach us */
146 recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
148 if (verbose)
149 printf(_("DUMMYCMD: %s\n%s\n"),from_str,buffer);
151 } /* smtp_use_dummycmd */
153 /* tell the server we're done */
154 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
156 /* finally close the connection */
157 close (sd);
158 }
160 /* reset the alarm */
161 alarm (0);
163 microsec = deltime (tv);
164 elapsed_time = (double)microsec / 1.0e6;
166 if (check_critical_time && elapsed_time > (double) critical_time)
167 result = STATE_CRITICAL;
168 else if (check_warning_time && elapsed_time > (double) warning_time)
169 result = STATE_WARNING;
171 printf (_("SMTP %s - %.3f sec. response time%s%s|%s\n"),
172 state_text (result), elapsed_time,
173 verbose?", ":"", verbose?buffer:"",
174 perfdata ("time", microsec, "us",
175 check_warning_time, (long)(1000000*warning_time),
176 check_critical_time, (long)(1000000*critical_time),
177 TRUE, 0, FALSE, 0));
179 return result;
180 }
186 \f
187 /* process command-line arguments */
188 int
189 process_arguments (int argc, char **argv)
190 {
191 int c;
193 int option = 0;
194 static struct option longopts[] = {
195 {"hostname", required_argument, 0, 'H'},
196 {"expect", required_argument, 0, 'e'},
197 {"critical", required_argument, 0, 'c'},
198 {"warning", required_argument, 0, 'w'},
199 {"timeout", required_argument, 0, 't'},
200 {"port", required_argument, 0, 'p'},
201 {"from", required_argument, 0, 'f'},
202 {"command", required_argument, 0, 'C'},
203 {"nocommand", required_argument, 0, 'n'},
204 {"verbose", no_argument, 0, 'v'},
205 {"version", no_argument, 0, 'V'},
206 {"use-ipv4", no_argument, 0, '4'},
207 {"use-ipv6", no_argument, 0, '6'},
208 {"help", no_argument, 0, 'h'},
209 {0, 0, 0, 0}
210 };
212 if (argc < 2)
213 return ERROR;
215 for (c = 1; c < argc; c++) {
216 if (strcmp ("-to", argv[c]) == 0)
217 strcpy (argv[c], "-t");
218 else if (strcmp ("-wt", argv[c]) == 0)
219 strcpy (argv[c], "-w");
220 else if (strcmp ("-ct", argv[c]) == 0)
221 strcpy (argv[c], "-c");
222 }
224 while (1) {
225 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:",
226 longopts, &option);
228 if (c == -1 || c == EOF)
229 break;
231 switch (c) {
232 case 'H': /* hostname */
233 if (is_host (optarg)) {
234 server_address = optarg;
235 }
236 else {
237 usage (_("Invalid host name\n"));
238 }
239 break;
240 case 'p': /* port */
241 if (is_intpos (optarg))
242 server_port = atoi (optarg);
243 else
244 usage (_("Server port must be a positive integer\n"));
245 break;
246 case 'f': /* from argument */
247 from_arg = optarg;
248 break;
249 case 'e': /* server expect string on 220 */
250 server_expect = optarg;
251 break;
252 case 'C': /* server expect string on 220 */
253 mail_command = optarg;
254 smtp_use_dummycmd = 1;
255 break;
256 case 'n': /* server expect string on 220 */
257 smtp_use_dummycmd = 0;
258 break;
259 case 'c': /* critical time threshold */
260 if (is_intnonneg (optarg)) {
261 critical_time = atoi (optarg);
262 check_critical_time = TRUE;
263 }
264 else {
265 usage (_("Critical time must be a nonnegative integer\n"));
266 }
267 break;
268 case 'w': /* warning time threshold */
269 if (is_intnonneg (optarg)) {
270 warning_time = atoi (optarg);
271 check_warning_time = TRUE;
272 }
273 else {
274 usage (_("Warning time must be a nonnegative integer\n"));
275 }
276 break;
277 case 'v': /* verbose */
278 verbose++;
279 break;
280 case 't': /* timeout */
281 if (is_intnonneg (optarg)) {
282 socket_timeout = atoi (optarg);
283 }
284 else {
285 usage (_("Time interval must be a nonnegative integer\n"));
286 }
287 break;
288 case '4':
289 address_family = AF_INET;
290 break;
291 case '6':
292 #ifdef USE_IPV6
293 address_family = AF_INET6;
294 #else
295 usage (_("IPv6 support not available\n"));
296 #endif
297 break;
298 case 'V': /* version */
299 print_revision (progname, revision);
300 exit (STATE_OK);
301 case 'h': /* help */
302 print_help ();
303 exit (STATE_OK);
304 case '?': /* help */
305 usage (_("Invalid argument\n"));
306 }
307 }
309 c = optind;
310 if (server_address == NULL) {
311 if (argv[c]) {
312 if (is_host (argv[c]))
313 server_address = argv[c];
314 else
315 usage (_("Invalid host name"));
316 }
317 else {
318 asprintf (&server_address, "127.0.0.1");
319 }
320 }
322 if (server_expect == NULL)
323 server_expect = strdup (SMTP_EXPECT);
325 if (mail_command == NULL)
326 mail_command = strdup("MAIL ");
328 if (from_arg==NULL)
329 from_arg = strdup(" ");
331 return validate_arguments ();
332 }
338 int
339 validate_arguments (void)
340 {
341 return OK;
342 }
348 \f
349 void
350 print_help (void)
351 {
352 char *myport;
353 asprintf (&myport, "%d", SMTP_PORT);
355 print_revision (progname, revision);
357 printf (_("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"));
358 printf (_(COPYRIGHT), copyright, email);
360 printf(_("\
361 This plugin will attempt to open an SMTP connection with the host.\n\n"));
363 print_usage ();
365 printf (_(UT_HELP_VRSN));
367 printf (_(UT_HOST_PORT), 'p', myport);
369 printf (_(UT_IPv46));
371 printf (_("\
372 -e, --expect=STRING\n\
373 String to expect in first line of server response (default: '%s')\n\
374 -n, nocommand\n\
375 Suppress SMTP command\n\
376 -C, --command=STRING\n\
377 SMTP command (default: '%s')\n\
378 -f, --from=STRING\n\
379 FROM-address to include in MAIL command, required by Exchange 2000\n\
380 (default: '%s')\n"), SMTP_EXPECT, mail_command, from_arg);
382 printf (_(UT_WARN_CRIT));
384 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
386 printf (_(UT_VERBOSE));
388 printf(_("\n\
389 Successul connects return STATE_OK, refusals and timeouts return\n\
390 STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
391 connects, but incorrect reponse messages from the host result in\n\
392 STATE_WARNING return values.\n"));
394 printf (_(UT_SUPPORT));
395 }
401 void
402 print_usage (void)
403 {
404 printf ("\
405 Usage: %s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
406 [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]\n", progname);
407 printf (_(UT_HLP_VRS), progname, progname);
408 }