check_real.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /* Parse extra opts if any */
  70. argv=np_extra_opts (&argc, argv, progname);
  71. if (process_arguments (argc, argv) == ERROR)
  72. usage4 (_("Could not parse arguments"));
  73. /* initialize alarm signal handling */
  74. signal (SIGALRM, socket_timeout_alarm_handler);
  75. /* set socket timeout */
  76. alarm (socket_timeout);
  77. time (&start_time);
  78. /* try to connect to the host at the given port number */
  79. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
  80. die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
  81. server_address, server_port);
  82. /* Part I - Server Check */
  83. /* send the OPTIONS request */
  84. sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
  85. result = send (sd, buffer, strlen (buffer), 0);
  86. /* send the header sync */
  87. sprintf (buffer, "CSeq: 1\r\n");
  88. result = send (sd, buffer, strlen (buffer), 0);
  89. /* send a newline so the server knows we're done with the request */
  90. sprintf (buffer, "\r\n");
  91. result = send (sd, buffer, strlen (buffer), 0);
  92. /* watch for the REAL connection string */
  93. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  94. /* return a CRITICAL status if we couldn't read any data */
  95. if (result == -1)
  96. die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
  97. /* make sure we find the response we are looking for */
  98. if (!strstr (buffer, server_expect)) {
  99. if (server_port == PORT)
  100. printf ("%s\n", _("Invalid REAL response received from host"));
  101. else
  102. printf (_("Invalid REAL response received from host on port %d\n"),
  103. server_port);
  104. }
  105. else {
  106. /* else we got the REAL string, so check the return code */
  107. time (&end_time);
  108. result = STATE_OK;
  109. status_line = (char *) strtok (buffer, "\n");
  110. if (strstr (status_line, "200"))
  111. result = STATE_OK;
  112. /* client errors result in a warning state */
  113. else if (strstr (status_line, "400"))
  114. result = STATE_WARNING;
  115. else if (strstr (status_line, "401"))
  116. result = STATE_WARNING;
  117. else if (strstr (status_line, "402"))
  118. result = STATE_WARNING;
  119. else if (strstr (status_line, "403"))
  120. result = STATE_WARNING;
  121. else if (strstr (status_line, "404"))
  122. result = STATE_WARNING;
  123. /* server errors result in a critical state */
  124. else if (strstr (status_line, "500"))
  125. result = STATE_CRITICAL;
  126. else if (strstr (status_line, "501"))
  127. result = STATE_CRITICAL;
  128. else if (strstr (status_line, "502"))
  129. result = STATE_CRITICAL;
  130. else if (strstr (status_line, "503"))
  131. result = STATE_CRITICAL;
  132. else
  133. result = STATE_UNKNOWN;
  134. }
  135. /* Part II - Check stream exists and is ok */
  136. if ((result == STATE_OK )&& (server_url != NULL) ) {
  137. /* Part I - Server Check */
  138. /* send the OPTIONS request */
  139. sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
  140. server_port, server_url);
  141. result = send (sd, buffer, strlen (buffer), 0);
  142. /* send the header sync */
  143. sprintf (buffer, "CSeq: 2\n");
  144. result = send (sd, buffer, strlen (buffer), 0);
  145. /* send a newline so the server knows we're done with the request */
  146. sprintf (buffer, "\n");
  147. result = send (sd, buffer, strlen (buffer), 0);
  148. /* watch for the REAL connection string */
  149. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  150. /* return a CRITICAL status if we couldn't read any data */
  151. if (result == -1) {
  152. printf (_("No data received from host\n"));
  153. result = STATE_CRITICAL;
  154. }
  155. else {
  156. /* make sure we find the response we are looking for */
  157. if (!strstr (buffer, server_expect)) {
  158. if (server_port == PORT)
  159. printf ("%s\n", _("Invalid REAL response received from host"));
  160. else
  161. printf (_("Invalid REAL response received from host on port %d\n"),
  162. server_port);
  163. }
  164. else {
  165. /* else we got the REAL string, so check the return code */
  166. time (&end_time);
  167. result = STATE_OK;
  168. status_line = (char *) strtok (buffer, "\n");
  169. if (strstr (status_line, "200"))
  170. result = STATE_OK;
  171. /* client errors result in a warning state */
  172. else if (strstr (status_line, "400"))
  173. result = STATE_WARNING;
  174. else if (strstr (status_line, "401"))
  175. result = STATE_WARNING;
  176. else if (strstr (status_line, "402"))
  177. result = STATE_WARNING;
  178. else if (strstr (status_line, "403"))
  179. result = STATE_WARNING;
  180. else if (strstr (status_line, "404"))
  181. result = STATE_WARNING;
  182. /* server errors result in a critical state */
  183. else if (strstr (status_line, "500"))
  184. result = STATE_CRITICAL;
  185. else if (strstr (status_line, "501"))
  186. result = STATE_CRITICAL;
  187. else if (strstr (status_line, "502"))
  188. result = STATE_CRITICAL;
  189. else if (strstr (status_line, "503"))
  190. result = STATE_CRITICAL;
  191. else
  192. result = STATE_UNKNOWN;
  193. }
  194. }
  195. }
  196. /* Return results */
  197. if (result == STATE_OK) {
  198. if (check_critical_time == TRUE
  199. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  200. else if (check_warning_time == TRUE
  201. && (end_time - start_time) > warning_time) result =
  202. STATE_WARNING;
  203. /* Put some HTML in here to create a dynamic link */
  204. printf (_("REAL %s - %d second response time\n"),
  205. state_text (result),
  206. (int) (end_time - start_time));
  207. }
  208. else
  209. printf ("%s\n", status_line);
  210. /* close the connection */
  211. close (sd);
  212. /* reset the alarm */
  213. alarm (0);
  214. return result;
  215. }
  216. /* process command-line arguments */
  217. int
  218. process_arguments (int argc, char **argv)
  219. {
  220. int c;
  221. int option = 0;
  222. static struct option longopts[] = {
  223. {"hostname", required_argument, 0, 'H'},
  224. {"IPaddress", required_argument, 0, 'I'},
  225. {"expect", required_argument, 0, 'e'},
  226. {"url", required_argument, 0, 'u'},
  227. {"port", required_argument, 0, 'p'},
  228. {"critical", required_argument, 0, 'c'},
  229. {"warning", required_argument, 0, 'w'},
  230. {"timeout", required_argument, 0, 't'},
  231. {"verbose", no_argument, 0, 'v'},
  232. {"version", no_argument, 0, 'V'},
  233. {"help", no_argument, 0, 'h'},
  234. {0, 0, 0, 0}
  235. };
  236. if (argc < 2)
  237. return ERROR;
  238. for (c = 1; c < argc; c++) {
  239. if (strcmp ("-to", argv[c]) == 0)
  240. strcpy (argv[c], "-t");
  241. else if (strcmp ("-wt", argv[c]) == 0)
  242. strcpy (argv[c], "-w");
  243. else if (strcmp ("-ct", argv[c]) == 0)
  244. strcpy (argv[c], "-c");
  245. }
  246. while (1) {
  247. c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
  248. &option);
  249. if (c == -1 || c == EOF)
  250. break;
  251. switch (c) {
  252. case 'I': /* hostname */
  253. case 'H': /* hostname */
  254. if (server_address)
  255. break;
  256. else if (is_host (optarg))
  257. server_address = optarg;
  258. else
  259. usage2 (_("Invalid hostname/address"), optarg);
  260. break;
  261. case 'e': /* string to expect in response header */
  262. server_expect = optarg;
  263. break;
  264. case 'u': /* server URL */
  265. server_url = optarg;
  266. break;
  267. case 'p': /* port */
  268. if (is_intpos (optarg)) {
  269. server_port = atoi (optarg);
  270. }
  271. else {
  272. usage4 (_("Port must be a positive integer"));
  273. }
  274. break;
  275. case 'w': /* warning time threshold */
  276. if (is_intnonneg (optarg)) {
  277. warning_time = atoi (optarg);
  278. check_warning_time = TRUE;
  279. }
  280. else {
  281. usage4 (_("Warning time must be a positive integer"));
  282. }
  283. break;
  284. case 'c': /* critical time threshold */
  285. if (is_intnonneg (optarg)) {
  286. critical_time = atoi (optarg);
  287. check_critical_time = TRUE;
  288. }
  289. else {
  290. usage4 (_("Critical time must be a positive integer"));
  291. }
  292. break;
  293. case 'v': /* verbose */
  294. verbose = TRUE;
  295. break;
  296. case 't': /* timeout */
  297. if (is_intnonneg (optarg)) {
  298. socket_timeout = atoi (optarg);
  299. }
  300. else {
  301. usage4 (_("Timeout interval must be a positive integer"));
  302. }
  303. break;
  304. case 'V': /* version */
  305. print_revision (progname, revision);
  306. exit (STATE_OK);
  307. case 'h': /* help */
  308. print_help ();
  309. exit (STATE_OK);
  310. case '?': /* usage */
  311. usage5 ();
  312. }
  313. }
  314. c = optind;
  315. if (server_address==NULL && argc>c) {
  316. if (is_host (argv[c])) {
  317. server_address = argv[c++];
  318. }
  319. else {
  320. usage2 (_("Invalid hostname/address"), argv[c]);
  321. }
  322. }
  323. if (server_address==NULL)
  324. usage4 (_("You must provide a server to check"));
  325. if (host_name==NULL)
  326. host_name = strdup (server_address);
  327. if (server_expect == NULL)
  328. server_expect = strdup(EXPECT);
  329. return validate_arguments ();
  330. }
  331. int
  332. validate_arguments (void)
  333. {
  334. return OK;
  335. }
  336. void
  337. print_help (void)
  338. {
  339. char *myport;
  340. asprintf (&myport, "%d", PORT);
  341. print_revision (progname, revision);
  342. printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
  343. printf (COPYRIGHT, copyright, email);
  344. printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
  345. printf ("\n\n");
  346. print_usage ();
  347. printf (_(UT_HELP_VRSN));
  348. printf (_(UT_EXTRA_OPTS));
  349. printf (_(UT_HOST_PORT), 'p', myport);
  350. printf (" %s\n", "-u, --url=STRING");
  351. printf (" %s\n", _("Connect to this url"));
  352. printf (" %s\n", "-e, --expect=STRING");
  353. printf (_("String to expect in first line of server response (default: %s)\n"),
  354. EXPECT);
  355. printf (_(UT_WARN_CRIT));
  356. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  357. printf (_(UT_VERBOSE));
  358. printf ("\n");
  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. #ifdef NP_EXTRA_OPTS
  365. printf ("\n");
  366. printf ("%s\n", _("Notes:"));
  367. printf (_(UT_EXTRA_OPTS_NOTES));
  368. #endif
  369. printf (_(UT_SUPPORT));
  370. }
  371. void
  372. print_usage (void)
  373. {
  374. printf (_("Usage:"));
  375. printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
  376. }