check_nagios.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /******************************************************************************
  2. *
  3. * CHECK_NAGIOS.C
  4. *
  5. * Program: Nagios process plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * $Id$
  10. *
  11. * License Information:
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. *
  27. *****************************************************************************/
  28. #include "common.h"
  29. #include "popen.h"
  30. #include "utils.h"
  31. #define PROGNAME "check_nagios"
  32. int process_arguments (int, char **);
  33. void print_usage (void);
  34. void print_help (void);
  35. char *status_log = NULL;
  36. char *process_string = NULL;
  37. int expire_minutes = 0;
  38. int
  39. main (int argc, char **argv)
  40. {
  41. int result = STATE_UNKNOWN;
  42. char input_buffer[MAX_INPUT_BUFFER];
  43. unsigned long latest_entry_time = 0L;
  44. unsigned long temp_entry_time = 0L;
  45. int proc_entries = 0;
  46. time_t current_time;
  47. char *temp_ptr;
  48. FILE *fp;
  49. if (process_arguments (argc, argv) == ERROR)
  50. usage ("Could not parse arguments\n");
  51. /* Set signal handling and alarm */
  52. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  53. printf ("Cannot catch SIGALRM");
  54. return STATE_UNKNOWN;
  55. }
  56. /* handle timeouts gracefully... */
  57. alarm (timeout_interval);
  58. /* open the status log */
  59. fp = fopen (status_log, "r");
  60. if (fp == NULL) {
  61. printf ("Error: Cannot open status log for reading!\n");
  62. return STATE_CRITICAL;
  63. }
  64. /* get the date/time of the last item updated in the log */
  65. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  66. temp_ptr = strtok (input_buffer, "]");
  67. temp_entry_time =
  68. (temp_ptr == NULL) ? 0L : strtoul (temp_ptr + 1, NULL, 10);
  69. if (temp_entry_time > latest_entry_time)
  70. latest_entry_time = temp_entry_time;
  71. }
  72. fclose (fp);
  73. /* run the command to check for the Nagios process.. */
  74. child_process = spopen (PS_RAW_COMMAND);
  75. if (child_process == NULL) {
  76. printf ("Could not open pipe: %s\n", PS_RAW_COMMAND);
  77. return STATE_UNKNOWN;
  78. }
  79. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  80. if (child_stderr == NULL) {
  81. printf ("Could not open stderr for %s\n", PS_RAW_COMMAND);
  82. }
  83. /* cound the number of matching Nagios processes... */
  84. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  85. if (strstr (input_buffer, process_string))
  86. proc_entries++;
  87. }
  88. /* If we get anything on stderr, at least set warning */
  89. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  90. result = max (result, STATE_WARNING);
  91. /* close stderr */
  92. (void) fclose (child_stderr);
  93. /* close the pipe */
  94. if (spclose (child_process))
  95. result = max (result, STATE_WARNING);
  96. /* reset the alarm handler */
  97. alarm (0);
  98. if (proc_entries == 0) {
  99. printf ("Could not locate a running Nagios process!\n");
  100. return STATE_CRITICAL;
  101. }
  102. result = STATE_OK;
  103. time (&current_time);
  104. if ((current_time - latest_entry_time) > (expire_minutes * 60))
  105. result = STATE_WARNING;
  106. printf
  107. ("Nagios %s: located %d process%s, status log updated %d second%s ago\n",
  108. (result == STATE_OK) ? "ok" : "problem", proc_entries,
  109. (proc_entries == 1) ? "" : "es",
  110. (int) (current_time - latest_entry_time),
  111. ((int) (current_time - latest_entry_time) == 1) ? "" : "s");
  112. return result;
  113. }
  114. /* process command-line arguments */
  115. int
  116. process_arguments (int argc, char **argv)
  117. {
  118. int c;
  119. #ifdef HAVE_GETOPT_H
  120. int option_index = 0;
  121. static struct option long_options[] = {
  122. {"filename", required_argument, 0, 'F'},
  123. {"expires", required_argument, 0, 'e'},
  124. {"command", required_argument, 0, 'C'},
  125. {"version", no_argument, 0, 'V'},
  126. {"help", no_argument, 0, 'h'},
  127. {0, 0, 0, 0}
  128. };
  129. #endif
  130. if (argc < 2)
  131. return ERROR;
  132. if (!is_option (argv[1])) {
  133. status_log = argv[1];
  134. if (is_intnonneg (argv[2]))
  135. expire_minutes = atoi (argv[2]);
  136. else
  137. terminate (STATE_UNKNOWN,
  138. "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
  139. PROGNAME);
  140. process_string = argv[3];
  141. return OK;
  142. }
  143. while (1) {
  144. #ifdef HAVE_GETOPT_H
  145. c = getopt_long (argc, argv, "+hVF:C:e:", long_options, &option_index);
  146. #else
  147. c = getopt (argc, argv, "+hVF:C:e:");
  148. #endif
  149. if (c == -1 || c == EOF || c == 1)
  150. break;
  151. switch (c) {
  152. case '?': /* print short usage statement if args not parsable */
  153. printf ("%s: Unknown argument: %c\n\n", my_basename (argv[0]), optopt);
  154. print_usage ();
  155. exit (STATE_UNKNOWN);
  156. case 'h': /* help */
  157. print_help ();
  158. exit (STATE_OK);
  159. case 'V': /* version */
  160. print_revision (my_basename (argv[0]), "$Revision$");
  161. exit (STATE_OK);
  162. case 'F': /* hostname */
  163. status_log = optarg;
  164. break;
  165. case 'C': /* hostname */
  166. process_string = optarg;
  167. break;
  168. case 'e': /* hostname */
  169. if (is_intnonneg (optarg))
  170. expire_minutes = atoi (optarg);
  171. else
  172. terminate (STATE_UNKNOWN,
  173. "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
  174. PROGNAME);
  175. break;
  176. }
  177. }
  178. if (status_log == NULL)
  179. terminate (STATE_UNKNOWN,
  180. "You must provide the status_log\nType '%s -h' for additional help\n",
  181. PROGNAME);
  182. else if (process_string == NULL)
  183. terminate (STATE_UNKNOWN,
  184. "You must provide a process string\nType '%s -h' for additional help\n",
  185. PROGNAME);
  186. return OK;
  187. }
  188. void
  189. print_usage (void)
  190. {
  191. printf
  192. ("Usage: %s -F <status log file> -e <expire_minutes> -C <process_string>\n",
  193. PROGNAME);
  194. }
  195. void
  196. print_help (void)
  197. {
  198. print_revision (PROGNAME, "$Revision$");
  199. printf
  200. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  201. "This plugin attempts to check the status of the Nagios process on the local\n"
  202. "machine. The plugin will check to make sure the Nagios status log is no older\n"
  203. "than the number of minutes specified by the <expire_minutes> option. It also\n"
  204. "uses the /bin/ps command to check for a process matching whatever you specify\n"
  205. "by the <process_string> argument.\n");
  206. print_usage ();
  207. printf
  208. ("\nOptions:\n"
  209. "-F, --filename=FILE\n"
  210. " Name of the log file to check\n"
  211. "-e, --expires=INTEGER\n"
  212. " Seconds aging afterwhich logfile is condsidered stale\n"
  213. "-C, --command=STRING\n"
  214. " Command to search for in process table\n"
  215. "-h, --help\n"
  216. " Print this help screen\n"
  217. "-V, --version\n"
  218. " Print version information\n\n"
  219. "Example:\n"
  220. " ./check_nagios -H /usr/local/nagios/var/status.log -e 5 -C /usr/local/nagios/bin/nagios\n");
  221. }