1 /*****************************************************************************
3 * Nagios check_apt plugin
6 * Copyright (c) 2006-2008 Nagios Plugins Development Team
8 * Original author: Sean Finney
12 * This file contains the check_apt plugin
14 * Check for available updates in apt package management systems
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
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
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/>.
30 *****************************************************************************/
32 const char *progname
= "check_apt";
33 const char *copyright
= "2006-2008";
34 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
42 typedef enum { UPGRADE
, DIST_UPGRADE
, NO_UPGRADE
} upgrade_type
;
44 /* the default opts can be overridden via the cmdline */
45 #define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq"
46 #define UPDATE_DEFAULT_OPTS "-q"
47 /* until i commit the configure.in patch which gets this, i'll define
49 #ifndef PATH_TO_APTGET
50 # define PATH_TO_APTGET "/usr/bin/apt-get"
51 #endif /* PATH_TO_APTGET */
52 /* the RE that catches security updates */
53 #define SECURITY_RE "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
55 /* some standard functions */
56 int process_arguments(int, char **);
57 void print_help(void);
58 void print_usage(void);
60 /* construct the appropriate apt-get cmdline */
61 char* construct_cmdline(upgrade_type u
, const char *opts
);
62 /* run an apt-get update */
64 /* run an apt-get upgrade */
65 int run_upgrade(int *pkgcount
, int *secpkgcount
);
66 /* add another clause to a regexp */
67 char* add_to_regexp(char *expr
, const char *next
);
69 /* configuration variables */
70 static int verbose
= 0; /* -v */
71 static int do_update
= 0; /* whether to call apt-get update */
72 static upgrade_type upgrade
= UPGRADE
; /* which type of upgrade to do */
73 static char *upgrade_opts
= NULL
; /* options to override defaults for upgrade */
74 static char *update_opts
= NULL
; /* options to override defaults for update */
75 static char *do_include
= NULL
; /* regexp to only include certain packages */
76 static char *do_exclude
= NULL
; /* regexp to only exclude certain packages */
77 static char *do_critical
= NULL
; /* regexp specifying critical packages */
79 /* other global variables */
80 static int stderr_warning
= 0; /* if a cmd issued output on stderr */
81 static int exec_warning
= 0; /* if a cmd exited non-zero */
83 int main (int argc
, char **argv
) {
84 int result
=STATE_UNKNOWN
, packages_available
=0, sec_count
=0;
86 /* Parse extra opts if any */
87 argv
=np_extra_opts(&argc
, argv
, progname
);
89 if (process_arguments(argc
, argv
) == ERROR
)
90 usage_va(_("Could not parse arguments"));
92 /* Set signal handling and alarm timeout */
93 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
) {
94 usage_va(_("Cannot catch SIGALRM"));
97 /* handle timeouts gracefully... */
98 alarm (timeout_interval
);
100 /* if they want to run apt-get update first... */
101 if(do_update
) result
= run_update();
103 /* apt-get upgrade */
104 result
= max_state(result
, run_upgrade(&packages_available
, &sec_count
));
107 result
= max_state(result
, STATE_CRITICAL
);
108 } else if(packages_available
> 0){
109 result
= max_state(result
, STATE_WARNING
);
111 result
= max_state(result
, STATE_OK
);
114 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"),
117 (upgrade
==DIST_UPGRADE
)?"dist-upgrade":"upgrade",
119 (stderr_warning
)?" warnings detected":"",
120 (stderr_warning
&& exec_warning
)?",":"",
121 (exec_warning
)?" errors detected":"",
122 (stderr_warning
||exec_warning
)?". run with -v for information.":""
128 /* process command-line arguments */
129 int process_arguments (int argc
, char **argv
) {
132 static struct option longopts
[] = {
133 {"version", no_argument
, 0, 'V'},
134 {"help", no_argument
, 0, 'h'},
135 {"verbose", no_argument
, 0, 'v'},
136 {"timeout", required_argument
, 0, 't'},
137 {"update", optional_argument
, 0, 'u'},
138 {"upgrade", optional_argument
, 0, 'U'},
139 {"no-upgrade", no_argument
, 0, 'n'},
140 {"dist-upgrade", optional_argument
, 0, 'd'},
141 {"include", required_argument
, 0, 'i'},
142 {"exclude", required_argument
, 0, 'e'},
143 {"critical", required_argument
, 0, 'c'},
148 c
= getopt_long(argc
, argv
, "hVvt:u::U::d::ni:e:c:", longopts
, NULL
);
150 if(c
== -1 || c
== EOF
|| c
== 1) break;
157 print_revision(progname
, NP_VERSION
);
163 timeout_interval
=atoi(optarg
);
166 upgrade
=DIST_UPGRADE
;
168 upgrade_opts
=strdup(optarg
);
169 if(upgrade_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
175 upgrade_opts
=strdup(optarg
);
176 if(upgrade_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
185 update_opts
=strdup(optarg
);
186 if(update_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
190 do_include
=add_to_regexp(do_include
, optarg
);
193 do_exclude
=add_to_regexp(do_exclude
, optarg
);
196 do_critical
=add_to_regexp(do_critical
, optarg
);
199 /* print short usage statement if args not parsable */
208 /* run an apt-get upgrade */
209 int run_upgrade(int *pkgcount
, int *secpkgcount
){
210 int i
=0, result
=STATE_UNKNOWN
, regres
=0, pc
=0, spc
=0;
211 struct output chld_out
, chld_err
;
212 regex_t ireg
, ereg
, sreg
;
213 char *cmdline
=NULL
, rerrbuf
[64];
214 const char *include_ptr
=NULL
, *crit_ptr
=NULL
;
216 if(upgrade
==NO_UPGRADE
) return STATE_OK
;
218 /* compile the regexps */
219 if(do_include
!=NULL
) include_ptr
=do_include
;
220 else include_ptr
="^Inst";
221 if(do_critical
!=NULL
) crit_ptr
=do_critical
;
222 else crit_ptr
=SECURITY_RE
;
224 regres
=regcomp(&ireg
, include_ptr
, REG_EXTENDED
);
226 regerror(regres
, &ireg
, rerrbuf
, 64);
227 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"), progname
, rerrbuf
);
230 if(do_exclude
!=NULL
){
231 regres
=regcomp(&ereg
, do_exclude
, REG_EXTENDED
);
233 regerror(regres
, &ereg
, rerrbuf
, 64);
234 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"),
238 regres
=regcomp(&sreg
, crit_ptr
, REG_EXTENDED
);
240 regerror(regres
, &ereg
, rerrbuf
, 64);
241 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"),
245 cmdline
=construct_cmdline(upgrade
, upgrade_opts
);
246 /* run the upgrade */
247 result
= np_runcmd(cmdline
, &chld_out
, &chld_err
, 0);
248 /* apt-get upgrade only changes exit status if there is an
249 * internal error when run in dry-run mode. therefore we will
250 * treat such an error as UNKNOWN */
253 result
= STATE_UNKNOWN
;
254 fprintf(stderr
, _("'%s' exited with non-zero status.\n"),
258 /* parse the output, which should only consist of lines like
263 * so we'll filter based on "Inst" for the time being. later
264 * we may need to switch to the --print-uris output format,
265 * in which case the logic here will slightly change.
267 for(i
= 0; i
< chld_out
.lines
; i
++) {
269 printf("%s\n", chld_out
.line
[i
]);
271 /* if it is a package we care about */
272 if(regexec(&ireg
, chld_out
.line
[i
], 0, NULL
, 0)==0){
273 /* if we're not excluding, or it's not in the
274 * list of stuff to exclude */
275 if(do_exclude
==NULL
||
276 regexec(&ereg
, chld_out
.line
[i
], 0, NULL
, 0)!=0){
278 if(regexec(&sreg
, chld_out
.line
[i
], 0, NULL
, 0)==0){
280 if(verbose
) printf("*");
283 printf("*%s\n", chld_out
.line
[i
]);
291 /* If we get anything on stderr, at least set warning */
294 result
= max_state(result
, STATE_WARNING
);
296 for(i
= 0; i
< chld_err
.lines
; i
++) {
297 fprintf(stderr
, "%s\n", chld_err
.line
[i
]);
303 if(do_exclude
!=NULL
) regfree(&ereg
);
308 /* run an apt-get update (needs root) */
309 int run_update(void){
310 int i
=0, result
=STATE_UNKNOWN
;
311 struct output chld_out
, chld_err
;
314 /* run the upgrade */
315 cmdline
= construct_cmdline(NO_UPGRADE
, update_opts
);
316 result
= np_runcmd(cmdline
, &chld_out
, &chld_err
, 0);
317 /* apt-get update changes exit status if it can't fetch packages.
318 * since we were explicitly asked to do so, this is treated as
319 * a critical error. */
322 result
= STATE_CRITICAL
;
323 fprintf(stderr
, _("'%s' exited with non-zero status.\n"),
328 for(i
= 0; i
< chld_out
.lines
; i
++) {
329 printf("%s\n", chld_out
.line
[i
]);
333 /* If we get anything on stderr, at least set warning */
336 result
= max_state(result
, STATE_WARNING
);
338 for(i
= 0; i
< chld_err
.lines
; i
++) {
339 fprintf(stderr
, "%s\n", chld_err
.line
[i
]);
347 char* add_to_regexp(char *expr
, const char *next
){
351 re
=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next
)+1));
352 if(!re
) die(STATE_UNKNOWN
, "malloc failed!\n");
353 sprintf(re
, "^Inst (%s) ", next
);
355 /* resize it, adding an extra char for the new '|' separator */
356 re
=realloc(expr
, sizeof(char)*strlen(expr
)+1+strlen(next
)+1);
357 if(!re
) die(STATE_UNKNOWN
, "realloc failed!\n");
358 /* append it starting at ')' in the old re */
359 sprintf((char*)(re
+strlen(re
)-2), "|%s) ", next
);
365 char* construct_cmdline(upgrade_type u
, const char *opts
){
367 const char *opts_ptr
=NULL
, *aptcmd
=NULL
;
372 if(opts
==NULL
) opts_ptr
=UPGRADE_DEFAULT_OPTS
;
377 if(opts
==NULL
) opts_ptr
=UPGRADE_DEFAULT_OPTS
;
379 aptcmd
="dist-upgrade";
382 if(opts
==NULL
) opts_ptr
=UPDATE_DEFAULT_OPTS
;
388 len
+=strlen(PATH_TO_APTGET
)+1; /* "/usr/bin/apt-get " */
389 len
+=strlen(opts_ptr
)+1; /* "opts " */
390 len
+=strlen(aptcmd
)+1; /* "upgrade\0" */
392 cmd
=(char*)malloc(sizeof(char)*len
);
393 if(cmd
==NULL
) die(STATE_UNKNOWN
, "malloc failed");
394 sprintf(cmd
, "%s %s %s", PATH_TO_APTGET
, opts_ptr
, aptcmd
);
398 /* informative help message */
402 print_revision(progname
, NP_VERSION
);
404 printf(_(COPYRIGHT
), copyright
, email
);
406 printf("%s\n", _("This plugin checks for software updates on systems that use"));
407 printf("%s\n", _("package management systems based on the apt-get(8) command"));
408 printf("%s\n", _("found in Debian GNU/Linux"));
414 printf(_(UT_HELP_VRSN
));
415 printf(_(UT_EXTRA_OPTS
));
417 printf(_(UT_TIMEOUT
), timeout_interval
);
419 printf (" %s\n", "-U, --upgrade=OPTS");
420 printf (" %s\n", _("[Default] Perform an upgrade. If an optional OPTS argument is provided,"));
421 printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
422 printf (" %s", _("default "));
423 printf ("(%s).\n", UPGRADE_DEFAULT_OPTS
);
424 printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
425 printf (" %s\n", _("the default options."));
426 printf (" %s\n", "-d, --dist-upgrade=OPTS");
427 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
428 printf (" %s\n", _("can be provided to override the default options."));
429 printf (" %s\n", " -n, --no-upgrade");
430 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
431 printf (" %s\n", "-i, --include=REGEXP");
432 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
433 printf (" %s\n", _("the values will be combined together. Any patches matching this list"));
434 printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
435 printf (" %s\n", _("Default is to include all packages."));
436 printf (" %s\n", "-e, --exclude=REGEXP");
437 printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
438 printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
439 printf (" %s\n", _("will be combined together. Default is to exclude no packages."));
440 printf (" %s\n", "-c, --critical=REGEXP");
441 printf (" %s\n", _("If the full package information of any of the upgradable packages match"));
442 printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
443 printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
444 printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
445 printf (" \t\%s\n", SECURITY_RE
);
446 printf (" %s\n", _("Note that the package must first match the include list before its"));
447 printf (" %s\n\n", _("information is compared against the critical list."));
449 printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
450 printf (" %s\n", "-u, --update=OPTS");
451 printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
452 printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
453 printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
454 printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
458 printf("%s\n", _("Notes:"));
459 printf(_(UT_EXTRA_OPTS_NOTES
));
462 printf(_(UT_SUPPORT
));
466 /* simple usage heading */
470 printf (_("Usage:"));
471 printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname
);