check_smtp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /******************************************************************************
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. *****************************************************************************/
  18. const char *progname = "check_smtp";
  19. const char *revision = "$Revision$";
  20. const char *copyright = "1999-2003";
  21. const char *authors = "Nagios Plugin Development Team";
  22. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  23. const char *summary = "\
  24. This plugin will attempt to open an SMTP connection with the host.\n";
  25. const char *description = "\
  26. Successul connects return STATE_OK, refusals and timeouts return\n\
  27. STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful\n\
  28. connects, but incorrect reponse messages from the host result in\n\
  29. STATE_WARNING return values.\n";
  30. const char *option_summary = "\
  31. -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
  32. [-w warn] [-c crit] [-t timeout] [-n] [-v] [-4|-6]";
  33. const char *options = "\
  34. -H, --hostname=STRING or IPADDRESS\n\
  35. Check server on the indicated host\n\
  36. -4, --use-ipv4\n\
  37. Use IPv4 protocol\n\
  38. -6, --use-ipv6\n\
  39. Use IPv6 protocol\n\
  40. -p, --port=INTEGER\n\
  41. Make connection on the indicated port (default: %d)\n\
  42. -e, --expect=STRING\n\
  43. String to expect in first line of server response (default: '%s')\n\
  44. -n, nocommand\n\
  45. Suppress SMTP command\n\
  46. -C, --command=STRING\n\
  47. SMTP command (default: '%s')\n\
  48. -f, --from=STRING\n\
  49. FROM-address to include in MAIL command, required by Exchange 2000\n\
  50. (default: '%s')\n\
  51. -w, --warning=INTEGER\n\
  52. Seconds necessary to result in a warning status\n\
  53. -c, --critical=INTEGER\n\
  54. Seconds necessary to result in a critical status\n\
  55. -t, --timeout=INTEGER\n\
  56. Seconds before connection attempt times out (default: %d)\n\
  57. -v, --verbose\n\
  58. Print extra information (command-line use only)\n\
  59. -h, --help\n\
  60. Print detailed help screen\n\
  61. -V, --version\n\
  62. Print version information\n\n";
  63. enum {
  64. SMTP_PORT = 25
  65. };
  66. const char *SMTP_EXPECT = "220";
  67. const char *SMTP_HELO = "HELO ";
  68. const char *SMTP_QUIT = "QUIT\r\n";
  69. #include "config.h"
  70. #include "common.h"
  71. #include "netutils.h"
  72. #include "utils.h"
  73. int process_arguments (int, char **);
  74. int validate_arguments (void);
  75. void print_help (void);
  76. void print_usage (void);
  77. int server_port = SMTP_PORT;
  78. char *server_address = NULL;
  79. char *server_expect = NULL;
  80. int smtp_use_dummycmd = 1;
  81. char *mail_command = "MAIL ";
  82. char *from_arg = " ";
  83. int warning_time = 0;
  84. int check_warning_time = FALSE;
  85. int critical_time = 0;
  86. int check_critical_time = FALSE;
  87. int verbose = 0;
  88. int
  89. main (int argc, char **argv)
  90. {
  91. int sd;
  92. double elapsed_time;
  93. int result = STATE_UNKNOWN;
  94. char buffer[MAX_INPUT_BUFFER] = "";
  95. char *from_str = NULL;
  96. char *helocmd = NULL;
  97. struct timeval tv;
  98. if (process_arguments (argc, argv) != OK)
  99. usage ("Invalid command arguments supplied\n");
  100. /* initialize the HELO command with the localhostname */
  101. #ifndef HOST_MAX_BYTES
  102. #define HOST_MAX_BYTES 255
  103. #endif
  104. helocmd = malloc (HOST_MAX_BYTES);
  105. gethostname(helocmd, HOST_MAX_BYTES);
  106. asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
  107. /* initialize the MAIL command with optional FROM command */
  108. asprintf (&from_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
  109. if (verbose)
  110. printf ("FROMCMD: %s\n", from_str);
  111. /* initialize alarm signal handling */
  112. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  113. /* set socket timeout */
  114. (void) alarm (socket_timeout);
  115. /* start timer */
  116. gettimeofday (&tv, NULL);
  117. /* try to connect to the host at the given port number */
  118. result = my_tcp_connect (server_address, server_port, &sd);
  119. /* we connected, so close connection before exiting */
  120. if (result == STATE_OK) {
  121. /* watch for the SMTP connection string and */
  122. /* return a WARNING status if we couldn't read any data */
  123. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
  124. printf ("recv() failed\n");
  125. result = STATE_WARNING;
  126. }
  127. else {
  128. /* strip the buffer of carriage returns */
  129. strip (buffer);
  130. /* make sure we find the response we are looking for */
  131. if (!strstr (buffer, server_expect)) {
  132. if (server_port == SMTP_PORT)
  133. printf ("Invalid SMTP response received from host\n");
  134. else
  135. printf ("Invalid SMTP response received from host on port %d\n",
  136. server_port);
  137. result = STATE_WARNING;
  138. }
  139. }
  140. /* send the HELO command */
  141. send(sd, helocmd, strlen(helocmd), 0);
  142. /* allow for response to helo command to reach us */
  143. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  144. /* sendmail will syslog a "NOQUEUE" error if session does not attempt
  145. * to do something useful. This can be prevented by giving a command
  146. * even if syntax is illegal (MAIL requires a FROM:<...> argument)
  147. *
  148. * According to rfc821 you can include a null reversepath in the from command
  149. * - but a log message is generated on the smtp server.
  150. *
  151. * You can disable sending mail_command with '--nocommand'
  152. * Use the -f option to provide a FROM address
  153. */
  154. if (smtp_use_dummycmd) {
  155. send(sd, from_str, strlen(from_str), 0);
  156. /* allow for response to mail_command to reach us */
  157. recv(sd, buffer, MAX_INPUT_BUFFER-1, 0);
  158. if (verbose)
  159. printf("DUMMYCMD: %s\n%s\n",from_str,buffer);
  160. } /* smtp_use_dummycmd */
  161. /* tell the server we're done */
  162. send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
  163. /* finally close the connection */
  164. close (sd);
  165. }
  166. /* reset the alarm */
  167. alarm (0);
  168. elapsed_time = delta_time (tv);
  169. if (check_critical_time && elapsed_time > (double) critical_time)
  170. result = STATE_CRITICAL;
  171. else if (check_warning_time && elapsed_time > (double) warning_time)
  172. result = STATE_WARNING;
  173. if (verbose)
  174. printf ("SMTP %s - %.3f sec. response time, %s|time=%.3f\n",
  175. state_text (result), elapsed_time, buffer, elapsed_time);
  176. else
  177. printf ("SMTP %s - %.3f second response time|time=%.3f\n",
  178. state_text (result), elapsed_time, elapsed_time);
  179. return result;
  180. }
  181. /* process command-line arguments */
  182. int
  183. process_arguments (int argc, char **argv)
  184. {
  185. int c;
  186. int option_index = 0;
  187. static struct option long_options[] = {
  188. {"hostname", required_argument, 0, 'H'},
  189. {"expect", required_argument, 0, 'e'},
  190. {"critical", required_argument, 0, 'c'},
  191. {"warning", required_argument, 0, 'w'},
  192. {"timeout", required_argument, 0, 't'},
  193. {"port", required_argument, 0, 'p'},
  194. {"from", required_argument, 0, 'f'},
  195. {"command", required_argument, 0, 'C'},
  196. {"nocommand", required_argument, 0, 'n'},
  197. {"verbose", no_argument, 0, 'v'},
  198. {"version", no_argument, 0, 'V'},
  199. {"use-ipv4", no_argument, 0, '4'},
  200. {"use-ipv6", no_argument, 0, '6'},
  201. {"help", no_argument, 0, 'h'},
  202. {0, 0, 0, 0}
  203. };
  204. if (argc < 2)
  205. return ERROR;
  206. for (c = 1; c < argc; c++) {
  207. if (strcmp ("-to", argv[c]) == 0)
  208. strcpy (argv[c], "-t");
  209. else if (strcmp ("-wt", argv[c]) == 0)
  210. strcpy (argv[c], "-w");
  211. else if (strcmp ("-ct", argv[c]) == 0)
  212. strcpy (argv[c], "-c");
  213. }
  214. while (1) {
  215. c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:",
  216. long_options, &option_index);
  217. if (c == -1 || c == EOF)
  218. break;
  219. switch (c) {
  220. case 'H': /* hostname */
  221. if (is_host (optarg)) {
  222. server_address = optarg;
  223. }
  224. else {
  225. usage ("Invalid host name\n");
  226. }
  227. break;
  228. case 'p': /* port */
  229. if (is_intpos (optarg)) {
  230. server_port = atoi (optarg);
  231. }
  232. else {
  233. usage ("Server port must be a positive integer\n");
  234. }
  235. break;
  236. case 'f': /* from argument */
  237. from_arg = optarg;
  238. break;
  239. case 'e': /* server expect string on 220 */
  240. server_expect = optarg;
  241. break;
  242. case 'C': /* server expect string on 220 */
  243. mail_command = optarg;
  244. smtp_use_dummycmd = 1;
  245. break;
  246. case 'n': /* server expect string on 220 */
  247. smtp_use_dummycmd = 0;
  248. break;
  249. case 'c': /* critical time threshold */
  250. if (is_intnonneg (optarg)) {
  251. critical_time = atoi (optarg);
  252. check_critical_time = TRUE;
  253. }
  254. else {
  255. usage ("Critical time must be a nonnegative integer\n");
  256. }
  257. break;
  258. case 'w': /* warning time threshold */
  259. if (is_intnonneg (optarg)) {
  260. warning_time = atoi (optarg);
  261. check_warning_time = TRUE;
  262. }
  263. else {
  264. usage ("Warning time must be a nonnegative integer\n");
  265. }
  266. break;
  267. case 'v': /* verbose */
  268. verbose++;
  269. break;
  270. case 't': /* timeout */
  271. if (is_intnonneg (optarg)) {
  272. socket_timeout = atoi (optarg);
  273. }
  274. else {
  275. usage ("Time interval must be a nonnegative integer\n");
  276. }
  277. break;
  278. case '4':
  279. address_family = AF_INET;
  280. break;
  281. case '6':
  282. #ifdef USE_IPV6
  283. address_family = AF_INET6;
  284. #else
  285. usage ("IPv6 support not available\n");
  286. #endif
  287. break;
  288. case 'V': /* version */
  289. print_revision (progname, revision);
  290. exit (STATE_OK);
  291. case 'h': /* help */
  292. print_help ();
  293. exit (STATE_OK);
  294. case '?': /* help */
  295. usage ("Invalid argument\n");
  296. }
  297. }
  298. c = optind;
  299. if (server_address == NULL) {
  300. if (argv[c]) {
  301. if (is_host (argv[c]))
  302. server_address = argv[c];
  303. else
  304. usage ("Invalid host name");
  305. }
  306. else {
  307. asprintf (&server_address, "127.0.0.1");
  308. }
  309. }
  310. if (server_expect == NULL)
  311. asprintf (&server_expect, SMTP_EXPECT);
  312. return validate_arguments ();
  313. }
  314. int
  315. validate_arguments (void)
  316. {
  317. return OK;
  318. }
  319. void
  320. print_help (void)
  321. {
  322. print_revision (progname, revision);
  323. printf ("Copyright (c) %s %s\n\t<%s>\n\n%s\n",
  324. copyright, authors, email, summary);
  325. print_usage ();
  326. printf ("\nOptions:\n");
  327. printf (options, SMTP_PORT, SMTP_EXPECT, mail_command, from_arg,
  328. DEFAULT_SOCKET_TIMEOUT);
  329. support ();
  330. }
  331. void
  332. print_usage (void)
  333. {
  334. printf ("Usage: %s %s\n"
  335. " %s --help\n"
  336. " %s --version\n",
  337. progname, option_summary, progname, progname);
  338. }