check_real.c 11 KB

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