Code

Don't try to print `optarg' (which will be a NULL pointer) if an unknown
[nagiosplug.git] / plugins / check_nagios.c
1 /******************************************************************************
2 *
3 * Nagios check_nagios plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins 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 argument.
18 *
19 *
20 * License Information:
21 *
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.
26 *
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.
31 *
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.
36  $Id$
37  
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";
45 #include "common.h"
46 #include "runcmd.h"
47 #include "utils.h"
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;
57 int verbose = 0;
59 int
60 main (int argc, char **argv)
61 {
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;
66         int proc_entries = 0;
67         time_t current_time;
68         char *temp_ptr;
69         FILE *fp;
70         int procuid = 0;
71         int procpid = 0;
72         int procppid = 0;
73         int procvsz = 0;
74         int procrss = 0;
75         float procpcpu = 0;
76         char procstat[8];
77 #ifdef PS_USES_PROCETIME
78         char procetime[MAX_INPUT_BUFFER];
79 #endif /* PS_USES_PROCETIME */
80         char procprog[MAX_INPUT_BUFFER];
81         char *procargs;
82         int pos, cols;
83         int expected_cols = PS_COLS - 1;
84         const char *zombie = "Z";
85         char *temp_string;
86         output chld_out, chld_err;
87         size_t i;
89         setlocale (LC_ALL, "");
90         bindtextdomain (PACKAGE, LOCALEDIR);
91         textdomain (PACKAGE);
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"));
99         }
101         /* handle timeouts gracefully... */
102         alarm (timeout_interval);
104         /* open the status log */
105         fp = fopen (status_log, "r");
106         if (fp == NULL) {
107                 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
108         }
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;
115                         break;
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;
120                 }
121         }
122         fclose (fp);
124         if (verbose >= 2)
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;
141                 }
142                 if ( cols >= expected_cols ) {
143                         asprintf (&procargs, "%s", chld_out.line[i] + pos);
144                         strip (procargs);
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, "/");
151                         }
153                         /* May get empty procargs */
154                         if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) {
155                                 proc_entries++;
156                                 if (verbose >= 2) {
157                                         printf (_("Found process: %s %s\n"), procprog, procargs);
158                                 }
159                         }
160                 }
161         }
163         /* If we get anything on stderr, at least set warning */
164         if(chld_err.buflen)
165                 result = max_state (result, STATE_WARNING);
167         /* reset the alarm handler */
168         alarm (0);
170         if (proc_entries == 0) {
171                 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
172         }
174         if (latest_entry_time == 0L) {
175                 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
176         }
178         time (&current_time);
179         if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
180                 result = STATE_WARNING;
181         } else {
182                 result = STATE_OK;
183         }
185         printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
186         printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries);
187         printf (", ");
188         printf (
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) );
193         printf ("\n");
195         return result;
200 /* process command-line arguments */
201 int
202 process_arguments (int argc, char **argv)
204         int c;
206         int option = 0;
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'},
214                 {0, 0, 0, 0}
215         };
217         if (argc < 2)
218                 return ERROR;
220         if (!is_option (argv[1])) {
221                 status_log = argv[1];
222                 if (is_intnonneg (argv[2]))
223                         expire_minutes = atoi (argv[2]);
224                 else
225                         die (STATE_UNKNOWN,
226                                                                  _("Expiration time must be an integer (seconds)\n"));
227                 process_string = argv[3];
228                 return OK;
229         }
231         while (1) {
232                 c = getopt_long (argc, argv, "+hVvF:C:e:", longopts, &option);
234                 if (c == -1 || c == EOF || c == 1)
235                         break;
237                 switch (c) {
238                 case 'h':                                                                       /* help */
239                         print_help ();
240                         exit (STATE_OK);
241                 case 'V':                                                                       /* version */
242                         print_revision (progname, revision);
243                         exit (STATE_OK);
244                 case 'F':                                                                       /* status log */
245                         status_log = optarg;
246                         break;
247                 case 'C':                                                                       /* command */
248                         process_string = optarg;
249                         break;
250                 case 'e':                                                                       /* expiry time */
251                         if (is_intnonneg (optarg))
252                                 expire_minutes = atoi (optarg);
253                         else
254                                 die (STATE_UNKNOWN,
255                                      _("Expiration time must be an integer (seconds)\n"));
256                         break;
257                 case 'v':
258                         verbose++;
259                         break;
260                 default:                                                                        /* print short usage_va statement if args not parsable */
261                         usage5();
262                 }
263         }
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"));
272         return OK;
277 void
278 print_help (void)
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."));
289   printf ("\n\n");
290   
291         print_usage ();
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));
302   printf ("\n");
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));
310 void
311 print_usage (void)
313   printf (_("Usage:"));
314         printf ("%s -F <status log file> -e <expire_minutes> -C <process_string>\n", progname);