check_real.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 = "";
  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\r\n", host_name, server_port);
  89. result = send (sd, buffer, strlen (buffer), 0);
  90. /* send the header sync */
  91. sprintf (buffer, "CSeq: 1\r\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, "\r\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. int option_index = 0;
  226. static struct option long_options[] = {
  227. {"hostname", required_argument, 0, 'H'},
  228. {"IPaddress", required_argument, 0, 'I'},
  229. {"expect", required_argument, 0, 'e'},
  230. {"url", required_argument, 0, 'u'},
  231. {"port", required_argument, 0, 'p'},
  232. {"critical", required_argument, 0, 'c'},
  233. {"warning", required_argument, 0, 'w'},
  234. {"timeout", required_argument, 0, 't'},
  235. {"verbose", no_argument, 0, 'v'},
  236. {"version", no_argument, 0, 'V'},
  237. {"help", no_argument, 0, 'h'},
  238. {0, 0, 0, 0}
  239. };
  240. if (argc < 2)
  241. return ERROR;
  242. for (c = 1; c < argc; c++) {
  243. if (strcmp ("-to", argv[c]) == 0)
  244. strcpy (argv[c], "-t");
  245. else if (strcmp ("-wt", argv[c]) == 0)
  246. strcpy (argv[c], "-w");
  247. else if (strcmp ("-ct", argv[c]) == 0)
  248. strcpy (argv[c], "-c");
  249. }
  250. while (1) {
  251. c = getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", long_options,
  252. &option_index);
  253. if (c == -1 || c == EOF)
  254. break;
  255. switch (c) {
  256. case 'I': /* hostname */
  257. case 'H': /* hostname */
  258. if (is_host (optarg)) {
  259. server_address = optarg;
  260. }
  261. else {
  262. usage ("Invalid host name\n");
  263. }
  264. break;
  265. case 'e': /* string to expect in response header */
  266. server_expect = optarg;
  267. break;
  268. case 'u': /* server URL */
  269. server_url = optarg;
  270. break;
  271. case 'p': /* port */
  272. if (is_intpos (optarg)) {
  273. server_port = atoi (optarg);
  274. }
  275. else {
  276. usage ("Server port must be a positive integer\n");
  277. }
  278. break;
  279. case 'w': /* warning time threshold */
  280. if (is_intnonneg (optarg)) {
  281. warning_time = atoi (optarg);
  282. check_warning_time = TRUE;
  283. }
  284. else {
  285. usage ("Warning time must be a nonnegative integer\n");
  286. }
  287. break;
  288. case 'c': /* critical time threshold */
  289. if (is_intnonneg (optarg)) {
  290. critical_time = atoi (optarg);
  291. check_critical_time = TRUE;
  292. }
  293. else {
  294. usage ("Critical time must be a nonnegative integer\n");
  295. }
  296. break;
  297. case 'v': /* verbose */
  298. verbose = TRUE;
  299. break;
  300. case 't': /* timeout */
  301. if (is_intnonneg (optarg)) {
  302. socket_timeout = atoi (optarg);
  303. }
  304. else {
  305. usage ("Time interval must be a nonnegative integer\n");
  306. }
  307. break;
  308. case 'V': /* version */
  309. print_revision (progname, "$Revision$");
  310. exit (STATE_OK);
  311. case 'h': /* help */
  312. print_help ();
  313. exit (STATE_OK);
  314. case '?': /* help */
  315. usage ("Invalid argument\n");
  316. }
  317. }
  318. c = optind;
  319. if (strlen(server_address)==0 && argc>c) {
  320. if (is_host (argv[c])) {
  321. server_address = argv[c++];
  322. }
  323. else {
  324. usage ("Invalid host name");
  325. }
  326. }
  327. if (strlen(server_address) == 0)
  328. usage ("You must provide a server to check\n");
  329. if (strlen(host_name) == 0)
  330. asprintf (&host_name, "%s", server_address);
  331. return validate_arguments ();
  332. }
  333. int
  334. validate_arguments (void)
  335. {
  336. return OK;
  337. }
  338. void
  339. print_help (void)
  340. {
  341. print_revision (progname, "$Revision$");
  342. printf
  343. ("Copyright (c) 2000 Pedro Leite (leite@cic.ua.pt)/Karl DeBisschop\n\n"
  344. "This plugin tests the REAL service on the specified host.\n\n");
  345. print_usage ();
  346. printf
  347. ("\nOptions:\n"
  348. " -H, --hostname=STRING or IPADDRESS\n"
  349. " Check this server on the indicated host\n"
  350. " -I, --IPaddress=STRING or IPADDRESS\n"
  351. " Check server at this host address\n"
  352. " -p, --port=INTEGER\n"
  353. " Make connection on the indicated port (default: %d)\n"
  354. " -u, --url=STRING\n"
  355. " Connect to this url\n"
  356. " -e, --expect=STRING\n"
  357. " String to expect in first line of server response (default: %s)\n"
  358. " -w, --warning=INTEGER\n"
  359. " Seconds necessary to result in a warning status\n"
  360. " -c, --critical=INTEGER\n"
  361. " Seconds necessary to result in a critical status\n"
  362. " -t, --timeout=INTEGER\n"
  363. " Seconds before connection attempt times out (default: %d)\n"
  364. " -v, --verbose\n"
  365. " Print extra information (command-line use only)\n"
  366. " -h, --help\n"
  367. " Print detailed help screen\n"
  368. " -V, --version\n"
  369. " Print version information\n\n",
  370. PORT, EXPECT, DEFAULT_SOCKET_TIMEOUT);
  371. support ();
  372. }
  373. void
  374. print_usage (void)
  375. {
  376. printf
  377. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
  378. " [-t timeout] [-v]\n"
  379. " %s --help\n"
  380. " %s --version\n", progname, progname, progname);
  381. }