summaryrefslogtreecommitdiffstats
path: root/plugins/check_apt.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_apt.c')
-rw-r--r--plugins/check_apt.c85
1 files changed, 76 insertions, 9 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index c90b3df..d7be575 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -66,12 +66,17 @@ char* construct_cmdline(upgrade_type u, const char *opts);
66/* run an apt-get update */ 66/* run an apt-get update */
67int run_update(void); 67int run_update(void);
68/* run an apt-get upgrade */ 68/* run an apt-get upgrade */
69int run_upgrade(int *pkgcount, int *secpkgcount); 69int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist);
70/* add another clause to a regexp */ 70/* add another clause to a regexp */
71char* add_to_regexp(char *expr, const char *next); 71char* add_to_regexp(char *expr, const char *next);
72/* extract package name from Inst line */
73char* pkg_name(char *line);
74/* string comparison function for qsort */
75int cmpstringp(const void *p1, const void *p2);
72 76
73/* configuration variables */ 77/* configuration variables */
74static int verbose = 0; /* -v */ 78static int verbose = 0; /* -v */
79static int list = 0; /* list packages available for upgrade */
75static int do_update = 0; /* whether to call apt-get update */ 80static int do_update = 0; /* whether to call apt-get update */
76static int only_critical = 0; /* whether to warn about non-critical updates */ 81static int only_critical = 0; /* whether to warn about non-critical updates */
77static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ 82static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
@@ -81,13 +86,16 @@ static char *do_include = NULL; /* regexp to only include certain packages */
81static char *do_exclude = NULL; /* regexp to only exclude certain packages */ 86static char *do_exclude = NULL; /* regexp to only exclude certain packages */
82static char *do_critical = NULL; /* regexp specifying critical packages */ 87static char *do_critical = NULL; /* regexp specifying critical packages */
83static char *input_filename = NULL; /* input filename for testing */ 88static char *input_filename = NULL; /* input filename for testing */
89/* number of packages available for upgrade to return WARNING status */
90static int packages_warning = 1;
84 91
85/* other global variables */ 92/* other global variables */
86static int stderr_warning = 0; /* if a cmd issued output on stderr */ 93static int stderr_warning = 0; /* if a cmd issued output on stderr */
87static int exec_warning = 0; /* if a cmd exited non-zero */ 94static int exec_warning = 0; /* if a cmd exited non-zero */
88 95
89int main (int argc, char **argv) { 96int main (int argc, char **argv) {
90 int result=STATE_UNKNOWN, packages_available=0, sec_count=0; 97 int result=STATE_UNKNOWN, packages_available=0, sec_count=0, i=0;
98 char **packages_list=NULL, **secpackages_list=NULL;
91 99
92 /* Parse extra opts if any */ 100 /* Parse extra opts if any */
93 argv=np_extra_opts(&argc, argv, progname); 101 argv=np_extra_opts(&argc, argv, progname);
@@ -107,11 +115,11 @@ int main (int argc, char **argv) {
107 if(do_update) result = run_update(); 115 if(do_update) result = run_update();
108 116
109 /* apt-get upgrade */ 117 /* apt-get upgrade */
110 result = max_state(result, run_upgrade(&packages_available, &sec_count)); 118 result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
111 119
112 if(sec_count > 0){ 120 if(sec_count > 0){
113 result = max_state(result, STATE_CRITICAL); 121 result = max_state(result, STATE_CRITICAL);
114 } else if(packages_available > 0 && only_critical == 0){ 122 } else if(packages_available >= packages_warning && only_critical == 0){
115 result = max_state(result, STATE_WARNING); 123 result = max_state(result, STATE_WARNING);
116 } else if(result > STATE_UNKNOWN){ 124 } else if(result > STATE_UNKNOWN){
117 result = STATE_UNKNOWN; 125 result = STATE_UNKNOWN;
@@ -130,6 +138,18 @@ int main (int argc, char **argv) {
130 sec_count 138 sec_count
131 ); 139 );
132 140
141 if(list) {
142 qsort(secpackages_list, sec_count, sizeof(char*), cmpstringp);
143 qsort(packages_list, packages_available-sec_count, sizeof(char*), cmpstringp);
144
145 for(i = 0; i < sec_count; i++)
146 printf("%s (security)\n", secpackages_list[i]);
147 if (only_critical == 0) {
148 for(i = 0; i < packages_available - sec_count; i++)
149 printf("%s\n", packages_list[i]);
150 }
151 }
152
133 return result; 153 return result;
134} 154}
135 155
@@ -146,16 +166,18 @@ int process_arguments (int argc, char **argv) {
146 {"upgrade", optional_argument, 0, 'U'}, 166 {"upgrade", optional_argument, 0, 'U'},
147 {"no-upgrade", no_argument, 0, 'n'}, 167 {"no-upgrade", no_argument, 0, 'n'},
148 {"dist-upgrade", optional_argument, 0, 'd'}, 168 {"dist-upgrade", optional_argument, 0, 'd'},
169 {"list", no_argument, 0, 'l'},
149 {"include", required_argument, 0, 'i'}, 170 {"include", required_argument, 0, 'i'},
150 {"exclude", required_argument, 0, 'e'}, 171 {"exclude", required_argument, 0, 'e'},
151 {"critical", required_argument, 0, 'c'}, 172 {"critical", required_argument, 0, 'c'},
152 {"only-critical", no_argument, 0, 'o'}, 173 {"only-critical", no_argument, 0, 'o'},
153 {"input-file", required_argument, 0, INPUT_FILE_OPT}, 174 {"input-file", required_argument, 0, INPUT_FILE_OPT},
175 {"packages-warning", required_argument, 0, 'w'},
154 {0, 0, 0, 0} 176 {0, 0, 0, 0}
155 }; 177 };
156 178
157 while(1) { 179 while(1) {
158 c = getopt_long(argc, argv, "hVvt:u::U::d::ni:e:c:o", longopts, NULL); 180 c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
159 181
160 if(c == -1 || c == EOF || c == 1) break; 182 if(c == -1 || c == EOF || c == 1) break;
161 183
@@ -196,6 +218,9 @@ int process_arguments (int argc, char **argv) {
196 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 218 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
197 } 219 }
198 break; 220 break;
221 case 'l':
222 list=1;
223 break;
199 case 'i': 224 case 'i':
200 do_include=add_to_regexp(do_include, optarg); 225 do_include=add_to_regexp(do_include, optarg);
201 break; 226 break;
@@ -211,6 +236,9 @@ int process_arguments (int argc, char **argv) {
211 case INPUT_FILE_OPT: 236 case INPUT_FILE_OPT:
212 input_filename = optarg; 237 input_filename = optarg;
213 break; 238 break;
239 case 'w':
240 packages_warning = atoi(optarg);
241 break;
214 default: 242 default:
215 /* print short usage statement if args not parsable */ 243 /* print short usage statement if args not parsable */
216 usage5(); 244 usage5();
@@ -222,7 +250,7 @@ int process_arguments (int argc, char **argv) {
222 250
223 251
224/* run an apt-get upgrade */ 252/* run an apt-get upgrade */
225int run_upgrade(int *pkgcount, int *secpkgcount){ 253int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist){
226 int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0; 254 int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
227 struct output chld_out, chld_err; 255 struct output chld_out, chld_err;
228 regex_t ireg, ereg, sreg; 256 regex_t ireg, ereg, sreg;
@@ -278,6 +306,11 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
278 cmdline); 306 cmdline);
279 } 307 }
280 308
309 *pkglist=malloc(sizeof(char *) * chld_out.lines);
310 if(!pkglist) die(STATE_UNKNOWN, "malloc failed!\n");
311 *secpkglist=malloc(sizeof(char *) * chld_out.lines);
312 if(!secpkglist) die(STATE_UNKNOWN, "malloc failed!\n");
313
281 /* parse the output, which should only consist of lines like 314 /* parse the output, which should only consist of lines like
282 * 315 *
283 * Inst package .... 316 * Inst package ....
@@ -302,6 +335,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
302 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){ 335 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
303 spc++; 336 spc++;
304 if(verbose) printf("*"); 337 if(verbose) printf("*");
338 (*secpkglist)[spc-1] = pkg_name(chld_out.line[i]);
339 } else {
340 (*pkglist)[pc-spc-1] = pkg_name(chld_out.line[i]);
305 } 341 }
306 if(verbose){ 342 if(verbose){
307 printf("*%s\n", chld_out.line[i]); 343 printf("*%s\n", chld_out.line[i]);
@@ -368,6 +404,31 @@ int run_update(void){
368 return result; 404 return result;
369} 405}
370 406
407char* pkg_name(char *line){
408 char *start=NULL, *space=NULL, *pkg=NULL;
409 int len=0;
410
411 start = line + strlen(PKGINST_PREFIX);
412 len = strlen(start);
413
414 space = index(start, ' ');
415 if(space!=NULL){
416 len = space - start;
417 }
418
419 pkg=malloc(sizeof(char)*(len+1));
420 if(!pkg) die(STATE_UNKNOWN, "malloc failed!\n");
421
422 strncpy(pkg, start, len);
423 pkg[len]='\0';
424
425 return pkg;
426}
427
428int cmpstringp(const void *p1, const void *p2){
429 return strcmp(* (char * const *) p1, * (char * const *) p2);
430}
431
371char* add_to_regexp(char *expr, const char *next){ 432char* add_to_regexp(char *expr, const char *next){
372 char *re=NULL; 433 char *re=NULL;
373 434
@@ -450,8 +511,11 @@ print_help (void)
450 printf (" %s\n", "-d, --dist-upgrade=OPTS"); 511 printf (" %s\n", "-d, --dist-upgrade=OPTS");
451 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS")); 512 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
452 printf (" %s\n", _("can be provided to override the default options.")); 513 printf (" %s\n", _("can be provided to override the default options."));
453 printf (" %s\n", " -n, --no-upgrade"); 514 printf (" %s\n", "-n, --no-upgrade");
454 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least).")); 515 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
516 printf (" %s\n", "-l, --list");
517 printf (" %s\n", _("List packages available for upgrade. Packages are printed sorted by"));
518 printf (" %s\n", _("name with security packages listed first."));
455 printf (" %s\n", "-i, --include=REGEXP"); 519 printf (" %s\n", "-i, --include=REGEXP");
456 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times")); 520 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
457 printf (" %s\n", _("the values will be combined together. Any packages matching this list")); 521 printf (" %s\n", _("the values will be combined together. Any packages matching this list"));
@@ -472,7 +536,10 @@ print_help (void)
472 printf (" %s\n", "-o, --only-critical"); 536 printf (" %s\n", "-o, --only-critical");
473 printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number")); 537 printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number"));
474 printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause")); 538 printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause"));
475 printf (" %s\n\n", _("the plugin to return WARNING status.")); 539 printf (" %s\n", _("the plugin to return WARNING status."));
540 printf (" %s\n", "-w, --packages-warning");
541 printf (" %s\n", _("Minumum number of packages available for upgrade to return WARNING status."));
542 printf (" %s\n\n", _("Default is 1 package."));
476 543
477 printf ("%s\n\n", _("The following options require root privileges and should be used with care:")); 544 printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
478 printf (" %s\n", "-u, --update=OPTS"); 545 printf (" %s\n", "-u, --update=OPTS");
@@ -490,5 +557,5 @@ void
490print_usage(void) 557print_usage(void)
491{ 558{
492 printf ("%s\n", _("Usage:")); 559 printf ("%s\n", _("Usage:"));
493 printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname); 560 printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname);
494} 561}