summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 12:53:50 +0100
committerRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 12:53:50 +0100
commiteb6632f57d1c6799f5eea36881e782d627080efd (patch)
tree3c47a9aac4a916cad87f9fa1729191d5fe7522b1 /plugins
parent290025f8a983baf5b227e50f318a2a16f2e741a8 (diff)
downloadmonitoring-plugins-eb6632f57d1c6799f5eea36881e782d627080efd.tar.gz
check_nagios: clang-format
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_nagios.c289
1 files changed, 131 insertions, 158 deletions
diff --git a/plugins/check_nagios.c b/plugins/check_nagios.c
index 40d68f03..7d647e69 100644
--- a/plugins/check_nagios.c
+++ b/plugins/check_nagios.c
@@ -1,36 +1,36 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_nagios plugin 3 * Monitoring check_nagios plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 1999-2007 Monitoring Plugins Development Team 6 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_nagios plugin 10 * This file contains the check_nagios plugin
11* 11 *
12* This plugin checks the status of the Nagios process on the local machine. 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 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. 14 * the number of minutes specified by the expires option.
15* It also checks the process table for a process matching the command 15 * It also checks the process table for a process matching the command
16* argument. 16 * argument.
17* 17 *
18* 18 *
19* This program is free software: you can redistribute it and/or modify 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 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 21 * the Free Software Foundation, either version 3 of the License, or
22* (at your option) any later version. 22 * (at your option) any later version.
23* 23 *
24* This program is distributed in the hope that it will be useful, 24 * This program is distributed in the hope that it will be useful,
25* but WITHOUT ANY WARRANTY; without even the implied warranty of 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27* GNU General Public License for more details. 27 * GNU General Public License for more details.
28* 28 *
29* You should have received a copy of the GNU General Public License 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/>. 30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31* 31 *
32* 32 *
33*****************************************************************************/ 33 *****************************************************************************/
34 34
35const char *progname = "check_nagios"; 35const char *progname = "check_nagios";
36const char *copyright = "1999-2007"; 36const char *copyright = "1999-2007";
@@ -40,9 +40,9 @@ const char *email = "devel@monitoring-plugins.org";
40#include "runcmd.h" 40#include "runcmd.h"
41#include "utils.h" 41#include "utils.h"
42 42
43int process_arguments (int, char **); 43int process_arguments(int, char **);
44void print_help (void); 44void print_help(void);
45void print_usage (void); 45void print_usage(void);
46 46
47char *status_log = NULL; 47char *status_log = NULL;
48char *process_string = NULL; 48char *process_string = NULL;
@@ -50,9 +50,7 @@ int expire_minutes = 0;
50 50
51int verbose = 0; 51int verbose = 0;
52 52
53int 53int main(int argc, char **argv) {
54main (int argc, char **argv)
55{
56 int result = STATE_UNKNOWN; 54 int result = STATE_UNKNOWN;
57 char input_buffer[MAX_INPUT_BUFFER]; 55 char input_buffer[MAX_INPUT_BUFFER];
58 unsigned long latest_entry_time = 0L; 56 unsigned long latest_entry_time = 0L;
@@ -80,246 +78,221 @@ main (int argc, char **argv)
80 output chld_out, chld_err; 78 output chld_out, chld_err;
81 size_t i; 79 size_t i;
82 80
83 setlocale (LC_ALL, ""); 81 setlocale(LC_ALL, "");
84 bindtextdomain (PACKAGE, LOCALEDIR); 82 bindtextdomain(PACKAGE, LOCALEDIR);
85 textdomain (PACKAGE); 83 textdomain(PACKAGE);
86 84
87 /* Parse extra opts if any */ 85 /* Parse extra opts if any */
88 argv=np_extra_opts (&argc, argv, progname); 86 argv = np_extra_opts(&argc, argv, progname);
89 87
90 if (process_arguments (argc, argv) == ERROR) 88 if (process_arguments(argc, argv) == ERROR)
91 usage_va(_("Could not parse arguments")); 89 usage_va(_("Could not parse arguments"));
92 90
93 /* Set signal handling and alarm timeout */ 91 /* Set signal handling and alarm timeout */
94 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { 92 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
95 usage_va(_("Cannot catch SIGALRM")); 93 usage_va(_("Cannot catch SIGALRM"));
96 } 94 }
97 95
98 /* handle timeouts gracefully... */ 96 /* handle timeouts gracefully... */
99 alarm (timeout_interval); 97 alarm(timeout_interval);
100 98
101 /* open the status log */ 99 /* open the status log */
102 fp = fopen (status_log, "r"); 100 fp = fopen(status_log, "r");
103 if (fp == NULL) { 101 if (fp == NULL) {
104 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!")); 102 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
105 } 103 }
106 104
107 /* get the date/time of the last item updated in the log */ 105 /* get the date/time of the last item updated in the log */
108 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 106 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
109 if ((temp_ptr = strstr (input_buffer, "created=")) != NULL) { 107 if ((temp_ptr = strstr(input_buffer, "created=")) != NULL) {
110 temp_entry_time = strtoul (temp_ptr + 8, NULL, 10); 108 temp_entry_time = strtoul(temp_ptr + 8, NULL, 10);
111 latest_entry_time = temp_entry_time; 109 latest_entry_time = temp_entry_time;
112 break; 110 break;
113 } else if ((temp_ptr = strtok (input_buffer, "]")) != NULL) { 111 } else if ((temp_ptr = strtok(input_buffer, "]")) != NULL) {
114 temp_entry_time = strtoul (temp_ptr + 1, NULL, 10); 112 temp_entry_time = strtoul(temp_ptr + 1, NULL, 10);
115 if (temp_entry_time > latest_entry_time) 113 if (temp_entry_time > latest_entry_time)
116 latest_entry_time = temp_entry_time; 114 latest_entry_time = temp_entry_time;
117 } 115 }
118 } 116 }
119 fclose (fp); 117 fclose(fp);
120 118
121 if (verbose >= 2) 119 if (verbose >= 2)
122 printf("command: %s\n", PS_COMMAND); 120 printf("command: %s\n", PS_COMMAND);
123 121
124 /* run the command to check for the Nagios process.. */ 122 /* run the command to check for the Nagios process.. */
125 if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) 123 if ((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0)
126 result = STATE_WARNING; 124 result = STATE_WARNING;
127 125
128 /* count the number of matching Nagios processes... */ 126 /* count the number of matching Nagios processes... */
129 for(i = 0; i < chld_out.lines; i++) { 127 for (i = 0; i < chld_out.lines; i++) {
130 cols = sscanf (chld_out.line[i], PS_FORMAT, PS_VARLIST); 128 cols = sscanf(chld_out.line[i], PS_FORMAT, PS_VARLIST);
131 /* Zombie processes do not give a procprog command */ 129 /* Zombie processes do not give a procprog command */
132 if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) { 130 if (cols == (expected_cols - 1) && strstr(procstat, zombie)) {
133 cols = expected_cols; 131 cols = expected_cols;
134 /* Set some value for procargs for the strip command further below 132 /* Set some value for procargs for the strip command further below
135 * Seen to be a problem on some Solaris 7 and 8 systems */ 133 * Seen to be a problem on some Solaris 7 and 8 systems */
136 chld_out.line[i][pos] = '\n'; 134 chld_out.line[i][pos] = '\n';
137 chld_out.line[i][pos+1] = 0x0; 135 chld_out.line[i][pos + 1] = 0x0;
138 } 136 }
139 if ( cols >= expected_cols ) { 137 if (cols >= expected_cols) {
140 xasprintf (&procargs, "%s", chld_out.line[i] + pos); 138 xasprintf(&procargs, "%s", chld_out.line[i] + pos);
141 strip (procargs); 139 strip(procargs);
142 140
143 /* Some ps return full pathname for command. This removes path */ 141 /* Some ps return full pathname for command. This removes path */
144 temp_string = strtok ((char *)procprog, "/"); 142 temp_string = strtok((char *)procprog, "/");
145 while (temp_string) { 143 while (temp_string) {
146 strcpy(procprog, temp_string); 144 strcpy(procprog, temp_string);
147 temp_string = strtok (NULL, "/"); 145 temp_string = strtok(NULL, "/");
148 } 146 }
149 147
150 /* May get empty procargs */ 148 /* May get empty procargs */
151 if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) { 149 if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs, "")) {
152 proc_entries++; 150 proc_entries++;
153 if (verbose >= 2) { 151 if (verbose >= 2) {
154 printf (_("Found process: %s %s\n"), procprog, procargs); 152 printf(_("Found process: %s %s\n"), procprog, procargs);
155 } 153 }
156 } 154 }
157 } 155 }
158 } 156 }
159 157
160 /* If we get anything on stderr, at least set warning */ 158 /* If we get anything on stderr, at least set warning */
161 if(chld_err.buflen) 159 if (chld_err.buflen)
162 result = max_state (result, STATE_WARNING); 160 result = max_state(result, STATE_WARNING);
163 161
164 /* reset the alarm handler */ 162 /* reset the alarm handler */
165 alarm (0); 163 alarm(0);
166 164
167 if (proc_entries == 0) { 165 if (proc_entries == 0) {
168 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!")); 166 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Could not locate a running Nagios process!"));
169 } 167 }
170 168
171 if (latest_entry_time == 0L) { 169 if (latest_entry_time == 0L) {
172 die (STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time")); 170 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
173 } 171 }
174 172
175 time (&current_time); 173 time(&current_time);
176 if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) { 174 if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) {
177 result = STATE_WARNING; 175 result = STATE_WARNING;
178 } else { 176 } else {
179 result = STATE_OK; 177 result = STATE_OK;
180 } 178 }
181 179
182 printf ("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING")); 180 printf("NAGIOS %s: ", (result == STATE_OK) ? _("OK") : _("WARNING"));
183 printf (ngettext ("%d process", "%d processes", proc_entries), proc_entries); 181 printf(ngettext("%d process", "%d processes", proc_entries), proc_entries);
184 printf (", "); 182 printf(", ");
185 printf ( 183 printf(ngettext("status log updated %d second ago", "status log updated %d seconds ago", (int)(current_time - latest_entry_time)),
186 ngettext ("status log updated %d second ago", 184 (int)(current_time - latest_entry_time));
187 "status log updated %d seconds ago", 185 printf("\n");
188 (int) (current_time - latest_entry_time) ),
189 (int) (current_time - latest_entry_time) );
190 printf ("\n");
191 186
192 return result; 187 return result;
193} 188}
194 189
195
196
197/* process command-line arguments */ 190/* process command-line arguments */
198int 191int process_arguments(int argc, char **argv) {
199process_arguments (int argc, char **argv)
200{
201 int c; 192 int c;
202 193
203 int option = 0; 194 int option = 0;
204 static struct option longopts[] = { 195 static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'},
205 {"filename", required_argument, 0, 'F'}, 196 {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'},
206 {"expires", required_argument, 0, 'e'}, 197 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'},
207 {"command", required_argument, 0, 'C'}, 198 {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}};
208 {"timeout", optional_argument, 0, 't'},
209 {"version", no_argument, 0, 'V'},
210 {"help", no_argument, 0, 'h'},
211 {"verbose", no_argument, 0, 'v'},
212 {0, 0, 0, 0}
213 };
214 199
215 if (argc < 2) 200 if (argc < 2)
216 return ERROR; 201 return ERROR;
217 202
218 if (!is_option (argv[1])) { 203 if (!is_option(argv[1])) {
219 status_log = argv[1]; 204 status_log = argv[1];
220 if (is_intnonneg (argv[2])) 205 if (is_intnonneg(argv[2]))
221 expire_minutes = atoi (argv[2]); 206 expire_minutes = atoi(argv[2]);
222 else 207 else
223 die (STATE_UNKNOWN, 208 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n"));
224 _("Expiration time must be an integer (seconds)\n"));
225 process_string = argv[3]; 209 process_string = argv[3];
226 return OK; 210 return OK;
227 } 211 }
228 212
229 while (1) { 213 while (1) {
230 c = getopt_long (argc, argv, "+hVvF:C:e:t:", longopts, &option); 214 c = getopt_long(argc, argv, "+hVvF:C:e:t:", longopts, &option);
231 215
232 if (c == -1 || c == EOF || c == 1) 216 if (c == -1 || c == EOF || c == 1)
233 break; 217 break;
234 218
235 switch (c) { 219 switch (c) {
236 case 'h': /* help */ 220 case 'h': /* help */
237 print_help (); 221 print_help();
238 exit (STATE_UNKNOWN); 222 exit(STATE_UNKNOWN);
239 case 'V': /* version */ 223 case 'V': /* version */
240 print_revision (progname, NP_VERSION); 224 print_revision(progname, NP_VERSION);
241 exit (STATE_UNKNOWN); 225 exit(STATE_UNKNOWN);
242 case 'F': /* status log */ 226 case 'F': /* status log */
243 status_log = optarg; 227 status_log = optarg;
244 break; 228 break;
245 case 'C': /* command */ 229 case 'C': /* command */
246 process_string = optarg; 230 process_string = optarg;
247 break; 231 break;
248 case 'e': /* expiry time */ 232 case 'e': /* expiry time */
249 if (is_intnonneg (optarg)) 233 if (is_intnonneg(optarg))
250 expire_minutes = atoi (optarg); 234 expire_minutes = atoi(optarg);
251 else 235 else
252 die (STATE_UNKNOWN, 236 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n"));
253 _("Expiration time must be an integer (seconds)\n"));
254 break; 237 break;
255 case 't': /* timeout */ 238 case 't': /* timeout */
256 if (is_intnonneg (optarg)) 239 if (is_intnonneg(optarg))
257 timeout_interval = atoi (optarg); 240 timeout_interval = atoi(optarg);
258 else 241 else
259 die (STATE_UNKNOWN, 242 die(STATE_UNKNOWN, _("Timeout must be an integer (seconds)\n"));
260 _("Timeout must be an integer (seconds)\n"));
261 break; 243 break;
262 case 'v': 244 case 'v':
263 verbose++; 245 verbose++;
264 break; 246 break;
265 default: /* print short usage_va statement if args not parsable */ 247 default: /* print short usage_va statement if args not parsable */
266 usage5(); 248 usage5();
267 } 249 }
268 } 250 }
269 251
270
271 if (status_log == NULL) 252 if (status_log == NULL)
272 die (STATE_UNKNOWN, _("You must provide the status_log\n")); 253 die(STATE_UNKNOWN, _("You must provide the status_log\n"));
273 254
274 if (process_string == NULL) 255 if (process_string == NULL)
275 die (STATE_UNKNOWN, _("You must provide a process string\n")); 256 die(STATE_UNKNOWN, _("You must provide a process string\n"));
276 257
277 return OK; 258 return OK;
278} 259}
279 260
261void print_help(void) {
262 print_revision(progname, NP_VERSION);
280 263
264 printf(_(COPYRIGHT), copyright, email);
281 265
282void 266 printf("%s\n", _("This plugin checks the status of the Nagios process on the local machine"));
283print_help (void) 267 printf("%s\n", _("The plugin will check to make sure the Nagios status log is no older than"));
284{ 268 printf("%s\n", _("the number of minutes specified by the expires option."));
285 print_revision (progname, NP_VERSION); 269 printf("%s\n", _("It also checks the process table for a process matching the command argument."));
286
287 printf (_(COPYRIGHT), copyright, email);
288 270
289 printf ("%s\n", _("This plugin checks the status of the Nagios process on the local machine")); 271 printf("\n\n");
290 printf ("%s\n", _("The plugin will check to make sure the Nagios status log is no older than"));
291 printf ("%s\n", _("the number of minutes specified by the expires option."));
292 printf ("%s\n", _("It also checks the process table for a process matching the command argument."));
293 272
294 printf ("\n\n"); 273 print_usage();
295 274
296 print_usage (); 275 printf(UT_HELP_VRSN);
276 printf(UT_EXTRA_OPTS);
297 277
298 printf (UT_HELP_VRSN); 278 printf(" %s\n", "-F, --filename=FILE");
299 printf (UT_EXTRA_OPTS); 279 printf(" %s\n", _("Name of the log file to check"));
280 printf(" %s\n", "-e, --expires=INTEGER");
281 printf(" %s\n", _("Minutes aging after which logfile is considered stale"));
282 printf(" %s\n", "-C, --command=STRING");
283 printf(" %s\n", _("Substring to search for in process arguments"));
284 printf(" %s\n", "-t, --timeout=INTEGER");
285 printf(" %s\n", _("Timeout for the plugin in seconds"));
286 printf(UT_VERBOSE);
300 287
301 printf (" %s\n", "-F, --filename=FILE"); 288 printf("\n");
302 printf (" %s\n", _("Name of the log file to check")); 289 printf("%s\n", _("Examples:"));
303 printf (" %s\n", "-e, --expires=INTEGER"); 290 printf(" %s\n", "check_nagios -t 20 -e 5 -F /usr/local/nagios/var/status.log -C /usr/local/nagios/bin/nagios");
304 printf (" %s\n", _("Minutes aging after which logfile is considered stale"));
305 printf (" %s\n", "-C, --command=STRING");
306 printf (" %s\n", _("Substring to search for in process arguments"));
307 printf (" %s\n", "-t, --timeout=INTEGER");
308 printf (" %s\n", _("Timeout for the plugin in seconds"));
309 printf (UT_VERBOSE);
310 291
311 printf ("\n"); 292 printf(UT_SUPPORT);
312 printf ("%s\n", _("Examples:"));
313 printf (" %s\n", "check_nagios -t 20 -e 5 -F /usr/local/nagios/var/status.log -C /usr/local/nagios/bin/nagios");
314
315 printf (UT_SUPPORT);
316} 293}
317 294
318 295void print_usage(void) {
319 296 printf("%s\n", _("Usage:"));
320void 297 printf("%s -F <status log file> -t <timeout_seconds> -e <expire_minutes> -C <process_string>\n", progname);
321print_usage (void)
322{
323 printf ("%s\n", _("Usage:"));
324 printf ("%s -F <status log file> -t <timeout_seconds> -e <expire_minutes> -C <process_string>\n", progname);
325} 298}