summaryrefslogtreecommitdiffstats
path: root/plugins/check_apt.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-10 11:52:32 +0100
committerGitHub <noreply@github.com>2025-03-10 11:52:32 +0100
commite8d580eb4bc9216518de8083d39fd857d91e0a46 (patch)
tree4a37db34074bdce03703fe74840aca9df5b87467 /plugins/check_apt.c
parent0f0290a52f70d132e09e5b373e2c75b4524d393b (diff)
parentcda3906b12f88388ca4caeadf9f351c9c018bf89 (diff)
downloadmonitoring-plugins-e8d580eb4bc9216518de8083d39fd857d91e0a46.tar.gz
Merge pull request #2080 from RincewindsHat/refactor/check_apt
Refactor check_apt
Diffstat (limited to 'plugins/check_apt.c')
-rw-r--r--plugins/check_apt.c151
1 files changed, 76 insertions, 75 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index 1eda45dd..e840184b 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -29,6 +29,7 @@
29 * 29 *
30 *****************************************************************************/ 30 *****************************************************************************/
31 31
32#include "states.h"
32const char *progname = "check_apt"; 33const char *progname = "check_apt";
33const char *copyright = "2006-2024"; 34const char *copyright = "2006-2024";
34const char *email = "devel@monitoring-plugins.org"; 35const char *email = "devel@monitoring-plugins.org";
@@ -37,13 +38,7 @@ const char *email = "devel@monitoring-plugins.org";
37#include "runcmd.h" 38#include "runcmd.h"
38#include "utils.h" 39#include "utils.h"
39#include "regex.h" 40#include "regex.h"
40 41#include "check_apt.d/config.h"
41/* some constants */
42typedef enum {
43 UPGRADE,
44 DIST_UPGRADE,
45 NO_UPGRADE
46} upgrade_type;
47 42
48/* Character for hidden input file option (for testing). */ 43/* Character for hidden input file option (for testing). */
49#define INPUT_FILE_OPT CHAR_MAX + 1 44#define INPUT_FILE_OPT CHAR_MAX + 1
@@ -61,14 +56,18 @@ typedef enum {
61#define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)" 56#define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
62 57
63/* some standard functions */ 58/* some standard functions */
64static int process_arguments(int /*argc*/, char ** /*argv*/); 59typedef struct {
60 int errorcode;
61 check_apt_config config;
62} check_apt_config_wrapper;
63static check_apt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
65static void print_help(void); 64static void print_help(void);
66void print_usage(void); 65void print_usage(void);
67 66
68/* construct the appropriate apt-get cmdline */ 67/* construct the appropriate apt-get cmdline */
69static char *construct_cmdline(upgrade_type u, const char *opts); 68static char *construct_cmdline(upgrade_type /*u*/, const char * /*opts*/);
70/* run an apt-get update */ 69/* run an apt-get update */
71static int run_update(void); 70static int run_update(char * /*update_opts*/);
72 71
73typedef struct { 72typedef struct {
74 int errorcode; 73 int errorcode;
@@ -79,42 +78,35 @@ typedef struct {
79} run_upgrade_result; 78} run_upgrade_result;
80 79
81/* run an apt-get upgrade */ 80/* run an apt-get upgrade */
82static run_upgrade_result run_upgrade(void); 81run_upgrade_result run_upgrade(upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical,
82 const char *upgrade_opts, const char *input_filename);
83 83
84/* add another clause to a regexp */ 84/* add another clause to a regexp */
85static char *add_to_regexp(char *expr, const char *next); 85static char *add_to_regexp(char * /*expr*/, const char * /*next*/);
86/* extract package name from Inst line */ 86/* extract package name from Inst line */
87static char *pkg_name(char *line); 87static char *pkg_name(char * /*line*/);
88/* string comparison function for qsort */ 88/* string comparison function for qsort */
89static int cmpstringp(const void *p1, const void *p2); 89static int cmpstringp(const void * /*p1*/, const void * /*p2*/);
90 90
91/* configuration variables */ 91/* configuration variables */
92static int verbose = 0; /* -v */ 92static int verbose = 0; /* -v */
93static bool list = false; /* list packages available for upgrade */
94static bool do_update = false; /* whether to call apt-get update */
95static bool only_critical = false; /* whether to warn about non-critical updates */
96static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
97static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
98static char *update_opts = NULL; /* options to override defaults for update */
99static char *do_include = NULL; /* regexp to only include certain packages */
100static char *do_exclude = NULL; /* regexp to only exclude certain packages */
101static char *do_critical = NULL; /* regexp specifying critical packages */
102static char *input_filename = NULL; /* input filename for testing */
103/* number of packages available for upgrade to return WARNING status */
104static int packages_warning = 1;
105 93
106/* other global variables */ 94/* other global variables */
107static int stderr_warning = 0; /* if a cmd issued output on stderr */ 95static bool stderr_warning = false; /* if a cmd issued output on stderr */
108static int exec_warning = 0; /* if a cmd exited non-zero */ 96static bool exec_warning = false; /* if a cmd exited non-zero */
109 97
110int main(int argc, char **argv) { 98int main(int argc, char **argv) {
111 /* Parse extra opts if any */ 99 /* Parse extra opts if any */
112 argv = np_extra_opts(&argc, argv, progname); 100 argv = np_extra_opts(&argc, argv, progname);
113 101
114 if (process_arguments(argc, argv) == ERROR) { 102 check_apt_config_wrapper tmp_config = process_arguments(argc, argv);
103
104 if (tmp_config.errorcode == ERROR) {
115 usage_va(_("Could not parse arguments")); 105 usage_va(_("Could not parse arguments"));
116 } 106 }
117 107
108 const check_apt_config config = tmp_config.config;
109
118 /* Set signal handling and alarm timeout */ 110 /* Set signal handling and alarm timeout */
119 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 111 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
120 usage_va(_("Cannot catch SIGALRM")); 112 usage_va(_("Cannot catch SIGALRM"));
@@ -123,14 +115,15 @@ int main(int argc, char **argv) {
123 /* handle timeouts gracefully... */ 115 /* handle timeouts gracefully... */
124 alarm(timeout_interval); 116 alarm(timeout_interval);
125 117
126 int result = STATE_UNKNOWN; 118 mp_state_enum result = STATE_UNKNOWN;
127 /* if they want to run apt-get update first... */ 119 /* if they want to run apt-get update first... */
128 if (do_update) { 120 if (config.do_update) {
129 result = run_update(); 121 result = run_update(config.update_opts);
130 } 122 }
131 123
132 /* apt-get upgrade */ 124 /* apt-get upgrade */
133 run_upgrade_result upgrad_res = run_upgrade(); 125 run_upgrade_result upgrad_res =
126 run_upgrade(config.upgrade, config.do_include, config.do_exclude, config.do_critical, config.upgrade_opts, config.input_filename);
134 127
135 result = max_state(result, upgrad_res.errorcode); 128 result = max_state(result, upgrad_res.errorcode);
136 int packages_available = upgrad_res.package_count; 129 int packages_available = upgrad_res.package_count;
@@ -140,18 +133,18 @@ int main(int argc, char **argv) {
140 133
141 if (sec_count > 0) { 134 if (sec_count > 0) {
142 result = max_state(result, STATE_CRITICAL); 135 result = max_state(result, STATE_CRITICAL);
143 } else if (packages_available >= packages_warning && only_critical == false) { 136 } else if (packages_available >= config.packages_warning && !config.only_critical) {
144 result = max_state(result, STATE_WARNING); 137 result = max_state(result, STATE_WARNING);
145 } else if (result > STATE_UNKNOWN) { 138 } else if (result > STATE_UNKNOWN) {
146 result = STATE_UNKNOWN; 139 result = STATE_UNKNOWN;
147 } 140 }
148 141
149 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), 142 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"),
150 state_text(result), packages_available, (upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, 143 state_text(result), packages_available, (config.upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count,
151 (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", 144 (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "",
152 (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); 145 (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count);
153 146
154 if (list) { 147 if (config.list) {
155 qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); 148 qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp);
156 qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); 149 qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp);
157 150
@@ -159,7 +152,7 @@ int main(int argc, char **argv) {
159 printf("%s (security)\n", secpackages_list[i]); 152 printf("%s (security)\n", secpackages_list[i]);
160 } 153 }
161 154
162 if (only_critical == false) { 155 if (!config.only_critical) {
163 for (int i = 0; i < packages_available - sec_count; i++) { 156 for (int i = 0; i < packages_available - sec_count; i++) {
164 printf("%s\n", packages_list[i]); 157 printf("%s\n", packages_list[i]);
165 } 158 }
@@ -170,7 +163,7 @@ int main(int argc, char **argv) {
170} 163}
171 164
172/* process command-line arguments */ 165/* process command-line arguments */
173int process_arguments(int argc, char **argv) { 166check_apt_config_wrapper process_arguments(int argc, char **argv) {
174 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 167 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
175 {"help", no_argument, 0, 'h'}, 168 {"help", no_argument, 0, 'h'},
176 {"verbose", no_argument, 0, 'v'}, 169 {"verbose", no_argument, 0, 'v'},
@@ -179,7 +172,7 @@ int process_arguments(int argc, char **argv) {
179 {"upgrade", optional_argument, 0, 'U'}, 172 {"upgrade", optional_argument, 0, 'U'},
180 {"no-upgrade", no_argument, 0, 'n'}, 173 {"no-upgrade", no_argument, 0, 'n'},
181 {"dist-upgrade", optional_argument, 0, 'd'}, 174 {"dist-upgrade", optional_argument, 0, 'd'},
182 {"list", no_argument, false, 'l'}, 175 {"list", no_argument, 0, 'l'},
183 {"include", required_argument, 0, 'i'}, 176 {"include", required_argument, 0, 'i'},
184 {"exclude", required_argument, 0, 'e'}, 177 {"exclude", required_argument, 0, 'e'},
185 {"critical", required_argument, 0, 'c'}, 178 {"critical", required_argument, 0, 'c'},
@@ -188,6 +181,11 @@ int process_arguments(int argc, char **argv) {
188 {"packages-warning", required_argument, 0, 'w'}, 181 {"packages-warning", required_argument, 0, 'w'},
189 {0, 0, 0, 0}}; 182 {0, 0, 0, 0}};
190 183
184 check_apt_config_wrapper result = {
185 .errorcode = OK,
186 .config = check_apt_config_init(),
187 };
188
191 while (true) { 189 while (true) {
192 int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); 190 int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
193 191
@@ -209,55 +207,55 @@ int process_arguments(int argc, char **argv) {
209 timeout_interval = atoi(optarg); 207 timeout_interval = atoi(optarg);
210 break; 208 break;
211 case 'd': 209 case 'd':
212 upgrade = DIST_UPGRADE; 210 result.config.upgrade = DIST_UPGRADE;
213 if (optarg != NULL) { 211 if (optarg != NULL) {
214 upgrade_opts = strdup(optarg); 212 result.config.upgrade_opts = strdup(optarg);
215 if (upgrade_opts == NULL) { 213 if (result.config.upgrade_opts == NULL) {
216 die(STATE_UNKNOWN, "strdup failed"); 214 die(STATE_UNKNOWN, "strdup failed");
217 } 215 }
218 } 216 }
219 break; 217 break;
220 case 'U': 218 case 'U':
221 upgrade = UPGRADE; 219 result.config.upgrade = UPGRADE;
222 if (optarg != NULL) { 220 if (optarg != NULL) {
223 upgrade_opts = strdup(optarg); 221 result.config.upgrade_opts = strdup(optarg);
224 if (upgrade_opts == NULL) { 222 if (result.config.upgrade_opts == NULL) {
225 die(STATE_UNKNOWN, "strdup failed"); 223 die(STATE_UNKNOWN, "strdup failed");
226 } 224 }
227 } 225 }
228 break; 226 break;
229 case 'n': 227 case 'n':
230 upgrade = NO_UPGRADE; 228 result.config.upgrade = NO_UPGRADE;
231 break; 229 break;
232 case 'u': 230 case 'u':
233 do_update = true; 231 result.config.do_update = true;
234 if (optarg != NULL) { 232 if (optarg != NULL) {
235 update_opts = strdup(optarg); 233 result.config.update_opts = strdup(optarg);
236 if (update_opts == NULL) { 234 if (result.config.update_opts == NULL) {
237 die(STATE_UNKNOWN, "strdup failed"); 235 die(STATE_UNKNOWN, "strdup failed");
238 } 236 }
239 } 237 }
240 break; 238 break;
241 case 'l': 239 case 'l':
242 list = true; 240 result.config.list = true;
243 break; 241 break;
244 case 'i': 242 case 'i':
245 do_include = add_to_regexp(do_include, optarg); 243 result.config.do_include = add_to_regexp(result.config.do_include, optarg);
246 break; 244 break;
247 case 'e': 245 case 'e':
248 do_exclude = add_to_regexp(do_exclude, optarg); 246 result.config.do_exclude = add_to_regexp(result.config.do_exclude, optarg);
249 break; 247 break;
250 case 'c': 248 case 'c':
251 do_critical = add_to_regexp(do_critical, optarg); 249 result.config.do_critical = add_to_regexp(result.config.do_critical, optarg);
252 break; 250 break;
253 case 'o': 251 case 'o':
254 only_critical = true; 252 result.config.only_critical = true;
255 break; 253 break;
256 case INPUT_FILE_OPT: 254 case INPUT_FILE_OPT:
257 input_filename = optarg; 255 result.config.input_filename = optarg;
258 break; 256 break;
259 case 'w': 257 case 'w':
260 packages_warning = atoi(optarg); 258 result.config.packages_warning = atoi(optarg);
261 break; 259 break;
262 default: 260 default:
263 /* print short usage statement if args not parsable */ 261 /* print short usage statement if args not parsable */
@@ -265,11 +263,12 @@ int process_arguments(int argc, char **argv) {
265 } 263 }
266 } 264 }
267 265
268 return OK; 266 return result;
269} 267}
270 268
271/* run an apt-get upgrade */ 269/* run an apt-get upgrade */
272run_upgrade_result run_upgrade(void) { 270run_upgrade_result run_upgrade(const upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical,
271 const char *upgrade_opts, const char *input_filename) {
273 regex_t ereg; 272 regex_t ereg;
274 /* initialize ereg as it is possible it is printed while uninitialized */ 273 /* initialize ereg as it is possible it is printed while uninitialized */
275 memset(&ereg, '\0', sizeof(ereg.buffer)); 274 memset(&ereg, '\0', sizeof(ereg.buffer));
@@ -332,7 +331,7 @@ run_upgrade_result run_upgrade(void) {
332 fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); 331 fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
333 } 332 }
334 333
335 char **pkglist = malloc(sizeof(char *) * chld_out.lines); 334 char **pkglist = malloc(sizeof(char *) * chld_out.lines);
336 if (!pkglist) { 335 if (!pkglist) {
337 die(STATE_UNKNOWN, "malloc failed!\n"); 336 die(STATE_UNKNOWN, "malloc failed!\n");
338 } 337 }
@@ -385,7 +384,7 @@ run_upgrade_result run_upgrade(void) {
385 384
386 /* If we get anything on stderr, at least set warning */ 385 /* If we get anything on stderr, at least set warning */
387 if (input_filename == NULL && chld_err.buflen) { 386 if (input_filename == NULL && chld_err.buflen) {
388 stderr_warning = 1; 387 stderr_warning = true;
389 result.errorcode = max_state(result.errorcode, STATE_WARNING); 388 result.errorcode = max_state(result.errorcode, STATE_WARNING);
390 if (verbose) { 389 if (verbose) {
391 for (size_t i = 0; i < chld_err.lines; i++) { 390 for (size_t i = 0; i < chld_err.lines; i++) {
@@ -405,7 +404,7 @@ run_upgrade_result run_upgrade(void) {
405} 404}
406 405
407/* run an apt-get update (needs root) */ 406/* run an apt-get update (needs root) */
408int run_update(void) { 407int run_update(char *update_opts) {
409 int result = STATE_UNKNOWN; 408 int result = STATE_UNKNOWN;
410 char *cmdline; 409 char *cmdline;
411 /* run the update */ 410 /* run the update */
@@ -418,7 +417,7 @@ int run_update(void) {
418 * since we were explicitly asked to do so, this is treated as 417 * since we were explicitly asked to do so, this is treated as
419 * a critical error. */ 418 * a critical error. */
420 if (result != 0) { 419 if (result != 0) {
421 exec_warning = 1; 420 exec_warning = true;
422 result = STATE_CRITICAL; 421 result = STATE_CRITICAL;
423 fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); 422 fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
424 } 423 }
@@ -446,7 +445,7 @@ int run_update(void) {
446char *pkg_name(char *line) { 445char *pkg_name(char *line) {
447 char *start = line + strlen(PKGINST_PREFIX); 446 char *start = line + strlen(PKGINST_PREFIX);
448 447
449 int len = strlen(start); 448 size_t len = strlen(start);
450 449
451 char *space = index(start, ' '); 450 char *space = index(start, ' ');
452 if (space != NULL) { 451 if (space != NULL) {
@@ -464,35 +463,37 @@ char *pkg_name(char *line) {
464 return pkg; 463 return pkg;
465} 464}
466 465
467int cmpstringp(const void *p1, const void *p2) { return strcmp(*(char *const *)p1, *(char *const *)p2); } 466int cmpstringp(const void *left_string, const void *right_string) {
467 return strcmp(*(char *const *)left_string, *(char *const *)right_string);
468}
468 469
469char *add_to_regexp(char *expr, const char *next) { 470char *add_to_regexp(char *expr, const char *next) {
470 char *re = NULL; 471 char *regex_string = NULL;
471 472
472 if (expr == NULL) { 473 if (expr == NULL) {
473 re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); 474 regex_string = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1));
474 if (!re) { 475 if (!regex_string) {
475 die(STATE_UNKNOWN, "malloc failed!\n"); 476 die(STATE_UNKNOWN, "malloc failed!\n");
476 } 477 }
477 sprintf(re, "(%s)", next); 478 sprintf(regex_string, "(%s)", next);
478 } else { 479 } else {
479 /* resize it, adding an extra char for the new '|' separator */ 480 /* resize it, adding an extra char for the new '|' separator */
480 re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); 481 regex_string = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1));
481 if (!re) { 482 if (!regex_string) {
482 die(STATE_UNKNOWN, "realloc failed!\n"); 483 die(STATE_UNKNOWN, "realloc failed!\n");
483 } 484 }
484 /* append it starting at ')' in the old re */ 485 /* append it starting at ')' in the old re */
485 sprintf((char *)(re + strlen(re) - 1), "|%s)", next); 486 sprintf((char *)(regex_string + strlen(regex_string) - 1), "|%s)", next);
486 } 487 }
487 488
488 return re; 489 return regex_string;
489} 490}
490 491
491char *construct_cmdline(upgrade_type u, const char *opts) { 492char *construct_cmdline(upgrade_type upgrade, const char *opts) {
492 const char *opts_ptr = NULL; 493 const char *opts_ptr = NULL;
493 const char *aptcmd = NULL; 494 const char *aptcmd = NULL;
494 495
495 switch (u) { 496 switch (upgrade) {
496 case UPGRADE: 497 case UPGRADE:
497 if (opts == NULL) { 498 if (opts == NULL) {
498 opts_ptr = UPGRADE_DEFAULT_OPTS; 499 opts_ptr = UPGRADE_DEFAULT_OPTS;