check_real.c 11 KB

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