diff options
-rw-r--r-- | plugins/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/check_mrtg.c | 141 | ||||
-rw-r--r-- | plugins/check_mrtg.d/config.h | 36 |
3 files changed, 110 insertions, 68 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 9d310a15..5e636f9b 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -55,6 +55,7 @@ EXTRA_DIST = t \ | |||
55 | check_dbi.d \ | 55 | check_dbi.d \ |
56 | check_ssh.d \ | 56 | check_ssh.d \ |
57 | check_dns.d \ | 57 | check_dns.d \ |
58 | check_mrtg.d \ | ||
58 | check_apt.d \ | 59 | check_apt.d \ |
59 | check_by_ssh.d \ | 60 | check_by_ssh.d \ |
60 | check_smtp.d \ | 61 | check_smtp.d \ |
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index e355fa11..5bd276dc 100644 --- a/plugins/check_mrtg.c +++ b/plugins/check_mrtg.c | |||
@@ -35,21 +35,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
35 | 35 | ||
36 | #include "common.h" | 36 | #include "common.h" |
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "check_mrtg.d/config.h" | ||
39 | |||
40 | typedef struct { | ||
41 | int errorcode; | ||
42 | check_mrtg_config config; | ||
43 | } check_mrtg_config_wrapper; | ||
44 | static check_mrtg_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
45 | static check_mrtg_config_wrapper validate_arguments(check_mrtg_config_wrapper /*config_wrapper*/); | ||
38 | 46 | ||
39 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
40 | static int validate_arguments(void); | ||
41 | static void print_help(void); | 47 | static void print_help(void); |
42 | void print_usage(void); | 48 | void print_usage(void); |
43 | 49 | ||
44 | static char *log_file = NULL; | ||
45 | static int expire_minutes = 0; | ||
46 | static bool use_average = true; | ||
47 | static int variable_number = -1; | ||
48 | static unsigned long value_warning_threshold = 0L; | ||
49 | static unsigned long value_critical_threshold = 0L; | ||
50 | static char *label; | ||
51 | static char *units; | ||
52 | |||
53 | int main(int argc, char **argv) { | 50 | int main(int argc, char **argv) { |
54 | setlocale(LC_ALL, ""); | 51 | setlocale(LC_ALL, ""); |
55 | bindtextdomain(PACKAGE, LOCALEDIR); | 52 | bindtextdomain(PACKAGE, LOCALEDIR); |
@@ -58,24 +55,26 @@ int main(int argc, char **argv) { | |||
58 | /* Parse extra opts if any */ | 55 | /* Parse extra opts if any */ |
59 | argv = np_extra_opts(&argc, argv, progname); | 56 | argv = np_extra_opts(&argc, argv, progname); |
60 | 57 | ||
61 | if (process_arguments(argc, argv) == ERROR) { | 58 | check_mrtg_config_wrapper tmp_config = process_arguments(argc, argv); |
59 | if (tmp_config.errorcode == ERROR) { | ||
62 | usage4(_("Could not parse arguments\n")); | 60 | usage4(_("Could not parse arguments\n")); |
63 | } | 61 | } |
64 | 62 | ||
63 | const check_mrtg_config config = tmp_config.config; | ||
64 | |||
65 | /* open the MRTG log file for reading */ | 65 | /* open the MRTG log file for reading */ |
66 | FILE *mtrg_log_file = fopen(log_file, "r"); | 66 | FILE *mtrg_log_file = fopen(config.log_file, "r"); |
67 | if (mtrg_log_file == NULL) { | 67 | if (mtrg_log_file == NULL) { |
68 | printf(_("Unable to open MRTG log file\n")); | 68 | printf(_("Unable to open MRTG log file\n")); |
69 | return STATE_UNKNOWN; | 69 | return STATE_UNKNOWN; |
70 | } | 70 | } |
71 | 71 | ||
72 | time_t timestamp = 0L; | 72 | time_t timestamp = 0; |
73 | unsigned long average_value_rate = 0L; | 73 | unsigned long average_value_rate = 0; |
74 | unsigned long maximum_value_rate = 0L; | 74 | unsigned long maximum_value_rate = 0; |
75 | char input_buffer[MAX_INPUT_BUFFER]; | 75 | char input_buffer[MAX_INPUT_BUFFER]; |
76 | int line = 0; | 76 | int line = 0; |
77 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { | 77 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { |
78 | |||
79 | line++; | 78 | line++; |
80 | 79 | ||
81 | /* skip the first line of the log file */ | 80 | /* skip the first line of the log file */ |
@@ -94,25 +93,25 @@ int main(int argc, char **argv) { | |||
94 | 93 | ||
95 | /* grab the average value 1 rate */ | 94 | /* grab the average value 1 rate */ |
96 | temp_buffer = strtok(NULL, " "); | 95 | temp_buffer = strtok(NULL, " "); |
97 | if (variable_number == 1) { | 96 | if (config.variable_number == 1) { |
98 | average_value_rate = strtoul(temp_buffer, NULL, 10); | 97 | average_value_rate = strtoul(temp_buffer, NULL, 10); |
99 | } | 98 | } |
100 | 99 | ||
101 | /* grab the average value 2 rate */ | 100 | /* grab the average value 2 rate */ |
102 | temp_buffer = strtok(NULL, " "); | 101 | temp_buffer = strtok(NULL, " "); |
103 | if (variable_number == 2) { | 102 | if (config.variable_number == 2) { |
104 | average_value_rate = strtoul(temp_buffer, NULL, 10); | 103 | average_value_rate = strtoul(temp_buffer, NULL, 10); |
105 | } | 104 | } |
106 | 105 | ||
107 | /* grab the maximum value 1 rate */ | 106 | /* grab the maximum value 1 rate */ |
108 | temp_buffer = strtok(NULL, " "); | 107 | temp_buffer = strtok(NULL, " "); |
109 | if (variable_number == 1) { | 108 | if (config.variable_number == 1) { |
110 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); | 109 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); |
111 | } | 110 | } |
112 | 111 | ||
113 | /* grab the maximum value 2 rate */ | 112 | /* grab the maximum value 2 rate */ |
114 | temp_buffer = strtok(NULL, " "); | 113 | temp_buffer = strtok(NULL, " "); |
115 | if (variable_number == 2) { | 114 | if (config.variable_number == 2) { |
116 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); | 115 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); |
117 | } | 116 | } |
118 | } | 117 | } |
@@ -129,43 +128,49 @@ int main(int argc, char **argv) { | |||
129 | /* make sure the MRTG data isn't too old */ | 128 | /* make sure the MRTG data isn't too old */ |
130 | time_t current_time; | 129 | time_t current_time; |
131 | time(¤t_time); | 130 | time(¤t_time); |
132 | if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { | 131 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { |
133 | printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 132 | printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); |
134 | return STATE_WARNING; | 133 | return STATE_WARNING; |
135 | } | 134 | } |
136 | 135 | ||
137 | unsigned long rate = 0L; | 136 | unsigned long rate = 0L; |
138 | /* else check the incoming/outgoing rates */ | 137 | /* else check the incoming/outgoing rates */ |
139 | if (use_average) { | 138 | if (config.use_average) { |
140 | rate = average_value_rate; | 139 | rate = average_value_rate; |
141 | } else { | 140 | } else { |
142 | rate = maximum_value_rate; | 141 | rate = maximum_value_rate; |
143 | } | 142 | } |
144 | 143 | ||
145 | int result = STATE_OK; | 144 | int result = STATE_OK; |
146 | if (rate > value_critical_threshold) { | 145 | if (config.value_critical_threshold_set && rate > config.value_critical_threshold) { |
147 | result = STATE_CRITICAL; | 146 | result = STATE_CRITICAL; |
148 | } else if (rate > value_warning_threshold) { | 147 | } else if (config.value_warning_threshold_set && rate > config.value_warning_threshold) { |
149 | result = STATE_WARNING; | 148 | result = STATE_WARNING; |
150 | } | 149 | } |
151 | 150 | ||
152 | printf("%s. %s = %lu %s|%s\n", (use_average) ? _("Avg") : _("Max"), label, rate, units, | 151 | printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate, config.units, |
153 | perfdata(label, (long)rate, units, (int)value_warning_threshold, (long)value_warning_threshold, (int)value_critical_threshold, | 152 | perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set, (long)config.value_warning_threshold, |
154 | (long)value_critical_threshold, 0, 0, 0, 0)); | 153 | config.value_critical_threshold_set, (long)config.value_critical_threshold, 0, 0, 0, 0)); |
155 | 154 | ||
156 | return result; | 155 | return result; |
157 | } | 156 | } |
158 | 157 | ||
159 | /* process command-line arguments */ | 158 | /* process command-line arguments */ |
160 | int process_arguments(int argc, char **argv) { | 159 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { |
161 | static struct option longopts[] = { | 160 | static struct option longopts[] = { |
162 | {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, | 161 | {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, |
163 | {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, | 162 | {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, |
164 | {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, | 163 | {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, |
165 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 164 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; |
166 | 165 | ||
166 | check_mrtg_config_wrapper result = { | ||
167 | .errorcode = OK, | ||
168 | .config = check_mrtg_config_init(), | ||
169 | }; | ||
170 | |||
167 | if (argc < 2) { | 171 | if (argc < 2) { |
168 | return ERROR; | 172 | result.errorcode = ERROR; |
173 | return result; | ||
169 | } | 174 | } |
170 | 175 | ||
171 | for (int i = 1; i < argc; i++) { | 176 | for (int i = 1; i < argc; i++) { |
@@ -189,35 +194,33 @@ int process_arguments(int argc, char **argv) { | |||
189 | 194 | ||
190 | switch (option_char) { | 195 | switch (option_char) { |
191 | case 'F': /* input file */ | 196 | case 'F': /* input file */ |
192 | log_file = optarg; | 197 | result.config.log_file = optarg; |
193 | break; | 198 | break; |
194 | case 'e': /* ups name */ | 199 | case 'e': /* ups name */ |
195 | expire_minutes = atoi(optarg); | 200 | result.config.expire_minutes = atoi(optarg); |
196 | break; | 201 | break; |
197 | case 'a': /* port */ | 202 | case 'a': /* port */ |
198 | if (!strcmp(optarg, "MAX")) { | 203 | result.config.use_average = (bool)(strcmp(optarg, "MAX")); |
199 | use_average = false; | ||
200 | } else { | ||
201 | use_average = true; | ||
202 | } | ||
203 | break; | 204 | break; |
204 | case 'v': | 205 | case 'v': |
205 | variable_number = atoi(optarg); | 206 | result.config.variable_number = atoi(optarg); |
206 | if (variable_number < 1 || variable_number > 2) { | 207 | if (result.config.variable_number < 1 || result.config.variable_number > 2) { |
207 | usage4(_("Invalid variable number")); | 208 | usage4(_("Invalid variable number")); |
208 | } | 209 | } |
209 | break; | 210 | break; |
210 | case 'w': /* critical time threshold */ | 211 | case 'w': /* critical time threshold */ |
211 | value_warning_threshold = strtoul(optarg, NULL, 10); | 212 | result.config.value_warning_threshold_set = true; |
213 | result.config.value_warning_threshold = strtoul(optarg, NULL, 10); | ||
212 | break; | 214 | break; |
213 | case 'c': /* warning time threshold */ | 215 | case 'c': /* warning time threshold */ |
214 | value_critical_threshold = strtoul(optarg, NULL, 10); | 216 | result.config.value_critical_threshold_set = true; |
217 | result.config.value_critical_threshold = strtoul(optarg, NULL, 10); | ||
215 | break; | 218 | break; |
216 | case 'l': /* label */ | 219 | case 'l': /* label */ |
217 | label = optarg; | 220 | result.config.label = optarg; |
218 | break; | 221 | break; |
219 | case 'u': /* timeout */ | 222 | case 'u': /* timeout */ |
220 | units = optarg; | 223 | result.config.units = optarg; |
221 | break; | 224 | break; |
222 | case 'V': /* version */ | 225 | case 'V': /* version */ |
223 | print_revision(progname, NP_VERSION); | 226 | print_revision(progname, NP_VERSION); |
@@ -231,67 +234,69 @@ int process_arguments(int argc, char **argv) { | |||
231 | } | 234 | } |
232 | 235 | ||
233 | option_char = optind; | 236 | option_char = optind; |
234 | if (log_file == NULL && argc > option_char) { | 237 | if (result.config.log_file == NULL && argc > option_char) { |
235 | log_file = argv[option_char++]; | 238 | result.config.log_file = argv[option_char++]; |
236 | } | 239 | } |
237 | 240 | ||
238 | if (expire_minutes <= 0 && argc > option_char) { | 241 | if (result.config.expire_minutes <= 0 && argc > option_char) { |
239 | if (is_intpos(argv[option_char])) { | 242 | if (is_intpos(argv[option_char])) { |
240 | expire_minutes = atoi(argv[option_char++]); | 243 | result.config.expire_minutes = atoi(argv[option_char++]); |
241 | } else { | 244 | } else { |
242 | die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); | 245 | die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); |
243 | } | 246 | } |
244 | } | 247 | } |
245 | 248 | ||
246 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { | 249 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { |
247 | use_average = false; | 250 | result.config.use_average = false; |
248 | option_char++; | 251 | option_char++; |
249 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { | 252 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { |
250 | use_average = true; | 253 | result.config.use_average = true; |
251 | option_char++; | 254 | option_char++; |
252 | } | 255 | } |
253 | 256 | ||
254 | if (argc > option_char && variable_number == -1) { | 257 | if (argc > option_char && result.config.variable_number == -1) { |
255 | variable_number = atoi(argv[option_char++]); | 258 | result.config.variable_number = atoi(argv[option_char++]); |
256 | if (variable_number < 1 || variable_number > 2) { | 259 | if (result.config.variable_number < 1 || result.config.variable_number > 2) { |
257 | printf("%s :", argv[option_char]); | 260 | printf("%s :", argv[option_char]); |
258 | usage(_("Invalid variable number\n")); | 261 | usage(_("Invalid variable number\n")); |
259 | } | 262 | } |
260 | } | 263 | } |
261 | 264 | ||
262 | if (argc > option_char && value_warning_threshold == 0) { | 265 | if (argc > option_char && !result.config.value_warning_threshold_set) { |
263 | value_warning_threshold = strtoul(argv[option_char++], NULL, 10); | 266 | result.config.value_warning_threshold_set = true; |
267 | result.config.value_warning_threshold = strtoul(argv[option_char++], NULL, 10); | ||
264 | } | 268 | } |
265 | 269 | ||
266 | if (argc > option_char && value_critical_threshold == 0) { | 270 | if (argc > option_char && !result.config.value_critical_threshold_set) { |
267 | value_critical_threshold = strtoul(argv[option_char++], NULL, 10); | 271 | result.config.value_critical_threshold_set = true; |
272 | result.config.value_critical_threshold = strtoul(argv[option_char++], NULL, 10); | ||
268 | } | 273 | } |
269 | 274 | ||
270 | if (argc > option_char && strlen(label) == 0) { | 275 | if (argc > option_char && strlen(result.config.label) == 0) { |
271 | label = argv[option_char++]; | 276 | result.config.label = argv[option_char++]; |
272 | } | 277 | } |
273 | 278 | ||
274 | if (argc > option_char && strlen(units) == 0) { | 279 | if (argc > option_char && strlen(result.config.units) == 0) { |
275 | units = argv[option_char++]; | 280 | result.config.units = argv[option_char++]; |
276 | } | 281 | } |
277 | 282 | ||
278 | return validate_arguments(); | 283 | return validate_arguments(result); |
279 | } | 284 | } |
280 | 285 | ||
281 | int validate_arguments(void) { | 286 | check_mrtg_config_wrapper validate_arguments(check_mrtg_config_wrapper config_wrapper) { |
282 | if (variable_number == -1) { | 287 | if (config_wrapper.config.variable_number == -1) { |
283 | usage4(_("You must supply the variable number")); | 288 | usage4(_("You must supply the variable number")); |
284 | } | 289 | } |
285 | 290 | ||
286 | if (label == NULL) { | 291 | if (config_wrapper.config.label == NULL) { |
287 | label = strdup("value"); | 292 | config_wrapper.config.label = strdup("value"); |
288 | } | 293 | } |
289 | 294 | ||
290 | if (units == NULL) { | 295 | if (config_wrapper.config.units == NULL) { |
291 | units = strdup(""); | 296 | config_wrapper.config.units = strdup(""); |
292 | } | 297 | } |
293 | 298 | ||
294 | return OK; | 299 | return config_wrapper; |
295 | } | 300 | } |
296 | 301 | ||
297 | void print_help(void) { | 302 | void print_help(void) { |
diff --git a/plugins/check_mrtg.d/config.h b/plugins/check_mrtg.d/config.h new file mode 100644 index 00000000..96b849a2 --- /dev/null +++ b/plugins/check_mrtg.d/config.h | |||
@@ -0,0 +1,36 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include <stddef.h> | ||
5 | #include <stdlib.h> | ||
6 | |||
7 | typedef struct { | ||
8 | bool use_average; | ||
9 | int variable_number; | ||
10 | int expire_minutes; | ||
11 | char *label; | ||
12 | char *units; | ||
13 | char *log_file; | ||
14 | |||
15 | bool value_warning_threshold_set; | ||
16 | unsigned long value_warning_threshold; | ||
17 | bool value_critical_threshold_set; | ||
18 | unsigned long value_critical_threshold; | ||
19 | } check_mrtg_config; | ||
20 | |||
21 | check_mrtg_config check_mrtg_config_init() { | ||
22 | check_mrtg_config tmp = { | ||
23 | .use_average = true, | ||
24 | .variable_number = -1, | ||
25 | .expire_minutes = 0, | ||
26 | .label = NULL, | ||
27 | .units = NULL, | ||
28 | .log_file = NULL, | ||
29 | |||
30 | .value_warning_threshold_set = false, | ||
31 | .value_warning_threshold = 0, | ||
32 | .value_critical_threshold_set = false, | ||
33 | .value_critical_threshold = 0, | ||
34 | }; | ||
35 | return tmp; | ||
36 | } | ||