check_nagios.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. const char *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 verbose = 0;
  39. int
  40. main (int argc, char **argv)
  41. {
  42. int result = STATE_UNKNOWN;
  43. char input_buffer[MAX_INPUT_BUFFER];
  44. unsigned long latest_entry_time = 0L;
  45. unsigned long temp_entry_time = 0L;
  46. int proc_entries = 0;
  47. time_t current_time;
  48. char *temp_ptr;
  49. FILE *fp;
  50. int procuid = 0;
  51. int procppid = 0;
  52. int procvsz = 0;
  53. int procrss = 0;
  54. float procpcpu = 0;
  55. char procstat[8];
  56. char procprog[MAX_INPUT_BUFFER];
  57. char *procargs;
  58. int pos, cols;
  59. if (process_arguments (argc, argv) == ERROR)
  60. usage ("Could not parse arguments\n");
  61. /* Set signal handling and alarm */
  62. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  63. printf ("Cannot catch SIGALRM");
  64. return STATE_UNKNOWN;
  65. }
  66. /* handle timeouts gracefully... */
  67. alarm (timeout_interval);
  68. /* open the status log */
  69. fp = fopen (status_log, "r");
  70. if (fp == NULL) {
  71. printf ("Error: Cannot open status log for reading!\n");
  72. return STATE_CRITICAL;
  73. }
  74. /* get the date/time of the last item updated in the log */
  75. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  76. temp_ptr = strtok (input_buffer, "]");
  77. temp_entry_time =
  78. (temp_ptr == NULL) ? 0L : strtoul (temp_ptr + 1, NULL, 10);
  79. if (temp_entry_time > latest_entry_time)
  80. latest_entry_time = temp_entry_time;
  81. }
  82. fclose (fp);
  83. /* run the command to check for the Nagios process.. */
  84. child_process = spopen (PS_COMMAND);
  85. if (child_process == NULL) {
  86. printf ("Could not open pipe: %s\n", PS_COMMAND);
  87. return STATE_UNKNOWN;
  88. }
  89. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  90. if (child_stderr == NULL) {
  91. printf ("Could not open stderr for %s\n", PS_COMMAND);
  92. }
  93. fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
  94. /* count the number of matching Nagios processes... */
  95. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  96. cols = sscanf (input_buffer, PS_FORMAT, PS_VARLIST);
  97. if ( cols >= 6 ) {
  98. asprintf (&procargs, "%s", input_buffer + pos);
  99. strip (procargs);
  100. if (!strstr(procargs, argv[0]) && strstr(procargs, process_string)) {
  101. proc_entries++;
  102. if (verbose)
  103. printf ("Found process: %s\n", procargs);
  104. }
  105. }
  106. }
  107. /* If we get anything on stderr, at least set warning */
  108. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  109. result = max_state (result, STATE_WARNING);
  110. /* close stderr */
  111. (void) fclose (child_stderr);
  112. /* close the pipe */
  113. if (spclose (child_process))
  114. result = max_state (result, STATE_WARNING);
  115. /* reset the alarm handler */
  116. alarm (0);
  117. if (proc_entries == 0) {
  118. printf ("Could not locate a running Nagios process!\n");
  119. return STATE_CRITICAL;
  120. }
  121. result = STATE_OK;
  122. time (&current_time);
  123. if ((current_time - latest_entry_time) > (expire_minutes * 60))
  124. result = STATE_WARNING;
  125. printf
  126. ("Nagios %s: located %d process%s, status log updated %d second%s ago\n",
  127. (result == STATE_OK) ? "ok" : "problem", proc_entries,
  128. (proc_entries == 1) ? "" : "es",
  129. (int) (current_time - latest_entry_time),
  130. ((int) (current_time - latest_entry_time) == 1) ? "" : "s");
  131. return result;
  132. }
  133. /* process command-line arguments */
  134. int
  135. process_arguments (int argc, char **argv)
  136. {
  137. int c;
  138. int option_index = 0;
  139. static struct option long_options[] = {
  140. {"filename", required_argument, 0, 'F'},
  141. {"expires", required_argument, 0, 'e'},
  142. {"command", required_argument, 0, 'C'},
  143. {"version", no_argument, 0, 'V'},
  144. {"help", no_argument, 0, 'h'},
  145. {"verbose", no_argument, 0, 'v'},
  146. {0, 0, 0, 0}
  147. };
  148. if (argc < 2)
  149. return ERROR;
  150. if (!is_option (argv[1])) {
  151. status_log = argv[1];
  152. if (is_intnonneg (argv[2]))
  153. expire_minutes = atoi (argv[2]);
  154. else
  155. terminate (STATE_UNKNOWN,
  156. "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
  157. progname);
  158. process_string = argv[3];
  159. return OK;
  160. }
  161. while (1) {
  162. c = getopt_long (argc, argv, "+hVvF:C:e:", long_options, &option_index);
  163. if (c == -1 || c == EOF || c == 1)
  164. break;
  165. switch (c) {
  166. case '?': /* print short usage statement if args not parsable */
  167. printf ("%s: Unknown argument: %c\n\n", progname, optopt);
  168. print_usage ();
  169. exit (STATE_UNKNOWN);
  170. case 'h': /* help */
  171. print_help ();
  172. exit (STATE_OK);
  173. case 'V': /* version */
  174. print_revision (progname, "$Revision$");
  175. exit (STATE_OK);
  176. case 'F': /* status log */
  177. status_log = optarg;
  178. break;
  179. case 'C': /* command */
  180. process_string = optarg;
  181. break;
  182. case 'e': /* expiry time */
  183. if (is_intnonneg (optarg))
  184. expire_minutes = atoi (optarg);
  185. else
  186. terminate (STATE_UNKNOWN,
  187. "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
  188. progname);
  189. break;
  190. case 'v':
  191. verbose++;
  192. break;
  193. }
  194. }
  195. if (status_log == NULL)
  196. terminate (STATE_UNKNOWN,
  197. "You must provide the status_log\nType '%s -h' for additional help\n",
  198. progname);
  199. else if (process_string == NULL)
  200. terminate (STATE_UNKNOWN,
  201. "You must provide a process string\nType '%s -h' for additional help\n",
  202. progname);
  203. return OK;
  204. }
  205. void
  206. print_usage (void)
  207. {
  208. printf
  209. ("Usage: %s -F <status log file> -e <expire_minutes> -C <process_string>\n",
  210. progname);
  211. }
  212. void
  213. print_help (void)
  214. {
  215. print_revision (progname, "$Revision$");
  216. printf
  217. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  218. "This plugin attempts to check the status of the Nagios process on the local\n"
  219. "machine. The plugin will check to make sure the Nagios status log is no older\n"
  220. "than the number of minutes specified by the <expire_minutes> option. It also\n"
  221. "uses the /bin/ps command to check for a process matching whatever you specify\n"
  222. "by the <process_string> argument.\n");
  223. print_usage ();
  224. printf
  225. ("\nOptions:\n"
  226. "-F, --filename=FILE\n"
  227. " Name of the log file to check\n"
  228. "-e, --expires=INTEGER\n"
  229. " Seconds aging afterwhich logfile is condsidered stale\n"
  230. "-C, --command=STRING\n"
  231. " Command to search for in process table\n"
  232. "-h, --help\n"
  233. " Print this help screen\n"
  234. "-V, --version\n"
  235. " Print version information\n\n"
  236. "Example:\n"
  237. " ./check_nagios -F /usr/local/nagios/var/status.log -e 5 -C /usr/local/nagios/bin/nagios\n");
  238. }