check_nagios.c 7.2 KB

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