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

« back to all changes in this revision

Viewing changes to plugins/check_udp.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_UDP.C
 
4
*
 
5
* Program: UDP port plugin for Nagios
 
6
* License: GPL
 
7
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
 
8
*
 
9
* Last Modified: $Date: 2003/03/07 06:40:33 $
 
10
*
 
11
* Command line: CHECK_UDP <host_address> [-p port] [-s send] [-e expect] \
 
12
*                          [-wt warn_time] [-ct crit_time] [-to to_sec]
 
13
*
 
14
* Description:
 
15
*
 
16
* This plugin will attempt to connect to the specified port
 
17
* on the host.  Successul connects return STATE_OK, refusals
 
18
* and timeouts return STATE_CRITICAL, other errors return
 
19
* STATE_UNKNOWN.
 
20
*
 
21
* License Information:
 
22
*
 
23
* This program is free software; you can redistribute it and/or modify
 
24
* it under the terms of the GNU General Public License as published by
 
25
* the Free Software Foundation; either version 2 of the License, or
 
26
* (at your option) any later version.
 
27
*
 
28
* This program is distributed in the hope that it will be useful,
 
29
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
30
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
31
* GNU General Public License for more details.
 
32
*
 
33
* You should have received a copy of the GNU General Public License
 
34
* along with this program; if not, write to the Free Software
 
35
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
36
*
 
37
*****************************************************************************/
 
38
 
 
39
#include "config.h"
 
40
#include "common.h"
 
41
#include "netutils.h"
 
42
#include "utils.h"
 
43
 
 
44
const char *progname = "check_udp";
 
45
 
 
46
int warning_time = 0;
 
47
int check_warning_time = FALSE;
 
48
int critical_time = 0;
 
49
int check_critical_time = FALSE;
 
50
 
 
51
int process_arguments (int, char **);
 
52
void print_usage (void);
 
53
void print_help (void);
 
54
 
 
55
int verbose = FALSE;
 
56
int server_port = 0;
 
57
char *server_address = NULL;
 
58
char *server_expect = NULL;
 
59
char *server_send = "";
 
60
 
 
61
int
 
62
main (int argc, char **argv)
 
63
{
 
64
        int result;
 
65
        char recv_buffer[MAX_INPUT_BUFFER];
 
66
 
 
67
        if (process_arguments (argc, argv) == ERROR)
 
68
                usage ("\n");
 
69
 
 
70
        /* initialize alarm signal handling */
 
71
        signal (SIGALRM, socket_timeout_alarm_handler);
 
72
 
 
73
        /* set socket timeout */
 
74
        alarm (socket_timeout);
 
75
 
 
76
        time (&start_time);
 
77
        result =
 
78
                process_udp_request (server_address, server_port, server_send,
 
79
                                                                                                 recv_buffer, MAX_INPUT_BUFFER - 1);
 
80
        time (&end_time);
 
81
 
 
82
        if (result != STATE_OK) {
 
83
                printf ("No response from host on port %d\n", server_port);
 
84
                result = STATE_CRITICAL;
 
85
        }
 
86
 
 
87
        else {
 
88
 
 
89
                /* check to see if we got the response we wanted */
 
90
                if (server_expect) {
 
91
                        if (!strstr (recv_buffer, server_expect)) {
 
92
                                printf ("Invalid response received from host on port %d\n",
 
93
                                                                server_port);
 
94
                                result = STATE_CRITICAL;
 
95
                        }
 
96
                }
 
97
        }
 
98
 
 
99
        /* we connected, so close connection before exiting */
 
100
        if (result == STATE_OK) {
 
101
 
 
102
                if (check_critical_time == TRUE
 
103
                                && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
 
104
                else if (check_warning_time == TRUE
 
105
                                                 && (end_time - start_time) > warning_time) result =
 
106
                                STATE_WARNING;
 
107
 
 
108
                printf ("Connection %s on port %d - %d second response time\n",
 
109
                                                (result == STATE_OK) ? "accepted" : "problem", server_port,
 
110
                                                (int) (end_time - start_time));
 
111
        }
 
112
 
 
113
        /* reset the alarm */
 
114
        alarm (0);
 
115
 
 
116
        return result;
 
117
}
 
118
 
 
119
 
 
120
 
 
121
 
 
122
/* process command-line arguments */
 
123
int
 
124
process_arguments (int argc, char **argv)
 
125
{
 
126
        int c;
 
127
 
 
128
#ifdef HAVE_GETOPT_H
 
129
        int option_index = 0;
 
130
        static struct option long_options[] = {
 
131
                {"hostname", required_argument, 0, 'H'},
 
132
                {"critical", required_argument, 0, 'c'},
 
133
                {"warning", required_argument, 0, 'w'},
 
134
                {"timeout", required_argument, 0, 't'},
 
135
                {"port", required_argument, 0, 'p'},
 
136
                {"expect", required_argument, 0, 'e'},
 
137
                {"send", required_argument, 0, 's'},
 
138
                {"verbose", no_argument, 0, 'v'},
 
139
                {"version", no_argument, 0, 'V'},
 
140
                {"help", no_argument, 0, 'h'},
 
141
                {0, 0, 0, 0}
 
142
        };
 
143
#endif
 
144
 
 
145
        if (argc < 2)
 
146
                usage ("\n");
 
147
 
 
148
        for (c = 1; c < argc; c++) {
 
149
                if (strcmp ("-to", argv[c]) == 0)
 
150
                        strcpy (argv[c], "-t");
 
151
                else if (strcmp ("-wt", argv[c]) == 0)
 
152
                        strcpy (argv[c], "-w");
 
153
                else if (strcmp ("-ct", argv[c]) == 0)
 
154
                        strcpy (argv[c], "-c");
 
155
        }
 
156
 
 
157
        while (1) {
 
158
#ifdef HAVE_GETOPT_H
 
159
                c =     getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
 
160
#else
 
161
                c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
 
162
#endif
 
163
 
 
164
                if (c == -1 || c == EOF || c == 1)
 
165
                        break;
 
166
 
 
167
                switch (c) {
 
168
                case '?':                                                                       /* print short usage statement if args not parsable */
 
169
                        printf ("%s: Unknown argument: %s\n\n", progname, optarg);
 
170
                        print_usage ();
 
171
                        exit (STATE_UNKNOWN);
 
172
                case 'h':                                                                       /* help */
 
173
                        print_help ();
 
174
                        exit (STATE_OK);
 
175
                case 'V':                                                                       /* version */
 
176
                        print_revision (progname, "$Revision: 1.4.2.1 $");
 
177
                        exit (STATE_OK);
 
178
                case 'v':                                                                       /* verbose mode */
 
179
                        verbose = TRUE;
 
180
                        break;
 
181
                case 'H':                                                                       /* hostname */
 
182
                        if (is_host (optarg) == FALSE)
 
183
                                usage ("Invalid host name/address\n");
 
184
                        server_address = optarg;
 
185
                        break;
 
186
                case 'c':                                                                       /* critical */
 
187
                        if (!is_intnonneg (optarg))
 
188
                                usage ("Critical threshold must be a nonnegative integer\n");
 
189
                        critical_time = atoi (optarg);
 
190
                        check_critical_time = TRUE;
 
191
                        break;
 
192
                case 'w':                                                                       /* warning */
 
193
                        if (!is_intnonneg (optarg))
 
194
                                usage ("Warning threshold must be a nonnegative integer\n");
 
195
                        warning_time = atoi (optarg);
 
196
                        check_warning_time = TRUE;
 
197
                        break;
 
198
                case 't':                                                                       /* timeout */
 
199
                        if (!is_intnonneg (optarg))
 
200
                                usage ("Timeout interval must be a nonnegative integer\n");
 
201
                        socket_timeout = atoi (optarg);
 
202
                        break;
 
203
                case 'p':                                                                       /* port */
 
204
                        if (!is_intnonneg (optarg))
 
205
                                usage ("Serevr port must be a nonnegative integer\n");
 
206
                        server_port = atoi (optarg);
 
207
                        break;
 
208
                case 'e':                                                                       /* expect */
 
209
                        server_expect = optarg;
 
210
                        break;
 
211
                case 's':                                                                       /* send */
 
212
                        server_send = optarg;
 
213
                        break;
 
214
                }
 
215
        }
 
216
 
 
217
        c = optind;
 
218
        if (server_address == NULL && c < argc && argv[c]) {
 
219
                if (is_host (argv[c]) == FALSE)
 
220
                        usage ("Invalid host name/address\n");
 
221
                server_address = argv[c++];
 
222
        }
 
223
 
 
224
        if (server_address == NULL)
 
225
                usage ("Host name was not supplied\n");
 
226
 
 
227
        return c;
 
228
}
 
229
 
 
230
 
 
231
 
 
232
 
 
233
 
 
234
void
 
235
print_usage (void)
 
236
{
 
237
        printf
 
238
                ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
 
239
                 "         [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
 
240
}
 
241
 
 
242
 
 
243
 
 
244
 
 
245
 
 
246
void
 
247
print_help (void)
 
248
{
 
249
        print_revision (progname, "$Revision: 1.4.2.1 $");
 
250
        printf
 
251
                ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
 
252
                 "This plugin tests an UDP connection with the specified host.\n\n");
 
253
        print_usage ();
 
254
        printf
 
255
                ("Options:\n"
 
256
                 " -H, --hostname=ADDRESS\n"
 
257
                 "    Host name argument for servers using host headers (use numeric\n"
 
258
                 "    address if possible to bypass DNS lookup).\n"
 
259
                 " -p, --port=INTEGER\n"
 
260
                 "    Port number\n"
 
261
                 " -e, --expect=STRING <optional>\n"
 
262
                 "    String to expect in first line of server response\n"
 
263
                 " -s, --send=STRING <optional>\n"
 
264
                 "    String to send to the server when initiating the connection\n"
 
265
                 " -w, --warning=INTEGER <optional>\n"
 
266
                 "    Response time to result in warning status (seconds)\n"
 
267
                 " -c, --critical=INTEGER <optional>\n"
 
268
                 "    Response time to result in critical status (seconds)\n"
 
269
                 " -t, --timeout=INTEGER <optional>\n"
 
270
                 "    Seconds before connection times out (default: %d)\n"
 
271
                 " -v, --verbose <optional>\n"
 
272
                 "    Show details for command-line debugging (do not use with nagios server)\n"
 
273
                 " -h, --help\n"
 
274
                 "    Print detailed help screen and exit\n"
 
275
                 " -V, --version\n"
 
276
                 "    Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
 
277
}