check_smtp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. const char *progname = "check_smtp";
  41. #define SMTP_PORT 25
  42. #define SMTP_EXPECT "220"
  43. #define SMTP_HELO "HELO "
  44. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  45. * to do something useful. This can be prevented by giving a command
  46. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  47. * You can disable sending DUMMYCMD by undefining SMTP_USE_DUMMYCMD.
  48. *
  49. * According to rfc821 you can include a null reversepath in the from command
  50. * - but a log message is generated on the smtp server.
  51. *
  52. * Use the -f option to provide a FROM address
  53. */
  54. #define SMTP_DUMMYCMD "MAIL "
  55. #define SMTP_USE_DUMMYCMD 1
  56. #define SMTP_QUIT "QUIT\r\n"
  57. int process_arguments (int, char **);
  58. int validate_arguments (void);
  59. void print_help (void);
  60. void print_usage (void);
  61. int server_port = SMTP_PORT;
  62. char *server_address = NULL;
  63. char *server_expect = NULL;
  64. char *from_arg = " ";
  65. int warning_time = 0;
  66. int check_warning_time = FALSE;
  67. int critical_time = 0;
  68. int check_critical_time = FALSE;
  69. int verbose = 0;
  70. int
  71. main (int argc, char **argv)
  72. {
  73. int sd;
  74. double elapsed_time;
  75. int result = STATE_UNKNOWN;
  76. char buffer[MAX_INPUT_BUFFER] = "";
  77. char *from_str = NULL;
  78. char *helocmd = NULL;
  79. struct timeval tv;
  80. if (process_arguments (argc, argv) != OK)
  81. usage ("Invalid command arguments supplied\n");
  82. /* initialize the HELO command with the localhostname */
  83. #ifndef HOST_MAX_BYTES
  84. #define HOST_MAX_BYTES 255
  85. #endif
  86. helocmd = malloc (HOST_MAX_BYTES);
  87. gethostname(helocmd, HOST_MAX_BYTES);
  88. asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
  89. /* initialize the MAIL command with optional FROM command */
  90. asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n");
  91. if (verbose)
  92. printf ("FROMCMD: %s\n", from_str);
  93. /* initialize alarm signal handling */
  94. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  95. /* set socket timeout */
  96. (void) alarm (socket_timeout);
  97. /* start timer */
  98. gettimeofday (&tv, NULL);
  99. /* try to connect to the host at the given port number */
  100. result = my_tcp_connect (server_address, server_port, &sd);
  101. /* we connected, so close connection before exiting */
  102. if (result == STATE_OK) {
  103. /* watch for the SMTP connection string and */
  104. /* return a WARNING status if we couldn't read any data */
  105. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  106. printf ("recv() failed\n");
  107. result = STATE_WARNING;
  108. }
  109. else {
  110. /* strip the buffer of carriage returns */
  111. strip (buffer);
  112. /* make sure we find the response we are looking for */
  113. if (!strstr (buffer, server_expect)) {
  114. if (server_port == SMTP_PORT)
  115. printf ("Invalid SMTP response received from host\n");
  116. else
  117. printf ("Invalid SMTP response received from host on port %d\n",
  118. server_port);
  119. result = STATE_WARNING;
  120. }
  121. }
  122. /* send the HELO command */
  123. send(sd, helocmd, strlen(helocmd), 0);
  124. /* allow for response to helo command to reach us */
  125. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  126. #ifdef SMTP_USE_DUMMYCMD
  127. send(sd, from_str, strlen(from_str), 0);
  128. /* allow for response to DUMMYCMD to reach us */
  129. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  130. if (verbose)
  131. printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
  132. #endif /* SMTP_USE_DUMMYCMD */
  133. /* tell the server we're done */
  134. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  135. /* finally close the connection */
  136. close (sd);
  137. }
  138. /* reset the alarm */
  139. alarm (0);
  140. elapsed_time = delta_time (tv);
  141. if (check_critical_time && elapsed_time > (double) critical_time)
  142. result = STATE_CRITICAL;
  143. else if (check_warning_time && elapsed_time > (double) warning_time)
  144. result = STATE_WARNING;
  145. if (verbose)
  146. printf ("SMTP %s - %7.3f sec. response time, %s|time=%7.3f\n",
  147. state_text (result), elapsed_time, buffer, elapsed_time);
  148. else
  149. printf ("SMTP %s - %7.3f second response time|time=%7.3f\n",
  150. state_text (result), elapsed_time, elapsed_time);
  151. return result;
  152. }
  153. /* process command-line arguments */
  154. int
  155. process_arguments (int argc, char **argv)
  156. {
  157. int c;
  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. {"from", required_argument, 0, 'f'},
  166. {"verbose", no_argument, 0, 'v'},
  167. {"version", no_argument, 0, 'V'},
  168. {"help", no_argument, 0, 'h'},
  169. {0, 0, 0, 0}
  170. };
  171. if (argc < 2)
  172. return ERROR;
  173. for (c = 1; c < argc; c++) {
  174. if (strcmp ("-to", argv[c]) == 0)
  175. strcpy (argv[c], "-t");
  176. else if (strcmp ("-wt", argv[c]) == 0)
  177. strcpy (argv[c], "-w");
  178. else if (strcmp ("-ct", argv[c]) == 0)
  179. strcpy (argv[c], "-c");
  180. }
  181. while (1) {
  182. c = getopt_long (argc, argv, "+hVvt:p:f:e:c:w:H:", long_options,
  183. &option_index);
  184. if (c == -1 || c == EOF)
  185. break;
  186. switch (c) {
  187. case 'H': /* hostname */
  188. if (is_host (optarg)) {
  189. server_address = optarg;
  190. }
  191. else {
  192. usage ("Invalid host name\n");
  193. }
  194. break;
  195. case 'p': /* port */
  196. if (is_intpos (optarg)) {
  197. server_port = atoi (optarg);
  198. }
  199. else {
  200. usage ("Server port must be a positive integer\n");
  201. }
  202. break;
  203. case 'f': /* from argument */
  204. from_arg = optarg;
  205. break;
  206. case 'e': /* server expect string on 220 */
  207. server_expect = optarg;
  208. break;
  209. case 'c': /* critical time threshold */
  210. if (is_intnonneg (optarg)) {
  211. critical_time = atoi (optarg);
  212. check_critical_time = TRUE;
  213. }
  214. else {
  215. usage ("Critical time must be a nonnegative integer\n");
  216. }
  217. break;
  218. case 'w': /* warning time threshold */
  219. if (is_intnonneg (optarg)) {
  220. warning_time = atoi (optarg);
  221. check_warning_time = TRUE;
  222. }
  223. else {
  224. usage ("Warning time must be a nonnegative integer\n");
  225. }
  226. break;
  227. case 'v': /* verbose */
  228. verbose++;
  229. break;
  230. case 't': /* timeout */
  231. if (is_intnonneg (optarg)) {
  232. socket_timeout = atoi (optarg);
  233. }
  234. else {
  235. usage ("Time interval must be a nonnegative integer\n");
  236. }
  237. break;
  238. case 'V': /* version */
  239. print_revision (progname, "$Revision$");
  240. exit (STATE_OK);
  241. case 'h': /* help */
  242. print_help ();
  243. exit (STATE_OK);
  244. case '?': /* help */
  245. usage ("Invalid argument\n");
  246. }
  247. }
  248. c = optind;
  249. if (server_address == NULL) {
  250. if (argv[c]) {
  251. if (is_host (argv[c]))
  252. server_address = argv[c];
  253. else
  254. usage ("Invalid host name");
  255. }
  256. else {
  257. asprintf (&server_address, "127.0.0.1");
  258. }
  259. }
  260. if (server_expect == NULL)
  261. asprintf (&server_expect, SMTP_EXPECT);
  262. return validate_arguments ();
  263. }
  264. int
  265. validate_arguments (void)
  266. {
  267. return OK;
  268. }
  269. void
  270. print_help (void)
  271. {
  272. print_revision (progname, "$Revision$");
  273. printf
  274. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  275. "This plugin test the SMTP service on the specified host.\n\n");
  276. print_usage ();
  277. printf
  278. ("\nOptions:\n"
  279. " -H, --hostname=STRING or IPADDRESS\n"
  280. " Check server on the indicated host\n"
  281. " -p, --port=INTEGER\n"
  282. " Make connection on the indicated port (default: %d)\n"
  283. " -e, --expect=STRING\n"
  284. " String to expect in first line of server response (default: %s)\n"
  285. " -f, --from=STRING\n"
  286. " from address to include in MAIL command (default NULL, Exchange2000 requires one)\n"
  287. " -w, --warning=INTEGER\n"
  288. " Seconds necessary to result in a warning status\n"
  289. " -c, --critical=INTEGER\n"
  290. " Seconds necessary to result in a critical status\n"
  291. " -t, --timeout=INTEGER\n"
  292. " Seconds before connection attempt times out (default: %d)\n"
  293. " -v, --verbose\n"
  294. " Print extra information (command-line use only)\n"
  295. " -h, --help\n"
  296. " Print detailed help screen\n"
  297. " -V, --version\n"
  298. " Print version information\n\n",
  299. SMTP_PORT, SMTP_EXPECT, DEFAULT_SOCKET_TIMEOUT);
  300. support ();
  301. }
  302. void
  303. print_usage (void)
  304. {
  305. printf
  306. ("Usage: %s -H host [-e expect] [-p port] [-f from addr] [-w warn] [-c crit] [-t timeout] [-v]\n"
  307. " %s --help\n"
  308. " %s --version\n", progname, progname, progname);
  309. }