diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-27 23:42:56 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-27 23:42:56 +0100 |
| commit | d91d02619d4c1330b871a5418e436b2f81db533a (patch) | |
| tree | 68808e5cd64e652f52e491d6dd249e0ee92192d7 /plugins | |
| parent | 40405c07f91477d22b5e505b12ea3484a7e5f50f (diff) | |
| download | monitoring-plugins-d91d02619d4c1330b871a5418e436b2f81db533a.tar.gz | |
check_dig: Move flag parsing to parameter processing and out of main
The processing of the forbid-flags and the require-flags parameter
was done in the main function previous to this.
Since no further information is actually needed during runtime,
I moved the processing in the cli param processing stage to
reduce actual complexity later.
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_dig.c | 67 | ||||
| -rw-r--r-- | plugins/check_dig.d/config.h | 14 |
2 files changed, 34 insertions, 47 deletions
diff --git a/plugins/check_dig.c b/plugins/check_dig.c index 2db0f66b..9ea19e6a 100644 --- a/plugins/check_dig.c +++ b/plugins/check_dig.c | |||
| @@ -58,10 +58,6 @@ void print_usage(void); | |||
| 58 | static int verbose = 0; | 58 | static int verbose = 0; |
| 59 | 59 | ||
| 60 | /* helpers for flag parsing */ | 60 | /* helpers for flag parsing */ |
| 61 | typedef struct { | ||
| 62 | char **items; | ||
| 63 | size_t count; | ||
| 64 | } flag_list; | ||
| 65 | static flag_list parse_flags_line(const char *line); | 61 | static flag_list parse_flags_line(const char *line); |
| 66 | static flag_list split_csv_trim(const char *csv); | 62 | static flag_list split_csv_trim(const char *csv); |
| 67 | static bool flag_list_contains(const flag_list *list, const char *needle); | 63 | static bool flag_list_contains(const flag_list *list, const char *needle); |
| @@ -208,49 +204,36 @@ int main(int argc, char **argv) { | |||
| 208 | } | 204 | } |
| 209 | 205 | ||
| 210 | /* Optional: evaluate dig flags only if -E/-X were provided */ | 206 | /* Optional: evaluate dig flags only if -E/-X were provided */ |
| 211 | if ((config.require_flags && *config.require_flags) || | 207 | if ((config.require_flags.count > 0) || (config.forbid_flags.count > 0)) { |
| 212 | (config.forbid_flags && *config.forbid_flags)) { | ||
| 213 | |||
| 214 | if (dig_flags.count > 0) { | 208 | if (dig_flags.count > 0) { |
| 215 | 209 | for (size_t r = 0; r < config.require_flags.count; r++) { | |
| 216 | if (config.require_flags && *config.require_flags) { | 210 | if (!flag_list_contains(&dig_flags, config.require_flags.items[r])) { |
| 217 | flag_list req = split_csv_trim(config.require_flags); | 211 | result = STATE_CRITICAL; |
| 218 | 212 | if (!msg) { | |
| 219 | for (size_t r = 0; r < req.count; r++) { | 213 | xasprintf(&msg, _("Missing required DNS flag: %s"), |
| 220 | if (!flag_list_contains(&dig_flags, req.items[r])) { | 214 | config.require_flags.items[r]); |
| 221 | result = STATE_CRITICAL; | 215 | } else { |
| 222 | if (!msg) { | 216 | char *newmsg = NULL; |
| 223 | xasprintf(&msg, _("Missing required DNS flag: %s"), req.items[r]); | 217 | xasprintf(&newmsg, _("%s; missing required DNS flag: %s"), msg, |
| 224 | } else { | 218 | config.require_flags.items[r]); |
| 225 | char *newmsg = NULL; | 219 | msg = newmsg; |
| 226 | xasprintf(&newmsg, _("%s; missing required DNS flag: %s"), msg, | ||
| 227 | req.items[r]); | ||
| 228 | msg = newmsg; | ||
| 229 | } | ||
| 230 | } | 220 | } |
| 231 | } | 221 | } |
| 232 | |||
| 233 | free_flag_list(&req); | ||
| 234 | } | 222 | } |
| 235 | 223 | ||
| 236 | if (config.forbid_flags && *config.forbid_flags) { | 224 | for (size_t r = 0; r < config.forbid_flags.count; r++) { |
| 237 | flag_list bad = split_csv_trim(config.forbid_flags); | 225 | if (flag_list_contains(&dig_flags, config.forbid_flags.items[r])) { |
| 238 | 226 | result = STATE_CRITICAL; | |
| 239 | for (size_t r = 0; r < bad.count; r++) { | 227 | if (!msg) { |
| 240 | if (flag_list_contains(&dig_flags, bad.items[r])) { | 228 | xasprintf(&msg, _("Forbidden DNS flag present: %s"), |
| 241 | result = STATE_CRITICAL; | 229 | config.forbid_flags.items[r]); |
| 242 | if (!msg) { | 230 | } else { |
| 243 | xasprintf(&msg, _("Forbidden DNS flag present: %s"), bad.items[r]); | 231 | char *newmsg = NULL; |
| 244 | } else { | 232 | xasprintf(&newmsg, _("%s; forbidden DNS flag present: %s"), msg, |
| 245 | char *newmsg = NULL; | 233 | config.forbid_flags.items[r]); |
| 246 | xasprintf(&newmsg, _("%s; forbidden DNS flag present: %s"), msg, | 234 | msg = newmsg; |
| 247 | bad.items[r]); | ||
| 248 | msg = newmsg; | ||
| 249 | } | ||
| 250 | } | 235 | } |
| 251 | } | 236 | } |
| 252 | |||
| 253 | free_flag_list(&bad); | ||
| 254 | } | 237 | } |
| 255 | } | 238 | } |
| 256 | } | 239 | } |
| @@ -351,10 +334,10 @@ check_dig_config_wrapper process_arguments(int argc, char **argv) { | |||
| 351 | result.config.dig_args = strdup(optarg); | 334 | result.config.dig_args = strdup(optarg); |
| 352 | break; | 335 | break; |
| 353 | case 'E': /* require flags */ | 336 | case 'E': /* require flags */ |
| 354 | result.config.require_flags = strdup(optarg); | 337 | result.config.require_flags = split_csv_trim(optarg); |
| 355 | break; | 338 | break; |
| 356 | case 'X': /* forbid flags */ | 339 | case 'X': /* forbid flags */ |
| 357 | result.config.forbid_flags = strdup(optarg); | 340 | result.config.forbid_flags = split_csv_trim(optarg); |
| 358 | break; | 341 | break; |
| 359 | case 'v': /* verbose */ | 342 | case 'v': /* verbose */ |
| 360 | verbose++; | 343 | verbose++; |
diff --git a/plugins/check_dig.d/config.h b/plugins/check_dig.d/config.h index 392848e5..dd1f58b5 100644 --- a/plugins/check_dig.d/config.h +++ b/plugins/check_dig.d/config.h | |||
| @@ -8,6 +8,11 @@ | |||
| 8 | #define DEFAULT_TRIES 2 | 8 | #define DEFAULT_TRIES 2 |
| 9 | 9 | ||
| 10 | typedef struct { | 10 | typedef struct { |
| 11 | char **items; | ||
| 12 | size_t count; | ||
| 13 | } flag_list; | ||
| 14 | |||
| 15 | typedef struct { | ||
| 11 | char *query_address; | 16 | char *query_address; |
| 12 | char *record_type; | 17 | char *record_type; |
| 13 | char *expected_address; | 18 | char *expected_address; |
| @@ -19,8 +24,8 @@ typedef struct { | |||
| 19 | 24 | ||
| 20 | double warning_interval; | 25 | double warning_interval; |
| 21 | double critical_interval; | 26 | double critical_interval; |
| 22 | char *require_flags; | 27 | flag_list require_flags; |
| 23 | char *forbid_flags; | 28 | flag_list forbid_flags; |
| 24 | } check_dig_config; | 29 | } check_dig_config; |
| 25 | 30 | ||
| 26 | check_dig_config check_dig_config_init() { | 31 | check_dig_config check_dig_config_init() { |
| @@ -36,9 +41,8 @@ check_dig_config check_dig_config_init() { | |||
| 36 | 41 | ||
| 37 | .warning_interval = UNDEFINED, | 42 | .warning_interval = UNDEFINED, |
| 38 | .critical_interval = UNDEFINED, | 43 | .critical_interval = UNDEFINED, |
| 39 | .require_flags = NULL, | 44 | .require_flags = {.count = 0, .items = NULL}, |
| 40 | .forbid_flags = NULL, | 45 | .forbid_flags = {.count = 0, .items = NULL}, |
| 41 | |||
| 42 | }; | 46 | }; |
| 43 | return tmp; | 47 | return tmp; |
| 44 | } | 48 | } |
