1 /******************************************************************************
3 * Nagios check_nagios plugin
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
12 * This file contains the check_nagios plugin
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 argument.
20 * License Information:
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 ******************************************************************************/
40 const char *progname
= "check_nagios";
41 const char *revision
= "$Revision$";
42 const char *copyright
= "1999-2006";
43 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
49 int process_arguments (int, char **);
50 void print_help (void);
51 void print_usage (void);
53 char *status_log
= NULL
;
54 char *process_string
= NULL
;
55 int expire_minutes
= 0;
60 main (int argc
, char **argv
)
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;
77 #ifdef PS_USES_PROCETIME
78 char procetime
[MAX_INPUT_BUFFER
];
79 #endif /* PS_USES_PROCETIME */
80 char procprog
[MAX_INPUT_BUFFER
];
83 int expected_cols
= PS_COLS
- 1;
84 const char *zombie
= "Z";
86 output chld_out
, chld_err
;
89 setlocale (LC_ALL
, "");
90 bindtextdomain (PACKAGE
, LOCALEDIR
);
93 if (process_arguments (argc
, argv
) == ERROR
)
94 usage_va(_("Could not parse arguments"));
96 /* Set signal handling and alarm timeout */
97 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
) {
98 usage_va(_("Cannot catch SIGALRM"));
101 /* handle timeouts gracefully... */
102 alarm (timeout_interval
);
104 /* open the status log */
105 fp
= fopen (status_log
, "r");
107 die (STATE_CRITICAL
, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
110 /* get the date/time of the last item updated in the log */
111 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, fp
)) {
112 if ((temp_ptr
= strstr (input_buffer
, "created=")) != NULL
) {
113 temp_entry_time
= strtoul (temp_ptr
+ 8, NULL
, 10);
114 latest_entry_time
= temp_entry_time
;
116 } else if ((temp_ptr
= strtok (input_buffer
, "]")) != NULL
) {
117 temp_entry_time
= strtoul (temp_ptr
+ 1, NULL
, 10);
118 if (temp_entry_time
> latest_entry_time
)
119 latest_entry_time
= temp_entry_time
;
125 printf("command: %s\n", PS_COMMAND
);
127 /* run the command to check for the Nagios process.. */
128 if((result
= np_runcmd(PS_COMMAND
, &chld_out
, &chld_err
, 0)) != 0)
129 result
= STATE_WARNING
;
131 /* count the number of matching Nagios processes... */
132 for(i
= 0; i
< chld_out
.lines
; i
++) {
133 cols
= sscanf (chld_out
.line
[i
], PS_FORMAT
, PS_VARLIST
);
134 /* Zombie processes do not give a procprog command */
135 if ( cols
== (expected_cols
- 1) && strstr(procstat
, zombie
) ) {
136 cols
= expected_cols
;
137 /* Set some value for procargs for the strip command further below
138 * Seen to be a problem on some Solaris 7 and 8 systems */
139 chld_out
.line
[i
][pos
] = '\n';
140 chld_out
.line
[i
][pos
+1] = 0x0;
142 if ( cols
>= expected_cols
) {
143 asprintf (&procargs
, "%s", chld_out
.line
[i
] + pos
);
146 /* Some ps return full pathname for command. This removes path */
147 temp_string
= strtok ((char *)procprog
, "/");
148 while (temp_string
) {
149 strcpy(procprog
, temp_string
);
150 temp_string
= strtok (NULL
, "/");
153 /* May get empty procargs */
154 if (!strstr(procargs
, argv
[0]) && strstr(procargs
, process_string
) && strcmp(procargs
,"")) {
157 printf (_("Found process: %s %s\n"), procprog
, procargs
);
163 /* If we get anything on stderr, at least set warning */
165 result
= max_state (result
, STATE_WARNING
);
167 /* reset the alarm handler */
170 if (proc_entries
== 0) {
171 die (STATE_CRITICAL
, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
174 if (latest_entry_time
== 0L) {
175 die (STATE_CRITICAL
, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
178 time (¤t_time
);
179 if ((int)(current_time
- latest_entry_time
) > (expire_minutes
* 60)) {
180 result
= STATE_WARNING
;
185 printf ("NAGIOS %s: ", (result
== STATE_OK
) ? _("OK") : _("WARNING"));
186 printf (ngettext ("%d process", "%d processes", proc_entries
), proc_entries
);
189 ngettext ("status log updated %d second ago",
190 "status log updated %d seconds ago",
191 (int) (current_time
- latest_entry_time
) ),
192 (int) (current_time
- latest_entry_time
) );
200 /* process command-line arguments */
202 process_arguments (int argc
, char **argv
)
207 static struct option longopts
[] = {
208 {"filename", required_argument
, 0, 'F'},
209 {"expires", required_argument
, 0, 'e'},
210 {"command", required_argument
, 0, 'C'},
211 {"version", no_argument
, 0, 'V'},
212 {"help", no_argument
, 0, 'h'},
213 {"verbose", no_argument
, 0, 'v'},
220 if (!is_option (argv
[1])) {
221 status_log
= argv
[1];
222 if (is_intnonneg (argv
[2]))
223 expire_minutes
= atoi (argv
[2]);
226 _("Expiration time must be an integer (seconds)\n"));
227 process_string
= argv
[3];
232 c
= getopt_long (argc
, argv
, "+hVvF:C:e:", longopts
, &option
);
234 if (c
== -1 || c
== EOF
|| c
== 1)
241 case 'V': /* version */
242 print_revision (progname
, revision
);
244 case 'F': /* status log */
247 case 'C': /* command */
248 process_string
= optarg
;
250 case 'e': /* expiry time */
251 if (is_intnonneg (optarg
))
252 expire_minutes
= atoi (optarg
);
255 _("Expiration time must be an integer (seconds)\n"));
260 default: /* print short usage_va statement if args not parsable */
266 if (status_log
== NULL
)
267 die (STATE_UNKNOWN
, _("You must provide the status_log\n"));
269 if (process_string
== NULL
)
270 die (STATE_UNKNOWN
, _("You must provide a process string\n"));
280 print_revision (progname
, revision
);
282 printf (_(COPYRIGHT
), copyright
, email
);
284 printf ("%s\n", _("This plugin checks the status of the Nagios process on the local machine"));
285 printf ("%s\n", _("The plugin will check to make sure the Nagios status log is no older than"));
286 printf ("%s\n", _("the number of minutes specified by the expires option."));
287 printf ("%s\n", _("It also checks the process table for a process matching the command argument."));
293 printf (_(UT_HELP_VRSN
));
295 printf (" %s\n", "-F, --filename=FILE");
296 printf (" %s\n", _("Name of the log file to check"));
297 printf (" %s\n", "-e, --expires=INTEGER");
298 printf (" %s\n", _("Minutes aging after which logfile is considered stale"));
299 printf (" %s\n", "-C, --command=STRING");
300 printf (" %s\n", _("Substring to search for in process arguments"));
301 printf (_(UT_VERBOSE
));
303 printf ("%s\n", _("Examples:"));
304 printf (" %s\n", "check_nagios -e 5 -F /usr/local/nagios/var/status.log -C /usr/local/nagios/bin/nagios");
305 printf (_(UT_SUPPORT
));
313 printf (_("Usage:"));
314 printf ("%s -F <status log file> -e <expire_minutes> -C <process_string>\n", progname
);