check_smtp.c 8.6 KB

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