Code

- compartmentalized ssl code into seperate sslutils.c
[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  $Id$
18  
19 *****************************************************************************/
21 const char *progname = "check_udp";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
30 int process_arguments (int, char **);
31 void print_help (void);
32 void print_usage (void);
34 int warning_time = 0;
35 int check_warning_time = FALSE;
36 int critical_time = 0;
37 int check_critical_time = FALSE;
38 int verbose = FALSE;
39 int server_port = 0;
40 char *server_address = NULL;
41 char *server_expect = NULL;
42 char *server_send;
44 int
45 main (int argc, char **argv)
46 {
47         int result = STATE_UNKNOWN;
48         char recv_buffer[MAX_INPUT_BUFFER];
50         setlocale (LC_ALL, "");
51         bindtextdomain (PACKAGE, LOCALEDIR);
52         textdomain (PACKAGE);
54         if (process_arguments (argc, argv) == ERROR)
55                 usage4 (_("Could not parse arguments"));
57         /* initialize alarm signal handling */
58         signal (SIGALRM, socket_timeout_alarm_handler);
60         /* set socket timeout */
61         alarm (socket_timeout);
63         time (&start_time);
64         result = process_udp_request (server_address, server_port, server_send,
65                         recv_buffer, MAX_INPUT_BUFFER - 1);
66                         
67         time (&end_time);
69         if (result != STATE_OK) {
70                 printf (_("No response from host on port %d\n"), server_port);
71                 result = STATE_CRITICAL;
72         }
74         else {
76                 /* check to see if we got the response we wanted */
77                 if (server_expect) {
78                         if (!strstr (recv_buffer, server_expect)) {
79                                 printf (_("Invalid response received from host on port %d\n"),
80                                                                 server_port);
81                                 result = STATE_CRITICAL;
82                         }
83                 }
84         }
86         /* we connected, so close connection before exiting */
87         if (result == STATE_OK) {
89                 if (check_critical_time == TRUE
90                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
91                 else if (check_warning_time == TRUE
92                                                  && (end_time - start_time) > warning_time) result =
93                                 STATE_WARNING;
95                 printf (_("Connection %s on port %d - %d second response time\n"),
96                                                 (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
97                                                 (int) (end_time - start_time));
98         }
100         /* reset the alarm */
101         alarm (0);
103         return result;
108 /* process command-line arguments */
109 int
110 process_arguments (int argc, char **argv)
112         int c;
114         int option = 0;
115         static struct option longopts[] = {
116                 {"hostname", required_argument, 0, 'H'},
117                 {"critical", required_argument, 0, 'c'},
118                 {"warning", required_argument, 0, 'w'},
119                 {"timeout", required_argument, 0, 't'},
120                 {"port", required_argument, 0, 'p'},
121                 {"expect", required_argument, 0, 'e'},
122                 {"send", required_argument, 0, 's'},
123                 {"verbose", no_argument, 0, 'v'},
124                 {"version", no_argument, 0, 'V'},
125                 {"help", no_argument, 0, 'h'},
126                 {0, 0, 0, 0}
127         };
129         if (argc < 2)
130                 usage ("\n");
132         for (c = 1; c < argc; c++) {
133                 if (strcmp ("-to", argv[c]) == 0)
134                         strcpy (argv[c], "-t");
135                 else if (strcmp ("-wt", argv[c]) == 0)
136                         strcpy (argv[c], "-w");
137                 else if (strcmp ("-ct", argv[c]) == 0)
138                         strcpy (argv[c], "-c");
139         }
141         while (1) {
142                 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", longopts, &option);
144                 if (c == -1 || c == EOF || c == 1)
145                         break;
147                 switch (c) {
148                 case '?':                                                                       /* print short usage statement if args not parsable */
149                         usage2 (_("Unknown argument"), optarg);
150                 case 'h':                                                                       /* help */
151                         print_help ();
152                         exit (STATE_OK);
153                 case 'V':                                                                       /* version */
154                         print_revision (progname, revision);
155                         exit (STATE_OK);
156                 case 'v':                                                                       /* verbose mode */
157                         verbose = TRUE;
158                         break;
159                 case 'H':                                                                       /* hostname */
160                         if (is_host (optarg) == FALSE)
161                                 usage2 (_("Invalid hostname/address"), optarg);
162                         server_address = optarg;
163                         break;
164                 case 'c':                                                                       /* critical */
165                         if (!is_intnonneg (optarg))
166                                 usage4 (_("Critical threshold must be a positive integer"));
167                         else
168                                 critical_time = atoi (optarg);
169                         check_critical_time = TRUE;
170                         break;
171                 case 'w':                                                                       /* warning */
172                         if (!is_intnonneg (optarg))
173                                 usage4 (_("Warning threshold must be a positive integer"));
174                         else
175                                 warning_time = atoi (optarg);
176                         check_warning_time = TRUE;
177                         break;
178                 case 't':                                                                       /* timeout */
179                         if (!is_intnonneg (optarg))
180                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
181                         else
182                                 socket_timeout = atoi (optarg);
183                         break;
184                 case 'p':                                                                       /* port */
185                         if (!is_intnonneg (optarg))
186                                 usage4 (_("Port must be a positive integer"));
187                         else
188                                 server_port = atoi (optarg);
189                         break;
190                 case 'e':                                                                       /* expect */
191                         server_expect = optarg;
192                         break;
193                 case 's':                                                                       /* send */
194                         server_send = optarg;
195                         break;
196                 }
197         }
199         c = optind;
200         if (server_address == NULL && c < argc && argv[c]) {
201                 if (is_host (argv[c]) == FALSE)
202                         usage2 (_("Invalid hostname/address"), optarg);
203                 server_address = argv[c++];
204         }
206         if (server_address == NULL)
207                 usage4 (_("Hostname was not supplied"));
209         if (server_send == NULL)
210                 server_send = strdup("");
212         return c;
217 void
218 print_help (void)
220         print_revision (progname, revision);
222         printf ("Copyright (c) 1999 Ethan Galstad\n");
223         printf (COPYRIGHT, copyright, email);
225         printf (_("\
226         This plugin tests an UDP connection with the specified host.\n\n"));
228         print_usage ();
230         printf (_(UT_HELP_VRSN));
232         printf (_(UT_HOST_PORT), 'p', "none");
234         printf (_("\
235  -e, --expect=STRING <optional>\n\
236     String to expect in first line of server response\n\
237  -s, --send=STRING <optional>\n\
238     String to send to the server when initiating the connection\n"));
240         printf (_(UT_WARN_CRIT));
242         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
244         printf (_(UT_VERBOSE));
246         printf (_("\
247 This plugin will attempt to connect to the specified port on the host.\n\
248 Successful connects return STATE_OK, refusals and timeouts return\n\
249 STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
251         printf(_(UT_SUPPORT));
255 /* Original Command line: 
256    check_udp <host_address> [-p port] [-s send] [-e expect] \
257    [-wt warn_time] [-ct crit_time] [-to to_sec] */
258 void
259 print_usage (void)
261         printf ("\
262 Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
263                   [-e expect] [-s send] [-t to_sec] [-v]\n", progname);