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, 47 insertions, 38 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index b69680c2..fa982ae3 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -1,32 +1,32 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Monitoring check_apt plugin 3* Monitoring check_apt plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2006-2008 Monitoring Plugins Development Team 6* Copyright (c) 2006-2008 Monitoring Plugins Development Team
7* 7*
8* Original author: Sean Finney 8* Original author: Sean Finney
9* 9*
10* Description: 10* Description:
11* 11*
12* This file contains the check_apt plugin 12* This file contains the check_apt plugin
13* 13*
14* Check for available updates in apt package management systems 14* Check for available updates in apt package management systems
15* 15*
16* 16*
17* This program is free software: you can redistribute it and/or modify 17* This program is free software: you can redistribute it and/or modify
18* it under the terms of the GNU General Public License as published by 18* it under the terms of the GNU General Public License as published by
19* the Free Software Foundation, either version 3 of the License, or 19* the Free Software Foundation, either version 3 of the License, or
20* (at your option) any later version. 20* (at your option) any later version.
21* 21*
22* This program is distributed in the hope that it will be useful, 22* This program is distributed in the hope that it will be useful,
23* but WITHOUT ANY WARRANTY; without even the implied warranty of 23* but WITHOUT ANY WARRANTY; without even the implied warranty of
24* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25* GNU General Public License for more details. 25* GNU General Public License for more details.
26* 26*
27* You should have received a copy of the GNU General Public License 27* You should have received a copy of the GNU General Public License
28* along with this program. If not, see <http://www.gnu.org/licenses/>. 28* along with this program. If not, see <http://www.gnu.org/licenses/>.
29* 29*
30*****************************************************************************/ 30*****************************************************************************/
31 31
32const char *progname = "check_apt"; 32const char *progname = "check_apt";
@@ -76,9 +76,9 @@ int cmpstringp(const void *p1, const void *p2);
76 76
77/* configuration variables */ 77/* configuration variables */
78static int verbose = 0; /* -v */ 78static int verbose = 0; /* -v */
79static int list = 0; /* list packages available for upgrade */ 79static bool list = false; /* list packages available for upgrade */
80static int do_update = 0; /* whether to call apt-get update */ 80static bool do_update = false; /* whether to call apt-get update */
81static int only_critical = 0; /* whether to warn about non-critical updates */ 81static bool only_critical = false; /* whether to warn about non-critical updates */
82static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ 82static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
83static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ 83static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
84static char *update_opts = NULL; /* options to override defaults for update */ 84static char *update_opts = NULL; /* options to override defaults for update */
@@ -86,6 +86,8 @@ static char *do_include = NULL; /* regexp to only include certain packages */
86static char *do_exclude = NULL; /* regexp to only exclude certain packages */ 86static char *do_exclude = NULL; /* regexp to only exclude certain packages */
87static char *do_critical = NULL; /* regexp specifying critical packages */ 87static char *do_critical = NULL; /* regexp specifying critical packages */
88static 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;
89 91
90/* other global variables */ 92/* other global variables */
91static int stderr_warning = 0; /* if a cmd issued output on stderr */ 93static int stderr_warning = 0; /* if a cmd issued output on stderr */
@@ -117,7 +119,7 @@ int main (int argc, char **argv) {
117 119
118 if(sec_count > 0){ 120 if(sec_count > 0){
119 result = max_state(result, STATE_CRITICAL); 121 result = max_state(result, STATE_CRITICAL);
120 } else if(packages_available > 0 && only_critical == 0){ 122 } else if(packages_available >= packages_warning && only_critical == false){
121 result = max_state(result, STATE_WARNING); 123 result = max_state(result, STATE_WARNING);
122 } else if(result > STATE_UNKNOWN){ 124 } else if(result > STATE_UNKNOWN){
123 result = STATE_UNKNOWN; 125 result = STATE_UNKNOWN;
@@ -142,7 +144,7 @@ int main (int argc, char **argv) {
142 144
143 for(i = 0; i < sec_count; i++) 145 for(i = 0; i < sec_count; i++)
144 printf("%s (security)\n", secpackages_list[i]); 146 printf("%s (security)\n", secpackages_list[i]);
145 if (only_critical == 0) { 147 if (only_critical == false) {
146 for(i = 0; i < packages_available - sec_count; i++) 148 for(i = 0; i < packages_available - sec_count; i++)
147 printf("%s\n", packages_list[i]); 149 printf("%s\n", packages_list[i]);
148 } 150 }
@@ -164,17 +166,18 @@ int process_arguments (int argc, char **argv) {
164 {"upgrade", optional_argument, 0, 'U'}, 166 {"upgrade", optional_argument, 0, 'U'},
165 {"no-upgrade", no_argument, 0, 'n'}, 167 {"no-upgrade", no_argument, 0, 'n'},
166 {"dist-upgrade", optional_argument, 0, 'd'}, 168 {"dist-upgrade", optional_argument, 0, 'd'},
167 {"list", no_argument, 0, 'l'}, 169 {"list", no_argument, false, 'l'},
168 {"include", required_argument, 0, 'i'}, 170 {"include", required_argument, 0, 'i'},
169 {"exclude", required_argument, 0, 'e'}, 171 {"exclude", required_argument, 0, 'e'},
170 {"critical", required_argument, 0, 'c'}, 172 {"critical", required_argument, 0, 'c'},
171 {"only-critical", no_argument, 0, 'o'}, 173 {"only-critical", no_argument, 0, 'o'},
172 {"input-file", required_argument, 0, INPUT_FILE_OPT}, 174 {"input-file", required_argument, 0, INPUT_FILE_OPT},
175 {"packages-warning", required_argument, 0, 'w'},
173 {0, 0, 0, 0} 176 {0, 0, 0, 0}
174 }; 177 };
175 178
176 while(1) { 179 while(1) {
177 c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:o", longopts, NULL); 180 c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
178 181
179 if(c == -1 || c == EOF || c == 1) break; 182 if(c == -1 || c == EOF || c == 1) break;
180 183
@@ -209,14 +212,14 @@ int process_arguments (int argc, char **argv) {
209 upgrade=NO_UPGRADE; 212 upgrade=NO_UPGRADE;
210 break; 213 break;
211 case 'u': 214 case 'u':
212 do_update=1; 215 do_update=true;
213 if(optarg!=NULL){ 216 if(optarg!=NULL){
214 update_opts=strdup(optarg); 217 update_opts=strdup(optarg);
215 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 218 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
216 } 219 }
217 break; 220 break;
218 case 'l': 221 case 'l':
219 list=1; 222 list=true;
220 break; 223 break;
221 case 'i': 224 case 'i':
222 do_include=add_to_regexp(do_include, optarg); 225 do_include=add_to_regexp(do_include, optarg);
@@ -228,11 +231,14 @@ int process_arguments (int argc, char **argv) {
228 do_critical=add_to_regexp(do_critical, optarg); 231 do_critical=add_to_regexp(do_critical, optarg);
229 break; 232 break;
230 case 'o': 233 case 'o':
231 only_critical=1; 234 only_critical=true;
232 break; 235 break;
233 case INPUT_FILE_OPT: 236 case INPUT_FILE_OPT:
234 input_filename = optarg; 237 input_filename = optarg;
235 break; 238 break;
239 case 'w':
240 packages_warning = atoi(optarg);
241 break;
236 default: 242 default:
237 /* print short usage statement if args not parsable */ 243 /* print short usage statement if args not parsable */
238 usage5(); 244 usage5();
@@ -263,7 +269,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
263 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); 269 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
264 } 270 }
265 } 271 }
266 272
267 if(do_exclude!=NULL){ 273 if(do_exclude!=NULL){
268 regres=regcomp(&ereg, do_exclude, REG_EXTENDED); 274 regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
269 if(regres!=0) { 275 if(regres!=0) {
@@ -272,7 +278,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
272 progname, rerrbuf); 278 progname, rerrbuf);
273 } 279 }
274 } 280 }
275 281
276 const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE; 282 const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE;
277 regres=regcomp(&sreg, crit_ptr, REG_EXTENDED); 283 regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
278 if(regres!=0) { 284 if(regres!=0) {
@@ -289,7 +295,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
289 /* run the upgrade */ 295 /* run the upgrade */
290 result = np_runcmd(cmdline, &chld_out, &chld_err, 0); 296 result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
291 } 297 }
292 298
293 /* apt-get upgrade only changes exit status if there is an 299 /* apt-get upgrade only changes exit status if there is an
294 * internal error when run in dry-run mode. therefore we will 300 * internal error when run in dry-run mode. therefore we will
295 * treat such an error as UNKNOWN */ 301 * treat such an error as UNKNOWN */
@@ -365,7 +371,7 @@ int run_update(void){
365 struct output chld_out, chld_err; 371 struct output chld_out, chld_err;
366 char *cmdline; 372 char *cmdline;
367 373
368 /* run the upgrade */ 374 /* run the update */
369 cmdline = construct_cmdline(NO_UPGRADE, update_opts); 375 cmdline = construct_cmdline(NO_UPGRADE, update_opts);
370 result = np_runcmd(cmdline, &chld_out, &chld_err, 0); 376 result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
371 /* apt-get update changes exit status if it can't fetch packages. 377 /* apt-get update changes exit status if it can't fetch packages.
@@ -495,16 +501,6 @@ print_help (void)
495 501
496 printf(UT_PLUG_TIMEOUT, timeout_interval); 502 printf(UT_PLUG_TIMEOUT, timeout_interval);
497 503
498 printf (" %s\n", "-U, --upgrade=OPTS");
499 printf (" %s\n", _("[Default] Perform an upgrade. If an optional OPTS argument is provided,"));
500 printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
501 printf (" %s", _("default "));
502 printf ("(%s).\n", UPGRADE_DEFAULT_OPTS);
503 printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
504 printf (" %s\n", _("the default options."));
505 printf (" %s\n", "-d, --dist-upgrade=OPTS");
506 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
507 printf (" %s\n", _("can be provided to override the default options."));
508 printf (" %s\n", "-n, --no-upgrade"); 504 printf (" %s\n", "-n, --no-upgrade");
509 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least).")); 505 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
510 printf (" %s\n", "-l, --list"); 506 printf (" %s\n", "-l, --list");
@@ -524,13 +520,16 @@ print_help (void)
524 printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified")); 520 printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
525 printf (" %s\n", _("multiple times like above. Default is a regexp matching security")); 521 printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
526 printf (" %s\n", _("upgrades for Debian and Ubuntu:")); 522 printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
527 printf (" \t\%s\n", SECURITY_RE); 523 printf (" \t%s\n", SECURITY_RE);
528 printf (" %s\n", _("Note that the package must first match the include list before its")); 524 printf (" %s\n", _("Note that the package must first match the include list before its"));
529 printf (" %s\n", _("information is compared against the critical list.")); 525 printf (" %s\n", _("information is compared against the critical list."));
530 printf (" %s\n", "-o, --only-critical"); 526 printf (" %s\n", "-o, --only-critical");
531 printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number")); 527 printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number"));
532 printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause")); 528 printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause"));
533 printf (" %s\n\n", _("the plugin to return WARNING status.")); 529 printf (" %s\n", _("the plugin to return WARNING status."));
530 printf (" %s\n", "-w, --packages-warning");
531 printf (" %s\n", _("Minimum number of packages available for upgrade to return WARNING status."));
532 printf (" %s\n\n", _("Default is 1 package."));
534 533
535 printf ("%s\n\n", _("The following options require root privileges and should be used with care:")); 534 printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
536 printf (" %s\n", "-u, --update=OPTS"); 535 printf (" %s\n", "-u, --update=OPTS");
@@ -538,6 +537,16 @@ print_help (void)
538 printf (" %s\n", _("the default options. Note: you may also need to adjust the global")); 537 printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
539 printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get")); 538 printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
540 printf (" %s\n", _("upgrade is expected to take longer than the default timeout.")); 539 printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
540 printf (" %s\n", "-U, --upgrade=OPTS");
541 printf (" %s\n", _("Perform an upgrade. If an optional OPTS argument is provided,"));
542 printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
543 printf (" %s", _("default "));
544 printf ("(%s).\n", UPGRADE_DEFAULT_OPTS);
545 printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
546 printf (" %s\n", _("the default options, which will only run a simulation and NOT perform the upgrade"));
547 printf (" %s\n", "-d, --dist-upgrade=OPTS");
548 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
549 printf (" %s\n", _("can be provided to override the default options."));
541 550
542 printf(UT_SUPPORT); 551 printf(UT_SUPPORT);
543} 552}
@@ -548,5 +557,5 @@ void
548print_usage(void) 557print_usage(void)
549{ 558{
550 printf ("%s\n", _("Usage:")); 559 printf ("%s\n", _("Usage:"));
551 printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout]\n", progname); 560 printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname);
552} 561}