check_smtp.c 9.4 KB

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