check_real.c 11 KB

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