diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-26 14:45:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-26 14:45:51 +0100 |
| commit | 3daf643d3394e2a77a53cc58dcd57c4bf814ba23 (patch) | |
| tree | cbe0174da4842f8cc5d9852c53e498362ef273ec | |
| parent | 0378484a57f219fc0156e208bff5152ee8624837 (diff) | |
| parent | 326d3996248346353894f50bc85133c4e045be92 (diff) | |
| download | monitoring-plugins-3daf643d3394e2a77a53cc58dcd57c4bf814ba23.tar.gz | |
Merge pull request #2182 from RincewindsHat/modern_output/check_mrtg
check_mrtg: implement modern output
| -rw-r--r-- | plugins/check_mrtg.c | 138 | ||||
| -rw-r--r-- | plugins/check_mrtg.d/config.h | 17 |
2 files changed, 110 insertions, 45 deletions
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index 4a17049a..cdc2a035 100644 --- a/plugins/check_mrtg.c +++ b/plugins/check_mrtg.c | |||
| @@ -29,14 +29,18 @@ | |||
| 29 | * | 29 | * |
| 30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
| 31 | 31 | ||
| 32 | const char *progname = "check_mrtg"; | ||
| 33 | const char *copyright = "1999-2024"; | ||
| 34 | const char *email = "devel@monitoring-plugins.org"; | ||
| 35 | |||
| 36 | #include "common.h" | 32 | #include "common.h" |
| 33 | #include "output.h" | ||
| 34 | #include "perfdata.h" | ||
| 35 | #include "states.h" | ||
| 36 | #include "thresholds.h" | ||
| 37 | #include "utils.h" | 37 | #include "utils.h" |
| 38 | #include "check_mrtg.d/config.h" | 38 | #include "check_mrtg.d/config.h" |
| 39 | 39 | ||
| 40 | const char *progname = "check_mrtg"; | ||
| 41 | const char *copyright = "1999-2024"; | ||
| 42 | const char *email = "devel@monitoring-plugins.org"; | ||
| 43 | |||
| 40 | typedef struct { | 44 | typedef struct { |
| 41 | int errorcode; | 45 | int errorcode; |
| 42 | check_mrtg_config config; | 46 | check_mrtg_config config; |
| @@ -62,11 +66,24 @@ int main(int argc, char **argv) { | |||
| 62 | 66 | ||
| 63 | const check_mrtg_config config = tmp_config.config; | 67 | const check_mrtg_config config = tmp_config.config; |
| 64 | 68 | ||
| 69 | if (config.output_format_is_set) { | ||
| 70 | mp_set_format(config.output_format); | ||
| 71 | } | ||
| 72 | |||
| 73 | mp_check overall = mp_check_init(); | ||
| 74 | |||
| 65 | /* open the MRTG log file for reading */ | 75 | /* open the MRTG log file for reading */ |
| 76 | mp_subcheck sc_open_mrtg_log_file = mp_subcheck_init(); | ||
| 66 | FILE *mtrg_log_file = fopen(config.log_file, "r"); | 77 | FILE *mtrg_log_file = fopen(config.log_file, "r"); |
| 67 | if (mtrg_log_file == NULL) { | 78 | if (mtrg_log_file == NULL) { |
| 68 | printf(_("Unable to open MRTG log file\n")); | 79 | xasprintf(&sc_open_mrtg_log_file.output, "unable to open MRTG log file"); |
| 69 | return STATE_UNKNOWN; | 80 | sc_open_mrtg_log_file = mp_set_subcheck_state(sc_open_mrtg_log_file, STATE_UNKNOWN); |
| 81 | mp_add_subcheck_to_check(&overall, sc_open_mrtg_log_file); | ||
| 82 | mp_exit(overall); | ||
| 83 | } else { | ||
| 84 | xasprintf(&sc_open_mrtg_log_file.output, "opened MRTG log file"); | ||
| 85 | sc_open_mrtg_log_file = mp_set_subcheck_state(sc_open_mrtg_log_file, STATE_OK); | ||
| 86 | mp_add_subcheck_to_check(&overall, sc_open_mrtg_log_file); | ||
| 70 | } | 87 | } |
| 71 | 88 | ||
| 72 | time_t timestamp = 0; | 89 | time_t timestamp = 0; |
| @@ -120,18 +137,32 @@ int main(int argc, char **argv) { | |||
| 120 | fclose(mtrg_log_file); | 137 | fclose(mtrg_log_file); |
| 121 | 138 | ||
| 122 | /* if we couldn't read enough data, return an unknown error */ | 139 | /* if we couldn't read enough data, return an unknown error */ |
| 140 | mp_subcheck sc_process_mrtg_log_file = mp_subcheck_init(); | ||
| 123 | if (line <= 2) { | 141 | if (line <= 2) { |
| 124 | printf(_("Unable to process MRTG log file\n")); | 142 | xasprintf(&sc_process_mrtg_log_file.output, "unable to process MRTG log file"); |
| 125 | return STATE_UNKNOWN; | 143 | sc_process_mrtg_log_file = mp_set_subcheck_state(sc_process_mrtg_log_file, STATE_UNKNOWN); |
| 144 | mp_exit(overall); | ||
| 145 | } else { | ||
| 146 | xasprintf(&sc_process_mrtg_log_file.output, "processed MRTG log file"); | ||
| 147 | sc_process_mrtg_log_file = mp_set_subcheck_state(sc_process_mrtg_log_file, STATE_OK); | ||
| 148 | mp_add_subcheck_to_check(&overall, sc_process_mrtg_log_file); | ||
| 126 | } | 149 | } |
| 127 | 150 | ||
| 128 | /* make sure the MRTG data isn't too old */ | 151 | /* make sure the MRTG data isn't too old */ |
| 129 | time_t current_time; | 152 | time_t current_time; |
| 130 | time(¤t_time); | 153 | time(¤t_time); |
| 154 | mp_subcheck sc_data_expired = mp_subcheck_init(); | ||
| 131 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { | 155 | if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { |
| 132 | printf(_("MRTG data has expired (%d minutes old)\n"), | 156 | xasprintf(&sc_data_expired.output, "MRTG data has expired (%d minutes old)", |
| 133 | (int)((current_time - timestamp) / 60)); | 157 | (int)((current_time - timestamp) / 60)); |
| 134 | return STATE_WARNING; | 158 | sc_data_expired = mp_set_subcheck_state(sc_data_expired, STATE_WARNING); |
| 159 | mp_add_subcheck_to_check(&overall, sc_data_expired); | ||
| 160 | mp_exit(overall); | ||
| 161 | } else { | ||
| 162 | xasprintf(&sc_data_expired.output, "MRTG data should be valid (%d minutes old)", | ||
| 163 | (int)((current_time - timestamp) / 60)); | ||
| 164 | sc_data_expired = mp_set_subcheck_state(sc_data_expired, STATE_OK); | ||
| 165 | mp_add_subcheck_to_check(&overall, sc_data_expired); | ||
| 135 | } | 166 | } |
| 136 | 167 | ||
| 137 | unsigned long rate = 0L; | 168 | unsigned long rate = 0L; |
| @@ -142,24 +173,27 @@ int main(int argc, char **argv) { | |||
| 142 | rate = maximum_value_rate; | 173 | rate = maximum_value_rate; |
| 143 | } | 174 | } |
| 144 | 175 | ||
| 145 | int result = STATE_OK; | 176 | mp_subcheck sc_values = mp_subcheck_init(); |
| 146 | if (config.value_critical_threshold_set && rate > config.value_critical_threshold) { | 177 | mp_perfdata pd_value = perfdata_init(); |
| 147 | result = STATE_CRITICAL; | 178 | pd_value = mp_set_pd_value(pd_value, rate); |
| 148 | } else if (config.value_warning_threshold_set && rate > config.value_warning_threshold) { | 179 | pd_value.label = config.label; |
| 149 | result = STATE_WARNING; | 180 | pd_value = mp_pd_set_thresholds(pd_value, config.values_threshold); |
| 150 | } | ||
| 151 | 181 | ||
| 152 | printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate, | 182 | sc_values = mp_set_subcheck_state(sc_values, mp_get_pd_status(pd_value)); |
| 153 | config.units, | 183 | xasprintf(&sc_values.output, "%s. %s = %lu %s", (config.use_average) ? _("Avg") : _("Max"), |
| 154 | perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set, | 184 | config.label, rate, config.units); |
| 155 | (long)config.value_warning_threshold, config.value_critical_threshold_set, | ||
| 156 | (long)config.value_critical_threshold, 0, 0, 0, 0)); | ||
| 157 | 185 | ||
| 158 | return result; | 186 | mp_add_subcheck_to_check(&overall, sc_values); |
| 187 | |||
| 188 | mp_exit(overall); | ||
| 159 | } | 189 | } |
| 160 | 190 | ||
| 161 | /* process command-line arguments */ | 191 | /* process command-line arguments */ |
| 162 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | 192 | check_mrtg_config_wrapper process_arguments(int argc, char **argv) { |
| 193 | enum { | ||
| 194 | output_format_index, | ||
| 195 | }; | ||
| 196 | |||
| 163 | static struct option longopts[] = {{"logfile", required_argument, 0, 'F'}, | 197 | static struct option longopts[] = {{"logfile", required_argument, 0, 'F'}, |
| 164 | {"expires", required_argument, 0, 'e'}, | 198 | {"expires", required_argument, 0, 'e'}, |
| 165 | {"aggregation", required_argument, 0, 'a'}, | 199 | {"aggregation", required_argument, 0, 'a'}, |
| @@ -171,6 +205,7 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | |||
| 171 | {"variable", required_argument, 0, 'v'}, | 205 | {"variable", required_argument, 0, 'v'}, |
| 172 | {"version", no_argument, 0, 'V'}, | 206 | {"version", no_argument, 0, 'V'}, |
| 173 | {"help", no_argument, 0, 'h'}, | 207 | {"help", no_argument, 0, 'h'}, |
| 208 | {"output-format", required_argument, 0, output_format_index}, | ||
| 174 | {0, 0, 0, 0}}; | 209 | {0, 0, 0, 0}}; |
| 175 | 210 | ||
| 176 | check_mrtg_config_wrapper result = { | 211 | check_mrtg_config_wrapper result = { |
| @@ -218,14 +253,22 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | |||
| 218 | usage4(_("Invalid variable number")); | 253 | usage4(_("Invalid variable number")); |
| 219 | } | 254 | } |
| 220 | break; | 255 | break; |
| 221 | case 'w': /* critical time threshold */ | 256 | case 'w': /* critical time threshold */ { |
| 222 | result.config.value_warning_threshold_set = true; | 257 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 223 | result.config.value_warning_threshold = strtoul(optarg, NULL, 10); | 258 | if (tmp.error != MP_PARSING_SUCCES) { |
| 224 | break; | 259 | die(STATE_UNKNOWN, "failed to parse warning threshold"); |
| 225 | case 'c': /* warning time threshold */ | 260 | } |
| 226 | result.config.value_critical_threshold_set = true; | 261 | result.config.values_threshold = |
| 227 | result.config.value_critical_threshold = strtoul(optarg, NULL, 10); | 262 | mp_thresholds_set_warn(result.config.values_threshold, tmp.range); |
| 228 | break; | 263 | } break; |
| 264 | case 'c': /* warning time threshold */ { | ||
| 265 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 266 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 267 | die(STATE_UNKNOWN, "failed to parse critical threshold"); | ||
| 268 | } | ||
| 269 | result.config.values_threshold = | ||
| 270 | mp_thresholds_set_crit(result.config.values_threshold, tmp.range); | ||
| 271 | } break; | ||
| 229 | case 'l': /* label */ | 272 | case 'l': /* label */ |
| 230 | result.config.label = optarg; | 273 | result.config.label = optarg; |
| 231 | break; | 274 | break; |
| @@ -240,6 +283,17 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | |||
| 240 | exit(STATE_UNKNOWN); | 283 | exit(STATE_UNKNOWN); |
| 241 | case '?': /* help */ | 284 | case '?': /* help */ |
| 242 | usage5(); | 285 | usage5(); |
| 286 | case output_format_index: { | ||
| 287 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 288 | if (!parser.parsing_success) { | ||
| 289 | printf("Invalid output format: %s\n", optarg); | ||
| 290 | exit(STATE_UNKNOWN); | ||
| 291 | } | ||
| 292 | |||
| 293 | result.config.output_format_is_set = true; | ||
| 294 | result.config.output_format = parser.output_format; | ||
| 295 | break; | ||
| 296 | } | ||
| 243 | } | 297 | } |
| 244 | } | 298 | } |
| 245 | 299 | ||
| @@ -274,14 +328,22 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { | |||
| 274 | } | 328 | } |
| 275 | } | 329 | } |
| 276 | 330 | ||
| 277 | if (argc > option_char && !result.config.value_warning_threshold_set) { | 331 | if (argc > option_char && !result.config.values_threshold.warning_is_set) { |
| 278 | result.config.value_warning_threshold_set = true; | 332 | mp_range_parsed tmp = mp_parse_range_string(argv[option_char++]); |
| 279 | result.config.value_warning_threshold = strtoul(argv[option_char++], NULL, 10); | 333 | if (tmp.error != MP_PARSING_SUCCES) { |
| 334 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
| 335 | } | ||
| 336 | result.config.values_threshold = | ||
| 337 | mp_thresholds_set_warn(result.config.values_threshold, tmp.range); | ||
| 280 | } | 338 | } |
| 281 | 339 | ||
| 282 | if (argc > option_char && !result.config.value_critical_threshold_set) { | 340 | if (argc > option_char && !result.config.values_threshold.critical_is_set) { |
| 283 | result.config.value_critical_threshold_set = true; | 341 | mp_range_parsed tmp = mp_parse_range_string(argv[option_char++]); |
| 284 | result.config.value_critical_threshold = strtoul(argv[option_char++], NULL, 10); | 342 | if (tmp.error != MP_PARSING_SUCCES) { |
| 343 | die(STATE_UNKNOWN, "failed to parse critical threshold"); | ||
| 344 | } | ||
| 345 | result.config.values_threshold = | ||
| 346 | mp_thresholds_set_crit(result.config.values_threshold, tmp.range); | ||
| 285 | } | 347 | } |
| 286 | 348 | ||
| 287 | if (argc > option_char && strlen(result.config.label) == 0) { | 349 | if (argc > option_char && strlen(result.config.label) == 0) { |
| @@ -345,6 +407,8 @@ void print_help(void) { | |||
| 345 | printf(" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,")); | 407 | printf(" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,")); |
| 346 | printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); | 408 | printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); |
| 347 | 409 | ||
| 410 | printf(UT_OUTPUT_FORMAT); | ||
| 411 | |||
| 348 | printf("\n"); | 412 | printf("\n"); |
| 349 | printf(" %s\n", | 413 | printf(" %s\n", |
| 350 | _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If")); | 414 | _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If")); |
diff --git a/plugins/check_mrtg.d/config.h b/plugins/check_mrtg.d/config.h index 96b849a2..4a5b5595 100644 --- a/plugins/check_mrtg.d/config.h +++ b/plugins/check_mrtg.d/config.h | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "../../config.h" | 3 | #include "../../config.h" |
| 4 | #include "output.h" | ||
| 5 | #include "thresholds.h" | ||
| 4 | #include <stddef.h> | 6 | #include <stddef.h> |
| 5 | #include <stdlib.h> | 7 | #include <stdlib.h> |
| 6 | 8 | ||
| @@ -12,10 +14,10 @@ typedef struct { | |||
| 12 | char *units; | 14 | char *units; |
| 13 | char *log_file; | 15 | char *log_file; |
| 14 | 16 | ||
| 15 | bool value_warning_threshold_set; | 17 | mp_thresholds values_threshold; |
| 16 | unsigned long value_warning_threshold; | 18 | |
| 17 | bool value_critical_threshold_set; | 19 | bool output_format_is_set; |
| 18 | unsigned long value_critical_threshold; | 20 | mp_output_format output_format; |
| 19 | } check_mrtg_config; | 21 | } check_mrtg_config; |
| 20 | 22 | ||
| 21 | check_mrtg_config check_mrtg_config_init() { | 23 | check_mrtg_config check_mrtg_config_init() { |
| @@ -27,10 +29,9 @@ check_mrtg_config check_mrtg_config_init() { | |||
| 27 | .units = NULL, | 29 | .units = NULL, |
| 28 | .log_file = NULL, | 30 | .log_file = NULL, |
| 29 | 31 | ||
| 30 | .value_warning_threshold_set = false, | 32 | .values_threshold = mp_thresholds_init(), |
| 31 | .value_warning_threshold = 0, | 33 | |
| 32 | .value_critical_threshold_set = false, | 34 | .output_format_is_set = false, |
| 33 | .value_critical_threshold = 0, | ||
| 34 | }; | 35 | }; |
| 35 | return tmp; | 36 | return tmp; |
| 36 | } | 37 | } |
