check_real.c 11 KB

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