diff options
Diffstat (limited to 'plugins/check_nagios.c')
-rw-r--r-- | plugins/check_nagios.c | 168 |
1 files changed, 92 insertions, 76 deletions
diff --git a/plugins/check_nagios.c b/plugins/check_nagios.c index 48629be3..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" | |
43 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 43 | #include "check_nagios.d/config.h" |
44 | |||
45 | typedef struct { | ||
46 | int errorcode; | ||
47 | check_nagios_config config; | ||
48 | } check_nagios_config_wrapper; | ||
49 | static check_nagios_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
44 | static void print_help(void); | 50 | static void print_help(void); |
45 | void print_usage(void); | 51 | void print_usage(void); |
46 | 52 | ||
47 | static char *status_log = NULL; | ||
48 | static char *process_string = NULL; | ||
49 | static int expire_minutes = 0; | ||
50 | |||
51 | static int verbose = 0; | 53 | static int verbose = 0; |
52 | 54 | ||
53 | int main(int argc, char **argv) { | 55 | int 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,8 +60,13 @@ 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")); |
67 | } | ||
68 | |||
69 | const check_nagios_config config = tmp_config.config; | ||
92 | 70 | ||
93 | /* Set signal handling and alarm timeout */ | 71 | /* Set signal handling and alarm timeout */ |
94 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 72 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
@@ -99,13 +77,17 @@ int main(int argc, char **argv) { | |||
99 | alarm(timeout_interval); | 77 | alarm(timeout_interval); |
100 | 78 | ||
101 | /* open the status log */ | 79 | /* open the status log */ |
102 | fp = fopen(status_log, "r"); | 80 | FILE *log_file = fopen(config.status_log, "r"); |
103 | if (fp == NULL) { | 81 | if (log_file == NULL) { |
104 | 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!")); |
105 | } | 83 | } |
106 | 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; | ||
107 | /* 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 */ |
108 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | 90 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, log_file)) { |
109 | if ((temp_ptr = strstr(input_buffer, "created=")) != NULL) { | 91 | if ((temp_ptr = strstr(input_buffer, "created=")) != NULL) { |
110 | temp_entry_time = strtoul(temp_ptr + 8, NULL, 10); | 92 | temp_entry_time = strtoul(temp_ptr + 8, NULL, 10); |
111 | latest_entry_time = temp_entry_time; | 93 | latest_entry_time = temp_entry_time; |
@@ -113,22 +95,44 @@ int main(int argc, char **argv) { | |||
113 | } | 95 | } |
114 | if ((temp_ptr = strtok(input_buffer, "]")) != NULL) { | 96 | if ((temp_ptr = strtok(input_buffer, "]")) != NULL) { |
115 | temp_entry_time = strtoul(temp_ptr + 1, NULL, 10); | 97 | temp_entry_time = strtoul(temp_ptr + 1, NULL, 10); |
116 | if (temp_entry_time > latest_entry_time) | 98 | if (temp_entry_time > latest_entry_time) { |
117 | latest_entry_time = temp_entry_time; | 99 | latest_entry_time = temp_entry_time; |
100 | } | ||
118 | } | 101 | } |
119 | } | 102 | } |
120 | fclose(fp); | 103 | fclose(log_file); |
121 | 104 | ||
122 | if (verbose >= 2) | 105 | if (verbose >= 2) { |
123 | printf("command: %s\n", PS_COMMAND); | 106 | printf("command: %s\n", PS_COMMAND); |
107 | } | ||
124 | 108 | ||
125 | /* run the command to check for the Nagios process.. */ | 109 | /* run the command to check for the Nagios process.. */ |
126 | if ((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) | 110 | mp_state_enum result = STATE_UNKNOWN; |
111 | output chld_out; | ||
112 | output chld_err; | ||
113 | if ((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) { | ||
127 | result = STATE_WARNING; | 114 | result = STATE_WARNING; |
115 | } | ||
128 | 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"; | ||
129 | /* count the number of matching Nagios processes... */ | 133 | /* count the number of matching Nagios processes... */ |
130 | for (i = 0; i < chld_out.lines; i++) { | 134 | for (size_t i = 0; i < chld_out.lines; i++) { |
131 | cols = sscanf(chld_out.line[i], PS_FORMAT, PS_VARLIST); | 135 | int cols = sscanf(chld_out.line[i], PS_FORMAT, PS_VARLIST); |
132 | /* Zombie processes do not give a procprog command */ | 136 | /* Zombie processes do not give a procprog command */ |
133 | if (cols == (expected_cols - 1) && strstr(procstat, zombie)) { | 137 | if (cols == (expected_cols - 1) && strstr(procstat, zombie)) { |
134 | cols = expected_cols; | 138 | cols = expected_cols; |
@@ -142,14 +146,14 @@ int main(int argc, char **argv) { | |||
142 | strip(procargs); | 146 | strip(procargs); |
143 | 147 | ||
144 | /* Some ps return full pathname for command. This removes path */ | 148 | /* Some ps return full pathname for command. This removes path */ |
145 | temp_string = strtok((char *)procprog, "/"); | 149 | char *temp_string = strtok((char *)procprog, "/"); |
146 | while (temp_string) { | 150 | while (temp_string) { |
147 | strcpy(procprog, temp_string); | 151 | strcpy(procprog, temp_string); |
148 | temp_string = strtok(NULL, "/"); | 152 | temp_string = strtok(NULL, "/"); |
149 | } | 153 | } |
150 | 154 | ||
151 | /* May get empty procargs */ | 155 | /* May get empty procargs */ |
152 | if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs, "")) { | 156 | if (!strstr(procargs, argv[0]) && strstr(procargs, config.process_string) && strcmp(procargs, "")) { |
153 | proc_entries++; | 157 | proc_entries++; |
154 | if (verbose >= 2) { | 158 | if (verbose >= 2) { |
155 | printf(_("Found process: %s %s\n"), procprog, procargs); | 159 | printf(_("Found process: %s %s\n"), procprog, procargs); |
@@ -159,8 +163,9 @@ int main(int argc, char **argv) { | |||
159 | } | 163 | } |
160 | 164 | ||
161 | /* If we get anything on stderr, at least set warning */ | 165 | /* If we get anything on stderr, at least set warning */ |
162 | if (chld_err.buflen) | 166 | if (chld_err.buflen) { |
163 | result = max_state(result, STATE_WARNING); | 167 | result = max_state(result, STATE_WARNING); |
168 | } | ||
164 | 169 | ||
165 | /* reset the alarm handler */ | 170 | /* reset the alarm handler */ |
166 | alarm(0); | 171 | alarm(0); |
@@ -173,8 +178,9 @@ int main(int argc, char **argv) { | |||
173 | 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")); |
174 | } | 179 | } |
175 | 180 | ||
181 | time_t current_time; | ||
176 | time(¤t_time); | 182 | time(¤t_time); |
177 | if ((int)(current_time - latest_entry_time) > (expire_minutes * 60)) { | 183 | if ((int)(current_time - latest_entry_time) > (config.expire_minutes * 60)) { |
178 | result = STATE_WARNING; | 184 | result = STATE_WARNING; |
179 | } else { | 185 | } else { |
180 | result = STATE_OK; | 186 | result = STATE_OK; |
@@ -187,39 +193,45 @@ int main(int argc, char **argv) { | |||
187 | (int)(current_time - latest_entry_time)); | 193 | (int)(current_time - latest_entry_time)); |
188 | printf("\n"); | 194 | printf("\n"); |
189 | 195 | ||
190 | return result; | 196 | exit(result); |
191 | } | 197 | } |
192 | 198 | ||
193 | /* process command-line arguments */ | 199 | /* process command-line arguments */ |
194 | int process_arguments(int argc, char **argv) { | 200 | check_nagios_config_wrapper process_arguments(int argc, char **argv) { |
195 | int c; | ||
196 | |||
197 | int option = 0; | ||
198 | 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'}, |
199 | {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'}, | 202 | {"command", required_argument, 0, 'C'}, {"timeout", optional_argument, 0, 't'}, |
200 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, | 203 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, |
201 | {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; | 204 | {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; |
202 | 205 | ||
203 | if (argc < 2) | 206 | check_nagios_config_wrapper result = { |
204 | return ERROR; | 207 | .errorcode = OK, |
208 | .config = check_nagios_config_init(), | ||
209 | }; | ||
210 | if (argc < 2) { | ||
211 | result.errorcode = ERROR; | ||
212 | return result; | ||
213 | } | ||
205 | 214 | ||
206 | if (!is_option(argv[1])) { | 215 | if (!is_option(argv[1])) { |
207 | status_log = argv[1]; | 216 | result.config.status_log = argv[1]; |
208 | if (is_intnonneg(argv[2])) | 217 | if (is_intnonneg(argv[2])) { |
209 | expire_minutes = atoi(argv[2]); | 218 | result.config.expire_minutes = atoi(argv[2]); |
210 | else | 219 | } else { |
211 | die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); | 220 | die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); |
212 | process_string = argv[3]; | 221 | } |
213 | return OK; | 222 | result.config.process_string = argv[3]; |
223 | return result; | ||
214 | } | 224 | } |
215 | 225 | ||
216 | while (1) { | 226 | int option = 0; |
217 | 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); | ||
218 | 229 | ||
219 | if (c == -1 || c == EOF || c == 1) | 230 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
220 | break; | 231 | break; |
232 | } | ||
221 | 233 | ||
222 | switch (c) { | 234 | switch (option_index) { |
223 | case 'h': /* help */ | 235 | case 'h': /* help */ |
224 | print_help(); | 236 | print_help(); |
225 | exit(STATE_UNKNOWN); | 237 | exit(STATE_UNKNOWN); |
@@ -227,22 +239,24 @@ int process_arguments(int argc, char **argv) { | |||
227 | print_revision(progname, NP_VERSION); | 239 | print_revision(progname, NP_VERSION); |
228 | exit(STATE_UNKNOWN); | 240 | exit(STATE_UNKNOWN); |
229 | case 'F': /* status log */ | 241 | case 'F': /* status log */ |
230 | status_log = optarg; | 242 | result.config.status_log = optarg; |
231 | break; | 243 | break; |
232 | case 'C': /* command */ | 244 | case 'C': /* command */ |
233 | process_string = optarg; | 245 | result.config.process_string = optarg; |
234 | break; | 246 | break; |
235 | case 'e': /* expiry time */ | 247 | case 'e': /* expiry time */ |
236 | if (is_intnonneg(optarg)) | 248 | if (is_intnonneg(optarg)) { |
237 | expire_minutes = atoi(optarg); | 249 | result.config.expire_minutes = atoi(optarg); |
238 | else | 250 | } else { |
239 | die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); | 251 | die(STATE_UNKNOWN, _("Expiration time must be an integer (seconds)\n")); |
252 | } | ||
240 | break; | 253 | break; |
241 | case 't': /* timeout */ | 254 | case 't': /* timeout */ |
242 | if (is_intnonneg(optarg)) | 255 | if (is_intnonneg(optarg)) { |
243 | timeout_interval = atoi(optarg); | 256 | timeout_interval = atoi(optarg); |
244 | else | 257 | } else { |
245 | die(STATE_UNKNOWN, _("Timeout must be an integer (seconds)\n")); | 258 | die(STATE_UNKNOWN, _("Timeout must be an integer (seconds)\n")); |
259 | } | ||
246 | break; | 260 | break; |
247 | case 'v': | 261 | case 'v': |
248 | verbose++; | 262 | verbose++; |
@@ -252,13 +266,15 @@ int process_arguments(int argc, char **argv) { | |||
252 | } | 266 | } |
253 | } | 267 | } |
254 | 268 | ||
255 | if (status_log == NULL) | 269 | if (result.config.status_log == NULL) { |
256 | die(STATE_UNKNOWN, _("You must provide the status_log\n")); | 270 | die(STATE_UNKNOWN, _("You must provide the status_log\n")); |
271 | } | ||
257 | 272 | ||
258 | if (process_string == NULL) | 273 | if (result.config.process_string == NULL) { |
259 | die(STATE_UNKNOWN, _("You must provide a process string\n")); | 274 | die(STATE_UNKNOWN, _("You must provide a process string\n")); |
275 | } | ||
260 | 276 | ||
261 | return OK; | 277 | return result; |
262 | } | 278 | } |
263 | 279 | ||
264 | void print_help(void) { | 280 | void print_help(void) { |