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