Merge pull request #2045 from RincewindsHat/fix/calloc_argument_order
[monitoring-plugins.git] / plugins / check_real.c
blob369a88b13a630ba2bbdaf57c56942746daa1a8b6
1 /*****************************************************************************
3 * Monitoring check_real plugin
5 * License: GPL
6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
8 * Description:
10 * This file contains the check_real plugin
12 * This plugin tests the REAL service on the specified host.
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
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
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/>.
29 *****************************************************************************/
31 const char *progname = "check_real";
32 const char *copyright = "2000-2024";
33 const char *email = "devel@monitoring-plugins.org";
35 #include "common.h"
36 #include "netutils.h"
37 #include "utils.h"
39 enum {
40 PORT = 554
43 #define EXPECT "RTSP/1."
44 #define URL ""
46 static int process_arguments(int, char **);
47 static void print_help(void);
48 void print_usage(void);
50 static int server_port = PORT;
51 static char *server_address;
52 static char *host_name;
53 static char *server_url = NULL;
54 static char *server_expect;
55 static int warning_time = 0;
56 static bool check_warning_time = false;
57 static int critical_time = 0;
58 static bool check_critical_time = false;
59 static bool verbose = false;
61 int main(int argc, char **argv) {
62 setlocale(LC_ALL, "");
63 bindtextdomain(PACKAGE, LOCALEDIR);
64 textdomain(PACKAGE);
66 /* Parse extra opts if any */
67 argv = np_extra_opts(&argc, argv, progname);
69 if (process_arguments(argc, argv) == ERROR)
70 usage4(_("Could not parse arguments"));
72 /* initialize alarm signal handling */
73 signal(SIGALRM, socket_timeout_alarm_handler);
75 /* set socket timeout */
76 alarm(socket_timeout);
77 time(&start_time);
79 /* try to connect to the host at the given port number */
80 int socket;
81 if (my_tcp_connect(server_address, server_port, &socket) != STATE_OK)
82 die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), server_address, server_port);
84 /* Part I - Server Check */
86 /* send the OPTIONS request */
87 char buffer[MAX_INPUT_BUFFER];
88 sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
89 int result = send(socket, buffer, strlen(buffer), 0);
91 /* send the header sync */
92 sprintf(buffer, "CSeq: 1\r\n");
93 result = send(socket, buffer, strlen(buffer), 0);
95 /* send a newline so the server knows we're done with the request */
96 sprintf(buffer, "\r\n");
97 result = send(socket, buffer, strlen(buffer), 0);
99 /* watch for the REAL connection string */
100 result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
102 /* return a CRITICAL status if we couldn't read any data */
103 if (result == -1)
104 die(STATE_CRITICAL, _("No data received from %s\n"), host_name);
106 char *status_line = NULL;
107 /* make sure we find the response we are looking for */
108 if (!strstr(buffer, server_expect)) {
109 if (server_port == PORT)
110 printf("%s\n", _("Invalid REAL response received from host"));
111 else
112 printf(_("Invalid REAL response received from host on port %d\n"), server_port);
113 } else {
114 /* else we got the REAL string, so check the return code */
116 time(&end_time);
118 result = STATE_OK;
120 status_line = (char *)strtok(buffer, "\n");
122 if (strstr(status_line, "200"))
123 result = STATE_OK;
125 /* client errors result in a warning state */
126 else if (strstr(status_line, "400"))
127 result = STATE_WARNING;
128 else if (strstr(status_line, "401"))
129 result = STATE_WARNING;
130 else if (strstr(status_line, "402"))
131 result = STATE_WARNING;
132 else if (strstr(status_line, "403"))
133 result = STATE_WARNING;
134 else if (strstr(status_line, "404"))
135 result = STATE_WARNING;
137 /* server errors result in a critical state */
138 else if (strstr(status_line, "500"))
139 result = STATE_CRITICAL;
140 else if (strstr(status_line, "501"))
141 result = STATE_CRITICAL;
142 else if (strstr(status_line, "502"))
143 result = STATE_CRITICAL;
144 else if (strstr(status_line, "503"))
145 result = STATE_CRITICAL;
147 else
148 result = STATE_UNKNOWN;
151 /* Part II - Check stream exists and is ok */
152 if ((result == STATE_OK) && (server_url != NULL)) {
154 /* Part I - Server Check */
156 /* send the DESCRIBE request */
157 sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name, server_port, server_url);
158 result = send(socket, buffer, strlen(buffer), 0);
160 /* send the header sync */
161 sprintf(buffer, "CSeq: 2\r\n");
162 result = send(socket, buffer, strlen(buffer), 0);
164 /* send a newline so the server knows we're done with the request */
165 sprintf(buffer, "\r\n");
166 result = send(socket, buffer, strlen(buffer), 0);
168 /* watch for the REAL connection string */
169 result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0);
170 buffer[result] = '\0'; /* null terminate received buffer */
172 /* return a CRITICAL status if we couldn't read any data */
173 if (result == -1) {
174 printf(_("No data received from host\n"));
175 result = STATE_CRITICAL;
176 } else {
177 /* make sure we find the response we are looking for */
178 if (!strstr(buffer, server_expect)) {
179 if (server_port == PORT)
180 printf("%s\n", _("Invalid REAL response received from host"));
181 else
182 printf(_("Invalid REAL response received from host on port %d\n"), server_port);
183 } else {
185 /* else we got the REAL string, so check the return code */
187 time(&end_time);
189 result = STATE_OK;
191 status_line = (char *)strtok(buffer, "\n");
193 if (strstr(status_line, "200"))
194 result = STATE_OK;
196 /* client errors result in a warning state */
197 else if (strstr(status_line, "400"))
198 result = STATE_WARNING;
199 else if (strstr(status_line, "401"))
200 result = STATE_WARNING;
201 else if (strstr(status_line, "402"))
202 result = STATE_WARNING;
203 else if (strstr(status_line, "403"))
204 result = STATE_WARNING;
205 else if (strstr(status_line, "404"))
206 result = STATE_WARNING;
208 /* server errors result in a critical state */
209 else if (strstr(status_line, "500"))
210 result = STATE_CRITICAL;
211 else if (strstr(status_line, "501"))
212 result = STATE_CRITICAL;
213 else if (strstr(status_line, "502"))
214 result = STATE_CRITICAL;
215 else if (strstr(status_line, "503"))
216 result = STATE_CRITICAL;
218 else
219 result = STATE_UNKNOWN;
224 /* Return results */
225 if (result == STATE_OK) {
227 if (check_critical_time && (end_time - start_time) > critical_time)
228 result = STATE_CRITICAL;
229 else if (check_warning_time && (end_time - start_time) > warning_time)
230 result = STATE_WARNING;
232 /* 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));
234 } else
235 printf("%s\n", status_line);
237 /* close the connection */
238 close(socket);
240 /* reset the alarm */
241 alarm(0);
243 return result;
246 /* process command-line arguments */
247 int process_arguments(int argc, char **argv) {
248 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'},
249 {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'},
250 {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'},
251 {"warning", required_argument, 0, 'w'}, {"timeout", required_argument, 0, 't'},
252 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
253 {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
255 if (argc < 2)
256 return ERROR;
258 for (int i = 1; i < argc; i++) {
259 if (strcmp("-to", argv[i]) == 0)
260 strcpy(argv[i], "-t");
261 else if (strcmp("-wt", argv[i]) == 0)
262 strcpy(argv[i], "-w");
263 else if (strcmp("-ct", argv[i]) == 0)
264 strcpy(argv[i], "-c");
267 int option_char;
268 while (true) {
269 int option = 0;
270 option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option);
272 if (option_char == -1 || option_char == EOF)
273 break;
275 switch (option_char) {
276 case 'I': /* hostname */
277 case 'H': /* hostname */
278 if (server_address)
279 break;
280 else if (is_host(optarg))
281 server_address = optarg;
282 else
283 usage2(_("Invalid hostname/address"), optarg);
284 break;
285 case 'e': /* string to expect in response header */
286 server_expect = optarg;
287 break;
288 case 'u': /* server URL */
289 server_url = optarg;
290 break;
291 case 'p': /* port */
292 if (is_intpos(optarg)) {
293 server_port = atoi(optarg);
294 } else {
295 usage4(_("Port must be a positive integer"));
297 break;
298 case 'w': /* warning time threshold */
299 if (is_intnonneg(optarg)) {
300 warning_time = atoi(optarg);
301 check_warning_time = true;
302 } else {
303 usage4(_("Warning time must be a positive integer"));
305 break;
306 case 'c': /* critical time threshold */
307 if (is_intnonneg(optarg)) {
308 critical_time = atoi(optarg);
309 check_critical_time = true;
310 } else {
311 usage4(_("Critical time must be a positive integer"));
313 break;
314 case 'v': /* verbose */
315 verbose = true;
316 break;
317 case 't': /* timeout */
318 if (is_intnonneg(optarg)) {
319 socket_timeout = atoi(optarg);
320 } else {
321 usage4(_("Timeout interval must be a positive integer"));
323 break;
324 case 'V': /* version */
325 print_revision(progname, NP_VERSION);
326 exit(STATE_UNKNOWN);
327 case 'h': /* help */
328 print_help();
329 exit(STATE_UNKNOWN);
330 case '?': /* usage */
331 usage5();
335 option_char = optind;
336 if (server_address == NULL && argc > option_char) {
337 if (is_host(argv[option_char])) {
338 server_address = argv[option_char++];
339 } else {
340 usage2(_("Invalid hostname/address"), argv[option_char]);
344 if (server_address == NULL)
345 usage4(_("You must provide a server to check"));
347 if (host_name == NULL)
348 host_name = strdup(server_address);
350 if (server_expect == NULL)
351 server_expect = strdup(EXPECT);
353 return OK;
356 void print_help(void) {
357 char *myport;
358 xasprintf(&myport, "%d", PORT);
360 print_revision(progname, NP_VERSION);
362 printf("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
363 printf(COPYRIGHT, copyright, email);
365 printf("%s\n", _("This plugin tests the REAL service on the specified host."));
367 printf("\n\n");
369 print_usage();
371 printf(UT_HELP_VRSN);
372 printf(UT_EXTRA_OPTS);
374 printf(UT_HOST_PORT, 'p', myport);
376 printf(" %s\n", "-u, --url=STRING");
377 printf(" %s\n", _("Connect to this url"));
378 printf(" %s\n", "-e, --expect=STRING");
379 printf(_("String to expect in first line of server response (default: %s)\n"), EXPECT);
381 printf(UT_WARN_CRIT);
383 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
385 printf(UT_VERBOSE);
387 printf("\n");
388 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"));
390 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"));
392 printf("%s\n", _("values."));
394 printf(UT_SUPPORT);
397 void print_usage(void) {
398 printf("%s\n", _("Usage:"));
399 printf("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);