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.c269
1 files changed, 157 insertions, 112 deletions
diff --git a/plugins/check_real.c b/plugins/check_real.c
index 369a88b1..66d07f8f 100644
--- a/plugins/check_real.c
+++ b/plugins/check_real.c
@@ -28,6 +28,8 @@
28 * 28 *
29 *****************************************************************************/ 29 *****************************************************************************/
30 30
31#include "states.h"
32#include <stdio.h>
31const char *progname = "check_real"; 33const char *progname = "check_real";
32const char *copyright = "2000-2024"; 34const char *copyright = "2000-2024";
33const char *email = "devel@monitoring-plugins.org"; 35const char *email = "devel@monitoring-plugins.org";
@@ -35,27 +37,20 @@ const char *email = "devel@monitoring-plugins.org";
35#include "common.h" 37#include "common.h"
36#include "netutils.h" 38#include "netutils.h"
37#include "utils.h" 39#include "utils.h"
38 40#include "check_real.d/config.h"
39enum {
40 PORT = 554
41};
42 41
43#define EXPECT "RTSP/1." 42#define EXPECT "RTSP/1."
44#define URL "" 43#define URL ""
45 44
46static int process_arguments(int, char **); 45typedef struct {
46 int errorcode;
47 check_real_config config;
48} check_real_config_wrapper;
49static check_real_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
50
47static void print_help(void); 51static void print_help(void);
48void print_usage(void); 52void print_usage(void);
49 53
50static int server_port = PORT;
51static char *server_address;
52static char *host_name;
53static char *server_url = NULL;
54static char *server_expect;
55static int warning_time = 0;
56static bool check_warning_time = false;
57static int critical_time = 0;
58static bool check_critical_time = false;
59static bool verbose = false; 54static bool verbose = false;
60 55
61int main(int argc, char **argv) { 56int main(int argc, char **argv) {
@@ -66,8 +61,12 @@ int main(int argc, char **argv) {
66 /* Parse extra opts if any */ 61 /* Parse extra opts if any */
67 argv = np_extra_opts(&argc, argv, progname); 62 argv = np_extra_opts(&argc, argv, progname);
68 63
69 if (process_arguments(argc, argv) == ERROR) 64 check_real_config_wrapper tmp_config = process_arguments(argc, argv);
65 if (tmp_config.errorcode == ERROR) {
70 usage4(_("Could not parse arguments")); 66 usage4(_("Could not parse arguments"));
67 }
68
69 const check_real_config config = tmp_config.config;
71 70
72 /* initialize alarm signal handling */ 71 /* initialize alarm signal handling */
73 signal(SIGALRM, socket_timeout_alarm_handler); 72 signal(SIGALRM, socket_timeout_alarm_handler);
@@ -78,38 +77,52 @@ int main(int argc, char **argv) {
78 77
79 /* try to connect to the host at the given port number */ 78 /* try to connect to the host at the given port number */
80 int socket; 79 int socket;
81 if (my_tcp_connect(server_address, server_port, &socket) != STATE_OK) 80 if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) {
82 die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), server_address, server_port); 81 die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), config.server_address,
82 config.server_port);
83 }
83 84
84 /* Part I - Server Check */ 85 /* Part I - Server Check */
85 86
86 /* send the OPTIONS request */ 87 /* send the OPTIONS request */
87 char buffer[MAX_INPUT_BUFFER]; 88 char buffer[MAX_INPUT_BUFFER];
88 sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port); 89 sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", config.host_name, config.server_port);
89 int result = send(socket, buffer, strlen(buffer), 0); 90 ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0);
91 if (sent_bytes == -1) {
92 die(STATE_CRITICAL, _("Sending options to %s failed\n"), config.host_name);
93 }
90 94
91 /* send the header sync */ 95 /* send the header sync */
92 sprintf(buffer, "CSeq: 1\r\n"); 96 sprintf(buffer, "CSeq: 1\r\n");
93 result = send(socket, buffer, strlen(buffer), 0); 97 sent_bytes = send(socket, buffer, strlen(buffer), 0);
98 if (sent_bytes == -1) {
99 die(STATE_CRITICAL, _("Sending header sync to %s failed\n"), config.host_name);
100 }
94 101
95 /* send a newline so the server knows we're done with the request */ 102 /* send a newline so the server knows we're done with the request */
96 sprintf(buffer, "\r\n"); 103 sprintf(buffer, "\r\n");
97 result = send(socket, buffer, strlen(buffer), 0); 104 sent_bytes = send(socket, buffer, strlen(buffer), 0);
105 if (sent_bytes == -1) {
106 die(STATE_CRITICAL, _("Sending newline to %s failed\n"), config.host_name);
107 }
98 108
99 /* watch for the REAL connection string */ 109 /* watch for the REAL connection string */
100 result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); 110 ssize_t received_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
101 111
102 /* return a CRITICAL status if we couldn't read any data */ 112 /* return a CRITICAL status if we couldn't read any data */
103 if (result == -1) 113 if (received_bytes == -1) {
104 die(STATE_CRITICAL, _("No data received from %s\n"), host_name); 114 die(STATE_CRITICAL, _("No data received from %s\n"), config.host_name);
115 }
105 116
117 mp_state_enum result = STATE_OK;
106 char *status_line = NULL; 118 char *status_line = NULL;
107 /* make sure we find the response we are looking for */ 119 /* make sure we find the response we are looking for */
108 if (!strstr(buffer, server_expect)) { 120 if (!strstr(buffer, config.server_expect)) {
109 if (server_port == PORT) 121 if (config.server_port == PORT) {
110 printf("%s\n", _("Invalid REAL response received from host")); 122 printf("%s\n", _("Invalid REAL response received from host"));
111 else 123 } else {
112 printf(_("Invalid REAL response received from host on port %d\n"), server_port); 124 printf(_("Invalid REAL response received from host on port %d\n"), config.server_port);
125 }
113 } else { 126 } else {
114 /* else we got the REAL string, so check the return code */ 127 /* else we got the REAL string, so check the return code */
115 128
@@ -117,69 +130,81 @@ int main(int argc, char **argv) {
117 130
118 result = STATE_OK; 131 result = STATE_OK;
119 132
120 status_line = (char *)strtok(buffer, "\n"); 133 status_line = strtok(buffer, "\n");
121 134
122 if (strstr(status_line, "200")) 135 if (strstr(status_line, "200")) {
123 result = STATE_OK; 136 result = STATE_OK;
137 }
124 138
125 /* client errors result in a warning state */ 139 /* client errors result in a warning state */
126 else if (strstr(status_line, "400")) 140 else if (strstr(status_line, "400")) {
127 result = STATE_WARNING; 141 result = STATE_WARNING;
128 else if (strstr(status_line, "401")) 142 } else if (strstr(status_line, "401")) {
129 result = STATE_WARNING; 143 result = STATE_WARNING;
130 else if (strstr(status_line, "402")) 144 } else if (strstr(status_line, "402")) {
131 result = STATE_WARNING; 145 result = STATE_WARNING;
132 else if (strstr(status_line, "403")) 146 } else if (strstr(status_line, "403")) {
133 result = STATE_WARNING; 147 result = STATE_WARNING;
134 else if (strstr(status_line, "404")) 148 } else if (strstr(status_line, "404")) {
135 result = STATE_WARNING; 149 result = STATE_WARNING;
136 150 } else if (strstr(status_line, "500")) {
137 /* server errors result in a critical state */ 151 /* server errors result in a critical state */
138 else if (strstr(status_line, "500"))
139 result = STATE_CRITICAL; 152 result = STATE_CRITICAL;
140 else if (strstr(status_line, "501")) 153 } else if (strstr(status_line, "501")) {
141 result = STATE_CRITICAL; 154 result = STATE_CRITICAL;
142 else if (strstr(status_line, "502")) 155 } else if (strstr(status_line, "502")) {
143 result = STATE_CRITICAL; 156 result = STATE_CRITICAL;
144 else if (strstr(status_line, "503")) 157 } else if (strstr(status_line, "503")) {
145 result = STATE_CRITICAL; 158 result = STATE_CRITICAL;
146 159 } else {
147 else
148 result = STATE_UNKNOWN; 160 result = STATE_UNKNOWN;
161 }
149 } 162 }
150 163
151 /* Part II - Check stream exists and is ok */ 164 /* Part II - Check stream exists and is ok */
152 if ((result == STATE_OK) && (server_url != NULL)) { 165 if ((result == STATE_OK) && (config.server_url != NULL)) {
153 166
154 /* Part I - Server Check */ 167 /* Part I - Server Check */
155 168
156 /* send the DESCRIBE request */ 169 /* send the DESCRIBE request */
157 sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name, server_port, server_url); 170 sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name,
158 result = send(socket, buffer, strlen(buffer), 0); 171 config.server_port, config.server_url);
172
173 ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0);
174 if (sent_bytes == -1) {
175 die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name);
176 }
159 177
160 /* send the header sync */ 178 /* send the header sync */
161 sprintf(buffer, "CSeq: 2\r\n"); 179 sprintf(buffer, "CSeq: 2\r\n");
162 result = send(socket, buffer, strlen(buffer), 0); 180 sent_bytes = send(socket, buffer, strlen(buffer), 0);
181 if (sent_bytes == -1) {
182 die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name);
183 }
163 184
164 /* send a newline so the server knows we're done with the request */ 185 /* send a newline so the server knows we're done with the request */
165 sprintf(buffer, "\r\n"); 186 sprintf(buffer, "\r\n");
166 result = send(socket, buffer, strlen(buffer), 0); 187 sent_bytes = send(socket, buffer, strlen(buffer), 0);
188 if (sent_bytes == -1) {
189 die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name);
190 }
167 191
168 /* watch for the REAL connection string */ 192 /* watch for the REAL connection string */
169 result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); 193 ssize_t recv_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
170 buffer[result] = '\0'; /* null terminate received buffer */ 194 if (recv_bytes == -1) {
171 195 /* return a CRITICAL status if we couldn't read any data */
172 /* return a CRITICAL status if we couldn't read any data */
173 if (result == -1) {
174 printf(_("No data received from host\n")); 196 printf(_("No data received from host\n"));
175 result = STATE_CRITICAL; 197 result = STATE_CRITICAL;
176 } else { 198 } else {
199 buffer[result] = '\0'; /* null terminate received buffer */
177 /* make sure we find the response we are looking for */ 200 /* make sure we find the response we are looking for */
178 if (!strstr(buffer, server_expect)) { 201 if (!strstr(buffer, config.server_expect)) {
179 if (server_port == PORT) 202 if (config.server_port == PORT) {
180 printf("%s\n", _("Invalid REAL response received from host")); 203 printf("%s\n", _("Invalid REAL response received from host"));
181 else 204 } else {
182 printf(_("Invalid REAL response received from host on port %d\n"), server_port); 205 printf(_("Invalid REAL response received from host on port %d\n"),
206 config.server_port);
207 }
183 } else { 208 } else {
184 209
185 /* else we got the REAL string, so check the return code */ 210 /* else we got the REAL string, so check the return code */
@@ -188,51 +213,57 @@ int main(int argc, char **argv) {
188 213
189 result = STATE_OK; 214 result = STATE_OK;
190 215
191 status_line = (char *)strtok(buffer, "\n"); 216 status_line = strtok(buffer, "\n");
192 217
193 if (strstr(status_line, "200")) 218 if (strstr(status_line, "200")) {
194 result = STATE_OK; 219 result = STATE_OK;
220 }
195 221
196 /* client errors result in a warning state */ 222 /* client errors result in a warning state */
197 else if (strstr(status_line, "400")) 223 else if (strstr(status_line, "400")) {
198 result = STATE_WARNING; 224 result = STATE_WARNING;
199 else if (strstr(status_line, "401")) 225 } else if (strstr(status_line, "401")) {
200 result = STATE_WARNING; 226 result = STATE_WARNING;
201 else if (strstr(status_line, "402")) 227 } else if (strstr(status_line, "402")) {
202 result = STATE_WARNING; 228 result = STATE_WARNING;
203 else if (strstr(status_line, "403")) 229 } else if (strstr(status_line, "403")) {
204 result = STATE_WARNING; 230 result = STATE_WARNING;
205 else if (strstr(status_line, "404")) 231 } else if (strstr(status_line, "404")) {
206 result = STATE_WARNING; 232 result = STATE_WARNING;
233 }
207 234
208 /* server errors result in a critical state */ 235 /* server errors result in a critical state */
209 else if (strstr(status_line, "500")) 236 else if (strstr(status_line, "500")) {
210 result = STATE_CRITICAL; 237 result = STATE_CRITICAL;
211 else if (strstr(status_line, "501")) 238 } else if (strstr(status_line, "501")) {
212 result = STATE_CRITICAL; 239 result = STATE_CRITICAL;
213 else if (strstr(status_line, "502")) 240 } else if (strstr(status_line, "502")) {
214 result = STATE_CRITICAL; 241 result = STATE_CRITICAL;
215 else if (strstr(status_line, "503")) 242 } else if (strstr(status_line, "503")) {
216 result = STATE_CRITICAL; 243 result = STATE_CRITICAL;
244 }
217 245
218 else 246 else {
219 result = STATE_UNKNOWN; 247 result = STATE_UNKNOWN;
248 }
220 } 249 }
221 } 250 }
222 } 251 }
223 252
224 /* Return results */ 253 /* Return results */
225 if (result == STATE_OK) { 254 if (result == STATE_OK) {
226 255 if (config.check_critical_time && (end_time - start_time) > config.critical_time) {
227 if (check_critical_time && (end_time - start_time) > critical_time)
228 result = STATE_CRITICAL; 256 result = STATE_CRITICAL;
229 else if (check_warning_time && (end_time - start_time) > warning_time) 257 } else if (config.check_warning_time && (end_time - start_time) > config.warning_time) {
230 result = STATE_WARNING; 258 result = STATE_WARNING;
259 }
231 260
232 /* Put some HTML in here to create a dynamic link */ 261 /* Put some HTML in here to create a dynamic link */
233 printf(_("REAL %s - %d second response time\n"), state_text(result), (int)(end_time - start_time)); 262 printf(_("REAL %s - %d second response time\n"), state_text(result),
234 } else 263 (int)(end_time - start_time));
264 } else {
235 printf("%s\n", status_line); 265 printf("%s\n", status_line);
266 }
236 267
237 /* close the connection */ 268 /* close the connection */
238 close(socket); 269 close(socket);
@@ -240,73 +271,83 @@ int main(int argc, char **argv) {
240 /* reset the alarm */ 271 /* reset the alarm */
241 alarm(0); 272 alarm(0);
242 273
243 return result; 274 exit(result);
244} 275}
245 276
246/* process command-line arguments */ 277/* process command-line arguments */
247int process_arguments(int argc, char **argv) { 278check_real_config_wrapper process_arguments(int argc, char **argv) {
248 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, 279 static struct option longopts[] = {
249 {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, 280 {"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'},
250 {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, 281 {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'},
251 {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'}, 282 {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'},
252 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, 283 {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'},
253 {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; 284 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
254 285 {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
255 if (argc < 2) 286
256 return ERROR; 287 check_real_config_wrapper result = {
288 .errorcode = OK,
289 .config = check_real_config_init(),
290 };
291
292 if (argc < 2) {
293 result.errorcode = ERROR;
294 return result;
295 }
257 296
258 for (int i = 1; i < argc; i++) { 297 for (int i = 1; i < argc; i++) {
259 if (strcmp("-to", argv[i]) == 0) 298 if (strcmp("-to", argv[i]) == 0) {
260 strcpy(argv[i], "-t"); 299 strcpy(argv[i], "-t");
261 else if (strcmp("-wt", argv[i]) == 0) 300 } else if (strcmp("-wt", argv[i]) == 0) {
262 strcpy(argv[i], "-w"); 301 strcpy(argv[i], "-w");
263 else if (strcmp("-ct", argv[i]) == 0) 302 } else if (strcmp("-ct", argv[i]) == 0) {
264 strcpy(argv[i], "-c"); 303 strcpy(argv[i], "-c");
304 }
265 } 305 }
266 306
267 int option_char;
268 while (true) { 307 while (true) {
269 int option = 0; 308 int option = 0;
270 option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); 309 int option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option);
271 310
272 if (option_char == -1 || option_char == EOF) 311 if (option_char == -1 || option_char == EOF) {
273 break; 312 break;
313 }
274 314
275 switch (option_char) { 315 switch (option_char) {
276 case 'I': /* hostname */ 316 case 'I': /* hostname */
277 case 'H': /* hostname */ 317 case 'H': /* hostname */
278 if (server_address) 318 if (result.config.server_address) {
279 break; 319 break;
280 else if (is_host(optarg)) 320 } else if (is_host(optarg)) {
281 server_address = optarg; 321 result.config.server_address = optarg;
282 else 322 } else {
283 usage2(_("Invalid hostname/address"), optarg); 323 usage2(_("Invalid hostname/address"), optarg);
324 }
284 break; 325 break;
285 case 'e': /* string to expect in response header */ 326 case 'e': /* string to expect in response header */
286 server_expect = optarg; 327 result.config.server_expect = optarg;
287 break; 328 break;
288 case 'u': /* server URL */ 329 case 'u': /* server URL */
289 server_url = optarg; 330 result.config.server_url = optarg;
290 break; 331 break;
291 case 'p': /* port */ 332 case 'p': /* port */
292 if (is_intpos(optarg)) { 333 if (is_intpos(optarg)) {
293 server_port = atoi(optarg); 334 result.config.server_port = atoi(optarg);
294 } else { 335 } else {
295 usage4(_("Port must be a positive integer")); 336 usage4(_("Port must be a positive integer"));
296 } 337 }
297 break; 338 break;
298 case 'w': /* warning time threshold */ 339 case 'w': /* warning time threshold */
299 if (is_intnonneg(optarg)) { 340 if (is_intnonneg(optarg)) {
300 warning_time = atoi(optarg); 341 result.config.warning_time = atoi(optarg);
301 check_warning_time = true; 342 result.config.check_warning_time = true;
302 } else { 343 } else {
303 usage4(_("Warning time must be a positive integer")); 344 usage4(_("Warning time must be a positive integer"));
304 } 345 }
305 break; 346 break;
306 case 'c': /* critical time threshold */ 347 case 'c': /* critical time threshold */
307 if (is_intnonneg(optarg)) { 348 if (is_intnonneg(optarg)) {
308 critical_time = atoi(optarg); 349 result.config.critical_time = atoi(optarg);
309 check_critical_time = true; 350 result.config.check_critical_time = true;
310 } else { 351 } else {
311 usage4(_("Critical time must be a positive integer")); 352 usage4(_("Critical time must be a positive integer"));
312 } 353 }
@@ -332,25 +373,28 @@ int process_arguments(int argc, char **argv) {
332 } 373 }
333 } 374 }
334 375
335 option_char = optind; 376 int option_char = optind;
336 if (server_address == NULL && argc > option_char) { 377 if (result.config.server_address == NULL && argc > option_char) {
337 if (is_host(argv[option_char])) { 378 if (is_host(argv[option_char])) {
338 server_address = argv[option_char++]; 379 result.config.server_address = argv[option_char++];
339 } else { 380 } else {
340 usage2(_("Invalid hostname/address"), argv[option_char]); 381 usage2(_("Invalid hostname/address"), argv[option_char]);
341 } 382 }
342 } 383 }
343 384
344 if (server_address == NULL) 385 if (result.config.server_address == NULL) {
345 usage4(_("You must provide a server to check")); 386 usage4(_("You must provide a server to check"));
387 }
346 388
347 if (host_name == NULL) 389 if (result.config.host_name == NULL) {
348 host_name = strdup(server_address); 390 result.config.host_name = strdup(result.config.server_address);
391 }
349 392
350 if (server_expect == NULL) 393 if (result.config.server_expect == NULL) {
351 server_expect = strdup(EXPECT); 394 result.config.server_expect = strdup(EXPECT);
395 }
352 396
353 return OK; 397 return result;
354} 398}
355 399
356void print_help(void) { 400void print_help(void) {
@@ -388,7 +432,8 @@ void print_help(void) {
388 printf("%s\n", _("This plugin will attempt to open an RTSP connection with the host.")); 432 printf("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
389 printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); 433 printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return"));
390 printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,")); 434 printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
391 printf("%s\n", _("but incorrect response messages from the host result in STATE_WARNING return")); 435 printf("%s\n",
436 _("but incorrect response messages from the host result in STATE_WARNING return"));
392 printf("%s\n", _("values.")); 437 printf("%s\n", _("values."));
393 438
394 printf(UT_SUPPORT); 439 printf(UT_SUPPORT);