check_real.c 16 KB

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