check_real.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*****************************************************************************
  2. *
  3. * CHECK_REAL.C
  4. *
  5. * Program: RealMedia plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Pedro Leite (leite@cic.ua.pt)
  8. *
  9. * Based on CHECK_HTTP.C
  10. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  11. *
  12. * Last Modified: $Date$
  13. *
  14. * Command line: CHECK_REAL <host_address> [-e expect] [-u url] [-p port]
  15. * [-hn host_name] [-wt warn_time] [-ct crit_time]
  16. * [-to to_sec]
  17. *
  18. * Description:
  19. *
  20. * This plugin will attempt to open an RTSP connection with the host.
  21. * Successul connects return STATE_OK, refusals and timeouts return
  22. * STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,
  23. * but incorrect reponse messages from the host result in STATE_WARNING return
  24. * values. If you are checking a virtual server that uses "host headers"you
  25. * must supply the FQDN (fully qualified domain name) as the [host_name]
  26. * argument.
  27. *
  28. * License Information:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  43. *
  44. ****************************************************************************/
  45. #include "config.h"
  46. #include "common.h"
  47. #include "netutils.h"
  48. #include "utils.h"
  49. #define PROGNAME "check_real"
  50. #define PORT 554
  51. #define EXPECT "RTSP/1."
  52. #define URL ""
  53. int process_arguments (int, char **);
  54. int call_getopt (int, char **);
  55. int validate_arguments (void);
  56. int check_disk (int usp, int free_disk);
  57. void print_help (void);
  58. void print_usage (void);
  59. int server_port = PORT;
  60. char *server_address = NULL;
  61. char *host_name = NULL;
  62. char *server_url = NULL;
  63. char *server_expect = NULL;
  64. int warning_time = 0;
  65. int check_warning_time = FALSE;
  66. int critical_time = 0;
  67. int check_critical_time = FALSE;
  68. int verbose = FALSE;
  69. int
  70. main (int argc, char **argv)
  71. {
  72. int sd;
  73. int result;
  74. char buffer[MAX_INPUT_BUFFER];
  75. char *status_line = NULL;
  76. if (process_arguments (argc, argv) != OK)
  77. usage ("Invalid command arguments supplied\n");
  78. /* initialize alarm signal handling */
  79. signal (SIGALRM, socket_timeout_alarm_handler);
  80. /* set socket timeout */
  81. alarm (socket_timeout);
  82. time (&start_time);
  83. /* try to connect to the host at the given port number */
  84. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
  85. terminate (STATE_CRITICAL, "Unable to connect to %s on port %d\n",
  86. server_address, server_port);
  87. /* Part I - Server Check */
  88. /* send the OPTIONS request */
  89. sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\n", host_name, server_port);
  90. result = send (sd, buffer, strlen (buffer), 0);
  91. /* send the header sync */
  92. sprintf (buffer, "CSeq: 1\n");
  93. result = send (sd, buffer, strlen (buffer), 0);
  94. /* send a newline so the server knows we're done with the request */
  95. sprintf (buffer, "\n");
  96. result = send (sd, buffer, strlen (buffer), 0);
  97. /* watch for the REAL connection string */
  98. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  99. /* return a CRITICAL status if we couldn't read any data */
  100. if (result == -1)
  101. terminate (STATE_CRITICAL, "No data received from %s\n", host_name);
  102. /* make sure we find the response we are looking for */
  103. if (!strstr (buffer, EXPECT)) {
  104. if (server_port == PORT)
  105. printf ("Invalid REAL response received from host\n");
  106. else
  107. printf ("Invalid REAL response received from host on port %d\n",
  108. server_port);
  109. }
  110. else {
  111. /* else we got the REAL string, so check the return code */
  112. time (&end_time);
  113. result = STATE_OK;
  114. status_line = (char *) strtok (buffer, "\n");
  115. if (strstr (status_line, "200"))
  116. result = STATE_OK;
  117. /* client errors result in a warning state */
  118. else if (strstr (status_line, "400"))
  119. result = STATE_WARNING;
  120. else if (strstr (status_line, "401"))
  121. result = STATE_WARNING;
  122. else if (strstr (status_line, "402"))
  123. result = STATE_WARNING;
  124. else if (strstr (status_line, "403"))
  125. result = STATE_WARNING;
  126. else if (strstr (status_line, "404"))
  127. result = STATE_WARNING;
  128. /* server errors result in a critical state */
  129. else if (strstr (status_line, "500"))
  130. result = STATE_CRITICAL;
  131. else if (strstr (status_line, "501"))
  132. result = STATE_CRITICAL;
  133. else if (strstr (status_line, "502"))
  134. result = STATE_CRITICAL;
  135. else if (strstr (status_line, "503"))
  136. result = STATE_CRITICAL;
  137. else
  138. result = STATE_UNKNOWN;
  139. }
  140. /* Part II - Check stream exists and is ok */
  141. if ((result == STATE_OK) && (server_url != NULL)) {
  142. /* Part I - Server Check */
  143. /* send the OPTIONS request */
  144. sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
  145. server_port, server_url);
  146. result = send (sd, buffer, strlen (buffer), 0);
  147. /* send the header sync */
  148. sprintf (buffer, "CSeq: 2\n");
  149. result = send (sd, buffer, strlen (buffer), 0);
  150. /* send a newline so the server knows we're done with the request */
  151. sprintf (buffer, "\n");
  152. result = send (sd, buffer, strlen (buffer), 0);
  153. /* watch for the REAL connection string */
  154. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  155. /* return a CRITICAL status if we couldn't read any data */
  156. if (result == -1) {
  157. printf ("No data received from host\n");
  158. result = STATE_CRITICAL;
  159. }
  160. else {
  161. /* make sure we find the response we are looking for */
  162. if (!strstr (buffer, EXPECT)) {
  163. if (server_port == PORT)
  164. printf ("Invalid REAL response received from host\n");
  165. else
  166. printf ("Invalid REAL response received from host on port %d\n",
  167. server_port);
  168. }
  169. else {
  170. /* else we got the REAL string, so check the return code */
  171. time (&end_time);
  172. result = STATE_OK;
  173. status_line = (char *) strtok (buffer, "\n");
  174. if (strstr (status_line, "200"))
  175. result = STATE_OK;
  176. /* client errors result in a warning state */
  177. else if (strstr (status_line, "400"))
  178. result = STATE_WARNING;
  179. else if (strstr (status_line, "401"))
  180. result = STATE_WARNING;
  181. else if (strstr (status_line, "402"))
  182. result = STATE_WARNING;
  183. else if (strstr (status_line, "403"))
  184. result = STATE_WARNING;
  185. else if (strstr (status_line, "404"))
  186. result = STATE_WARNING;
  187. /* server errors result in a critical state */
  188. else if (strstr (status_line, "500"))
  189. result = STATE_CRITICAL;
  190. else if (strstr (status_line, "501"))
  191. result = STATE_CRITICAL;
  192. else if (strstr (status_line, "502"))
  193. result = STATE_CRITICAL;
  194. else if (strstr (status_line, "503"))
  195. result = STATE_CRITICAL;
  196. else
  197. result = STATE_UNKNOWN;
  198. }
  199. }
  200. }
  201. /* Return results */
  202. if (result == STATE_OK) {
  203. if (check_critical_time == TRUE
  204. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  205. else if (check_warning_time == TRUE
  206. && (end_time - start_time) > warning_time) result =
  207. STATE_WARNING;
  208. /* Put some HTML in here to create a dynamic link */
  209. printf ("REAL %s - %d second response time\n",
  210. (result == STATE_OK) ? "ok" : "problem",
  211. (int) (end_time - start_time));
  212. }
  213. else
  214. printf ("%s\n", status_line);
  215. /* close the connection */
  216. close (sd);
  217. /* reset the alarm */
  218. alarm (0);
  219. return result;
  220. }
  221. /* process command-line arguments */
  222. int
  223. process_arguments (int argc, char **argv)
  224. {
  225. int c;
  226. if (argc < 2)
  227. return ERROR;
  228. for (c = 1; c < argc; c++) {
  229. if (strcmp ("-to", argv[c]) == 0)
  230. strcpy (argv[c], "-t");
  231. else if (strcmp ("-wt", argv[c]) == 0)
  232. strcpy (argv[c], "-w");
  233. else if (strcmp ("-ct", argv[c]) == 0)
  234. strcpy (argv[c], "-c");
  235. }
  236. c = 0;
  237. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  238. if (is_option (argv[c]))
  239. continue;
  240. if (server_address == NULL) {
  241. if (is_host (argv[c])) {
  242. server_address = argv[c];
  243. }
  244. else {
  245. usage ("Invalid host name");
  246. }
  247. }
  248. }
  249. if (server_expect == NULL)
  250. server_expect = strscpy (NULL, EXPECT);
  251. return validate_arguments ();
  252. }
  253. int
  254. call_getopt (int argc, char **argv)
  255. {
  256. int c, i = 0;
  257. #ifdef HAVE_GETOPT_H
  258. int option_index = 0;
  259. static struct option long_options[] = {
  260. {"hostname", required_argument, 0, 'H'},
  261. {"IPaddress", required_argument, 0, 'I'},
  262. {"expect", required_argument, 0, 'e'},
  263. {"url", required_argument, 0, 'u'},
  264. {"port", required_argument, 0, 'p'},
  265. {"critical", required_argument, 0, 'c'},
  266. {"warning", required_argument, 0, 'w'},
  267. {"timeout", required_argument, 0, 't'},
  268. {"verbose", no_argument, 0, 'v'},
  269. {"version", no_argument, 0, 'V'},
  270. {"help", no_argument, 0, 'h'},
  271. {0, 0, 0, 0}
  272. };
  273. #endif
  274. while (1) {
  275. #ifdef HAVE_GETOPT_H
  276. c =
  277. getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", long_options,
  278. &option_index);
  279. #else
  280. c = getopt (argc, argv, "+?hVI:H:e:u:p:w:c:t");
  281. #endif
  282. i++;
  283. if (c == -1 || c == EOF || c == 1)
  284. break;
  285. switch (c) {
  286. case 'I':
  287. case 'H':
  288. case 'e':
  289. case 'u':
  290. case 'p':
  291. case 'w':
  292. case 'c':
  293. case 't':
  294. i++;
  295. }
  296. switch (c) {
  297. case 'I': /* hostname */
  298. if (is_host (optarg)) {
  299. server_address = optarg;
  300. }
  301. else {
  302. usage ("Invalid host name\n");
  303. }
  304. break;
  305. case 'H': /* hostname */
  306. if (is_host (optarg)) {
  307. server_address = optarg;
  308. }
  309. else {
  310. usage ("Invalid host name\n");
  311. }
  312. break;
  313. case 'e': /* string to expect in response header */
  314. server_expect = optarg;
  315. break;
  316. case 'u': /* string to expect in response header */
  317. server_url = optarg;
  318. break;
  319. case 'p': /* port */
  320. if (is_intpos (optarg)) {
  321. server_port = atoi (optarg);
  322. }
  323. else {
  324. usage ("Server port must be a positive integer\n");
  325. }
  326. break;
  327. case 'w': /* warning time threshold */
  328. if (is_intnonneg (optarg)) {
  329. warning_time = atoi (optarg);
  330. check_warning_time = TRUE;
  331. }
  332. else {
  333. usage ("Warning time must be a nonnegative integer\n");
  334. }
  335. break;
  336. case 'c': /* critical time threshold */
  337. if (is_intnonneg (optarg)) {
  338. critical_time = atoi (optarg);
  339. check_critical_time = TRUE;
  340. }
  341. else {
  342. usage ("Critical time must be a nonnegative integer\n");
  343. }
  344. break;
  345. case 'v': /* verbose */
  346. verbose = TRUE;
  347. break;
  348. case 't': /* timeout */
  349. if (is_intnonneg (optarg)) {
  350. socket_timeout = atoi (optarg);
  351. }
  352. else {
  353. usage ("Time interval must be a nonnegative integer\n");
  354. }
  355. break;
  356. case 'V': /* version */
  357. print_revision (PROGNAME, "$Revision$");
  358. exit (STATE_OK);
  359. case 'h': /* help */
  360. print_help ();
  361. exit (STATE_OK);
  362. case '?': /* help */
  363. usage ("Invalid argument\n");
  364. }
  365. }
  366. return i;
  367. }
  368. int
  369. validate_arguments (void)
  370. {
  371. return OK;
  372. }
  373. void
  374. print_help (void)
  375. {
  376. print_revision (PROGNAME, "$Revision$");
  377. printf
  378. ("Copyright (c) 2000 Pedro Leite (leite@cic.ua.pt)/Karl DeBisschop\n\n"
  379. "This plugin tests the REAL service on the specified host.\n\n");
  380. print_usage ();
  381. printf
  382. ("\nOptions:\n"
  383. " -H, --hostname=STRING or IPADDRESS\n"
  384. " Check this server on the indicated host\n"
  385. " -I, --IPaddress=STRING or IPADDRESS\n"
  386. " Check server at this host address\n"
  387. " -p, --port=INTEGER\n"
  388. " Make connection on the indicated port (default: %d)\n"
  389. " -u, --url=STRING\n"
  390. " Connect to this url\n"
  391. " -e, --expect=STRING\n"
  392. " String to expect in first line of server response (default: %s)\n"
  393. " -w, --warning=INTEGER\n"
  394. " Seconds necessary to result in a warning status\n"
  395. " -c, --critical=INTEGER\n"
  396. " Seconds necessary to result in a critical status\n"
  397. " -t, --timeout=INTEGER\n"
  398. " Seconds before connection attempt times out (default: %d)\n"
  399. " -v, --verbose\n"
  400. " Print extra information (command-line use only)\n"
  401. " -h, --help\n"
  402. " Print detailed help screen\n"
  403. " -V, --version\n"
  404. " Print version information\n\n",
  405. PORT, EXPECT, DEFAULT_SOCKET_TIMEOUT);
  406. support ();
  407. }
  408. void
  409. print_usage (void)
  410. {
  411. printf
  412. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
  413. " [-t timeout] [-v]\n"
  414. " %s --help\n"
  415. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  416. }
  417. /*
  418. // process command-line arguments
  419. int
  420. process_arguments (int argc, char **argv)
  421. {
  422. int x;
  423. // no options were supplied
  424. if (argc < 2)
  425. return ERROR;
  426. // first option is always the server name/address
  427. strncpy (server_address, argv[1], sizeof (server_address) - 1);
  428. server_address[sizeof (server_address) - 1] = 0;
  429. // set the host name to the server address (until its overridden)
  430. strcpy (host_name, server_address);
  431. // process all remaining arguments
  432. for (x = 3; x <= argc; x++)
  433. {
  434. // we got the string to expect from the server
  435. if (!strcmp (argv[x - 1], "-e"))
  436. {
  437. if (x < argc)
  438. {
  439. strncpy (server_expect, argv[x], sizeof (server_expect) - 1);
  440. server_expect[sizeof (server_expect) - 1] = 0;
  441. x++;
  442. }
  443. else
  444. return ERROR;
  445. }
  446. // we got the URL to check
  447. else if (!strcmp (argv[x - 1], "-u"))
  448. {
  449. if (x < argc)
  450. {
  451. strncpy (server_url, argv[x], sizeof (server_url) - 1);
  452. server_url[sizeof (server_url) - 1] = 0;
  453. x++;
  454. }
  455. else
  456. return ERROR;
  457. }
  458. // we go the host name to use in the host header
  459. else if (!strcmp (argv[x - 1], "-hn"))
  460. {
  461. if (x < argc)
  462. {
  463. strncpy (host_name, argv[x], sizeof (host_name) - 1);
  464. host_name[sizeof (host_name) - 1] = 0;
  465. x++;
  466. }
  467. else
  468. return ERROR;
  469. }
  470. // we got the port number to use
  471. else if (!strcmp (argv[x - 1], "-p"))
  472. {
  473. if (x < argc)
  474. {
  475. server_port = atoi (argv[x]);
  476. x++;
  477. }
  478. else
  479. return ERROR;
  480. }
  481. // we got the socket timeout
  482. else if (!strcmp (argv[x - 1], "-to"))
  483. {
  484. if (x < argc)
  485. {
  486. socket_timeout = atoi (argv[x]);
  487. if (socket_timeout <= 0)
  488. return ERROR;
  489. x++;
  490. }
  491. else
  492. return ERROR;
  493. }
  494. // we got the warning threshold time
  495. else if (!strcmp (argv[x - 1], "-wt"))
  496. {
  497. if (x < argc)
  498. {
  499. warning_time = atoi (argv[x]);
  500. check_warning_time = TRUE;
  501. x++;
  502. }
  503. else
  504. return ERROR;
  505. }
  506. // we got the critical threshold time
  507. else if (!strcmp (argv[x - 1], "-ct"))
  508. {
  509. if (x < argc)
  510. {
  511. critical_time = atoi (argv[x]);
  512. check_critical_time = TRUE;
  513. x++;
  514. }
  515. else
  516. return ERROR;
  517. }
  518. // else we got something else...
  519. else
  520. return ERROR;
  521. }
  522. return OK;
  523. }
  524. result = process_arguments (argc, argv);
  525. if (result != OK)
  526. {
  527. printf ("Incorrect number of arguments supplied\n");
  528. printf ("\n");
  529. print_revision(argv[0],"$Revision$");
  530. printf ("Copyright (c) 1999 Pedro Leite (leite@cic.ua.pt)\n");
  531. printf ("Last Modified: 30-10-1999\n");
  532. printf ("License: GPL\n");
  533. printf ("\n");
  534. printf ("Usage: %s <host_address> [-e expect] [-u url] [-p port] [-hn host_name] [-wt warn_time]\n",argv[0]);
  535. printf(" [-ct crit_time] [-to to_sec] [-a auth]\n");
  536. printf ("\n");
  537. printf ("Options:\n");
  538. printf (" [expect] = String to expect in first line of server response - default is \"%s\"\n", EXPECT);
  539. printf (" [url] = Optional URL to GET - default is root document\n");
  540. printf (" [port] = Optional port number to use - default is %d\n", PORT);
  541. printf (" [host_name] = Optional host name argument to GET command - used for servers using host headers\n");
  542. printf (" [warn_time] = Response time in seconds necessary to result in a warning status\n");
  543. printf (" [crit_time] = Response time in seconds necessary to result in a critical status\n");
  544. printf (" [to_sec] = Number of seconds before connection attempt times out - default is %d seconds\n", DEFAULT_SOCKET_TIMEOUT);
  545. printf (" [auth] = Optional username:password for sites requiring basic authentication\n");
  546. printf ("\n");
  547. printf ("This plugin attempts to contact the REAL service on the specified host.\n");
  548. printf ("If possible, supply an IP address for the host address, as this will bypass the DNS lookup.\n");
  549. printf ("\n");
  550. return STATE_UNKNOWN;
  551. }
  552. */