check_nagios.c 8.7 KB

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