check_smtp.c 9.2 KB

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