Code

the last round of pedantic compiler warnings
[nagiosplug.git] / plugins / check_udp.c
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_udp";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2002";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 int process_arguments (int, char **);
29 void print_help (void);
30 void print_usage (void);
32 int warning_time = 0;
33 int check_warning_time = FALSE;
34 int critical_time = 0;
35 int check_critical_time = FALSE;
36 int verbose = FALSE;
37 int server_port = 0;
38 char *server_address = NULL;
39 char *server_expect = NULL;
40 char *server_send;
42 int
43 main (int argc, char **argv)
44 {
45         int result;
46         char recv_buffer[MAX_INPUT_BUFFER];
48         if (process_arguments (argc, argv) == ERROR)
49                 usage ("\n");
51         /* initialize alarm signal handling */
52         signal (SIGALRM, socket_timeout_alarm_handler);
54         /* set socket timeout */
55         alarm (socket_timeout);
57         time (&start_time);
58         result = process_udp_request (server_address, server_port, server_send,
59                         recv_buffer, MAX_INPUT_BUFFER - 1);
60         time (&end_time);
62         if (result != STATE_OK) {
63                 printf ("No response from host on port %d\n", server_port);
64                 result = STATE_CRITICAL;
65         }
67         else {
69                 /* check to see if we got the response we wanted */
70                 if (server_expect) {
71                         if (!strstr (recv_buffer, server_expect)) {
72                                 printf ("Invalid response received from host on port %d\n",
73                                                                 server_port);
74                                 result = STATE_CRITICAL;
75                         }
76                 }
77         }
79         /* we connected, so close connection before exiting */
80         if (result == STATE_OK) {
82                 if (check_critical_time == TRUE
83                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
84                 else if (check_warning_time == TRUE
85                                                  && (end_time - start_time) > warning_time) result =
86                                 STATE_WARNING;
88                 printf (_("Connection %s on port %d - %d second response time\n"),
89                                                 (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
90                                                 (int) (end_time - start_time));
91         }
93         /* reset the alarm */
94         alarm (0);
96         return result;
97 }
102 /* process command-line arguments */
103 int
104 process_arguments (int argc, char **argv)
106         int c;
108         int option = 0;
109         static struct option longopts[] = {
110                 {"hostname", required_argument, 0, 'H'},
111                 {"critical", required_argument, 0, 'c'},
112                 {"warning", required_argument, 0, 'w'},
113                 {"timeout", required_argument, 0, 't'},
114                 {"port", required_argument, 0, 'p'},
115                 {"expect", required_argument, 0, 'e'},
116                 {"send", required_argument, 0, 's'},
117                 {"verbose", no_argument, 0, 'v'},
118                 {"version", no_argument, 0, 'V'},
119                 {"help", no_argument, 0, 'h'},
120                 {0, 0, 0, 0}
121         };
123         if (argc < 2)
124                 usage ("\n");
126         for (c = 1; c < argc; c++) {
127                 if (strcmp ("-to", argv[c]) == 0)
128                         strcpy (argv[c], "-t");
129                 else if (strcmp ("-wt", argv[c]) == 0)
130                         strcpy (argv[c], "-w");
131                 else if (strcmp ("-ct", argv[c]) == 0)
132                         strcpy (argv[c], "-c");
133         }
135         while (1) {
136                 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", longopts, &option);
138                 if (c == -1 || c == EOF || c == 1)
139                         break;
141                 switch (c) {
142                 case '?':                                                                       /* print short usage statement if args not parsable */
143                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
144                         print_usage ();
145                         exit (STATE_UNKNOWN);
146                 case 'h':                                                                       /* help */
147                         print_help ();
148                         exit (STATE_OK);
149                 case 'V':                                                                       /* version */
150                         print_revision (progname, revision);
151                         exit (STATE_OK);
152                 case 'v':                                                                       /* verbose mode */
153                         verbose = TRUE;
154                         break;
155                 case 'H':                                                                       /* hostname */
156                         if (is_host (optarg) == FALSE)
157                                 usage (_("Invalid host name/address\n"));
158                         server_address = optarg;
159                         break;
160                 case 'c':                                                                       /* critical */
161                         if (!is_intnonneg (optarg))
162                                 usage (_("Critical threshold must be a nonnegative integer\n"));
163                         else
164                                 critical_time = atoi (optarg);
165                         check_critical_time = TRUE;
166                         break;
167                 case 'w':                                                                       /* warning */
168                         if (!is_intnonneg (optarg))
169                                 usage (_("Warning threshold must be a nonnegative integer\n"));
170                         else
171                                 warning_time = atoi (optarg);
172                         check_warning_time = TRUE;
173                         break;
174                 case 't':                                                                       /* timeout */
175                         if (!is_intnonneg (optarg))
176                                 usage (_("Timeout interval must be a nonnegative integer\n"));
177                         else
178                                 socket_timeout = atoi (optarg);
179                         break;
180                 case 'p':                                                                       /* port */
181                         if (!is_intnonneg (optarg))
182                                 usage (_("Server port must be a nonnegative integer\n"));
183                         else
184                                 server_port = atoi (optarg);
185                         break;
186                 case 'e':                                                                       /* expect */
187                         server_expect = optarg;
188                         break;
189                 case 's':                                                                       /* send */
190                         server_send = optarg;
191                         break;
192                 }
193         }
195         c = optind;
196         if (server_address == NULL && c < argc && argv[c]) {
197                 if (is_host (argv[c]) == FALSE)
198                         usage (_("Invalid host name/address\n"));
199                 server_address = argv[c++];
200         }
202         if (server_address == NULL)
203                 usage (_("Host name was not supplied\n"));
205         if (server_send == NULL)
206                 server_send = strdup("");
208         return c;
215 \f
216 void
217 print_help (void)
219         print_revision (progname, revision);
221         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
222         printf (_(COPYRIGHT), copyright, email);
224         printf (_("\
225 This plugin tests an UDP connection with the specified host.\n\n"));
227         print_usage ();
229         printf (_(UT_HELP_VRSN));
231         printf (_(UT_HOST_PORT), 'p', "none");
233         printf (_("\
234  -e, --expect=STRING <optional>\n\
235     String to expect in first line of server response\n\
236  -s, --send=STRING <optional>\n\
237     String to send to the server when initiating the connection\n"));
239         printf (_(UT_WARN_CRIT));
241         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
243         printf (_(UT_VERBOSE));
245         printf (_("\
246 This plugin will attempt to connect to the specified port on the host.\n\
247 Successful connects return STATE_OK, refusals and timeouts return\n\
248 STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
250         printf(_(UT_SUPPORT));
256 /* Original Command line: 
257    check_udp <host_address> [-p port] [-s send] [-e expect] \
258    [-wt warn_time] [-ct crit_time] [-to to_sec] */
259 void
260 print_usage (void)
262         printf (_("\
263 Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
264     [-e expect] [-s send] [-t to_sec] [-v]\n"), progname);
265         printf (_(UT_HLP_VRS), progname, progname);