summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Makefile.am1
-rw-r--r--plugins/check_nagios.c134
-rw-r--r--plugins/check_nagios.d/config.h19
3 files changed, 89 insertions, 65 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 1e4789ff..d27c9efd 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -53,6 +53,7 @@ EXTRA_DIST = t \
53 check_ldap.d \ 53 check_ldap.d \
54 check_hpjd.d \ 54 check_hpjd.d \
55 check_game.d \ 55 check_game.d \
56 check_nagios.d \
56 check_dbi.d \ 57 check_dbi.d \
57 check_ssh.d \ 58 check_ssh.d \
58 check_dns.d \ 59 check_dns.d \
diff --git a/plugins/check_nagios.c b/plugins/check_nagios.c
index 72613f97..a46dc1ed 100644
--- a/plugins/check_nagios.c
+++ b/plugins/check_nagios.c
@@ -39,47 +39,20 @@ const char *email = "devel@monitoring-plugins.org";
39#include "common.h" 39#include "common.h"
40#include "runcmd.h" 40#include "runcmd.h"
41#include "utils.h" 41#include "utils.h"
42 42#include "states.h"
43static int process_arguments(int /*argc*/, char ** /*argv*/); 43#include "check_nagios.d/config.h"
44
45typedef struct {
46 int errorcode;
47 check_nagios_config config;
48} check_nagios_config_wrapper;
49static check_nagios_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
44static void print_help(void); 50static void print_help(void);
45void print_usage(void); 51void print_usage(void);
46 52
47static char *status_log = NULL;
48static char *process_string = NULL;
49static int expire_minutes = 0;
50
51static int verbose = 0; 53static int verbose = 0;
52 54
53int main(int argc, char **argv) { 55int main(int argc, char **argv) {
54 int result = STATE_UNKNOWN;
55 char input_buffer[MAX_INPUT_BUFFER];
56 unsigned long latest_entry_time = 0L;
57 unsigned long temp_entry_time = 0L;
58 int proc_entries = 0;
59 time_t current_time;
60 char *temp_ptr;
61 FILE *fp;
62 int procuid = 0;
63 int procpid = 0;
64 int procppid = 0;
65 int procvsz = 0;
66 int procrss = 0;
67 float procpcpu = 0;
68 char procstat[8];
69#ifdef PS_USES_PROCETIME
70 char procetime[MAX_INPUT_BUFFER];
71#endif /* PS_USES_PROCETIME */
72 char procprog[MAX_INPUT_BUFFER];
73 char *procargs;
74 int pos;
75 int cols;
76 int expected_cols = PS_COLS - 1;
77 const char *zombie = "Z";
78 char *temp_string;
79 output chld_out;
80 output chld_err;
81 size_t i;
82
83 setlocale(LC_ALL, ""); 56 setlocale(LC_ALL, "");
84 bindtextdomain(PACKAGE, LOCALEDIR); 57 bindtextdomain(PACKAGE, LOCALEDIR);
85 textdomain(PACKAGE); 58 textdomain(PACKAGE);
@@ -87,10 +60,14 @@ int main(int argc, char **argv) {
87 /* Parse extra opts if any */ 60 /* Parse extra opts if any */
88 argv = np_extra_opts(&argc, argv, progname); 61 argv = np_extra_opts(&argc, argv, progname);
89 62
90 if (process_arguments(argc, argv) == ERROR) { 63 check_nagios_config_wrapper tmp_config = process_arguments(argc, argv);
64
65 if (tmp_config.errorcode == ERROR) {
91 usage_va(_("Could not parse arguments")); 66 usage_va(_("Could not parse arguments"));
92 } 67 }
93 68
69 const check_nagios_config config = tmp_config.config;
70
94 /* Set signal handling and alarm timeout */ 71 /* Set signal handling and alarm timeout */
95 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 72 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
96 usage_va(_("Cannot catch SIGALRM")); 73 usage_va(_("Cannot catch SIGALRM"));
@@ -100,13 +77,17 @@ int main(int argc, char **argv) {
100 alarm(timeout_interval); 77 alarm(timeout_interval);
101 78
102 /* open the status log */ 79 /* open the status log */
103 fp = fopen(status_log, "r"); 80 FILE *log_file = fopen(config.status_log, "r");
104 if (fp == NULL) { 81 if (log_file == NULL) {
105 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!")); 82 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot open status log for reading!"));
106 } 83 }
107 84
85 unsigned long latest_entry_time = 0L;
86 unsigned long temp_entry_time = 0L;
87 char input_buffer[MAX_INPUT_BUFFER];
88 char *temp_ptr;
108 /* get the date/time of the last item updated in the log */ 89 /* get the date/time of the last item updated in the log */
109 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 90 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, log_file)) {
110 if ((temp_ptr = strstr(input_buffer, "created=")) != NULL) { 91 if ((temp_ptr = strstr(input_buffer, "created=")) != NULL) {
111 temp_entry_time = strtoul(temp_ptr + 8, NULL, 10); 92 temp_entry_time = strtoul(temp_ptr + 8, NULL, 10);
112 latest_entry_time = temp_entry_time; 93 latest_entry_time = temp_entry_time;
@@ -119,20 +100,39 @@ int main(int argc, char **argv) {
119 } 100 }
120 } 101 }
121 } 102 }
122 fclose(fp); 103 fclose(log_file);
123 104
124 if (verbose >= 2) { 105 if (verbose >= 2) {
125 printf("command: %s\n", PS_COMMAND); 106 printf("command: %s\n", PS_COMMAND);
126 } 107 }
127 108
128 /* run the command to check for the Nagios process.. */ 109 /* run the command to check for the Nagios process.. */
110 mp_state_enum result = STATE_UNKNOWN;
111 output chld_out;
112 output chld_err;
129 if ((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) { 113 if ((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) {
130 result = STATE_WARNING; 114 result = STATE_WARNING;
131 } 115 }
132 116
117 int procuid = 0;
118 int procpid = 0;
119 int procppid = 0;
120 int procvsz = 0;
121 int procrss = 0;
122 int proc_entries = 0;
123 float procpcpu = 0;
124 char procstat[8];
125 char procprog[MAX_INPUT_BUFFER];
126 char *procargs;
127#ifdef PS_USES_PROCETIME
128 char procetime[MAX_INPUT_BUFFER];
129#endif /* PS_USES_PROCETIME */
130 int pos;
131 int expected_cols = PS_COLS - 1;
132 const char *zombie = "Z";
133 /* count the number of matching Nagios processes... */ 133 /* count the number of matching Nagios processes... */
134 for (i = 0; i < chld_out.lines; i++) { 134 for (size_t i = 0; i < chld_out.lines; i++) {
135 cols = sscanf(chld_out.line[i], PS_FORMAT, PS_VARLIST); 135 int cols = sscanf(chld_out.line[i], PS_FORMAT, PS_VARLIST);
136 /* Zombie processes do not give a procprog command */ 136 /* Zombie processes do not give a procprog command */
137 if (cols == (expected_cols - 1) && strstr(procstat, zombie)) { 137 if (cols == (expected_cols - 1) && strstr(procstat, zombie)) {
138 cols = expected_cols; 138 cols = expected_cols;
@@ -146,14 +146,14 @@ int main(int argc, char **argv) {
146 strip(procargs); 146 strip(procargs);
147 147
148 /* Some ps return full pathname for command. This removes path */ 148 /* Some ps return full pathname for command. This removes path */
149 temp_string = strtok((char *)procprog, "/"); 149 char *temp_string = strtok((char *)procprog, "/");
150 while (temp_string) { 150 while (temp_string) {
151 strcpy(procprog, temp_string); 151 strcpy(procprog, temp_string);
152 temp_string = strtok(NULL, "/"); 152 temp_string = strtok(NULL, "/");
153 } 153 }
154 154
155 /* May get empty procargs */ 155 /* May get empty procargs */
156 if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs, "")) { 156 if (!strstr(procargs, argv[0]) && strstr(procargs, config.process_string) && strcmp(procargs, "")) {
157 proc_entries++; 157 proc_entries++;
158 if (verbose >= 2) { 158 if (verbose >= 2) {
159 printf(_("Found process: %s %s\n"), procprog, procargs); 159 printf(_("Found process: %s %s\n"), procprog, procargs);
@@ -178,8 +178,9 @@ int main(int argc, char **argv) {
178 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time")); 178 die(STATE_CRITICAL, "NAGIOS %s: %s\n", _("CRITICAL"), _("Cannot parse Nagios log file for valid time"));
179 } 179 }
180 180
181 time_t current_time;
181 time(&current_time); 182 time(&current_time);
182 if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) { 183 if ((int)(current_time - latest_entry_time) > (config.expire_minutes * 60)) {
183 result = STATE_WARNING; 184 result = STATE_WARNING;
184 } else { 185 } else {
185 result = STATE_OK; 186 result = STATE_OK;
@@ -192,42 +193,45 @@ int main(int argc, char **argv) {
192 (int)(current_time - latest_entry_time)); 193 (int)(current_time - latest_entry_time));
193 printf("\n"); 194 printf("\n");
194 195
195 return result; 196 exit(result);
196} 197}
197 198
198/* process command-line arguments */ 199/* process command-line arguments */
199int process_arguments(int argc, char **argv) { 200check_nagios_config_wrapper process_arguments(int argc, char **argv) {
200 int c;
201
202 int option = 0;
203 static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, 201 static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'},
204 {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'}, 202 {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'},
205 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, 203 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'},
206 {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; 204 {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}};
207 205
206 check_nagios_config_wrapper result = {
207 .errorcode = OK,
208 .config = check_nagios_config_init(),
209 };
208 if (argc < 2) { 210 if (argc < 2) {
209 return ERROR; 211 result.errorcode = ERROR;
212 return result;
210 } 213 }
211 214
212 if (!is_option(argv[1])) { 215 if (!is_option(argv[1])) {
213 status_log = argv[1]; 216 result.config.status_log = argv[1];
214 if (is_intnonneg(argv[2])) { 217 if (is_intnonneg(argv[2])) {
215 expire_minutes = atoi(argv[2]); 218 result.config.expire_minutes = atoi(argv[2]);
216 } else { 219 } else {
217 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); 220 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n"));
218 } 221 }
219 process_string = argv[3]; 222 result.config.process_string = argv[3];
220 return OK; 223 return result;
221 } 224 }
222 225
223 while (1) { 226 int option = 0;
224 c = getopt_long(argc, argv, "+hVvF:C:e:t:", longopts, &option); 227 while (true) {
228 int option_index = getopt_long(argc, argv, "+hVvF:C:e:t:", longopts, &option);
225 229
226 if (c == -1 || c == EOF || c == 1) { 230 if (option_index == -1 || option_index == EOF || option_index == 1) {
227 break; 231 break;
228 } 232 }
229 233
230 switch (c) { 234 switch (option_index) {
231 case 'h': /* help */ 235 case 'h': /* help */
232 print_help(); 236 print_help();
233 exit(STATE_UNKNOWN); 237 exit(STATE_UNKNOWN);
@@ -235,14 +239,14 @@ int process_arguments(int argc, char **argv) {
235 print_revision(progname, NP_VERSION); 239 print_revision(progname, NP_VERSION);
236 exit(STATE_UNKNOWN); 240 exit(STATE_UNKNOWN);
237 case 'F': /* status log */ 241 case 'F': /* status log */
238 status_log = optarg; 242 result.config.status_log = optarg;
239 break; 243 break;
240 case 'C': /* command */ 244 case 'C': /* command */
241 process_string = optarg; 245 result.config.process_string = optarg;
242 break; 246 break;
243 case 'e': /* expiry time */ 247 case 'e': /* expiry time */
244 if (is_intnonneg(optarg)) { 248 if (is_intnonneg(optarg)) {
245 expire_minutes = atoi(optarg); 249 result.config.expire_minutes = atoi(optarg);
246 } else { 250 } else {
247 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); 251 die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n"));
248 } 252 }
@@ -262,15 +266,15 @@ int process_arguments(int argc, char **argv) {
262 } 266 }
263 } 267 }
264 268
265 if (status_log == NULL) { 269 if (result.config.status_log == NULL) {
266 die(STATE_UNKNOWN, _("You must provide the status_log\n")); 270 die(STATE_UNKNOWN, _("You must provide the status_log\n"));
267 } 271 }
268 272
269 if (process_string == NULL) { 273 if (result.config.process_string == NULL) {
270 die(STATE_UNKNOWN, _("You must provide a process string\n")); 274 die(STATE_UNKNOWN, _("You must provide a process string\n"));
271 } 275 }
272 276
273 return OK; 277 return result;
274} 278}
275 279
276void print_help(void) { 280void print_help(void) {
diff --git a/plugins/check_nagios.d/config.h b/plugins/check_nagios.d/config.h
new file mode 100644
index 00000000..efe139f9
--- /dev/null
+++ b/plugins/check_nagios.d/config.h
@@ -0,0 +1,19 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6typedef struct {
7 char *status_log;
8 char *process_string;
9 int expire_minutes;
10} check_nagios_config;
11
12check_nagios_config check_nagios_config_init() {
13 check_nagios_config tmp = {
14 .status_log = NULL,
15 .process_string = NULL,
16 .expire_minutes = 0,
17 };
18 return tmp;
19}