summaryrefslogtreecommitdiffstats
path: root/plugins/check_real.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_real.c')
-rw-r--r--plugins/check_real.c761
1 files changed, 416 insertions, 345 deletions
diff --git a/plugins/check_real.c b/plugins/check_real.c
index 15e035b6..15c8a20c 100644
--- a/plugins/check_real.c
+++ b/plugins/check_real.c
@@ -1,454 +1,525 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_real plugin 3 * Monitoring check_real plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2000-2007 Monitoring Plugins Development Team 6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_real plugin 10 * This file contains the check_real plugin
11* 11 *
12* This plugin tests the REAL service on the specified host. 12 * This plugin tests the REAL service on the specified host.
13* 13 *
14* 14 *
15* This program is free software: you can redistribute it and/or modify 15 * This program is free software: you can redistribute it and/or modify
16* it under the terms of the GNU General Public License as published by 16 * it under the terms of the GNU General Public License as published by
17* the Free Software Foundation, either version 3 of the License, or 17 * the Free Software Foundation, either version 3 of the License, or
18* (at your option) any later version. 18 * (at your option) any later version.
19* 19 *
20* This program is distributed in the hope that it will be useful, 20 * This program is distributed in the hope that it will be useful,
21* but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23* GNU General Public License for more details. 23 * GNU General Public License for more details.
24* 24 *
25* You should have received a copy of the GNU General Public License 25 * You should have received a copy of the GNU General Public License
26* along with this program. If not, see <http://www.gnu.org/licenses/>. 26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27* 27 *
28* 28 *
29*****************************************************************************/ 29 *****************************************************************************/
30 30
31const char *progname = "check_real"; 31#include "output.h"
32const char *copyright = "2000-2007"; 32#include "perfdata.h"
33const char *email = "devel@monitoring-plugins.org"; 33#include "states.h"
34 34#include <stdio.h>
35#include "common.h" 35#include "common.h"
36#include "netutils.h" 36#include "netutils.h"
37#include "thresholds.h"
37#include "utils.h" 38#include "utils.h"
39#include "check_real.d/config.h"
38 40
39enum { 41const char *progname = "check_real";
40 PORT = 554 42const char *copyright = "2000-2024";
41}; 43const char *email = "devel@monitoring-plugins.org";
42 44
43#define EXPECT "RTSP/1." 45#define URL ""
44#define URL ""
45 46
46int process_arguments (int, char **); 47typedef struct {
47int validate_arguments (void); 48 int errorcode;
48void print_help (void); 49 check_real_config config;
49void print_usage (void); 50} check_real_config_wrapper;
51static check_real_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
50 52
51int server_port = PORT; 53static void print_help(void);
52char *server_address; 54void print_usage(void);
53char *host_name;
54char *server_url = NULL;
55char *server_expect;
56int warning_time = 0;
57bool check_warning_time = false;
58int critical_time = 0;
59bool check_critical_time = false;
60bool verbose = false;
61 55
56static bool verbose = false;
62 57
58int main(int argc, char **argv) {
59 setlocale(LC_ALL, "");
60 bindtextdomain(PACKAGE, LOCALEDIR);
61 textdomain(PACKAGE);
63 62
64int 63 /* Parse extra opts if any */
65main (int argc, char **argv) 64 argv = np_extra_opts(&argc, argv, progname);
66{
67 int sd;
68 int result = STATE_UNKNOWN;
69 char buffer[MAX_INPUT_BUFFER];
70 char *status_line = NULL;
71 65
72 setlocale (LC_ALL, ""); 66 check_real_config_wrapper tmp_config = process_arguments(argc, argv);
73 bindtextdomain (PACKAGE, LOCALEDIR); 67 if (tmp_config.errorcode == ERROR) {
74 textdomain (PACKAGE); 68 usage4(_("Could not parse arguments"));
69 }
75 70
76 /* Parse extra opts if any */ 71 const check_real_config config = tmp_config.config;
77 argv=np_extra_opts (&argc, argv, progname);
78 72
79 if (process_arguments (argc, argv) == ERROR) 73 if (config.output_format_is_set) {
80 usage4 (_("Could not parse arguments")); 74 mp_set_format(config.output_format);
75 }
81 76
82 /* initialize alarm signal handling */ 77 /* initialize alarm signal handling */
83 signal (SIGALRM, socket_timeout_alarm_handler); 78 signal(SIGALRM, socket_timeout_alarm_handler);
84 79
85 /* set socket timeout */ 80 /* set socket timeout */
86 alarm (socket_timeout); 81 alarm(socket_timeout);
87 time (&start_time); 82 time_t start_time;
83 time(&start_time);
84
85 mp_check overall = mp_check_init();
86 mp_subcheck sc_connect = mp_subcheck_init();
88 87
89 /* try to connect to the host at the given port number */ 88 /* try to connect to the host at the given port number */
90 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) 89 int socket;
91 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), 90 if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) {
92 server_address, server_port); 91 xasprintf(&sc_connect.output, _("unable to connect to %s on port %d"),
92 config.server_address, config.server_port);
93 sc_connect = mp_set_subcheck_state(sc_connect, STATE_CRITICAL);
94 mp_add_subcheck_to_check(&overall, sc_connect);
95 mp_exit(overall);
96 }
97
98 xasprintf(&sc_connect.output, _("connected to %s on port %d"), config.server_address,
99 config.server_port);
100 sc_connect = mp_set_subcheck_state(sc_connect, STATE_OK);
101 mp_add_subcheck_to_check(&overall, sc_connect);
93 102
94 /* Part I - Server Check */ 103 /* Part I - Server Check */
104 mp_subcheck sc_send = mp_subcheck_init();
95 105
96 /* send the OPTIONS request */ 106 /* send the OPTIONS request */
97 sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port); 107 char buffer[MAX_INPUT_BUFFER];
98 result = send (sd, buffer, strlen (buffer), 0); 108 sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", config.host_name, config.server_port);
109 ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0);
110 if (sent_bytes == -1) {
111 xasprintf(&sc_send.output, _("Sending options to %s failed"), config.host_name);
112 sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL);
113 mp_add_subcheck_to_check(&overall, sc_send);
114 mp_exit(overall);
115 }
99 116
100 /* send the header sync */ 117 /* send the header sync */
101 sprintf (buffer, "CSeq: 1\r\n"); 118 sprintf(buffer, "CSeq: 1\r\n");
102 result = send (sd, buffer, strlen (buffer), 0); 119 sent_bytes = send(socket, buffer, strlen(buffer), 0);
120 if (sent_bytes == -1) {
121 xasprintf(&sc_send.output, _("Sending header sync to %s failed"), config.host_name);
122 sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL);
123 mp_add_subcheck_to_check(&overall, sc_send);
124 mp_exit(overall);
125 }
103 126
104 /* send a newline so the server knows we're done with the request */ 127 /* send a newline so the server knows we're done with the request */
105 sprintf (buffer, "\r\n"); 128 sprintf(buffer, "\r\n");
106 result = send (sd, buffer, strlen (buffer), 0); 129 sent_bytes = send(socket, buffer, strlen(buffer), 0);
130 if (sent_bytes == -1) {
131 xasprintf(&sc_send.output, _("Sending newline to %s failed"), config.host_name);
132 sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL);
133 mp_add_subcheck_to_check(&overall, sc_send);
134 mp_exit(overall);
135 }
107 136
108 /* watch for the REAL connection string */ 137 /* watch for the REAL connection string */
109 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0); 138 ssize_t received_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
110 139
111 /* return a CRITICAL status if we couldn't read any data */ 140 /* return a CRITICAL status if we couldn't read any data */
112 if (result == -1) 141 if (received_bytes == -1) {
113 die (STATE_CRITICAL, _("No data received from %s\n"), host_name); 142 xasprintf(&sc_send.output, _("No data received from %s"), config.host_name);
114 143 sc_send = mp_set_subcheck_state(sc_send, STATE_CRITICAL);
115 /* make sure we find the response we are looking for */ 144 mp_add_subcheck_to_check(&overall, sc_send);
116 if (!strstr (buffer, server_expect)) { 145 mp_exit(overall);
117 if (server_port == PORT)
118 printf ("%s\n", _("Invalid REAL response received from host"));
119 else
120 printf (_("Invalid REAL response received from host on port %d\n"),
121 server_port);
122 } 146 }
123 else { 147
124 /* else we got the REAL string, so check the return code */ 148 time_t end_time;
125 149 {
126 time (&end_time); 150 mp_subcheck sc_options_request = mp_subcheck_init();
127 151 mp_state_enum options_result = STATE_OK;
128 result = STATE_OK; 152 /* make sure we find the response we are looking for */
129 153 if (!strstr(buffer, config.server_expect)) {
130 status_line = (char *) strtok (buffer, "\n"); 154 if (config.server_port == PORT) {
131 155 xasprintf(&sc_options_request.output, "invalid REAL response received from host");
132 if (strstr (status_line, "200")) 156 } else {
133 result = STATE_OK; 157 xasprintf(&sc_options_request.output,
134 158 "invalid REAL response received from host on port %d",
135 /* client errors result in a warning state */ 159 config.server_port);
136 else if (strstr (status_line, "400")) 160 }
137 result = STATE_WARNING; 161 } else {
138 else if (strstr (status_line, "401")) 162 /* else we got the REAL string, so check the return code */
139 result = STATE_WARNING; 163 time(&end_time);
140 else if (strstr (status_line, "402")) 164
141 result = STATE_WARNING; 165 options_result = STATE_OK;
142 else if (strstr (status_line, "403")) 166
143 result = STATE_WARNING; 167 char *status_line = strtok(buffer, "\n");
144 else if (strstr (status_line, "404")) 168 xasprintf(&sc_options_request.output, "status line: %s", status_line);
145 result = STATE_WARNING; 169
146 170 if (strstr(status_line, "200")) {
147 /* server errors result in a critical state */ 171 options_result = STATE_OK;
148 else if (strstr (status_line, "500")) 172 }
149 result = STATE_CRITICAL; 173 /* client errors options_result in a warning state */
150 else if (strstr (status_line, "501")) 174 else if (strstr(status_line, "400")) {
151 result = STATE_CRITICAL; 175 options_result = STATE_WARNING;
152 else if (strstr (status_line, "502")) 176 } else if (strstr(status_line, "401")) {
153 result = STATE_CRITICAL; 177 options_result = STATE_WARNING;
154 else if (strstr (status_line, "503")) 178 } else if (strstr(status_line, "402")) {
155 result = STATE_CRITICAL; 179 options_result = STATE_WARNING;
156 180 } else if (strstr(status_line, "403")) {
157 else 181 options_result = STATE_WARNING;
158 result = STATE_UNKNOWN; 182 } else if (strstr(status_line, "404")) {
183 options_result = STATE_WARNING;
184 } else if (strstr(status_line, "500")) {
185 /* server errors options_result in a critical state */
186 options_result = STATE_CRITICAL;
187 } else if (strstr(status_line, "501")) {
188 options_result = STATE_CRITICAL;
189 } else if (strstr(status_line, "502")) {
190 options_result = STATE_CRITICAL;
191 } else if (strstr(status_line, "503")) {
192 options_result = STATE_CRITICAL;
193 } else {
194 options_result = STATE_UNKNOWN;
195 }
196 }
197
198 sc_options_request = mp_set_subcheck_state(sc_options_request, options_result);
199 mp_add_subcheck_to_check(&overall, sc_options_request);
200
201 if (options_result != STATE_OK) {
202 // exit here if Setting options already failed
203 mp_exit(overall);
204 }
159 } 205 }
160 206
161 /* Part II - Check stream exists and is ok */ 207 /* Part II - Check stream exists and is ok */
162 if ((result == STATE_OK )&& (server_url != NULL) ) { 208 if (config.server_url != NULL) {
163
164 /* Part I - Server Check */ 209 /* Part I - Server Check */
210 mp_subcheck sc_describe = mp_subcheck_init();
165 211
166 /* send the DESCRIBE request */ 212 /* send the DESCRIBE request */
167 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name, 213 sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name,
168 server_port, server_url); 214 config.server_port, config.server_url);
169 result = send (sd, buffer, strlen (buffer), 0); 215
216 ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0);
217 if (sent_bytes == -1) {
218 sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL);
219 xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed",
220 config.host_name);
221 mp_add_subcheck_to_check(&overall, sc_describe);
222 mp_exit(overall);
223 }
170 224
171 /* send the header sync */ 225 /* send the header sync */
172 sprintf (buffer, "CSeq: 2\r\n"); 226 sprintf(buffer, "CSeq: 2\r\n");
173 result = send (sd, buffer, strlen (buffer), 0); 227 sent_bytes = send(socket, buffer, strlen(buffer), 0);
228 if (sent_bytes == -1) {
229 sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL);
230 xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed",
231 config.host_name);
232 mp_add_subcheck_to_check(&overall, sc_describe);
233 mp_exit(overall);
234 }
174 235
175 /* send a newline so the server knows we're done with the request */ 236 /* send a newline so the server knows we're done with the request */
176 sprintf (buffer, "\r\n"); 237 sprintf(buffer, "\r\n");
177 result = send (sd, buffer, strlen (buffer), 0); 238 sent_bytes = send(socket, buffer, strlen(buffer), 0);
239 if (sent_bytes == -1) {
240 sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL);
241 xasprintf(&sc_describe.output, "sending DESCRIBE request to %s failed",
242 config.host_name);
243 mp_add_subcheck_to_check(&overall, sc_describe);
244 mp_exit(overall);
245 }
178 246
179 /* watch for the REAL connection string */ 247 /* watch for the REAL connection string */
180 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0); 248 ssize_t recv_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
181 buffer[result] = '\0'; /* null terminate received buffer */ 249 if (recv_bytes == -1) {
182 250 /* return a CRITICAL status if we couldn't read any data */
183 /* return a CRITICAL status if we couldn't read any data */ 251 sc_describe = mp_set_subcheck_state(sc_describe, STATE_CRITICAL);
184 if (result == -1) { 252 xasprintf(&sc_describe.output, "No data received from host on DESCRIBE request");
185 printf (_("No data received from host\n")); 253 mp_add_subcheck_to_check(&overall, sc_describe);
186 result = STATE_CRITICAL; 254 mp_exit(overall);
187 } 255 } else {
188 else { 256 buffer[recv_bytes] = '\0'; /* null terminate received buffer */
189 /* make sure we find the response we are looking for */ 257 /* make sure we find the response we are looking for */
190 if (!strstr (buffer, server_expect)) { 258 if (!strstr(buffer, config.server_expect)) {
191 if (server_port == PORT) 259 if (config.server_port == PORT) {
192 printf ("%s\n", _("Invalid REAL response received from host")); 260 xasprintf(&sc_describe.output, "invalid REAL response received from host");
193 else 261 } else {
194 printf (_("Invalid REAL response received from host on port %d\n"), 262 xasprintf(&sc_describe.output,
195 server_port); 263 "invalid REAL response received from host on port %d",
196 } 264 config.server_port);
197 else { 265 }
198 266
267 sc_describe = mp_set_subcheck_state(sc_describe, STATE_UNKNOWN);
268 mp_add_subcheck_to_check(&overall, sc_describe);
269 mp_exit(overall);
270 } else {
199 /* else we got the REAL string, so check the return code */ 271 /* else we got the REAL string, so check the return code */
200 272
201 time (&end_time); 273 time(&end_time);
202 274
203 result = STATE_OK; 275 char *status_line = strtok(buffer, "\n");
204 276 xasprintf(&sc_describe.output, "status line: %s", status_line);
205 status_line = (char *) strtok (buffer, "\n"); 277
206 278 mp_state_enum describe_result;
207 if (strstr (status_line, "200")) 279 if (strstr(status_line, "200")) {
208 result = STATE_OK; 280 describe_result = STATE_OK;
209 281 }
210 /* client errors result in a warning state */ 282 /* client errors describe_result in a warning state */
211 else if (strstr (status_line, "400")) 283 else if (strstr(status_line, "400")) {
212 result = STATE_WARNING; 284 describe_result = STATE_WARNING;
213 else if (strstr (status_line, "401")) 285 } else if (strstr(status_line, "401")) {
214 result = STATE_WARNING; 286 describe_result = STATE_WARNING;
215 else if (strstr (status_line, "402")) 287 } else if (strstr(status_line, "402")) {
216 result = STATE_WARNING; 288 describe_result = STATE_WARNING;
217 else if (strstr (status_line, "403")) 289 } else if (strstr(status_line, "403")) {
218 result = STATE_WARNING; 290 describe_result = STATE_WARNING;
219 else if (strstr (status_line, "404")) 291 } else if (strstr(status_line, "404")) {
220 result = STATE_WARNING; 292 describe_result = STATE_WARNING;
221 293 }
222 /* server errors result in a critical state */ 294 /* server errors describe_result in a critical state */
223 else if (strstr (status_line, "500")) 295 else if (strstr(status_line, "500")) {
224 result = STATE_CRITICAL; 296 describe_result = STATE_CRITICAL;
225 else if (strstr (status_line, "501")) 297 } else if (strstr(status_line, "501")) {
226 result = STATE_CRITICAL; 298 describe_result = STATE_CRITICAL;
227 else if (strstr (status_line, "502")) 299 } else if (strstr(status_line, "502")) {
228 result = STATE_CRITICAL; 300 describe_result = STATE_CRITICAL;
229 else if (strstr (status_line, "503")) 301 } else if (strstr(status_line, "503")) {
230 result = STATE_CRITICAL; 302 describe_result = STATE_CRITICAL;
231 303 } else {
232 else 304 describe_result = STATE_UNKNOWN;
233 result = STATE_UNKNOWN; 305 }
306
307 sc_describe = mp_set_subcheck_state(sc_describe, describe_result);
308 mp_add_subcheck_to_check(&overall, sc_describe);
234 } 309 }
235 } 310 }
236 } 311 }
237 312
238 /* Return results */ 313 /* Return results */
239 if (result == STATE_OK) { 314 mp_subcheck sc_timing = mp_subcheck_init();
240 315 xasprintf(&sc_timing.output, "response time: %lds", end_time - start_time);
241 if (check_critical_time 316 sc_timing = mp_set_subcheck_default_state(sc_timing, STATE_OK);
242 && (end_time - start_time) > critical_time) result = STATE_CRITICAL; 317
243 else if (check_warning_time 318 mp_perfdata pd_response_time = perfdata_init();
244 && (end_time - start_time) > warning_time) result = 319 pd_response_time = mp_set_pd_value(pd_response_time, (end_time - start_time));
245 STATE_WARNING; 320 pd_response_time.label = "response_time";
246 321 pd_response_time.uom = "s";
247 /* Put some HTML in here to create a dynamic link */ 322 pd_response_time = mp_pd_set_thresholds(pd_response_time, config.time_thresholds);
248 printf (_("REAL %s - %d second response time\n"), 323 mp_add_perfdata_to_subcheck(&sc_connect, pd_response_time);
249 state_text (result), 324 sc_timing = mp_set_subcheck_state(sc_timing, mp_get_pd_status(pd_response_time));
250 (int) (end_time - start_time)); 325
251 } 326 mp_add_subcheck_to_check(&overall, sc_timing);
252 else
253 printf ("%s\n", status_line);
254 327
255 /* close the connection */ 328 /* close the connection */
256 close (sd); 329 close(socket);
257 330
258 /* reset the alarm */ 331 /* reset the alarm */
259 alarm (0); 332 alarm(0);
260 333
261 return result; 334 mp_exit(overall);
262} 335}
263 336
264
265
266/* process command-line arguments */ 337/* process command-line arguments */
267int 338check_real_config_wrapper process_arguments(int argc, char **argv) {
268process_arguments (int argc, char **argv) 339 enum {
269{ 340 output_format_index = CHAR_MAX + 1,
270 int c; 341 };
271 342
272 int option = 0; 343 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
273 static struct option longopts[] = { 344 {"IPaddress", required_argument, 0, 'I'},
274 {"hostname", required_argument, 0, 'H'}, 345 {"expect", required_argument, 0, 'e'},
275 {"IPaddress", required_argument, 0, 'I'}, 346 {"url", required_argument, 0, 'u'},
276 {"expect", required_argument, 0, 'e'}, 347 {"port", required_argument, 0, 'p'},
277 {"url", required_argument, 0, 'u'}, 348 {"critical", required_argument, 0, 'c'},
278 {"port", required_argument, 0, 'p'}, 349 {"warning", required_argument, 0, 'w'},
279 {"critical", required_argument, 0, 'c'}, 350 {"timeout", required_argument, 0, 't'},
280 {"warning", required_argument, 0, 'w'}, 351 {"verbose", no_argument, 0, 'v'},
281 {"timeout", required_argument, 0, 't'}, 352 {"version", no_argument, 0, 'V'},
282 {"verbose", no_argument, 0, 'v'}, 353 {"help", no_argument, 0, 'h'},
283 {"version", no_argument, 0, 'V'}, 354 {"output-format", required_argument, 0, output_format_index},
284 {"help", no_argument, 0, 'h'}, 355 {0, 0, 0, 0}};
285 {0, 0, 0, 0} 356
357 check_real_config_wrapper result = {
358 .errorcode = OK,
359 .config = check_real_config_init(),
286 }; 360 };
287 361
288 if (argc < 2) 362 if (argc < 2) {
289 return ERROR; 363 result.errorcode = ERROR;
364 return result;
365 }
290 366
291 for (c = 1; c < argc; c++) { 367 for (int i = 1; i < argc; i++) {
292 if (strcmp ("-to", argv[c]) == 0) 368 if (strcmp("-to", argv[i]) == 0) {
293 strcpy (argv[c], "-t"); 369 strcpy(argv[i], "-t");
294 else if (strcmp ("-wt", argv[c]) == 0) 370 } else if (strcmp("-wt", argv[i]) == 0) {
295 strcpy (argv[c], "-w"); 371 strcpy(argv[i], "-w");
296 else if (strcmp ("-ct", argv[c]) == 0) 372 } else if (strcmp("-ct", argv[i]) == 0) {
297 strcpy (argv[c], "-c"); 373 strcpy(argv[i], "-c");
374 }
298 } 375 }
299 376
300 while (1) { 377 while (true) {
301 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, 378 int option = 0;
302 &option); 379 int option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option);
303 380
304 if (c == -1 || c == EOF) 381 if (option_char == -1 || option_char == EOF) {
305 break; 382 break;
383 }
306 384
307 switch (c) { 385 switch (option_char) {
308 case 'I': /* hostname */ 386 case 'I': /* hostname */
309 case 'H': /* hostname */ 387 case 'H': /* hostname */
310 if (server_address) 388 if (result.config.server_address) {
311 break; 389 break;
312 else if (is_host (optarg)) 390 } else if (is_host(optarg)) {
313 server_address = optarg; 391 result.config.server_address = optarg;
314 else 392 } else {
315 usage2 (_("Invalid hostname/address"), optarg); 393 usage2(_("Invalid hostname/address"), optarg);
316 break; 394 }
317 case 'e': /* string to expect in response header */
318 server_expect = optarg;
319 break; 395 break;
320 case 'u': /* server URL */ 396 case 'e': /* string to expect in response header */
321 server_url = optarg; 397 result.config.server_expect = optarg;
322 break; 398 break;
323 case 'p': /* port */ 399 case 'u': /* server URL */
324 if (is_intpos (optarg)) { 400 result.config.server_url = optarg;
325 server_port = atoi (optarg);
326 }
327 else {
328 usage4 (_("Port must be a positive integer"));
329 }
330 break; 401 break;
331 case 'w': /* warning time threshold */ 402 case 'p': /* port */
332 if (is_intnonneg (optarg)) { 403 if (is_intpos(optarg)) {
333 warning_time = atoi (optarg); 404 result.config.server_port = atoi(optarg);
334 check_warning_time = true; 405 } else {
335 } 406 usage4(_("Port must be a positive integer"));
336 else {
337 usage4 (_("Warning time must be a positive integer"));
338 } 407 }
339 break; 408 break;
340 case 'c': /* critical time threshold */ 409 case 'w': /* warning time threshold */
341 if (is_intnonneg (optarg)) { 410 {
342 critical_time = atoi (optarg); 411 mp_range_parsed critical_range = mp_parse_range_string(optarg);
343 check_critical_time = true; 412 if (critical_range.error != MP_PARSING_SUCCES) {
413 die(STATE_UNKNOWN, "failed to parse warning threshold: %s", optarg);
344 } 414 }
345 else { 415 result.config.time_thresholds =
346 usage4 (_("Critical time must be a positive integer")); 416 mp_thresholds_set_warn(result.config.time_thresholds, critical_range.range);
417 } break;
418 case 'c': /* critical time threshold */
419 {
420 mp_range_parsed critical_range = mp_parse_range_string(optarg);
421 if (critical_range.error != MP_PARSING_SUCCES) {
422 die(STATE_UNKNOWN, "failed to parse critical threshold: %s", optarg);
347 } 423 }
348 break; 424 result.config.time_thresholds =
349 case 'v': /* verbose */ 425 mp_thresholds_set_crit(result.config.time_thresholds, critical_range.range);
426 } break;
427 case 'v': /* verbose */
350 verbose = true; 428 verbose = true;
351 break; 429 break;
352 case 't': /* timeout */ 430 case 't': /* timeout */
353 if (is_intnonneg (optarg)) { 431 if (is_intnonneg(optarg)) {
354 socket_timeout = atoi (optarg); 432 socket_timeout = atoi(optarg);
433 } else {
434 usage4(_("Timeout interval must be a positive integer"));
355 } 435 }
356 else { 436 break;
357 usage4 (_("Timeout interval must be a positive integer")); 437 case 'V': /* version */
438 print_revision(progname, NP_VERSION);
439 exit(STATE_UNKNOWN);
440 case 'h': /* help */
441 print_help();
442 exit(STATE_UNKNOWN);
443 case output_format_index: {
444 parsed_output_format parser = mp_parse_output_format(optarg);
445 if (!parser.parsing_success) {
446 // TODO List all available formats here, maybe add anothoer usage function
447 printf("Invalid output format: %s\n", optarg);
448 exit(STATE_UNKNOWN);
358 } 449 }
450
451 result.config.output_format_is_set = true;
452 result.config.output_format = parser.output_format;
359 break; 453 break;
360 case 'V': /* version */ 454 }
361 print_revision (progname, NP_VERSION); 455 case '?': /* usage */
362 exit (STATE_UNKNOWN); 456 usage5();
363 case 'h': /* help */
364 print_help ();
365 exit (STATE_UNKNOWN);
366 case '?': /* usage */
367 usage5 ();
368 } 457 }
369 } 458 }
370 459
371 c = optind; 460 int option_char = optind;
372 if (server_address==NULL && argc>c) { 461 if (result.config.server_address == NULL && argc > option_char) {
373 if (is_host (argv[c])) { 462 if (is_host(argv[option_char])) {
374 server_address = argv[c++]; 463 result.config.server_address = argv[option_char++];
375 } 464 } else {
376 else { 465 usage2(_("Invalid hostname/address"), argv[option_char]);
377 usage2 (_("Invalid hostname/address"), argv[c]);
378 } 466 }
379 } 467 }
380 468
381 if (server_address==NULL) 469 if (result.config.server_address == NULL) {
382 usage4 (_("You must provide a server to check")); 470 usage4(_("You must provide a server to check"));
383 471 }
384 if (host_name==NULL)
385 host_name = strdup (server_address);
386
387 if (server_expect == NULL)
388 server_expect = strdup(EXPECT);
389
390 return validate_arguments ();
391}
392
393 472
473 if (result.config.host_name == NULL) {
474 result.config.host_name = strdup(result.config.server_address);
475 }
394 476
395int 477 return result;
396validate_arguments (void)
397{
398 return OK;
399} 478}
400 479
401 480void print_help(void) {
402
403void
404print_help (void)
405{
406 char *myport; 481 char *myport;
407 xasprintf (&myport, "%d", PORT); 482 xasprintf(&myport, "%d", PORT);
408 483
409 print_revision (progname, NP_VERSION); 484 print_revision(progname, NP_VERSION);
410 485
411 printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n"); 486 printf("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
412 printf (COPYRIGHT, copyright, email); 487 printf(COPYRIGHT, copyright, email);
413 488
414 printf ("%s\n", _("This plugin tests the REAL service on the specified host.")); 489 printf("%s\n", _("This plugin tests the REAL service on the specified host."));
415 490
416 printf ("\n\n"); 491 printf("\n\n");
417 492
418 print_usage (); 493 print_usage();
419 494
420 printf (UT_HELP_VRSN); 495 printf(UT_HELP_VRSN);
421 printf (UT_EXTRA_OPTS); 496 printf(UT_EXTRA_OPTS);
422 497
423 printf (UT_HOST_PORT, 'p', myport); 498 printf(UT_HOST_PORT, 'p', myport);
424 499
425 printf (" %s\n", "-u, --url=STRING"); 500 printf(" %s\n", "-u, --url=STRING");
426 printf (" %s\n", _("Connect to this url")); 501 printf(" %s\n", _("Connect to this url"));
427 printf (" %s\n", "-e, --expect=STRING"); 502 printf(" %s\n", "-e, --expect=STRING");
428 printf (_("String to expect in first line of server response (default: %s)\n"), 503 printf(_("String to expect in first line of server response (default: %s)\n"), default_expect);
429 EXPECT);
430 504
431 printf (UT_WARN_CRIT); 505 printf(UT_WARN_CRIT);
432 506
433 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 507 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
434 508
435 printf (UT_VERBOSE); 509 printf(UT_VERBOSE);
436 510
437 printf ("\n"); 511 printf("\n");
438 printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host.")); 512 printf("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
439 printf ("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); 513 printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return"));
440 printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,")); 514 printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
441 printf ("%s\n", _("but incorrect response messages from the host result in STATE_WARNING return")); 515 printf("%s\n",
442 printf ("%s\n", _("values.")); 516 _("but incorrect response messages from the host result in STATE_WARNING return"));
517 printf("%s\n", _("values."));
443 518
444 printf (UT_SUPPORT); 519 printf(UT_SUPPORT);
445} 520}
446 521
447 522void print_usage(void) {
448 523 printf("%s\n", _("Usage:"));
449void 524 printf("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
450print_usage (void)
451{
452 printf ("%s\n", _("Usage:"));
453 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
454} 525}