check_nagios.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. $Id$
  14. ******************************************************************************/
  15. const char *progname = "check_nagios";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "1999-2006";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "runcmd.h"
  21. #include "utils.h"
  22. int process_arguments (int, char **);
  23. void print_help (void);
  24. void print_usage (void);
  25. char *status_log = NULL;
  26. char *process_string = NULL;
  27. int expire_minutes = 0;
  28. int verbose = 0;
  29. int
  30. main (int argc, char **argv)
  31. {
  32. int result = STATE_UNKNOWN;
  33. char input_buffer[MAX_INPUT_BUFFER];
  34. unsigned long latest_entry_time = 0L;
  35. unsigned long temp_entry_time = 0L;
  36. int proc_entries = 0;
  37. time_t current_time;
  38. char *temp_ptr;
  39. FILE *fp;
  40. int procuid = 0;
  41. int procpid = 0;
  42. int procppid = 0;
  43. int procvsz = 0;
  44. int procrss = 0;
  45. float procpcpu = 0;
  46. char procstat[8];
  47. #ifdef PS_USES_PROCETIME
  48. char procetime[MAX_INPUT_BUFFER];
  49. #endif /* PS_USES_PROCETIME */
  50. char procprog[MAX_INPUT_BUFFER];
  51. char *procargs;
  52. int pos, cols;
  53. int expected_cols = PS_COLS - 1;
  54. const char *zombie = "Z";
  55. char *temp_string;
  56. output chld_out, chld_err;
  57. size_t i;
  58. setlocale (LC_ALL, "");
  59. bindtextdomain (PACKAGE, LOCALEDIR);
  60. textdomain (PACKAGE);
  61. if (process_arguments (argc, argv) == ERROR)
  62. usage_va(_("Could not parse arguments"));
  63. /* Set signal handling and alarm timeout */
  64. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  65. usage_va(_("Cannot catch SIGALRM"));
  66. }
  67. /* handle timeouts gracefully... */
  68. alarm (timeout_interval);
  69. /* open the status log */
  70. fp = fopen (status_log, "r");
  71. if (fp == NULL) {
  72. die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
  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. if ((temp_ptr = strstr (input_buffer, "created=")) != NULL) {
  77. temp_entry_time = strtoul (temp_ptr + 8, NULL, 10);
  78. latest_entry_time = temp_entry_time;
  79. break;
  80. } else if ((temp_ptr = strtok (input_buffer, "]")) != NULL) {
  81. temp_entry_time = strtoul (temp_ptr + 1, NULL, 10);
  82. if (temp_entry_time > latest_entry_time)
  83. latest_entry_time = temp_entry_time;
  84. }
  85. }
  86. fclose (fp);
  87. if (verbose >= 2)
  88. printf("command: %s\n", PS_COMMAND);
  89. /* run the command to check for the Nagios process.. */
  90. if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0)
  91. result = STATE_WARNING;
  92. /* count the number of matching Nagios processes... */
  93. for(i = 0; i < chld_out.lines; i++) {
  94. cols = sscanf (chld_out.line[i], PS_FORMAT, PS_VARLIST);
  95. /* Zombie processes do not give a procprog command */
  96. if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) {
  97. cols = expected_cols;
  98. /* Set some value for procargs for the strip command further below
  99. * Seen to be a problem on some Solaris 7 and 8 systems */
  100. chld_out.line[i][pos] = '\n';
  101. chld_out.line[i][pos+1] = 0x0;
  102. }
  103. if ( cols >= expected_cols ) {
  104. asprintf (&procargs, "%s", chld_out.line[i] + pos);
  105. strip (procargs);
  106. /* Some ps return full pathname for command. This removes path */
  107. temp_string = strtok ((char *)procprog, "/");
  108. while (temp_string) {
  109. strcpy(procprog, temp_string);
  110. temp_string = strtok (NULL, "/");
  111. }
  112. /* May get empty procargs */
  113. if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) {
  114. proc_entries++;
  115. if (verbose >= 2) {
  116. printf (_("Found process: %s %s\n"), procprog, procargs);
  117. }
  118. }
  119. }
  120. }
  121. /* If we get anything on stderr, at least set warning */
  122. if(chld_err.buflen)
  123. result = max_state (result, STATE_WARNING);
  124. /* reset the alarm handler */
  125. alarm (0);
  126. if (proc_entries == 0) {
  127. die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
  128. }
  129. if (latest_entry_time == 0L) {
  130. die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
  131. }
  132. time (&current_time);
  133. if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
  134. result = STATE_WARNING;
  135. } else {
  136. result = STATE_OK;
  137. }
  138. printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
  139. printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries);
  140. printf (", ");
  141. printf (
  142. ngettext ("status log updated %d second ago",
  143. "status log updated %d seconds ago",
  144. (int) (current_time - latest_entry_time) ),
  145. (int) (current_time - latest_entry_time) );
  146. printf ("\n");
  147. return result;
  148. }
  149. /* process command-line arguments */
  150. int
  151. process_arguments (int argc, char **argv)
  152. {
  153. int c;
  154. int option = 0;
  155. static struct option longopts[] = {
  156. {"filename", required_argument, 0, 'F'},
  157. {"expires", required_argument, 0, 'e'},
  158. {"command", required_argument, 0, 'C'},
  159. {"version", no_argument, 0, 'V'},
  160. {"help", no_argument, 0, 'h'},
  161. {"verbose", no_argument, 0, 'v'},
  162. {0, 0, 0, 0}
  163. };
  164. if (argc < 2)
  165. return ERROR;
  166. if (!is_option (argv[1])) {
  167. status_log = argv[1];
  168. if (is_intnonneg (argv[2]))
  169. expire_minutes = atoi (argv[2]);
  170. else
  171. die (STATE_UNKNOWN,
  172. _("Expiration time must be an integer (seconds)\n"));
  173. process_string = argv[3];
  174. return OK;
  175. }
  176. while (1) {
  177. c = getopt_long (argc, argv, "+hVvF:C:e:", longopts, &option);
  178. if (c == -1 || c == EOF || c == 1)
  179. break;
  180. switch (c) {
  181. case 'h': /* help */
  182. print_help ();
  183. exit (STATE_OK);
  184. case 'V': /* version */
  185. print_revision (progname, revision);
  186. exit (STATE_OK);
  187. case 'F': /* status log */
  188. status_log = optarg;
  189. break;
  190. case 'C': /* command */
  191. process_string = optarg;
  192. break;
  193. case 'e': /* expiry time */
  194. if (is_intnonneg (optarg))
  195. expire_minutes = atoi (optarg);
  196. else
  197. die (STATE_UNKNOWN,
  198. _("Expiration time must be an integer (seconds)\n"));
  199. break;
  200. case 'v':
  201. verbose++;
  202. break;
  203. default: /* print short usage_va statement if args not parsable */
  204. usage_va(_("Unknown argument - %s"), optarg);
  205. }
  206. }
  207. if (status_log == NULL)
  208. die (STATE_UNKNOWN, _("You must provide the status_log\n"));
  209. if (process_string == NULL)
  210. die (STATE_UNKNOWN, _("You must provide a process string\n"));
  211. return OK;
  212. }
  213. void
  214. print_help (void)
  215. {
  216. print_revision (progname, revision);
  217. printf (_(COPYRIGHT), copyright, email);
  218. printf ("%s\n", _("This plugin checks the status of the Nagios process on the local machine"));
  219. printf ("%s\n", _("The plugin will check to make sure the Nagios status log is no older than"));
  220. printf ("%s\n", _("the number of minutes specified by the expires option."));
  221. printf ("%s\n", _("It also checks the process table for a process matching the command argument."));
  222. printf ("\n\n");
  223. print_usage ();
  224. printf (_(UT_HELP_VRSN));
  225. printf (" %s\n", "-F, --filename=FILE");
  226. printf (" %s\n", _("Name of the log file to check"));
  227. printf (" %s\n", "-e, --expires=INTEGER");
  228. printf (" %s\n", _("Minutes aging after which logfile is considered stale"));
  229. printf (" %s\n", "-C, --command=STRING");
  230. printf (" %s\n", _("Substring to search for in process arguments"));
  231. printf (_(UT_VERBOSE));
  232. printf ("\n");
  233. printf ("%s\n", _("Examples:"));
  234. printf (" %s\n", "check_nagios -e 5 -F /usr/local/nagios/var/status.log -C /usr/local/nagios/bin/nagios");
  235. printf (_(UT_SUPPORT));
  236. }
  237. void
  238. print_usage (void)
  239. {
  240. printf (_("Usage:"));
  241. printf ("%s -F <status log file> -e <expire_minutes> -C <process_string>\n", progname);
  242. }