check_pop.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /******************************************************************************
  2. *
  3. * CHECK_POP.C
  4. *
  5. * Program: POP3 plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * $Id$
  10. *
  11. * Description:
  12. *
  13. * This plugin will attempt to open an POP connection with the host.
  14. * Successul connects return STATE_OK, refusals and timeouts return
  15. * STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful
  16. * connects, but incorrect reponse messages from the host result in
  17. * STATE_WARNING return values.
  18. *
  19. * License Information:
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. *
  35. *****************************************************************************/
  36. #include "config.h"
  37. #include "common.h"
  38. #include "netutils.h"
  39. #include "utils.h"
  40. #define PROGNAME "check_pop"
  41. #define POP_PORT 110
  42. #define POP_EXPECT "+OK"
  43. #define POP_QUIT "QUIT\n"
  44. int process_arguments (int, char **);
  45. int call_getopt (int, char **);
  46. int validate_arguments (void);
  47. int check_disk (int usp, int free_disk);
  48. void print_help (void);
  49. void print_usage (void);
  50. int server_port = POP_PORT;
  51. char *server_address = NULL;
  52. char *server_expect = NULL;
  53. int warning_time = 0;
  54. int check_warning_time = FALSE;
  55. int critical_time = 0;
  56. int check_critical_time = FALSE;
  57. int verbose = FALSE;
  58. int
  59. main (int argc, char **argv)
  60. {
  61. int sd;
  62. int result;
  63. char buffer[MAX_INPUT_BUFFER];
  64. if (process_arguments (argc, argv) != OK)
  65. usage ("Invalid command arguments supplied\n");
  66. /* initialize alarm signal handling */
  67. signal (SIGALRM, socket_timeout_alarm_handler);
  68. /* set socket timeout */
  69. alarm (socket_timeout);
  70. /* try to connect to the host at the given port number */
  71. time (&start_time);
  72. result = my_tcp_connect (server_address, server_port, &sd);
  73. /* we connected, so close connection before exiting */
  74. if (result == STATE_OK) {
  75. /* watch for the SMTP connection string */
  76. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  77. /* strip the buffer of carriage returns */
  78. strip (buffer);
  79. /* return a WARNING status if we couldn't read any data */
  80. if (result == -1) {
  81. printf ("recv() failed\n");
  82. result = STATE_WARNING;
  83. }
  84. else {
  85. /* make sure we find the response we are looking for */
  86. if (!strstr (buffer, server_expect)) {
  87. if (server_port == POP_PORT)
  88. printf ("Invalid POP response received from host\n");
  89. else
  90. printf ("Invalid POP response received from host on port %d\n",
  91. server_port);
  92. result = STATE_WARNING;
  93. }
  94. else {
  95. time (&end_time);
  96. result = STATE_OK;
  97. if (check_critical_time == TRUE
  98. && (end_time - start_time) > critical_time) result =
  99. STATE_CRITICAL;
  100. else if (check_warning_time == TRUE
  101. && (end_time - start_time) > warning_time) result =
  102. STATE_WARNING;
  103. if (verbose == TRUE)
  104. printf ("POP %s - %d sec. response time, %s\n",
  105. (result == STATE_OK) ? "ok" : "problem",
  106. (int) (end_time - start_time), buffer);
  107. else
  108. printf ("POP %s - %d second response time\n",
  109. (result == STATE_OK) ? "ok" : "problem",
  110. (int) (end_time - start_time));
  111. }
  112. }
  113. /* close the connection */
  114. send (sd, POP_QUIT, strlen (POP_QUIT), 0);
  115. close (sd);
  116. }
  117. /* reset the alarm */
  118. alarm (0);
  119. return result;
  120. }
  121. /* process command-line arguments */
  122. int
  123. process_arguments (int argc, char **argv)
  124. {
  125. int c;
  126. if (argc < 2)
  127. return ERROR;
  128. for (c = 1; c < argc; c++) {
  129. if (strcmp ("-to", argv[c]) == 0)
  130. strcpy (argv[c], "-t");
  131. else if (strcmp ("-wt", argv[c]) == 0)
  132. strcpy (argv[c], "-w");
  133. else if (strcmp ("-ct", argv[c]) == 0)
  134. strcpy (argv[c], "-c");
  135. }
  136. c = 0;
  137. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  138. if (is_option (argv[c]))
  139. continue;
  140. if (server_address == NULL) {
  141. if (is_host (argv[c])) {
  142. server_address = argv[c];
  143. }
  144. else {
  145. usage ("Invalid host name");
  146. }
  147. }
  148. }
  149. if (server_address == NULL)
  150. server_address = strscpy (NULL, "127.0.0.1");
  151. if (server_expect == NULL)
  152. server_expect = strscpy (NULL, POP_EXPECT);
  153. return validate_arguments ();
  154. }
  155. int
  156. call_getopt (int argc, char **argv)
  157. {
  158. int c, i = 0;
  159. #ifdef HAVE_GETOPT_H
  160. int option_index = 0;
  161. static struct option long_options[] = {
  162. {"hostname", required_argument, 0, 'H'},
  163. {"expect", required_argument, 0, 'e'},
  164. {"critical", required_argument, 0, 'c'},
  165. {"warning", required_argument, 0, 'w'},
  166. {"port", required_argument, 0, 'P'},
  167. {"verbose", no_argument, 0, 'v'},
  168. {"version", no_argument, 0, 'V'},
  169. {"help", no_argument, 0, 'h'},
  170. {0, 0, 0, 0}
  171. };
  172. #endif
  173. while (1) {
  174. #ifdef HAVE_GETOPT_H
  175. c =
  176. getopt_long (argc, argv, "+hVvt:p:e:c:w:H:", long_options,
  177. &option_index);
  178. #else
  179. c = getopt (argc, argv, "+?hVvt:p:e:c:w:H:");
  180. #endif
  181. i++;
  182. if (c == -1 || c == EOF || c == 1)
  183. break;
  184. switch (c) {
  185. case 't':
  186. case 'p':
  187. case 'e':
  188. case 'c':
  189. case 'w':
  190. case 'H':
  191. i++;
  192. }
  193. switch (c) {
  194. case 'H': /* hostname */
  195. if (is_host (optarg)) {
  196. server_address = optarg;
  197. }
  198. else {
  199. usage ("Invalid host name\n");
  200. }
  201. break;
  202. case 'p': /* port */
  203. if (is_intpos (optarg)) {
  204. server_port = atoi (optarg);
  205. }
  206. else {
  207. usage ("Server port must be a positive integer\n");
  208. }
  209. break;
  210. case 'e': /* username */
  211. server_expect = optarg;
  212. break;
  213. case 'c': /* critical time threshold */
  214. if (is_intnonneg (optarg)) {
  215. critical_time = atoi (optarg);
  216. check_critical_time = TRUE;
  217. }
  218. else {
  219. usage ("Critical time must be a nonnegative integer\n");
  220. }
  221. break;
  222. case 'w': /* warning time threshold */
  223. if (is_intnonneg (optarg)) {
  224. warning_time = atoi (optarg);
  225. check_warning_time = TRUE;
  226. }
  227. else {
  228. usage ("Warning time must be a nonnegative integer\n");
  229. }
  230. break;
  231. case 'v': /* verbose */
  232. verbose = TRUE;
  233. break;
  234. case 't': /* timeout */
  235. if (is_intnonneg (optarg)) {
  236. socket_timeout = atoi (optarg);
  237. }
  238. else {
  239. usage ("Time interval must be a nonnegative integer\n");
  240. }
  241. break;
  242. case 'V': /* version */
  243. print_revision (PROGNAME, "$Revision$");
  244. exit (STATE_OK);
  245. case 'h': /* help */
  246. print_help ();
  247. exit (STATE_OK);
  248. case '?': /* help */
  249. usage ("Invalid argument\n");
  250. }
  251. }
  252. return i;
  253. }
  254. int
  255. validate_arguments (void)
  256. {
  257. return OK;
  258. }
  259. void
  260. print_help (void)
  261. {
  262. print_revision (PROGNAME, "$Revision$");
  263. printf
  264. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  265. "This plugin tests the POP service on the specified host.\n\n");
  266. print_usage ();
  267. printf
  268. ("\nOptions:\n"
  269. " -H, --hostname=STRING or IPADDRESS\n"
  270. " Check server on the indicated host\n"
  271. " -p, --port=INTEGER\n"
  272. " Make connection on the indicated port (default: %d)\n"
  273. " -e, --expect=STRING\n"
  274. " String to expect in first line of server response (default: %s)\n"
  275. " -w, --warning=INTEGER\n"
  276. " Seconds necessary to result in a warning status\n"
  277. " -c, --critical=INTEGER\n"
  278. " Seconds necessary to result in a critical status\n"
  279. " -t, --timeout=INTEGER\n"
  280. " Seconds before connection attempt times out (default: %d)\n"
  281. " -v, --verbose\n"
  282. " Print extra information (command-line use only)\n"
  283. " -h, --help\n"
  284. " Print detailed help screen\n"
  285. " -V, --version\n"
  286. " Print version information\n\n",
  287. POP_PORT, POP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
  288. support ();
  289. }
  290. void
  291. print_usage (void)
  292. {
  293. printf
  294. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n"
  295. " %s --help\n"
  296. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  297. }