4
0

check_real.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. if (send (sd, buffer, strlen (buffer), 0) == -1)
  82. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  83. /* send the header sync */
  84. sprintf (buffer, "CSeq: 1\r\n");
  85. if (send (sd, buffer, strlen (buffer), 0) == -1)
  86. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  87. /* send a newline so the server knows we're done with the request */
  88. sprintf (buffer, "\r\n");
  89. if (send (sd, buffer, strlen (buffer), 0) == -1)
  90. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  91. /* watch for the REAL connection string */
  92. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  93. /* return a CRITICAL status if we couldn't read any data */
  94. die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
  95. }
  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. 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 DESCRIBE request */
  137. sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name,
  138. server_port, server_url);
  139. if (send (sd, buffer, strlen (buffer), 0) == -1)
  140. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  141. /* send the header sync */
  142. sprintf (buffer, "CSeq: 2\r\n");
  143. if (send (sd, buffer, strlen (buffer), 0) == -1)
  144. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  145. /* send a newline so the server knows we're done with the request */
  146. sprintf (buffer, "\r\n");
  147. if (send (sd, buffer, strlen (buffer), 0) == -1)
  148. die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
  149. /* watch for the REAL connection string */
  150. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  151. buffer[result] = '\0'; /* null terminate received buffer */
  152. /* return a CRITICAL status if we couldn't read any data */
  153. if (result == -1) {
  154. printf (_("No data received from host\n"));
  155. result = STATE_CRITICAL;
  156. }
  157. else {
  158. /* make sure we find the response we are looking for */
  159. if (!strstr (buffer, server_expect)) {
  160. if (server_port == PORT)
  161. printf ("%s\n", _("Invalid REAL response received from host"));
  162. else
  163. printf (_("Invalid REAL response received from host on port %d\n"),
  164. server_port);
  165. }
  166. else {
  167. /* else we got the REAL string, so check the return code */
  168. time (&end_time);
  169. status_line = (char *) strtok (buffer, "\n");
  170. if (strstr (status_line, "200"))
  171. result = STATE_OK;
  172. /* client errors result in a warning state */
  173. else if (strstr (status_line, "400"))
  174. result = STATE_WARNING;
  175. else if (strstr (status_line, "401"))
  176. result = STATE_WARNING;
  177. else if (strstr (status_line, "402"))
  178. result = STATE_WARNING;
  179. else if (strstr (status_line, "403"))
  180. result = STATE_WARNING;
  181. else if (strstr (status_line, "404"))
  182. result = STATE_WARNING;
  183. /* server errors result in a critical state */
  184. else if (strstr (status_line, "500"))
  185. result = STATE_CRITICAL;
  186. else if (strstr (status_line, "501"))
  187. result = STATE_CRITICAL;
  188. else if (strstr (status_line, "502"))
  189. result = STATE_CRITICAL;
  190. else if (strstr (status_line, "503"))
  191. result = STATE_CRITICAL;
  192. else
  193. result = STATE_UNKNOWN;
  194. }
  195. }
  196. }
  197. /* Return results */
  198. if (result == STATE_OK) {
  199. if (check_critical_time == TRUE
  200. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  201. else if (check_warning_time == TRUE
  202. && (end_time - start_time) > warning_time) result =
  203. STATE_WARNING;
  204. /* Put some HTML in here to create a dynamic link */
  205. printf (_("REAL %s - %d second response time\n"),
  206. state_text (result),
  207. (int) (end_time - start_time));
  208. }
  209. else
  210. printf ("%s\n", status_line);
  211. /* close the connection */
  212. close (sd);
  213. /* reset the alarm */
  214. alarm (0);
  215. return result;
  216. }
  217. /* process command-line arguments */
  218. int
  219. process_arguments (int argc, char **argv)
  220. {
  221. int c;
  222. int option = 0;
  223. static struct option longopts[] = {
  224. {"hostname", required_argument, 0, 'H'},
  225. {"IPaddress", required_argument, 0, 'I'},
  226. {"expect", required_argument, 0, 'e'},
  227. {"url", required_argument, 0, 'u'},
  228. {"port", required_argument, 0, 'p'},
  229. {"critical", required_argument, 0, 'c'},
  230. {"warning", required_argument, 0, 'w'},
  231. {"timeout", required_argument, 0, 't'},
  232. {"verbose", no_argument, 0, 'v'},
  233. {"version", no_argument, 0, 'V'},
  234. {"help", no_argument, 0, 'h'},
  235. {0, 0, 0, 0}
  236. };
  237. if (argc < 2)
  238. return ERROR;
  239. for (c = 1; c < argc; c++) {
  240. if (strcmp ("-to", argv[c]) == 0)
  241. strcpy (argv[c], "-t");
  242. else if (strcmp ("-wt", argv[c]) == 0)
  243. strcpy (argv[c], "-w");
  244. else if (strcmp ("-ct", argv[c]) == 0)
  245. strcpy (argv[c], "-c");
  246. }
  247. while (1) {
  248. c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
  249. &option);
  250. if (c == -1 || c == EOF)
  251. break;
  252. switch (c) {
  253. case 'I': /* hostname */
  254. case 'H': /* hostname */
  255. if (server_address)
  256. break;
  257. else if (is_host (optarg))
  258. server_address = optarg;
  259. else
  260. usage2 (_("Invalid hostname/address"), optarg);
  261. break;
  262. case 'e': /* string to expect in response header */
  263. server_expect = optarg;
  264. break;
  265. case 'u': /* server URL */
  266. server_url = optarg;
  267. break;
  268. case 'p': /* port */
  269. if (is_intpos (optarg)) {
  270. server_port = atoi (optarg);
  271. }
  272. else {
  273. usage4 (_("Port must be a positive integer"));
  274. }
  275. break;
  276. case 'w': /* warning time threshold */
  277. if (is_intnonneg (optarg)) {
  278. warning_time = atoi (optarg);
  279. check_warning_time = TRUE;
  280. }
  281. else {
  282. usage4 (_("Warning time must be a positive integer"));
  283. }
  284. break;
  285. case 'c': /* critical time threshold */
  286. if (is_intnonneg (optarg)) {
  287. critical_time = atoi (optarg);
  288. check_critical_time = TRUE;
  289. }
  290. else {
  291. usage4 (_("Critical time must be a positive integer"));
  292. }
  293. break;
  294. case 'v': /* verbose */
  295. verbose = TRUE;
  296. break;
  297. case 't': /* timeout */
  298. timeout_interval = parse_timeout_string (optarg);
  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 response 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. }