Code

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