Code

Remove getopt_long checks
[nagiosplug.git] / plugins / check_nagios.c
1 /******************************************************************************
2  *
3  * CHECK_NAGIOS.C
4  *
5  * Program: Nagios process plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8  *
9  * $Id$
10  *
11  * License Information:
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  *****************************************************************************/
29 #include "common.h"
30 #include "popen.h"
31 #include "utils.h"
33 const char *progname = "check_nagios";
35 int process_arguments (int, char **);
36 void print_usage (void);
37 void print_help (void);
39 char *status_log = NULL;
40 char *process_string = NULL;
41 int expire_minutes = 0;
43 int
44 main (int argc, char **argv)
45 {
46         int result = STATE_UNKNOWN;
47         char input_buffer[MAX_INPUT_BUFFER];
48         unsigned long latest_entry_time = 0L;
49         unsigned long temp_entry_time = 0L;
50         int proc_entries = 0;
51         time_t current_time;
52         char *temp_ptr;
53         FILE *fp;
55         if (process_arguments (argc, argv) == ERROR)
56                 usage ("Could not parse arguments\n");
58         /* Set signal handling and alarm */
59         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
60                 printf ("Cannot catch SIGALRM");
61                 return STATE_UNKNOWN;
62         }
64         /* handle timeouts gracefully... */
65         alarm (timeout_interval);
67         /* open the status log */
68         fp = fopen (status_log, "r");
69         if (fp == NULL) {
70                 printf ("Error: Cannot open status log for reading!\n");
71                 return STATE_CRITICAL;
72         }
74         /* get the date/time of the last item updated in the log */
75         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
76                 temp_ptr = strtok (input_buffer, "]");
77                 temp_entry_time =
78                         (temp_ptr == NULL) ? 0L : strtoul (temp_ptr + 1, NULL, 10);
79                 if (temp_entry_time > latest_entry_time)
80                         latest_entry_time = temp_entry_time;
81         }
82         fclose (fp);
84         /* run the command to check for the Nagios process.. */
85         child_process = spopen (PS_RAW_COMMAND);
86         if (child_process == NULL) {
87                 printf ("Could not open pipe: %s\n", PS_RAW_COMMAND);
88                 return STATE_UNKNOWN;
89         }
91         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
92         if (child_stderr == NULL) {
93                 printf ("Could not open stderr for %s\n", PS_RAW_COMMAND);
94         }
96         /* count the number of matching Nagios processes... */
97         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
98                 if (!strstr(input_buffer, argv[0]) && strstr(input_buffer, process_string))
99                         proc_entries++;
100         }
102         /* If we get anything on stderr, at least set warning */
103         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
104                 result = max_state (result, STATE_WARNING);
106         /* close stderr */
107         (void) fclose (child_stderr);
109         /* close the pipe */
110         if (spclose (child_process))
111                 result = max_state (result, STATE_WARNING);
113         /* reset the alarm handler */
114         alarm (0);
116         if (proc_entries == 0) {
117                 printf ("Could not locate a running Nagios process!\n");
118                 return STATE_CRITICAL;
119         }
121         result = STATE_OK;
123         time (&current_time);
124         if ((current_time - latest_entry_time) > (expire_minutes * 60))
125                 result = STATE_WARNING;
127         printf
128                 ("Nagios %s: located %d process%s, status log updated %d second%s ago\n",
129                  (result == STATE_OK) ? "ok" : "problem", proc_entries,
130                  (proc_entries == 1) ? "" : "es",
131                  (int) (current_time - latest_entry_time),
132                  ((int) (current_time - latest_entry_time) == 1) ? "" : "s");
134         return result;
141 /* process command-line arguments */
142 int
143 process_arguments (int argc, char **argv)
145         int c;
147         int option_index = 0;
148         static struct option long_options[] = {
149                 {"filename", required_argument, 0, 'F'},
150                 {"expires", required_argument, 0, 'e'},
151                 {"command", required_argument, 0, 'C'},
152                 {"version", no_argument, 0, 'V'},
153                 {"help", no_argument, 0, 'h'},
154                 {0, 0, 0, 0}
155         };
157         if (argc < 2)
158                 return ERROR;
160         if (!is_option (argv[1])) {
161                 status_log = argv[1];
162                 if (is_intnonneg (argv[2]))
163                         expire_minutes = atoi (argv[2]);
164                 else
165                         terminate (STATE_UNKNOWN,
166                                                                  "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
167                                                                  progname);
168                 process_string = argv[3];
169                 return OK;
170         }
172         while (1) {
173                 c = getopt_long (argc, argv, "+hVF:C:e:", long_options, &option_index);
175                 if (c == -1 || c == EOF || c == 1)
176                         break;
178                 switch (c) {
179                 case '?':                                                                       /* print short usage statement if args not parsable */
180                         printf ("%s: Unknown argument: %c\n\n", progname, optopt);
181                         print_usage ();
182                         exit (STATE_UNKNOWN);
183                 case 'h':                                                                       /* help */
184                         print_help ();
185                         exit (STATE_OK);
186                 case 'V':                                                                       /* version */
187                         print_revision (progname, "$Revision$");
188                         exit (STATE_OK);
189                 case 'F':                                                                       /* hostname */
190                         status_log = optarg;
191                         break;
192                 case 'C':                                                                       /* hostname */
193                         process_string = optarg;
194                         break;
195                 case 'e':                                                                       /* hostname */
196                         if (is_intnonneg (optarg))
197                                 expire_minutes = atoi (optarg);
198                         else
199                                 terminate (STATE_UNKNOWN,
200                                                                          "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
201                                                                          progname);
202                         break;
203                 }
204         }
207         if (status_log == NULL)
208                 terminate (STATE_UNKNOWN,
209                                                          "You must provide the status_log\nType '%s -h' for additional help\n",
210                                                          progname);
211         else if (process_string == NULL)
212                 terminate (STATE_UNKNOWN,
213                                                          "You must provide a process string\nType '%s -h' for additional help\n",
214                                                          progname);
216         return OK;
223 void
224 print_usage (void)
226         printf
227                 ("Usage: %s -F <status log file> -e <expire_minutes> -C <process_string>\n",
228                  progname);
235 void
236 print_help (void)
238         print_revision (progname, "$Revision$");
239         printf
240                 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
241                  "This plugin attempts to check the status of the Nagios process on the local\n"
242                  "machine. The plugin will check to make sure the Nagios status log is no older\n"
243                  "than the number of minutes specified by the <expire_minutes> option.  It also\n"
244                  "uses the /bin/ps command to check for a process matching whatever you specify\n"
245                  "by the <process_string> argument.\n");
246         print_usage ();
247         printf
248                 ("\nOptions:\n"
249                  "-F, --filename=FILE\n"
250                  "   Name of the log file to check\n"
251                  "-e, --expires=INTEGER\n"
252                  "   Seconds aging afterwhich logfile is condsidered stale\n"
253                  "-C, --command=STRING\n"
254                  "   Command to search for in process table\n"
255                  "-h, --help\n"
256                  "   Print this help screen\n"
257                  "-V, --version\n"
258                  "   Print version information\n\n"
259                  "Example:\n"
260                  "   ./check_nagios -F /usr/local/nagios/var/status.log -e 5 -C /usr/local/nagios/bin/nagios\n");