check_smtp.c 9.5 KB

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