check_smtp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /******************************************************************************
  2. *
  3. * CHECK_SMTP.C
  4. *
  5. * Program: SMTP 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 SMTP 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_smtp"
  41. #define SMTP_PORT 25
  42. #define SMTP_EXPECT "220"
  43. #define SMTP_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 = SMTP_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 == SMTP_PORT)
  88. printf ("Invalid SMTP response received from host\n");
  89. else
  90. printf ("Invalid SMTP 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 ("SMTP %s - %d sec. response time, %s\n",
  105. state_text (result), (int) (end_time - start_time), buffer);
  106. else
  107. printf ("SMTP %s - %d second response time\n", state_text (result),
  108. (int) (end_time - start_time));
  109. }
  110. }
  111. /* close the connection */
  112. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  113. close (sd);
  114. }
  115. /* reset the alarm */
  116. alarm (0);
  117. return result;
  118. }
  119. /* process command-line arguments */
  120. int
  121. process_arguments (int argc, char **argv)
  122. {
  123. int c;
  124. if (argc < 2)
  125. return ERROR;
  126. for (c = 1; c < argc; c++) {
  127. if (strcmp ("-to", argv[c]) == 0)
  128. strcpy (argv[c], "-t");
  129. else if (strcmp ("-wt", argv[c]) == 0)
  130. strcpy (argv[c], "-w");
  131. else if (strcmp ("-ct", argv[c]) == 0)
  132. strcpy (argv[c], "-c");
  133. }
  134. c = 0;
  135. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  136. if (is_option (argv[c]))
  137. continue;
  138. if (server_address == NULL) {
  139. if (is_host (argv[c])) {
  140. server_address = argv[c];
  141. }
  142. else {
  143. usage ("Invalid host name");
  144. }
  145. }
  146. }
  147. if (server_address == NULL)
  148. server_address = strscpy (NULL, "127.0.0.1");
  149. if (server_expect == NULL)
  150. server_expect = strscpy (NULL, SMTP_EXPECT);
  151. return validate_arguments ();
  152. }
  153. int
  154. call_getopt (int argc, char **argv)
  155. {
  156. int c, i = 0;
  157. #ifdef HAVE_GETOPT_H
  158. int option_index = 0;
  159. static struct option long_options[] = {
  160. {"hostname", required_argument, 0, 'H'},
  161. {"expect", required_argument, 0, 'e'},
  162. {"critical", required_argument, 0, 'c'},
  163. {"warning", required_argument, 0, 'w'},
  164. {"port", required_argument, 0, 'P'},
  165. {"verbose", no_argument, 0, 'v'},
  166. {"version", no_argument, 0, 'V'},
  167. {"help", no_argument, 0, 'h'},
  168. {0, 0, 0, 0}
  169. };
  170. #endif
  171. while (1) {
  172. #ifdef HAVE_GETOPT_H
  173. c =
  174. getopt_long (argc, argv, "+hVvt:p:e:c:w:H:", long_options,
  175. &option_index);
  176. #else
  177. c = getopt (argc, argv, "+?hVvt:p:e:c:w:H:");
  178. #endif
  179. i++;
  180. if (c == -1 || c == EOF || c == 1)
  181. break;
  182. switch (c) {
  183. case 't':
  184. case 'p':
  185. case 'e':
  186. case 'c':
  187. case 'w':
  188. case 'H':
  189. i++;
  190. }
  191. switch (c) {
  192. case 'H': /* hostname */
  193. if (is_host (optarg)) {
  194. server_address = optarg;
  195. }
  196. else {
  197. usage ("Invalid host name\n");
  198. }
  199. break;
  200. case 'p': /* port */
  201. if (is_intpos (optarg)) {
  202. server_port = atoi (optarg);
  203. }
  204. else {
  205. usage ("Server port must be a positive integer\n");
  206. }
  207. break;
  208. case 'e': /* username */
  209. server_expect = optarg;
  210. break;
  211. case 'c': /* critical time threshold */
  212. if (is_intnonneg (optarg)) {
  213. critical_time = atoi (optarg);
  214. check_critical_time = TRUE;
  215. }
  216. else {
  217. usage ("Critical time must be a nonnegative integer\n");
  218. }
  219. break;
  220. case 'w': /* warning time threshold */
  221. if (is_intnonneg (optarg)) {
  222. warning_time = atoi (optarg);
  223. check_warning_time = TRUE;
  224. }
  225. else {
  226. usage ("Warning time must be a nonnegative integer\n");
  227. }
  228. break;
  229. case 'v': /* verbose */
  230. verbose = TRUE;
  231. break;
  232. case 't': /* timeout */
  233. if (is_intnonneg (optarg)) {
  234. socket_timeout = atoi (optarg);
  235. }
  236. else {
  237. usage ("Time interval must be a nonnegative integer\n");
  238. }
  239. break;
  240. case 'V': /* version */
  241. print_revision (PROGNAME, "$Revision$");
  242. exit (STATE_OK);
  243. case 'h': /* help */
  244. print_help ();
  245. exit (STATE_OK);
  246. case '?': /* help */
  247. usage ("Invalid argument\n");
  248. }
  249. }
  250. return i;
  251. }
  252. int
  253. validate_arguments (void)
  254. {
  255. return OK;
  256. }
  257. void
  258. print_help (void)
  259. {
  260. print_revision (PROGNAME, "$Revision$");
  261. printf
  262. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  263. "This plugin test the SMTP service on the specified host.\n\n");
  264. print_usage ();
  265. printf
  266. ("\nOptions:\n"
  267. " -H, --hostname=STRING or IPADDRESS\n"
  268. " Check server on the indicated host\n"
  269. " -p, --port=INTEGER\n"
  270. " Make connection on the indicated port (default: %d)\n"
  271. " -e, --expect=STRING\n"
  272. " String to expect in first line of server response (default: %s)\n"
  273. " -w, --warning=INTEGER\n"
  274. " Seconds necessary to result in a warning status\n"
  275. " -c, --critical=INTEGER\n"
  276. " Seconds necessary to result in a critical status\n"
  277. " -t, --timeout=INTEGER\n"
  278. " Seconds before connection attempt times out (default: %d)\n"
  279. " -v, --verbose\n"
  280. " Print extra information (command-line use only)\n"
  281. " -h, --help\n"
  282. " Print detailed help screen\n"
  283. " -V, --version\n"
  284. " Print version information\n\n",
  285. SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
  286. support ();
  287. }
  288. void
  289. print_usage (void)
  290. {
  291. printf
  292. ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n"
  293. " %s --help\n"
  294. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  295. }