check_real.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /******************************************************************************
  2. *
  3. * Nagios check_real plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_real plugin
  13. *
  14. * This plugin tests the REAL service on the specified host.
  15. *
  16. *
  17. * License Information:
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. *
  34. * $Id$
  35. *
  36. ******************************************************************************/
  37. const char *progname = "check_real";
  38. const char *revision = "$Revision$";
  39. const char *copyright = "2000-2006";
  40. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  41. #include "common.h"
  42. #include "netutils.h"
  43. #include "utils.h"
  44. enum {
  45. PORT = 554
  46. };
  47. #define EXPECT "RTSP/1."
  48. #define URL ""
  49. int process_arguments (int, char **);
  50. int validate_arguments (void);
  51. void print_help (void);
  52. void print_usage (void);
  53. int server_port = PORT;
  54. char *server_address;
  55. char *host_name;
  56. char *server_url = NULL;
  57. char *server_expect;
  58. int warning_time = 0;
  59. int check_warning_time = FALSE;
  60. int critical_time = 0;
  61. int check_critical_time = FALSE;
  62. int verbose = FALSE;
  63. int
  64. main (int argc, char **argv)
  65. {
  66. int sd;
  67. int result = STATE_UNKNOWN;
  68. char buffer[MAX_INPUT_BUFFER];
  69. char *status_line = NULL;
  70. setlocale (LC_ALL, "");
  71. bindtextdomain (PACKAGE, LOCALEDIR);
  72. textdomain (PACKAGE);
  73. if (process_arguments (argc, argv) == ERROR)
  74. usage4 (_("Could not parse arguments"));
  75. /* initialize alarm signal handling */
  76. signal (SIGALRM, socket_timeout_alarm_handler);
  77. /* set socket timeout */
  78. alarm (socket_timeout);
  79. time (&start_time);
  80. /* try to connect to the host at the given port number */
  81. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
  82. die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
  83. server_address, server_port);
  84. /* Part I - Server Check */
  85. /* send the OPTIONS request */
  86. sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
  87. result = send (sd, buffer, strlen (buffer), 0);
  88. /* send the header sync */
  89. sprintf (buffer, "CSeq: 1\r\n");
  90. result = send (sd, buffer, strlen (buffer), 0);
  91. /* send a newline so the server knows we're done with the request */
  92. sprintf (buffer, "\r\n");
  93. result = send (sd, buffer, strlen (buffer), 0);
  94. /* watch for the REAL connection string */
  95. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  96. /* return a CRITICAL status if we couldn't read any data */
  97. if (result == -1)
  98. die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
  99. /* make sure we find the response we are looking for */
  100. if (!strstr (buffer, server_expect)) {
  101. if (server_port == PORT)
  102. printf ("%s\n", _("Invalid REAL response received from host"));
  103. else
  104. printf (_("Invalid REAL response received from host on port %d\n"),
  105. server_port);
  106. }
  107. else {
  108. /* else we got the REAL string, so check the return code */
  109. time (&end_time);
  110. result = STATE_OK;
  111. status_line = (char *) strtok (buffer, "\n");
  112. if (strstr (status_line, "200"))
  113. result = STATE_OK;
  114. /* client errors result in a warning state */
  115. else if (strstr (status_line, "400"))
  116. result = STATE_WARNING;
  117. else if (strstr (status_line, "401"))
  118. result = STATE_WARNING;
  119. else if (strstr (status_line, "402"))
  120. result = STATE_WARNING;
  121. else if (strstr (status_line, "403"))
  122. result = STATE_WARNING;
  123. else if (strstr (status_line, "404"))
  124. result = STATE_WARNING;
  125. /* server errors result in a critical state */
  126. else if (strstr (status_line, "500"))
  127. result = STATE_CRITICAL;
  128. else if (strstr (status_line, "501"))
  129. result = STATE_CRITICAL;
  130. else if (strstr (status_line, "502"))
  131. result = STATE_CRITICAL;
  132. else if (strstr (status_line, "503"))
  133. result = STATE_CRITICAL;
  134. else
  135. result = STATE_UNKNOWN;
  136. }
  137. /* Part II - Check stream exists and is ok */
  138. if ((result == STATE_OK )&& (server_url != NULL) ) {
  139. /* Part I - Server Check */
  140. /* send the OPTIONS request */
  141. sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
  142. server_port, server_url);
  143. result = send (sd, buffer, strlen (buffer), 0);
  144. /* send the header sync */
  145. sprintf (buffer, "CSeq: 2\n");
  146. result = send (sd, buffer, strlen (buffer), 0);
  147. /* send a newline so the server knows we're done with the request */
  148. sprintf (buffer, "\n");
  149. result = send (sd, buffer, strlen (buffer), 0);
  150. /* watch for the REAL connection string */
  151. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  152. /* return a CRITICAL status if we couldn't read any data */
  153. if (result == -1) {
  154. printf (_("No data received from host\n"));
  155. result = STATE_CRITICAL;
  156. }
  157. else {
  158. /* make sure we find the response we are looking for */
  159. if (!strstr (buffer, server_expect)) {
  160. if (server_port == PORT)
  161. printf ("%s\n", _("Invalid REAL response received from host"));
  162. else
  163. printf (_("Invalid REAL response received from host on port %d\n"),
  164. server_port);
  165. }
  166. else {
  167. /* else we got the REAL string, so check the return code */
  168. time (&end_time);
  169. result = STATE_OK;
  170. status_line = (char *) strtok (buffer, "\n");
  171. if (strstr (status_line, "200"))
  172. result = STATE_OK;
  173. /* client errors result in a warning state */
  174. else if (strstr (status_line, "400"))
  175. result = STATE_WARNING;
  176. else if (strstr (status_line, "401"))
  177. result = STATE_WARNING;
  178. else if (strstr (status_line, "402"))
  179. result = STATE_WARNING;
  180. else if (strstr (status_line, "403"))
  181. result = STATE_WARNING;
  182. else if (strstr (status_line, "404"))
  183. result = STATE_WARNING;
  184. /* server errors result in a critical state */
  185. else if (strstr (status_line, "500"))
  186. result = STATE_CRITICAL;
  187. else if (strstr (status_line, "501"))
  188. result = STATE_CRITICAL;
  189. else if (strstr (status_line, "502"))
  190. result = STATE_CRITICAL;
  191. else if (strstr (status_line, "503"))
  192. result = STATE_CRITICAL;
  193. else
  194. result = STATE_UNKNOWN;
  195. }
  196. }
  197. }
  198. /* Return results */
  199. if (result == STATE_OK) {
  200. if (check_critical_time == TRUE
  201. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  202. else if (check_warning_time == TRUE
  203. && (end_time - start_time) > warning_time) result =
  204. STATE_WARNING;
  205. /* Put some HTML in here to create a dynamic link */
  206. printf (_("REAL %s - %d second response time\n"),
  207. state_text (result),
  208. (int) (end_time - start_time));
  209. }
  210. else
  211. printf ("%s\n", status_line);
  212. /* close the connection */
  213. close (sd);
  214. /* reset the alarm */
  215. alarm (0);
  216. return result;
  217. }
  218. /* process command-line arguments */
  219. int
  220. process_arguments (int argc, char **argv)
  221. {
  222. int c;
  223. int option = 0;
  224. static struct option longopts[] = {
  225. {"hostname", required_argument, 0, 'H'},
  226. {"IPaddress", required_argument, 0, 'I'},
  227. {"expect", required_argument, 0, 'e'},
  228. {"url", required_argument, 0, 'u'},
  229. {"port", required_argument, 0, 'p'},
  230. {"critical", required_argument, 0, 'c'},
  231. {"warning", required_argument, 0, 'w'},
  232. {"timeout", required_argument, 0, 't'},
  233. {"verbose", no_argument, 0, 'v'},
  234. {"version", no_argument, 0, 'V'},
  235. {"help", no_argument, 0, 'h'},
  236. {0, 0, 0, 0}
  237. };
  238. if (argc < 2)
  239. return ERROR;
  240. for (c = 1; c < argc; c++) {
  241. if (strcmp ("-to", argv[c]) == 0)
  242. strcpy (argv[c], "-t");
  243. else if (strcmp ("-wt", argv[c]) == 0)
  244. strcpy (argv[c], "-w");
  245. else if (strcmp ("-ct", argv[c]) == 0)
  246. strcpy (argv[c], "-c");
  247. }
  248. while (1) {
  249. c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
  250. &option);
  251. if (c == -1 || c == EOF)
  252. break;
  253. switch (c) {
  254. case 'I': /* hostname */
  255. case 'H': /* hostname */
  256. if (server_address)
  257. break;
  258. else if (is_host (optarg))
  259. server_address = optarg;
  260. else
  261. usage2 (_("Invalid hostname/address"), optarg);
  262. break;
  263. case 'e': /* string to expect in response header */
  264. server_expect = optarg;
  265. break;
  266. case 'u': /* server URL */
  267. server_url = optarg;
  268. break;
  269. case 'p': /* port */
  270. if (is_intpos (optarg)) {
  271. server_port = atoi (optarg);
  272. }
  273. else {
  274. usage4 (_("Port must be a positive integer"));
  275. }
  276. break;
  277. case 'w': /* warning time threshold */
  278. if (is_intnonneg (optarg)) {
  279. warning_time = atoi (optarg);
  280. check_warning_time = TRUE;
  281. }
  282. else {
  283. usage4 (_("Warning time must be a positive integer"));
  284. }
  285. break;
  286. case 'c': /* critical time threshold */
  287. if (is_intnonneg (optarg)) {
  288. critical_time = atoi (optarg);
  289. check_critical_time = TRUE;
  290. }
  291. else {
  292. usage4 (_("Critical time must be a positive integer"));
  293. }
  294. break;
  295. case 'v': /* verbose */
  296. verbose = TRUE;
  297. break;
  298. case 't': /* timeout */
  299. if (is_intnonneg (optarg)) {
  300. socket_timeout = atoi (optarg);
  301. }
  302. else {
  303. usage4 (_("Timeout interval must be a positive integer"));
  304. }
  305. break;
  306. case 'V': /* version */
  307. print_revision (progname, revision);
  308. exit (STATE_OK);
  309. case 'h': /* help */
  310. print_help ();
  311. exit (STATE_OK);
  312. case '?': /* usage */
  313. usage5 ();
  314. }
  315. }
  316. c = optind;
  317. if (server_address==NULL && argc>c) {
  318. if (is_host (argv[c])) {
  319. server_address = argv[c++];
  320. }
  321. else {
  322. usage2 (_("Invalid hostname/address"), argv[c]);
  323. }
  324. }
  325. if (server_address==NULL)
  326. usage4 (_("You must provide a server to check"));
  327. if (host_name==NULL)
  328. host_name = strdup (server_address);
  329. if (server_expect == NULL)
  330. server_expect = strdup(EXPECT);
  331. return validate_arguments ();
  332. }
  333. int
  334. validate_arguments (void)
  335. {
  336. return OK;
  337. }
  338. void
  339. print_help (void)
  340. {
  341. char *myport;
  342. asprintf (&myport, "%d", PORT);
  343. print_revision (progname, revision);
  344. printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
  345. printf (COPYRIGHT, copyright, email);
  346. printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
  347. printf ("\n\n");
  348. print_usage ();
  349. printf (_(UT_HELP_VRSN));
  350. printf (_(UT_HOST_PORT), 'p', myport);
  351. printf (" %s\n", "-u, --url=STRING");
  352. printf (" %s\n", _("Connect to this url"));
  353. printf (" %s\n", "-e, --expect=STRING");
  354. printf (_("String to expect in first line of server response (default: %s)\n"),
  355. EXPECT);
  356. printf (_(UT_WARN_CRIT));
  357. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  358. printf (_(UT_VERBOSE));
  359. printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
  360. printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
  361. printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
  362. printf ("%s\n", _("but incorrect reponse messages from the host result in STATE_WARNING return"));
  363. printf ("%s\n", _("values."));
  364. printf (_(UT_SUPPORT));
  365. }
  366. void
  367. print_usage (void)
  368. {
  369. printf (_("Usage:"));
  370. printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
  371. }