check_real.c 11 KB

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