check_nagios.c 8.7 KB

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