1 /******************************************************************************
3 * Nagios check_apt plugin
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Original author: sean finney
10 * Last Modified: $Date$
14 * This file contains the check_apt plugin
16 * check for available updates in apt package management systems
18 * License Information:
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 ******************************************************************************/
38 const char *progname
= "check_apt";
39 const char *revision
= "$Revision$";
40 const char *copyright
= "2006";
41 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
49 typedef enum { UPGRADE
, DIST_UPGRADE
, NO_UPGRADE
} upgrade_type
;
51 /* the default opts can be overridden via the cmdline */
52 #define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq"
53 #define UPDATE_DEFAULT_OPTS "-q"
54 /* until i commit the configure.in patch which gets this, i'll define
56 #ifndef PATH_TO_APTGET
57 # define PATH_TO_APTGET "/usr/bin/apt-get"
58 #endif /* PATH_TO_APTGET */
59 /* the RE that catches security updates */
60 #define SECURITY_RE "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
62 /* some standard functions */
63 int process_arguments(int, char **);
64 void print_help(void);
65 void print_usage(void);
67 /* construct the appropriate apt-get cmdline */
68 char* construct_cmdline(upgrade_type u
, const char *opts
);
69 /* run an apt-get update */
71 /* run an apt-get upgrade */
72 int run_upgrade(int *pkgcount
, int *secpkgcount
);
73 /* add another clause to a regexp */
74 char* add_to_regexp(char *expr
, const char *next
);
76 /* configuration variables */
77 static int verbose
= 0; /* -v */
78 static int do_update
= 0; /* whether to call apt-get update */
79 static upgrade_type upgrade
= UPGRADE
; /* which type of upgrade to do */
80 static char *upgrade_opts
= NULL
; /* options to override defaults for upgrade */
81 static char *update_opts
= NULL
; /* options to override defaults for update */
82 static char *do_include
= NULL
; /* regexp to only include certain packages */
83 static char *do_exclude
= NULL
; /* regexp to only exclude certain packages */
84 static char *do_critical
= NULL
; /* regexp specifying critical packages */
86 /* other global variables */
87 static int stderr_warning
= 0; /* if a cmd issued output on stderr */
88 static int exec_warning
= 0; /* if a cmd exited non-zero */
90 int main (int argc
, char **argv
) {
91 int result
=STATE_UNKNOWN
, packages_available
=0, sec_count
=0;
93 if (process_arguments(argc
, argv
) == ERROR
)
94 usage_va(_("Could not parse arguments"));
96 /* Set signal handling and alarm timeout */
97 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
) {
98 usage_va(_("Cannot catch SIGALRM"));
101 /* handle timeouts gracefully... */
102 alarm (timeout_interval
);
104 /* if they want to run apt-get update first... */
105 if(do_update
) result
= run_update();
107 /* apt-get upgrade */
108 result
= max_state(result
, run_upgrade(&packages_available
, &sec_count
));
111 result
= max_state(result
, STATE_CRITICAL
);
112 } else if(packages_available
> 0){
113 result
= max_state(result
, STATE_WARNING
);
115 result
= max_state(result
, STATE_OK
);
118 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"),
121 (upgrade
==DIST_UPGRADE
)?"dist-upgrade":"upgrade",
123 (stderr_warning
)?" warnings detected":"",
124 (stderr_warning
&& exec_warning
)?",":"",
125 (exec_warning
)?" errors detected":"",
126 (stderr_warning
||exec_warning
)?". run with -v for information.":""
132 /* process command-line arguments */
133 int process_arguments (int argc
, char **argv
) {
136 static struct option longopts
[] = {
137 {"version", no_argument
, 0, 'V'},
138 {"help", no_argument
, 0, 'h'},
139 {"verbose", no_argument
, 0, 'v'},
140 {"timeout", required_argument
, 0, 't'},
141 {"update", optional_argument
, 0, 'u'},
142 {"upgrade", optional_argument
, 0, 'U'},
143 {"no-upgrade", no_argument
, 0, 'n'},
144 {"dist-upgrade", optional_argument
, 0, 'd'},
145 {"include", required_argument
, 0, 'i'},
146 {"exclude", required_argument
, 0, 'e'},
147 {"critical", required_argument
, 0, 'c'},
152 c
= getopt_long(argc
, argv
, "hVvt:u::U::d::ni:e:c:", longopts
, NULL
);
154 if(c
== -1 || c
== EOF
|| c
== 1) break;
161 print_revision(progname
, revision
);
167 timeout_interval
=atoi(optarg
);
170 upgrade
=DIST_UPGRADE
;
172 upgrade_opts
=strdup(optarg
);
173 if(upgrade_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
179 upgrade_opts
=strdup(optarg
);
180 if(upgrade_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
189 update_opts
=strdup(optarg
);
190 if(update_opts
==NULL
) die(STATE_UNKNOWN
, "strdup failed");
194 do_include
=add_to_regexp(do_include
, optarg
);
197 do_exclude
=add_to_regexp(do_exclude
, optarg
);
200 do_critical
=add_to_regexp(do_critical
, optarg
);
203 /* print short usage statement if args not parsable */
212 /* run an apt-get upgrade */
213 int run_upgrade(int *pkgcount
, int *secpkgcount
){
214 int i
=0, result
=STATE_UNKNOWN
, regres
=0, pc
=0, spc
=0;
215 struct output chld_out
, chld_err
;
216 regex_t ireg
, ereg
, sreg
;
217 char *cmdline
=NULL
, rerrbuf
[64];
218 const char *include_ptr
=NULL
, *crit_ptr
=NULL
;
220 if(upgrade
==NO_UPGRADE
) return STATE_OK
;
222 /* compile the regexps */
223 if(do_include
!=NULL
) include_ptr
=do_include
;
224 else include_ptr
="^Inst";
225 if(do_critical
!=NULL
) crit_ptr
=do_critical
;
226 else crit_ptr
=SECURITY_RE
;
228 regres
=regcomp(&ireg
, include_ptr
, REG_EXTENDED
);
230 regerror(regres
, &ireg
, rerrbuf
, 64);
231 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"), progname
, rerrbuf
);
234 if(do_exclude
!=NULL
){
235 regres
=regcomp(&ereg
, do_exclude
, REG_EXTENDED
);
237 regerror(regres
, &ereg
, rerrbuf
, 64);
238 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"),
242 regres
=regcomp(&sreg
, crit_ptr
, REG_EXTENDED
);
244 regerror(regres
, &ereg
, rerrbuf
, 64);
245 die(STATE_UNKNOWN
, _("%s: Error compiling regexp: %s"),
249 cmdline
=construct_cmdline(upgrade
, upgrade_opts
);
250 /* run the upgrade */
251 result
= np_runcmd(cmdline
, &chld_out
, &chld_err
, 0);
252 /* apt-get upgrade only changes exit status if there is an
253 * internal error when run in dry-run mode. therefore we will
254 * treat such an error as UNKNOWN */
257 result
= STATE_UNKNOWN
;
258 fprintf(stderr
, _("'%s' exited with non-zero status.\n"),
262 /* parse the output, which should only consist of lines like
267 * so we'll filter based on "Inst" for the time being. later
268 * we may need to switch to the --print-uris output format,
269 * in which case the logic here will slightly change.
271 for(i
= 0; i
< chld_out
.lines
; i
++) {
273 printf("%s\n", chld_out
.line
[i
]);
275 /* if it is a package we care about */
276 if(regexec(&ireg
, chld_out
.line
[i
], 0, NULL
, 0)==0){
277 /* if we're not excluding, or it's not in the
278 * list of stuff to exclude */
279 if(do_exclude
==NULL
||
280 regexec(&ereg
, chld_out
.line
[i
], 0, NULL
, 0)!=0){
282 if(regexec(&sreg
, chld_out
.line
[i
], 0, NULL
, 0)==0){
284 if(verbose
) printf("*");
287 printf("*%s\n", chld_out
.line
[i
]);
295 /* If we get anything on stderr, at least set warning */
298 result
= max_state(result
, STATE_WARNING
);
300 for(i
= 0; i
< chld_err
.lines
; i
++) {
301 fprintf(stderr
, "%s\n", chld_err
.line
[i
]);
307 if(do_exclude
!=NULL
) regfree(&ereg
);
312 /* run an apt-get update (needs root) */
313 int run_update(void){
314 int i
=0, result
=STATE_UNKNOWN
;
315 struct output chld_out
, chld_err
;
318 /* run the upgrade */
319 cmdline
= construct_cmdline(NO_UPGRADE
, update_opts
);
320 result
= np_runcmd(cmdline
, &chld_out
, &chld_err
, 0);
321 /* apt-get update changes exit status if it can't fetch packages.
322 * since we were explicitly asked to do so, this is treated as
323 * a critical error. */
326 result
= STATE_CRITICAL
;
327 fprintf(stderr
, _("'%s' exited with non-zero status.\n"),
332 for(i
= 0; i
< chld_out
.lines
; i
++) {
333 printf("%s\n", chld_out
.line
[i
]);
337 /* If we get anything on stderr, at least set warning */
340 result
= max_state(result
, STATE_WARNING
);
342 for(i
= 0; i
< chld_err
.lines
; i
++) {
343 fprintf(stderr
, "%s\n", chld_err
.line
[i
]);
351 char* add_to_regexp(char *expr
, const char *next
){
355 re
=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next
)+1));
356 if(!re
) die(STATE_UNKNOWN
, "malloc failed!\n");
357 sprintf(re
, "^Inst (%s) ", next
);
359 /* resize it, adding an extra char for the new '|' separator */
360 re
=realloc(expr
, sizeof(char)*strlen(expr
)+1+strlen(next
)+1);
361 if(!re
) die(STATE_UNKNOWN
, "realloc failed!\n");
362 /* append it starting at ')' in the old re */
363 sprintf((char*)(re
+strlen(re
)-2), "|%s) ", next
);
369 char* construct_cmdline(upgrade_type u
, const char *opts
){
371 const char *opts_ptr
=NULL
, *aptcmd
=NULL
;
376 if(opts
==NULL
) opts_ptr
=UPGRADE_DEFAULT_OPTS
;
381 if(opts
==NULL
) opts_ptr
=UPGRADE_DEFAULT_OPTS
;
383 aptcmd
="dist-upgrade";
386 if(opts
==NULL
) opts_ptr
=UPDATE_DEFAULT_OPTS
;
392 len
+=strlen(PATH_TO_APTGET
)+1; /* "/usr/bin/apt-get " */
393 len
+=strlen(opts_ptr
)+1; /* "opts " */
394 len
+=strlen(aptcmd
)+1; /* "upgrade\0" */
396 cmd
=(char*)malloc(sizeof(char)*len
);
397 if(cmd
==NULL
) die(STATE_UNKNOWN
, "malloc failed");
398 sprintf(cmd
, "%s %s %s", PATH_TO_APTGET
, opts_ptr
, aptcmd
);
402 /* informative help message */
406 print_revision(progname
, revision
);
408 printf(_(COPYRIGHT
), copyright
, email
);
410 printf("%s\n", _("This plugin checks for software updates on systems that use"));
411 printf("%s\n", _("package management systems based on the apt-get(8) command"));
412 printf("%s\n", _("found in Debian GNU/Linux"));
418 printf(_(UT_HELP_VRSN
));
420 printf(_(UT_TIMEOUT
), timeout_interval
);
422 printf (" %s\n", "-U, --upgrade=OPTS");
423 printf (" %s\n", _("[Default] Perform an upgrade. If an optional OPTS argument is provided,"));
424 printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
425 printf (" %s", _("default "));
426 printf ("(%s).\n", UPGRADE_DEFAULT_OPTS
);
427 printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
428 printf (" %s\n", _("the default options."));
429 printf (" %s\n", "-d, --dist-upgrade=OPTS");
430 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
431 printf (" %s\n", _("can be provided to override the default options."));
432 printf (" %s\n", " -n, --no-upgrade");
433 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
434 printf (" %s\n", "-i, --include=REGEXP");
435 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
436 printf (" %s\n", _("the values will be combined together. Any patches matching this list"));
437 printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
438 printf (" %s\n", _("Default is to include all packages."));
439 printf (" %s\n", "-e, --exclude=REGEXP");
440 printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
441 printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
442 printf (" %s\n", _("will be combined together. Default is to exclude no packages."));
443 printf (" %s\n", "-c, --critical=REGEXP");
444 printf (" %s\n", _("If the full package information of any of the upgradable packages match"));
445 printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
446 printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
447 printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
448 printf (" \t\%s\n", SECURITY_RE
);
449 printf (" %s\n", _("Note that the package must first match the include list before its"));
450 printf (" %s\n\n\n", _("information is compared against the critical list."));
452 printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
453 printf (" %s\n", "-u, --update=OPTS");
454 printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
455 printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
456 printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
457 printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
461 /* simple usage heading */
465 printf (_("Usage:"));
466 printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname
);