check_real.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*****************************************************************************
  2. *
  3. * Nagios check_real plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 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-2007";
  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 (socket_timeout);
  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 OPTIONS request */
  135. sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\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\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, "\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. /* return a CRITICAL status if we couldn't read any data */
  147. if (result == -1) {
  148. printf (_("No data received from host\n"));
  149. result = STATE_CRITICAL;
  150. }
  151. else {
  152. /* make sure we find the response we are looking for */
  153. if (!strstr (buffer, server_expect)) {
  154. if (server_port == PORT)
  155. printf ("%s\n", _("Invalid REAL response received from host"));
  156. else
  157. printf (_("Invalid REAL response received from host on port %d\n"),
  158. server_port);
  159. }
  160. else {
  161. /* else we got the REAL string, so check the return code */
  162. time (&end_time);
  163. result = STATE_OK;
  164. status_line = (char *) strtok (buffer, "\n");
  165. if (strstr (status_line, "200"))
  166. result = STATE_OK;
  167. /* client errors result in a warning state */
  168. else if (strstr (status_line, "400"))
  169. result = STATE_WARNING;
  170. else if (strstr (status_line, "401"))
  171. result = STATE_WARNING;
  172. else if (strstr (status_line, "402"))
  173. result = STATE_WARNING;
  174. else if (strstr (status_line, "403"))
  175. result = STATE_WARNING;
  176. else if (strstr (status_line, "404"))
  177. result = STATE_WARNING;
  178. /* server errors result in a critical state */
  179. else if (strstr (status_line, "500"))
  180. result = STATE_CRITICAL;
  181. else if (strstr (status_line, "501"))
  182. result = STATE_CRITICAL;
  183. else if (strstr (status_line, "502"))
  184. result = STATE_CRITICAL;
  185. else if (strstr (status_line, "503"))
  186. result = STATE_CRITICAL;
  187. else
  188. result = STATE_UNKNOWN;
  189. }
  190. }
  191. }
  192. /* Return results */
  193. if (result == STATE_OK) {
  194. if (check_critical_time == TRUE
  195. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  196. else if (check_warning_time == TRUE
  197. && (end_time - start_time) > warning_time) result =
  198. STATE_WARNING;
  199. /* Put some HTML in here to create a dynamic link */
  200. printf (_("REAL %s - %d second response time\n"),
  201. state_text (result),
  202. (int) (end_time - start_time));
  203. }
  204. else
  205. printf ("%s\n", status_line);
  206. /* close the connection */
  207. close (sd);
  208. /* reset the alarm */
  209. alarm (0);
  210. return result;
  211. }
  212. /* process command-line arguments */
  213. int
  214. process_arguments (int argc, char **argv)
  215. {
  216. int c;
  217. int option = 0;
  218. static struct option longopts[] = {
  219. {"hostname", required_argument, 0, 'H'},
  220. {"IPaddress", required_argument, 0, 'I'},
  221. {"expect", required_argument, 0, 'e'},
  222. {"url", required_argument, 0, 'u'},
  223. {"port", required_argument, 0, 'p'},
  224. {"critical", required_argument, 0, 'c'},
  225. {"warning", required_argument, 0, 'w'},
  226. {"timeout", required_argument, 0, 't'},
  227. {"verbose", no_argument, 0, 'v'},
  228. {"version", no_argument, 0, 'V'},
  229. {"help", no_argument, 0, 'h'},
  230. {0, 0, 0, 0}
  231. };
  232. if (argc < 2)
  233. return ERROR;
  234. for (c = 1; c < argc; c++) {
  235. if (strcmp ("-to", argv[c]) == 0)
  236. strcpy (argv[c], "-t");
  237. else if (strcmp ("-wt", argv[c]) == 0)
  238. strcpy (argv[c], "-w");
  239. else if (strcmp ("-ct", argv[c]) == 0)
  240. strcpy (argv[c], "-c");
  241. }
  242. while (1) {
  243. c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
  244. &option);
  245. if (c == -1 || c == EOF)
  246. break;
  247. switch (c) {
  248. case 'I': /* hostname */
  249. case 'H': /* hostname */
  250. if (server_address)
  251. break;
  252. else if (is_host (optarg))
  253. server_address = optarg;
  254. else
  255. usage2 (_("Invalid hostname/address"), optarg);
  256. break;
  257. case 'e': /* string to expect in response header */
  258. server_expect = optarg;
  259. break;
  260. case 'u': /* server URL */
  261. server_url = optarg;
  262. break;
  263. case 'p': /* port */
  264. if (is_intpos (optarg)) {
  265. server_port = atoi (optarg);
  266. }
  267. else {
  268. usage4 (_("Port must be a positive integer"));
  269. }
  270. break;
  271. case 'w': /* warning time threshold */
  272. if (is_intnonneg (optarg)) {
  273. warning_time = atoi (optarg);
  274. check_warning_time = TRUE;
  275. }
  276. else {
  277. usage4 (_("Warning time must be a positive integer"));
  278. }
  279. break;
  280. case 'c': /* critical time threshold */
  281. if (is_intnonneg (optarg)) {
  282. critical_time = atoi (optarg);
  283. check_critical_time = TRUE;
  284. }
  285. else {
  286. usage4 (_("Critical time must be a positive integer"));
  287. }
  288. break;
  289. case 'v': /* verbose */
  290. verbose = TRUE;
  291. break;
  292. case 't': /* timeout */
  293. if (is_intnonneg (optarg)) {
  294. socket_timeout = atoi (optarg);
  295. }
  296. else {
  297. usage4 (_("Timeout interval must be a positive integer"));
  298. }
  299. break;
  300. case 'V': /* version */
  301. print_revision (progname, NP_VERSION);
  302. exit (STATE_OK);
  303. case 'h': /* help */
  304. print_help ();
  305. exit (STATE_OK);
  306. case '?': /* usage */
  307. usage5 ();
  308. }
  309. }
  310. c = optind;
  311. if (server_address==NULL && argc>c) {
  312. if (is_host (argv[c])) {
  313. server_address = argv[c++];
  314. }
  315. else {
  316. usage2 (_("Invalid hostname/address"), argv[c]);
  317. }
  318. }
  319. if (server_address==NULL)
  320. usage4 (_("You must provide a server to check"));
  321. if (host_name==NULL)
  322. host_name = strdup (server_address);
  323. if (server_expect == NULL)
  324. server_expect = strdup(EXPECT);
  325. return validate_arguments ();
  326. }
  327. int
  328. validate_arguments (void)
  329. {
  330. return OK;
  331. }
  332. void
  333. print_help (void)
  334. {
  335. char *myport;
  336. xasprintf (&myport, "%d", PORT);
  337. print_revision (progname, NP_VERSION);
  338. printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
  339. printf (COPYRIGHT, copyright, email);
  340. printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
  341. printf ("\n\n");
  342. print_usage ();
  343. printf (UT_HELP_VRSN);
  344. printf (UT_EXTRA_OPTS);
  345. printf (UT_HOST_PORT, 'p', myport);
  346. printf (" %s\n", "-u, --url=STRING");
  347. printf (" %s\n", _("Connect to this url"));
  348. printf (" %s\n", "-e, --expect=STRING");
  349. printf (_("String to expect in first line of server response (default: %s)\n"),
  350. EXPECT);
  351. printf (UT_WARN_CRIT);
  352. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  353. printf (UT_VERBOSE);
  354. printf ("\n");
  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 ("%s\n", _("Usage:"));
  366. printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
  367. }