summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Wagner <waja@cyconet.org>2017-01-17 16:18:01 +0100
committerJan Wagner <waja@cyconet.org>2017-01-17 16:25:11 +0100
commit711a6aa2ff6210d53c2cc46cc15e13aa6f7535a8 (patch)
tree9f1638fa98c1257fc965b3646c702ad655161a92
parent89d00d1c009dd3d4d1c5a6c3769a27704ca3a154 (diff)
parent43ce70bcdbebb0b699bf936ac1763c423cd7f069 (diff)
downloadmonitoring-plugins-711a6aa.tar.gz
Merge remote-tracking branch 'upstream/pr/1427'
Closes: #1256 Closes: #1458
-rw-r--r--plugins/check_apt.c72
1 files changed, 65 insertions, 7 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index c90b3df7..b69680c2 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 */
@@ -87,7 +92,8 @@ static int stderr_warning = 0; /* if a cmd issued output on stderr */
87static int exec_warning = 0; /* if a cmd exited non-zero */ 92static int exec_warning = 0; /* if a cmd exited non-zero */
88 93
89int main (int argc, char **argv) { 94int main (int argc, char **argv) {
90 int result=STATE_UNKNOWN, packages_available=0, sec_count=0; 95 int result=STATE_UNKNOWN, packages_available=0, sec_count=0, i=0;
96 char **packages_list=NULL, **secpackages_list=NULL;
91 97
92 /* Parse extra opts if any */ 98 /* Parse extra opts if any */
93 argv=np_extra_opts(&argc, argv, progname); 99 argv=np_extra_opts(&argc, argv, progname);
@@ -107,7 +113,7 @@ int main (int argc, char **argv) {
107 if(do_update) result = run_update(); 113 if(do_update) result = run_update();
108 114
109 /* apt-get upgrade */ 115 /* apt-get upgrade */
110 result = max_state(result, run_upgrade(&packages_available, &sec_count)); 116 result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
111 117
112 if(sec_count > 0){ 118 if(sec_count > 0){
113 result = max_state(result, STATE_CRITICAL); 119 result = max_state(result, STATE_CRITICAL);
@@ -130,6 +136,18 @@ int main (int argc, char **argv) {
130 sec_count 136 sec_count
131 ); 137 );
132 138
139 if(list) {
140 qsort(secpackages_list, sec_count, sizeof(char*), cmpstringp);
141 qsort(packages_list, packages_available-sec_count, sizeof(char*), cmpstringp);
142
143 for(i = 0; i < sec_count; i++)
144 printf("%s (security)\n", secpackages_list[i]);
145 if (only_critical == 0) {
146 for(i = 0; i < packages_available - sec_count; i++)
147 printf("%s\n", packages_list[i]);
148 }
149 }
150
133 return result; 151 return result;
134} 152}
135 153
@@ -146,6 +164,7 @@ int process_arguments (int argc, char **argv) {
146 {"upgrade", optional_argument, 0, 'U'}, 164 {"upgrade", optional_argument, 0, 'U'},
147 {"no-upgrade", no_argument, 0, 'n'}, 165 {"no-upgrade", no_argument, 0, 'n'},
148 {"dist-upgrade", optional_argument, 0, 'd'}, 166 {"dist-upgrade", optional_argument, 0, 'd'},
167 {"list", no_argument, 0, 'l'},
149 {"include", required_argument, 0, 'i'}, 168 {"include", required_argument, 0, 'i'},
150 {"exclude", required_argument, 0, 'e'}, 169 {"exclude", required_argument, 0, 'e'},
151 {"critical", required_argument, 0, 'c'}, 170 {"critical", required_argument, 0, 'c'},
@@ -155,7 +174,7 @@ int process_arguments (int argc, char **argv) {
155 }; 174 };
156 175
157 while(1) { 176 while(1) {
158 c = getopt_long(argc, argv, "hVvt:u::U::d::ni:e:c:o", longopts, NULL); 177 c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:o", longopts, NULL);
159 178
160 if(c == -1 || c == EOF || c == 1) break; 179 if(c == -1 || c == EOF || c == 1) break;
161 180
@@ -196,6 +215,9 @@ int process_arguments (int argc, char **argv) {
196 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 215 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
197 } 216 }
198 break; 217 break;
218 case 'l':
219 list=1;
220 break;
199 case 'i': 221 case 'i':
200 do_include=add_to_regexp(do_include, optarg); 222 do_include=add_to_regexp(do_include, optarg);
201 break; 223 break;
@@ -222,7 +244,7 @@ int process_arguments (int argc, char **argv) {
222 244
223 245
224/* run an apt-get upgrade */ 246/* run an apt-get upgrade */
225int run_upgrade(int *pkgcount, int *secpkgcount){ 247int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist){
226 int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0; 248 int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
227 struct output chld_out, chld_err; 249 struct output chld_out, chld_err;
228 regex_t ireg, ereg, sreg; 250 regex_t ireg, ereg, sreg;
@@ -278,6 +300,11 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
278 cmdline); 300 cmdline);
279 } 301 }
280 302
303 *pkglist=malloc(sizeof(char *) * chld_out.lines);
304 if(!pkglist) die(STATE_UNKNOWN, "malloc failed!\n");
305 *secpkglist=malloc(sizeof(char *) * chld_out.lines);
306 if(!secpkglist) die(STATE_UNKNOWN, "malloc failed!\n");
307
281 /* parse the output, which should only consist of lines like 308 /* parse the output, which should only consist of lines like
282 * 309 *
283 * Inst package .... 310 * Inst package ....
@@ -302,6 +329,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
302 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){ 329 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
303 spc++; 330 spc++;
304 if(verbose) printf("*"); 331 if(verbose) printf("*");
332 (*secpkglist)[spc-1] = pkg_name(chld_out.line[i]);
333 } else {
334 (*pkglist)[pc-spc-1] = pkg_name(chld_out.line[i]);
305 } 335 }
306 if(verbose){ 336 if(verbose){
307 printf("*%s\n", chld_out.line[i]); 337 printf("*%s\n", chld_out.line[i]);
@@ -368,6 +398,31 @@ int run_update(void){
368 return result; 398 return result;
369} 399}
370 400
401char* pkg_name(char *line){
402 char *start=NULL, *space=NULL, *pkg=NULL;
403 int len=0;
404
405 start = line + strlen(PKGINST_PREFIX);
406 len = strlen(start);
407
408 space = index(start, ' ');
409 if(space!=NULL){
410 len = space - start;
411 }
412
413 pkg=malloc(sizeof(char)*(len+1));
414 if(!pkg) die(STATE_UNKNOWN, "malloc failed!\n");
415
416 strncpy(pkg, start, len);
417 pkg[len]='\0';
418
419 return pkg;
420}
421
422int cmpstringp(const void *p1, const void *p2){
423 return strcmp(* (char * const *) p1, * (char * const *) p2);
424}
425
371char* add_to_regexp(char *expr, const char *next){ 426char* add_to_regexp(char *expr, const char *next){
372 char *re=NULL; 427 char *re=NULL;
373 428
@@ -450,8 +505,11 @@ print_help (void)
450 printf (" %s\n", "-d, --dist-upgrade=OPTS"); 505 printf (" %s\n", "-d, --dist-upgrade=OPTS");
451 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS")); 506 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.")); 507 printf (" %s\n", _("can be provided to override the default options."));
453 printf (" %s\n", " -n, --no-upgrade"); 508 printf (" %s\n", "-n, --no-upgrade");
454 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least).")); 509 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
510 printf (" %s\n", "-l, --list");
511 printf (" %s\n", _("List packages available for upgrade. Packages are printed sorted by"));
512 printf (" %s\n", _("name with security packages listed first."));
455 printf (" %s\n", "-i, --include=REGEXP"); 513 printf (" %s\n", "-i, --include=REGEXP");
456 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times")); 514 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")); 515 printf (" %s\n", _("the values will be combined together. Any packages matching this list"));
@@ -490,5 +548,5 @@ void
490print_usage(void) 548print_usage(void)
491{ 549{
492 printf ("%s\n", _("Usage:")); 550 printf ("%s\n", _("Usage:"));
493 printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname); 551 printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout]\n", progname);
494} 552}