check_smtp.c 9.1 KB

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