summaryrefslogtreecommitdiffstats
path: root/plugins/check_smtp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_smtp.c')
-rw-r--r--plugins/check_smtp.c362
1 files changed, 362 insertions, 0 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
new file mode 100644
index 0000000..d57b784
--- /dev/null
+++ b/plugins/check_smtp.c
@@ -0,0 +1,362 @@
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*****************************************************************************/
36
37#include "config.h"
38#include "common.h"
39#include "netutils.h"
40#include "utils.h"
41
42#define PROGNAME "check_smtp"
43
44#define SMTP_PORT 25
45#define SMTP_EXPECT "220"
46#define SMTP_QUIT "QUIT\n"
47
48int process_arguments (int, char **);
49int call_getopt (int, char **);
50int validate_arguments (void);
51int check_disk (int usp, int free_disk);
52void print_help (void);
53void print_usage (void);
54
55int server_port = SMTP_PORT;
56char *server_address = NULL;
57char *server_expect = NULL;
58int warning_time = 0;
59int check_warning_time = FALSE;
60int critical_time = 0;
61int check_critical_time = FALSE;
62int verbose = FALSE;
63
64int
65main (int argc, char **argv)
66{
67 int sd;
68 int result;
69 char buffer[MAX_INPUT_BUFFER] = "";
70
71 if (process_arguments (argc, argv) != OK)
72 usage ("Invalid command arguments supplied\n");
73
74 /* initialize alarm signal handling */
75 signal (SIGALRM, socket_timeout_alarm_handler);
76
77 /* set socket timeout */
78 alarm (socket_timeout);
79
80 /* try to connect to the host at the given port number */
81 time (&start_time);
82 result = my_tcp_connect (server_address, server_port, &sd);
83
84 /* we connected, so close connection before exiting */
85 if (result == STATE_OK) {
86
87 /* watch for the SMTP connection string */
88 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
89
90 /* strip the buffer of carriage returns */
91 strip (buffer);
92
93 /* return a WARNING status if we couldn't read any data */
94 if (result == -1) {
95 printf ("recv() failed\n");
96 result = STATE_WARNING;
97 }
98
99 else {
100
101 /* make sure we find the response we are looking for */
102 if (!strstr (buffer, server_expect)) {
103 if (server_port == SMTP_PORT)
104 printf ("Invalid SMTP response received from host\n");
105 else
106 printf ("Invalid SMTP response received from host on port %d\n",
107 server_port);
108 result = STATE_WARNING;
109 }
110
111 else {
112
113 time (&end_time);
114
115 result = STATE_OK;
116
117 if (check_critical_time == TRUE
118 && (end_time - start_time) > critical_time) result =
119 STATE_CRITICAL;
120 else if (check_warning_time == TRUE
121 && (end_time - start_time) > warning_time) result =
122 STATE_WARNING;
123
124 if (verbose == TRUE)
125 printf ("SMTP %s - %d sec. response time, %s\n",
126 state_text (result), (int) (end_time - start_time), buffer);
127 else
128 printf ("SMTP %s - %d second response time\n", state_text (result),
129 (int) (end_time - start_time));
130 }
131 }
132
133 /* close the connection */
134 send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
135 close (sd);
136 }
137
138 /* reset the alarm */
139 alarm (0);
140
141 return result;
142}
143
144
145
146
147
148
149/* process command-line arguments */
150int
151process_arguments (int argc, char **argv)
152{
153 int c;
154
155 if (argc < 2)
156 return ERROR;
157
158 for (c = 1; c < argc; c++) {
159 if (strcmp ("-to", argv[c]) == 0)
160 strcpy (argv[c], "-t");
161 else if (strcmp ("-wt", argv[c]) == 0)
162 strcpy (argv[c], "-w");
163 else if (strcmp ("-ct", argv[c]) == 0)
164 strcpy (argv[c], "-c");
165 }
166
167
168
169 c = 0;
170 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
171
172 if (is_option (argv[c]))
173 continue;
174
175 if (server_address == NULL) {
176 if (is_host (argv[c])) {
177 server_address = argv[c];
178 }
179 else {
180 usage ("Invalid host name");
181 }
182 }
183 }
184
185 if (server_address == NULL)
186 server_address = strscpy (NULL, "127.0.0.1");
187
188 if (server_expect == NULL)
189 server_expect = strscpy (NULL, SMTP_EXPECT);
190
191 return validate_arguments ();
192}
193
194
195
196
197
198
199int
200call_getopt (int argc, char **argv)
201{
202 int c, i = 0;
203
204#ifdef HAVE_GETOPT_H
205 int option_index = 0;
206 static struct option long_options[] = {
207 {"hostname", required_argument, 0, 'H'},
208 {"expect", required_argument, 0, 'e'},
209 {"critical", required_argument, 0, 'c'},
210 {"warning", required_argument, 0, 'w'},
211 {"port", required_argument, 0, 'P'},
212 {"verbose", no_argument, 0, 'v'},
213 {"version", no_argument, 0, 'V'},
214 {"help", no_argument, 0, 'h'},
215 {0, 0, 0, 0}
216 };
217#endif
218
219 while (1) {
220#ifdef HAVE_GETOPT_H
221 c =
222 getopt_long (argc, argv, "+hVvt:p:e:c:w:H:", long_options,
223 &option_index);
224#else
225 c = getopt (argc, argv, "+?hVvt:p:e:c:w:H:");
226#endif
227
228 i++;
229
230 if (c == -1 || c == EOF || c == 1)
231 break;
232
233 switch (c) {
234 case 't':
235 case 'p':
236 case 'e':
237 case 'c':
238 case 'w':
239 case 'H':
240 i++;
241 }
242
243 switch (c) {
244 case 'H': /* hostname */
245 if (is_host (optarg)) {
246 server_address = optarg;
247 }
248 else {
249 usage ("Invalid host name\n");
250 }
251 break;
252 case 'p': /* port */
253 if (is_intpos (optarg)) {
254 server_port = atoi (optarg);
255 }
256 else {
257 usage ("Server port must be a positive integer\n");
258 }
259 break;
260 case 'e': /* username */
261 server_expect = optarg;
262 break;
263 case 'c': /* critical time threshold */
264 if (is_intnonneg (optarg)) {
265 critical_time = atoi (optarg);
266 check_critical_time = TRUE;
267 }
268 else {
269 usage ("Critical time must be a nonnegative integer\n");
270 }
271 break;
272 case 'w': /* warning time threshold */
273 if (is_intnonneg (optarg)) {
274 warning_time = atoi (optarg);
275 check_warning_time = TRUE;
276 }
277 else {
278 usage ("Warning time must be a nonnegative integer\n");
279 }
280 break;
281 case 'v': /* verbose */
282 verbose = TRUE;
283 break;
284 case 't': /* timeout */
285 if (is_intnonneg (optarg)) {
286 socket_timeout = atoi (optarg);
287 }
288 else {
289 usage ("Time interval must be a nonnegative integer\n");
290 }
291 break;
292 case 'V': /* version */
293 print_revision (PROGNAME, "$Revision$");
294 exit (STATE_OK);
295 case 'h': /* help */
296 print_help ();
297 exit (STATE_OK);
298 case '?': /* help */
299 usage ("Invalid argument\n");
300 }
301 }
302 return i;
303}
304
305
306
307
308
309int
310validate_arguments (void)
311{
312 return OK;
313}
314
315
316
317
318
319void
320print_help (void)
321{
322 print_revision (PROGNAME, "$Revision$");
323 printf
324 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
325 "This plugin test the SMTP service on the specified host.\n\n");
326 print_usage ();
327 printf
328 ("\nOptions:\n"
329 " -H, --hostname=STRING or IPADDRESS\n"
330 " Check server on the indicated host\n"
331 " -p, --port=INTEGER\n"
332 " Make connection on the indicated port (default: %d)\n"
333 " -e, --expect=STRING\n"
334 " String to expect in first line of server response (default: %s)\n"
335 " -w, --warning=INTEGER\n"
336 " Seconds necessary to result in a warning status\n"
337 " -c, --critical=INTEGER\n"
338 " Seconds necessary to result in a critical status\n"
339 " -t, --timeout=INTEGER\n"
340 " Seconds before connection attempt times out (default: %d)\n"
341 " -v, --verbose\n"
342 " Print extra information (command-line use only)\n"
343 " -h, --help\n"
344 " Print detailed help screen\n"
345 " -V, --version\n"
346 " Print version information\n\n",
347 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
348 support ();
349}
350
351
352
353
354
355void
356print_usage (void)
357{
358 printf
359 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n"
360 " %s --help\n"
361 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
362}