check_smtp.c 9.9 KB

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