check_nagios.c 8.9 KB

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