diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-08 11:37:30 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-08 11:37:30 +0100 |
commit | 4079ba3c474054af6c8bb2c3b426540b2de01628 (patch) | |
tree | 659ff04461a7a207dcc152ba95545accea83f969 | |
parent | 540d609d845869a5c826f8190bdfed47db1e4259 (diff) | |
download | monitoring-plugins-4079ba3c474054af6c8bb2c3b426540b2de01628.tar.gz |
check_apt: Purify run_upgrade function
-rw-r--r-- | plugins/check_apt.c | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index ac89f816..1eda45dd 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -69,8 +69,18 @@ void print_usage(void); | |||
69 | static char *construct_cmdline(upgrade_type u, const char *opts); | 69 | static char *construct_cmdline(upgrade_type u, const char *opts); |
70 | /* run an apt-get update */ | 70 | /* run an apt-get update */ |
71 | static int run_update(void); | 71 | static int run_update(void); |
72 | |||
73 | typedef struct { | ||
74 | int errorcode; | ||
75 | int package_count; | ||
76 | int security_package_count; | ||
77 | char **packages_list; | ||
78 | char **secpackages_list; | ||
79 | } run_upgrade_result; | ||
80 | |||
72 | /* run an apt-get upgrade */ | 81 | /* run an apt-get upgrade */ |
73 | static int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist); | 82 | static run_upgrade_result run_upgrade(void); |
83 | |||
74 | /* add another clause to a regexp */ | 84 | /* add another clause to a regexp */ |
75 | static char *add_to_regexp(char *expr, const char *next); | 85 | static char *add_to_regexp(char *expr, const char *next); |
76 | /* extract package name from Inst line */ | 86 | /* extract package name from Inst line */ |
@@ -119,12 +129,14 @@ int main(int argc, char **argv) { | |||
119 | result = run_update(); | 129 | result = run_update(); |
120 | } | 130 | } |
121 | 131 | ||
122 | int packages_available = 0; | ||
123 | int sec_count = 0; | ||
124 | char **packages_list = NULL; | ||
125 | char **secpackages_list = NULL; | ||
126 | /* apt-get upgrade */ | 132 | /* apt-get upgrade */ |
127 | result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); | 133 | run_upgrade_result upgrad_res = run_upgrade(); |
134 | |||
135 | result = max_state(result, upgrad_res.errorcode); | ||
136 | int packages_available = upgrad_res.package_count; | ||
137 | int sec_count = upgrad_res.security_package_count; | ||
138 | char **packages_list = upgrad_res.packages_list; | ||
139 | char **secpackages_list = upgrad_res.secpackages_list; | ||
128 | 140 | ||
129 | if (sec_count > 0) { | 141 | if (sec_count > 0) { |
130 | result = max_state(result, STATE_CRITICAL); | 142 | result = max_state(result, STATE_CRITICAL); |
@@ -257,13 +269,18 @@ int process_arguments(int argc, char **argv) { | |||
257 | } | 269 | } |
258 | 270 | ||
259 | /* run an apt-get upgrade */ | 271 | /* run an apt-get upgrade */ |
260 | int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) { | 272 | run_upgrade_result run_upgrade(void) { |
261 | regex_t ereg; | 273 | regex_t ereg; |
262 | /* initialize ereg as it is possible it is printed while uninitialized */ | 274 | /* initialize ereg as it is possible it is printed while uninitialized */ |
263 | memset(&ereg, '\0', sizeof(ereg.buffer)); | 275 | memset(&ereg, '\0', sizeof(ereg.buffer)); |
264 | 276 | ||
277 | run_upgrade_result result = { | ||
278 | .errorcode = STATE_UNKNOWN, | ||
279 | }; | ||
280 | |||
265 | if (upgrade == NO_UPGRADE) { | 281 | if (upgrade == NO_UPGRADE) { |
266 | return STATE_OK; | 282 | result.errorcode = STATE_OK; |
283 | return result; | ||
267 | } | 284 | } |
268 | 285 | ||
269 | int regres = 0; | 286 | int regres = 0; |
@@ -294,33 +311,32 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
294 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); | 311 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); |
295 | } | 312 | } |
296 | 313 | ||
297 | int result = STATE_UNKNOWN; | ||
298 | struct output chld_out; | 314 | struct output chld_out; |
299 | struct output chld_err; | 315 | struct output chld_err; |
300 | char *cmdline = NULL; | 316 | char *cmdline = NULL; |
301 | cmdline = construct_cmdline(upgrade, upgrade_opts); | 317 | cmdline = construct_cmdline(upgrade, upgrade_opts); |
302 | if (input_filename != NULL) { | 318 | if (input_filename != NULL) { |
303 | /* read input from a file for testing */ | 319 | /* read input from a file for testing */ |
304 | result = cmd_file_read(input_filename, &chld_out, 0); | 320 | result.errorcode = cmd_file_read(input_filename, &chld_out, 0); |
305 | } else { | 321 | } else { |
306 | /* run the upgrade */ | 322 | /* run the upgrade */ |
307 | result = np_runcmd(cmdline, &chld_out, &chld_err, 0); | 323 | result.errorcode = np_runcmd(cmdline, &chld_out, &chld_err, 0); |
308 | } | 324 | } |
309 | 325 | ||
310 | /* apt-get upgrade only changes exit status if there is an | 326 | /* apt-get upgrade only changes exit status if there is an |
311 | * internal error when run in dry-run mode. therefore we will | 327 | * internal error when run in dry-run mode. therefore we will |
312 | * treat such an error as UNKNOWN */ | 328 | * treat such an error as UNKNOWN */ |
313 | if (result != 0) { | 329 | if (result.errorcode != STATE_OK) { |
314 | exec_warning = 1; | 330 | exec_warning = 1; |
315 | result = STATE_UNKNOWN; | 331 | result.errorcode = STATE_UNKNOWN; |
316 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); | 332 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
317 | } | 333 | } |
318 | 334 | ||
319 | *pkglist = malloc(sizeof(char *) * chld_out.lines); | 335 | char **pkglist = malloc(sizeof(char *) * chld_out.lines); |
320 | if (!pkglist) { | 336 | if (!pkglist) { |
321 | die(STATE_UNKNOWN, "malloc failed!\n"); | 337 | die(STATE_UNKNOWN, "malloc failed!\n"); |
322 | } | 338 | } |
323 | *secpkglist = malloc(sizeof(char *) * chld_out.lines); | 339 | char **secpkglist = malloc(sizeof(char *) * chld_out.lines); |
324 | if (!secpkglist) { | 340 | if (!secpkglist) { |
325 | die(STATE_UNKNOWN, "malloc failed!\n"); | 341 | die(STATE_UNKNOWN, "malloc failed!\n"); |
326 | } | 342 | } |
@@ -334,8 +350,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
334 | * we may need to switch to the --print-uris output format, | 350 | * we may need to switch to the --print-uris output format, |
335 | * in which case the logic here will slightly change. | 351 | * in which case the logic here will slightly change. |
336 | */ | 352 | */ |
337 | int pc = 0; | 353 | int package_counter = 0; |
338 | int spc = 0; | 354 | int security_package_counter = 0; |
339 | for (size_t i = 0; i < chld_out.lines; i++) { | 355 | for (size_t i = 0; i < chld_out.lines; i++) { |
340 | if (verbose) { | 356 | if (verbose) { |
341 | printf("%s\n", chld_out.line[i]); | 357 | printf("%s\n", chld_out.line[i]); |
@@ -346,15 +362,15 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
346 | /* if we're not excluding, or it's not in the | 362 | /* if we're not excluding, or it's not in the |
347 | * list of stuff to exclude */ | 363 | * list of stuff to exclude */ |
348 | if (do_exclude == NULL || regexec(&ereg, chld_out.line[i], 0, NULL, 0) != 0) { | 364 | if (do_exclude == NULL || regexec(&ereg, chld_out.line[i], 0, NULL, 0) != 0) { |
349 | pc++; | 365 | package_counter++; |
350 | if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) { | 366 | if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) { |
351 | spc++; | 367 | security_package_counter++; |
352 | if (verbose) { | 368 | if (verbose) { |
353 | printf("*"); | 369 | printf("*"); |
354 | } | 370 | } |
355 | (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]); | 371 | (secpkglist)[security_package_counter - 1] = pkg_name(chld_out.line[i]); |
356 | } else { | 372 | } else { |
357 | (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]); | 373 | (pkglist)[package_counter - security_package_counter - 1] = pkg_name(chld_out.line[i]); |
358 | } | 374 | } |
359 | if (verbose) { | 375 | if (verbose) { |
360 | printf("*%s\n", chld_out.line[i]); | 376 | printf("*%s\n", chld_out.line[i]); |
@@ -362,13 +378,15 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
362 | } | 378 | } |
363 | } | 379 | } |
364 | } | 380 | } |
365 | *pkgcount = pc; | 381 | result.package_count = package_counter; |
366 | *secpkgcount = spc; | 382 | result.security_package_count = security_package_counter; |
383 | result.packages_list = pkglist; | ||
384 | result.secpackages_list = secpkglist; | ||
367 | 385 | ||
368 | /* If we get anything on stderr, at least set warning */ | 386 | /* If we get anything on stderr, at least set warning */ |
369 | if (input_filename == NULL && chld_err.buflen) { | 387 | if (input_filename == NULL && chld_err.buflen) { |
370 | stderr_warning = 1; | 388 | stderr_warning = 1; |
371 | result = max_state(result, STATE_WARNING); | 389 | result.errorcode = max_state(result.errorcode, STATE_WARNING); |
372 | if (verbose) { | 390 | if (verbose) { |
373 | for (size_t i = 0; i < chld_err.lines; i++) { | 391 | for (size_t i = 0; i < chld_err.lines; i++) { |
374 | fprintf(stderr, "%s\n", chld_err.line[i]); | 392 | fprintf(stderr, "%s\n", chld_err.line[i]); |