check_smtp.c 9.9 KB

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