diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 12:54:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-11 12:54:03 +0100 |
commit | 5c42acc41e090d49d17cea6071f174764ba429a3 (patch) | |
tree | a6226ac9207e2153fa1b672bd33e5d5242906551 /plugins/check_mrtg.c | |
parent | 74c3f4811ccc10911cdeff5cd793a8f3a7dd74e5 (diff) | |
parent | 23da18f10ce32c899f266ae703f0b9c2b3991d70 (diff) | |
download | monitoring-plugins-5c42acc41e090d49d17cea6071f174764ba429a3.tar.gz |
Merge pull request #2088 from RincewindsHat/refactor/check_mrtg
Refactor/check mrtg
Diffstat (limited to 'plugins/check_mrtg.c')
-rw-r--r-- | plugins/check_mrtg.c | 177 |
1 files changed, 100 insertions, 77 deletions
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index 632e66fb..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,32 +55,37 @@ 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")); |
61 | } | ||
62 | |||
63 | const check_mrtg_config config = tmp_config.config; | ||
63 | 64 | ||
64 | /* open the MRTG log file for reading */ | 65 | /* open the MRTG log file for reading */ |
65 | FILE *mtrg_log_file = fopen(log_file, "r"); | 66 | FILE *mtrg_log_file = fopen(config.log_file, "r"); |
66 | if (mtrg_log_file == NULL) { | 67 | if (mtrg_log_file == NULL) { |
67 | printf(_("Unable to open MRTG log file\n")); | 68 | printf(_("Unable to open MRTG log file\n")); |
68 | return STATE_UNKNOWN; | 69 | return STATE_UNKNOWN; |
69 | } | 70 | } |
70 | 71 | ||
71 | time_t timestamp = 0L; | 72 | time_t timestamp = 0; |
72 | unsigned long average_value_rate = 0L; | 73 | unsigned long average_value_rate = 0; |
73 | unsigned long maximum_value_rate = 0L; | 74 | unsigned long maximum_value_rate = 0; |
74 | char input_buffer[MAX_INPUT_BUFFER]; | 75 | char input_buffer[MAX_INPUT_BUFFER]; |
75 | int line = 0; | 76 | int line = 0; |
76 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { | 77 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { |
77 | |||
78 | line++; | 78 | line++; |
79 | 79 | ||
80 | /* skip the first line of the log file */ | 80 | /* skip the first line of the log file */ |
81 | if (line == 1) | 81 | if (line == 1) { |
82 | continue; | 82 | continue; |
83 | } | ||
83 | 84 | ||
84 | /* break out of read loop if we've passed the number of entries we want to read */ | 85 | /* break out of read loop if we've passed the number of entries we want to read */ |
85 | if (line > 2) | 86 | if (line > 2) { |
86 | break; | 87 | break; |
88 | } | ||
87 | 89 | ||
88 | /* grab the timestamp */ | 90 | /* grab the timestamp */ |
89 | char *temp_buffer = strtok(input_buffer, " "); | 91 | char *temp_buffer = strtok(input_buffer, " "); |
@@ -91,23 +93,27 @@ int main(int argc, char **argv) { | |||
91 | 93 | ||
92 | /* grab the average value 1 rate */ | 94 | /* grab the average value 1 rate */ |
93 | temp_buffer = strtok(NULL, " "); | 95 | temp_buffer = strtok(NULL, " "); |
94 | if (variable_number == 1) | 96 | if (config.variable_number == 1) { |
95 | average_value_rate = strtoul(temp_buffer, NULL, 10); | 97 | average_value_rate = strtoul(temp_buffer, NULL, 10); |
98 | } | ||
96 | 99 | ||
97 | /* grab the average value 2 rate */ | 100 | /* grab the average value 2 rate */ |
98 | temp_buffer = strtok(NULL, " "); | 101 | temp_buffer = strtok(NULL, " "); |
99 | if (variable_number == 2) | 102 | if (config.variable_number == 2) { |
100 | average_value_rate = strtoul(temp_buffer, NULL, 10); | 103 | average_value_rate = strtoul(temp_buffer, NULL, 10); |
104 | } | ||
101 | 105 | ||
102 | /* grab the maximum value 1 rate */ | 106 | /* grab the maximum value 1 rate */ |
103 | temp_buffer = strtok(NULL, " "); | 107 | temp_buffer = strtok(NULL, " "); |
104 | if (variable_number == 1) | 108 | if (config.variable_number == 1) { |
105 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); | 109 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); |
110 | } | ||
106 | 111 | ||
107 | /* grab the maximum value 2 rate */ | 112 | /* grab the maximum value 2 rate */ |
108 | temp_buffer = strtok(NULL, " "); | 113 | temp_buffer = strtok(NULL, " "); |
109 | if (variable_number == 2) | 114 | if (config.variable_number == 2) { |
110 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); | 115 | maximum_value_rate = strtoul(temp_buffer, NULL, 10); |
116 | } | ||
111 | } | 117 | } |
112 | 118 | ||
113 | /* close the log file */ | 119 | /* close the log file */ |
@@ -122,49 +128,59 @@ int main(int argc, char **argv) { | |||
122 | /* make sure the MRTG data isn't too old */ | 128 | /* make sure the MRTG data isn't too old */ |
123 | time_t current_time; | 129 | time_t current_time; |
124 | time(¤t_time); | 130 | time(¤t_time); |
125 | if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { | 131 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { |
126 | 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)); |
127 | return STATE_WARNING; | 133 | return STATE_WARNING; |
128 | } | 134 | } |
129 | 135 | ||
130 | unsigned long rate = 0L; | 136 | unsigned long rate = 0L; |
131 | /* else check the incoming/outgoing rates */ | 137 | /* else check the incoming/outgoing rates */ |
132 | if (use_average) | 138 | if (config.use_average) { |
133 | rate = average_value_rate; | 139 | rate = average_value_rate; |
134 | else | 140 | } else { |
135 | rate = maximum_value_rate; | 141 | rate = maximum_value_rate; |
142 | } | ||
136 | 143 | ||
137 | int result = STATE_OK; | 144 | int result = STATE_OK; |
138 | if (rate > value_critical_threshold) | 145 | if (config.value_critical_threshold_set && rate > config.value_critical_threshold) { |
139 | result = STATE_CRITICAL; | 146 | result = STATE_CRITICAL; |
140 | else if (rate > value_warning_threshold) | 147 | } else if (config.value_warning_threshold_set && rate > config.value_warning_threshold) { |
141 | result = STATE_WARNING; | 148 | result = STATE_WARNING; |
149 | } | ||
142 | 150 | ||
143 | 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, |
144 | 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, |
145 | (long)value_critical_threshold, 0, 0, 0, 0)); | 153 | config.value_critical_threshold_set, (long)config.value_critical_threshold, 0, 0, 0, 0)); |
146 | 154 | ||
147 | return result; | 155 | return result; |
148 | } | 156 | } |
149 | 157 | ||
150 | /* process command-line arguments */ | 158 | /* process command-line arguments */ |
151 | int process_arguments(int argc, char **argv) { | 159 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { |
152 | static struct option longopts[] = { | 160 | static struct option longopts[] = { |
153 | {"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'}, |
154 | {"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'}, |
155 | {"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'}, |
156 | {"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}}; |
157 | 165 | ||
158 | if (argc < 2) | 166 | check_mrtg_config_wrapper result = { |
159 | return ERROR; | 167 | .errorcode = OK, |
168 | .config = check_mrtg_config_init(), | ||
169 | }; | ||
170 | |||
171 | if (argc < 2) { | ||
172 | result.errorcode = ERROR; | ||
173 | return result; | ||
174 | } | ||
160 | 175 | ||
161 | for (int i = 1; i < argc; i++) { | 176 | for (int i = 1; i < argc; i++) { |
162 | if (strcmp("-to", argv[i]) == 0) | 177 | if (strcmp("-to", argv[i]) == 0) { |
163 | strcpy(argv[i], "-t"); | 178 | strcpy(argv[i], "-t"); |
164 | else if (strcmp("-wt", argv[i]) == 0) | 179 | } else if (strcmp("-wt", argv[i]) == 0) { |
165 | strcpy(argv[i], "-w"); | 180 | strcpy(argv[i], "-w"); |
166 | else if (strcmp("-ct", argv[i]) == 0) | 181 | } else if (strcmp("-ct", argv[i]) == 0) { |
167 | strcpy(argv[i], "-c"); | 182 | strcpy(argv[i], "-c"); |
183 | } | ||
168 | } | 184 | } |
169 | 185 | ||
170 | int option_char; | 186 | int option_char; |
@@ -172,38 +188,39 @@ int process_arguments(int argc, char **argv) { | |||
172 | while (1) { | 188 | while (1) { |
173 | option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); | 189 | option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); |
174 | 190 | ||
175 | if (option_char == -1 || option_char == EOF) | 191 | if (option_char == -1 || option_char == EOF) { |
176 | break; | 192 | break; |
193 | } | ||
177 | 194 | ||
178 | switch (option_char) { | 195 | switch (option_char) { |
179 | case 'F': /* input file */ | 196 | case 'F': /* input file */ |
180 | log_file = optarg; | 197 | result.config.log_file = optarg; |
181 | break; | 198 | break; |
182 | case 'e': /* ups name */ | 199 | case 'e': /* ups name */ |
183 | expire_minutes = atoi(optarg); | 200 | result.config.expire_minutes = atoi(optarg); |
184 | break; | 201 | break; |
185 | case 'a': /* port */ | 202 | case 'a': /* port */ |
186 | if (!strcmp(optarg, "MAX")) | 203 | result.config.use_average = (bool)(strcmp(optarg, "MAX")); |
187 | use_average = false; | ||
188 | else | ||
189 | use_average = true; | ||
190 | break; | 204 | break; |
191 | case 'v': | 205 | case 'v': |
192 | variable_number = atoi(optarg); | 206 | result.config.variable_number = atoi(optarg); |
193 | if (variable_number < 1 || variable_number > 2) | 207 | if (result.config.variable_number < 1 || result.config.variable_number > 2) { |
194 | usage4(_("Invalid variable number")); | 208 | usage4(_("Invalid variable number")); |
209 | } | ||
195 | break; | 210 | break; |
196 | case 'w': /* critical time threshold */ | 211 | case 'w': /* critical time threshold */ |
197 | 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); | ||
198 | break; | 214 | break; |
199 | case 'c': /* warning time threshold */ | 215 | case 'c': /* warning time threshold */ |
200 | 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); | ||
201 | break; | 218 | break; |
202 | case 'l': /* label */ | 219 | case 'l': /* label */ |
203 | label = optarg; | 220 | result.config.label = optarg; |
204 | break; | 221 | break; |
205 | case 'u': /* timeout */ | 222 | case 'u': /* timeout */ |
206 | units = optarg; | 223 | result.config.units = optarg; |
207 | break; | 224 | break; |
208 | case 'V': /* version */ | 225 | case 'V': /* version */ |
209 | print_revision(progname, NP_VERSION); | 226 | print_revision(progname, NP_VERSION); |
@@ -217,63 +234,69 @@ int process_arguments(int argc, char **argv) { | |||
217 | } | 234 | } |
218 | 235 | ||
219 | option_char = optind; | 236 | option_char = optind; |
220 | if (log_file == NULL && argc > option_char) { | 237 | if (result.config.log_file == NULL && argc > option_char) { |
221 | log_file = argv[option_char++]; | 238 | result.config.log_file = argv[option_char++]; |
222 | } | 239 | } |
223 | 240 | ||
224 | if (expire_minutes <= 0 && argc > option_char) { | 241 | if (result.config.expire_minutes <= 0 && argc > option_char) { |
225 | if (is_intpos(argv[option_char])) | 242 | if (is_intpos(argv[option_char])) { |
226 | expire_minutes = atoi(argv[option_char++]); | 243 | result.config.expire_minutes = atoi(argv[option_char++]); |
227 | else | 244 | } else { |
228 | 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); |
246 | } | ||
229 | } | 247 | } |
230 | 248 | ||
231 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { | 249 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { |
232 | use_average = false; | 250 | result.config.use_average = false; |
233 | option_char++; | 251 | option_char++; |
234 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { | 252 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { |
235 | use_average = true; | 253 | result.config.use_average = true; |
236 | option_char++; | 254 | option_char++; |
237 | } | 255 | } |
238 | 256 | ||
239 | if (argc > option_char && variable_number == -1) { | 257 | if (argc > option_char && result.config.variable_number == -1) { |
240 | variable_number = atoi(argv[option_char++]); | 258 | result.config.variable_number = atoi(argv[option_char++]); |
241 | if (variable_number < 1 || variable_number > 2) { | 259 | if (result.config.variable_number < 1 || result.config.variable_number > 2) { |
242 | printf("%s :", argv[option_char]); | 260 | printf("%s :", argv[option_char]); |
243 | usage(_("Invalid variable number\n")); | 261 | usage(_("Invalid variable number\n")); |
244 | } | 262 | } |
245 | } | 263 | } |
246 | 264 | ||
247 | if (argc > option_char && value_warning_threshold == 0) { | 265 | if (argc > option_char && !result.config.value_warning_threshold_set) { |
248 | 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); | ||
249 | } | 268 | } |
250 | 269 | ||
251 | if (argc > option_char && value_critical_threshold == 0) { | 270 | if (argc > option_char && !result.config.value_critical_threshold_set) { |
252 | 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); | ||
253 | } | 273 | } |
254 | 274 | ||
255 | if (argc > option_char && strlen(label) == 0) { | 275 | if (argc > option_char && strlen(result.config.label) == 0) { |
256 | label = argv[option_char++]; | 276 | result.config.label = argv[option_char++]; |
257 | } | 277 | } |
258 | 278 | ||
259 | if (argc > option_char && strlen(units) == 0) { | 279 | if (argc > option_char && strlen(result.config.units) == 0) { |
260 | units = argv[option_char++]; | 280 | result.config.units = argv[option_char++]; |
261 | } | 281 | } |
262 | 282 | ||
263 | return validate_arguments(); | 283 | return validate_arguments(result); |
264 | } | 284 | } |
265 | 285 | ||
266 | int validate_arguments(void) { | 286 | check_mrtg_config_wrapper validate_arguments(check_mrtg_config_wrapper config_wrapper) { |
267 | if (variable_number == -1) | 287 | if (config_wrapper.config.variable_number == -1) { |
268 | usage4(_("You must supply the variable number")); | 288 | usage4(_("You must supply the variable number")); |
289 | } | ||
269 | 290 | ||
270 | if (label == NULL) | 291 | if (config_wrapper.config.label == NULL) { |
271 | label = strdup("value"); | 292 | config_wrapper.config.label = strdup("value"); |
293 | } | ||
272 | 294 | ||
273 | if (units == NULL) | 295 | if (config_wrapper.config.units == NULL) { |
274 | units = strdup(""); | 296 | config_wrapper.config.units = strdup(""); |
297 | } | ||
275 | 298 | ||
276 | return OK; | 299 | return config_wrapper; |
277 | } | 300 | } |
278 | 301 | ||
279 | void print_help(void) { | 302 | void print_help(void) { |