~ubuntu-branches/ubuntu/precise/nagios-plugins/precise-proposed

« back to all changes in this revision

Viewing changes to plugins/check_smtp.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2004-06-15 15:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20040615153748-pq7702qdzghqfcns
Tags: upstream-1.3.1.0
ImportĀ upstreamĀ versionĀ 1.3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: check_smtp.c,v 1.9.2.2 2003/03/07 07:15:40 kdebisschop Exp $
 
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
const char *progname = "check_smtp";
 
43
 
 
44
#define SMTP_PORT       25
 
45
#define SMTP_EXPECT     "220"
 
46
#define SMTP_HELO "HELO "
 
47
 
 
48
/* sendmail will syslog a "NOQUEUE" error if session does not attempt
 
49
 * to do something useful. This can be prevented by giving a command
 
50
 * even if syntax is illegal (MAIL requires a FROM:<...> argument)
 
51
 * You can disable sending DUMMYCMD by undefining SMTP_USE_DUMMYCMD.
 
52
 *
 
53
 * According to rfc821 you can include a null reversepath in the from command
 
54
 * - but a log message is generated on the smtp server.
 
55
 *
 
56
 * Use the -f option to provide a FROM address
 
57
 */
 
58
  
 
59
#define SMTP_DUMMYCMD  "MAIL "
 
60
#define SMTP_USE_DUMMYCMD 1
 
61
#define SMTP_QUIT       "QUIT\r\n"
 
62
 
 
63
int process_arguments (int, char **);
 
64
int validate_arguments (void);
 
65
void print_help (void);
 
66
void print_usage (void);
 
67
 
 
68
int server_port = SMTP_PORT;
 
69
char *server_address = NULL;
 
70
char *server_expect = NULL;
 
71
char *from_arg = " ";
 
72
int warning_time = 0;
 
73
int check_warning_time = FALSE;
 
74
int critical_time = 0;
 
75
int check_critical_time = FALSE;
 
76
int verbose = FALSE;
 
77
 
 
78
int
 
79
main (int argc, char **argv)
 
80
{
 
81
        int sd;
 
82
        int result = STATE_UNKNOWN;
 
83
        char buffer[MAX_INPUT_BUFFER] = "";
 
84
        char *from_str = NULL;
 
85
        char *helocmd = NULL;
 
86
 
 
87
        if (process_arguments (argc, argv) != OK)
 
88
                usage ("Invalid command arguments supplied\n");
 
89
 
 
90
        /* initialize the HELO command with the localhostname */
 
91
#ifndef HOST_MAX_BYTES
 
92
#define HOST_MAX_BYTES 255
 
93
#endif
 
94
        helocmd = malloc (HOST_MAX_BYTES);
 
95
        gethostname(helocmd, HOST_MAX_BYTES);
 
96
        asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
 
97
 
 
98
        /* initialize the MAIL command with optional FROM command  */
 
99
        asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n");
 
100
 
 
101
        if (verbose == TRUE)
 
102
                printf ("FROMCMD: %s\n", from_str);
 
103
        
 
104
        /* initialize alarm signal handling */
 
105
        signal (SIGALRM, socket_timeout_alarm_handler);
 
106
 
 
107
        /* set socket timeout */
 
108
        alarm (socket_timeout);
 
109
 
 
110
        /* try to connect to the host at the given port number */
 
111
        time (&start_time);
 
112
        result = my_tcp_connect (server_address, server_port, &sd);
 
113
 
 
114
        /* we connected, so close connection before exiting */
 
115
        if (result == STATE_OK) {
 
116
 
 
117
                /* watch for the SMTP connection string and */
 
118
                /* return a WARNING status if we couldn't read any data */
 
119
                if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
 
120
                        printf ("recv() failed\n");
 
121
                        result = STATE_WARNING;
 
122
                }
 
123
                else {
 
124
                        /* strip the buffer of carriage returns */
 
125
                        strip (buffer);
 
126
                        /* make sure we find the response we are looking for */
 
127
                        if (!strstr (buffer, server_expect)) {
 
128
                                if (server_port == SMTP_PORT)
 
129
                                        printf ("Invalid SMTP response received from host\n");
 
130
                                else
 
131
                                        printf ("Invalid SMTP response received from host on port %d\n",
 
132
                                                                        server_port);
 
133
                                result = STATE_WARNING;
 
134
                        }
 
135
                }
 
136
 
 
137
                /* send the HELO command */
 
138
                send(sd, helocmd, strlen(helocmd), 0);
 
139
 
 
140
                /* allow for response to helo command to reach us */
 
141
                recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
 
142
                                
 
143
#ifdef SMTP_USE_DUMMYCMD
 
144
                send(sd, from_str, strlen(from_str), 0);
 
145
 
 
146
                /* allow for response to DUMMYCMD to reach us */
 
147
                recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
 
148
 
 
149
                if (verbose == TRUE) 
 
150
                        printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
 
151
#endif /* SMTP_USE_DUMMYCMD */
 
152
 
 
153
                /* tell the server we're done */
 
154
                send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
 
155
 
 
156
                /* finally close the connection */
 
157
                close (sd);
 
158
        }
 
159
 
 
160
        /* reset the alarm */
 
161
        alarm (0);
 
162
 
 
163
        time (&end_time);
 
164
 
 
165
        if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
 
166
                result = STATE_CRITICAL;
 
167
        else if (check_warning_time == TRUE && (end_time - start_time) > warning_time)
 
168
                result = STATE_WARNING;
 
169
 
 
170
        if (verbose == TRUE)
 
171
                printf ("SMTP %s - %d sec. response time, %s\n",
 
172
                                                state_text (result), (int) (end_time - start_time), buffer);
 
173
        else
 
174
                printf ("SMTP %s - %d second response time\n", state_text (result), (int) (end_time - start_time));
 
175
 
 
176
        return result;
 
177
}
 
178
 
 
179
 
 
180
 
 
181
 
 
182
 
 
183
 
 
184
/* process command-line arguments */
 
185
int
 
186
process_arguments (int argc, char **argv)
 
187
{
 
188
        int c;
 
189
 
 
190
#ifdef HAVE_GETOPT_H
 
191
        int option_index = 0;
 
192
        static struct option long_options[] = {
 
193
                {"hostname", required_argument, 0, 'H'},
 
194
                {"expect", required_argument, 0, 'e'},
 
195
                {"critical", required_argument, 0, 'c'},
 
196
                {"warning", required_argument, 0, 'w'},
 
197
                {"port", required_argument, 0, 'p'},
 
198
                {"from", required_argument, 0, 'f'},
 
199
                {"verbose", no_argument, 0, 'v'},
 
200
                {"version", no_argument, 0, 'V'},
 
201
                {"help", no_argument, 0, 'h'},
 
202
                {0, 0, 0, 0}
 
203
        };
 
204
#endif
 
205
 
 
206
        if (argc < 2)
 
207
                return ERROR;
 
208
 
 
209
        for (c = 1; c < argc; c++) {
 
210
                if (strcmp ("-to", argv[c]) == 0)
 
211
                        strcpy (argv[c], "-t");
 
212
                else if (strcmp ("-wt", argv[c]) == 0)
 
213
                        strcpy (argv[c], "-w");
 
214
                else if (strcmp ("-ct", argv[c]) == 0)
 
215
                        strcpy (argv[c], "-c");
 
216
        }
 
217
 
 
218
        while (1) {
 
219
#ifdef HAVE_GETOPT_H
 
220
                c =
 
221
                        getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
 
222
                                                                         &option_index);
 
223
#else
 
224
                c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:");
 
225
#endif
 
226
                if (c == -1 || c == EOF)
 
227
                        break;
 
228
 
 
229
                switch (c) {
 
230
                case 'H':                                                                       /* hostname */
 
231
                        if (is_host (optarg)) {
 
232
                                server_address = optarg;
 
233
                        }
 
234
                        else {
 
235
                                usage ("Invalid host name\n");
 
236
                        }
 
237
                        break;
 
238
                case 'p':                                                                       /* port */
 
239
                        if (is_intpos (optarg)) {
 
240
                                server_port = atoi (optarg);
 
241
                        }
 
242
                        else {
 
243
                                usage ("Server port must be a positive integer\n");
 
244
                        }
 
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':                                                                       /* critical time threshold */
 
253
                        if (is_intnonneg (optarg)) {
 
254
                                critical_time = atoi (optarg);
 
255
                                check_critical_time = TRUE;
 
256
                        }
 
257
                        else {
 
258
                                usage ("Critical time must be a nonnegative integer\n");
 
259
                        }
 
260
                        break;
 
261
                case 'w':                                                                       /* warning time threshold */
 
262
                        if (is_intnonneg (optarg)) {
 
263
                                warning_time = atoi (optarg);
 
264
                                check_warning_time = TRUE;
 
265
                        }
 
266
                        else {
 
267
                                usage ("Warning time must be a nonnegative integer\n");
 
268
                        }
 
269
                        break;
 
270
                case 'v':                                                                       /* verbose */
 
271
                        verbose = TRUE;
 
272
                        break;
 
273
                case 't':                                                                       /* timeout */
 
274
                        if (is_intnonneg (optarg)) {
 
275
                                socket_timeout = atoi (optarg);
 
276
                        }
 
277
                        else {
 
278
                                usage ("Time interval must be a nonnegative integer\n");
 
279
                        }
 
280
                        break;
 
281
                case 'V':                                                                       /* version */
 
282
                        print_revision (progname, "$Revision: 1.9.2.2 $");
 
283
                        exit (STATE_OK);
 
284
                case 'h':                                                                       /* help */
 
285
                        print_help ();
 
286
                        exit (STATE_OK);
 
287
                case '?':                                                                       /* help */
 
288
                        usage ("Invalid argument\n");
 
289
                }
 
290
        }
 
291
 
 
292
        c = optind;
 
293
        if (server_address == NULL) {
 
294
                if (argv[c]) {
 
295
                        if (is_host (argv[c]))
 
296
                                server_address = argv[c];
 
297
                        else
 
298
                                usage ("Invalid host name");
 
299
                }
 
300
                else {
 
301
                        asprintf (&server_address, "127.0.0.1");
 
302
                }
 
303
        }
 
304
 
 
305
        if (server_expect == NULL)
 
306
                asprintf (&server_expect, SMTP_EXPECT);
 
307
 
 
308
        return validate_arguments ();
 
309
}
 
310
 
 
311
 
 
312
 
 
313
 
 
314
 
 
315
int
 
316
validate_arguments (void)
 
317
{
 
318
        return OK;
 
319
}
 
320
 
 
321
 
 
322
 
 
323
 
 
324
 
 
325
void
 
326
print_help (void)
 
327
{
 
328
        print_revision (progname, "$Revision: 1.9.2.2 $");
 
329
        printf
 
330
                ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
 
331
                 "This plugin test the SMTP service on the specified host.\n\n");
 
332
        print_usage ();
 
333
        printf
 
334
                ("\nOptions:\n"
 
335
                 " -H, --hostname=STRING or IPADDRESS\n"
 
336
                 "   Check server on the indicated host\n"
 
337
                 " -p, --port=INTEGER\n"
 
338
                 "   Make connection on the indicated port (default: %d)\n"
 
339
                 " -e, --expect=STRING\n"
 
340
                 "   String to expect in first line of server response (default: %s)\n"
 
341
                 " -f, --from=STRING\n"
 
342
                 "   from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
 
343
                 " -w, --warning=INTEGER\n"
 
344
                 "   Seconds necessary to result in a warning status\n"
 
345
                 " -c, --critical=INTEGER\n"
 
346
                 "   Seconds necessary to result in a critical status\n"
 
347
                 " -t, --timeout=INTEGER\n"
 
348
                 "   Seconds before connection attempt times out (default: %d)\n"
 
349
                 " -v, --verbose\n"
 
350
                 "   Print extra information (command-line use only)\n"
 
351
                 " -h, --help\n"
 
352
                 "   Print detailed help screen\n"
 
353
                 " -V, --version\n"
 
354
                 "   Print version information\n\n",
 
355
                 SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
 
356
        support ();
 
357
}
 
358
 
 
359
 
 
360
 
 
361
 
 
362
 
 
363
void
 
364
print_usage (void)
 
365
{
 
366
        printf
 
367
                ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
 
368
                 "       %s --help\n"
 
369
                 "       %s --version\n", progname, progname, progname);
 
370
}