1 /*****************************************************************************
2 *
3 * Nagios check_apt plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006-2008 Nagios Plugins Development Team
7 *
8 * Original author: Sean Finney
9 *
10 * Last Modified: $Date$
11 *
12 * Description:
13 *
14 * This file contains the check_apt plugin
15 *
16 * Check for available updates in apt package management systems
17 *
18 *
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 *
32 * $Id$
33 *
34 *****************************************************************************/
36 const char *progname = "check_apt";
37 const char *revision = "$Revision$";
38 const char *copyright = "2006-2008";
39 const char *email = "nagiosplug-devel@lists.sourceforge.net";
41 #include "common.h"
42 #include "runcmd.h"
43 #include "utils.h"
44 #include "regex.h"
46 /* some constants */
47 typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type;
49 /* the default opts can be overridden via the cmdline */
50 #define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq"
51 #define UPDATE_DEFAULT_OPTS "-q"
52 /* until i commit the configure.in patch which gets this, i'll define
53 * it here as well */
54 #ifndef PATH_TO_APTGET
55 # define PATH_TO_APTGET "/usr/bin/apt-get"
56 #endif /* PATH_TO_APTGET */
57 /* the RE that catches security updates */
58 #define SECURITY_RE "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
60 /* some standard functions */
61 int process_arguments(int, char **);
62 void print_help(void);
63 void print_usage(void);
65 /* construct the appropriate apt-get cmdline */
66 char* construct_cmdline(upgrade_type u, const char *opts);
67 /* run an apt-get update */
68 int run_update(void);
69 /* run an apt-get upgrade */
70 int run_upgrade(int *pkgcount, int *secpkgcount);
71 /* add another clause to a regexp */
72 char* add_to_regexp(char *expr, const char *next);
74 /* configuration variables */
75 static int verbose = 0; /* -v */
76 static int do_update = 0; /* whether to call apt-get update */
77 static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
78 static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
79 static char *update_opts = NULL; /* options to override defaults for update */
80 static char *do_include = NULL; /* regexp to only include certain packages */
81 static char *do_exclude = NULL; /* regexp to only exclude certain packages */
82 static char *do_critical = NULL; /* regexp specifying critical packages */
84 /* other global variables */
85 static int stderr_warning = 0; /* if a cmd issued output on stderr */
86 static int exec_warning = 0; /* if a cmd exited non-zero */
88 int main (int argc, char **argv) {
89 int result=STATE_UNKNOWN, packages_available=0, sec_count=0;
91 /* Parse extra opts if any */
92 argv=np_extra_opts(&argc, argv, progname);
94 if (process_arguments(argc, argv) == ERROR)
95 usage_va(_("Could not parse arguments"));
97 /* Set signal handling and alarm timeout */
98 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
99 usage_va(_("Cannot catch SIGALRM"));
100 }
102 /* handle timeouts gracefully... */
103 alarm (timeout_interval);
105 /* if they want to run apt-get update first... */
106 if(do_update) result = run_update();
108 /* apt-get upgrade */
109 result = max_state(result, run_upgrade(&packages_available, &sec_count));
111 if(sec_count > 0){
112 result = max_state(result, STATE_CRITICAL);
113 } else if(packages_available > 0){
114 result = max_state(result, STATE_WARNING);
115 } else {
116 result = max_state(result, STATE_OK);
117 }
119 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"),
120 state_text(result),
121 packages_available,
122 (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade",
123 sec_count,
124 (stderr_warning)?" warnings detected":"",
125 (stderr_warning && exec_warning)?",":"",
126 (exec_warning)?" errors detected":"",
127 (stderr_warning||exec_warning)?". run with -v for information.":""
128 );
130 return result;
131 }
133 /* process command-line arguments */
134 int process_arguments (int argc, char **argv) {
135 int c;
137 static struct option longopts[] = {
138 {"version", no_argument, 0, 'V'},
139 {"help", no_argument, 0, 'h'},
140 {"verbose", no_argument, 0, 'v'},
141 {"timeout", required_argument, 0, 't'},
142 {"update", optional_argument, 0, 'u'},
143 {"upgrade", optional_argument, 0, 'U'},
144 {"no-upgrade", no_argument, 0, 'n'},
145 {"dist-upgrade", optional_argument, 0, 'd'},
146 {"include", required_argument, 0, 'i'},
147 {"exclude", required_argument, 0, 'e'},
148 {"critical", required_argument, 0, 'c'},
149 {0, 0, 0, 0}
150 };
152 while(1) {
153 c = getopt_long(argc, argv, "hVvt:u::U::d::ni:e:c:", longopts, NULL);
155 if(c == -1 || c == EOF || c == 1) break;
157 switch(c) {
158 case 'h':
159 print_help();
160 exit(STATE_OK);
161 case 'V':
162 print_revision(progname, revision);
163 exit(STATE_OK);
164 case 'v':
165 verbose++;
166 break;
167 case 't':
168 timeout_interval=atoi(optarg);
169 break;
170 case 'd':
171 upgrade=DIST_UPGRADE;
172 if(optarg!=NULL){
173 upgrade_opts=strdup(optarg);
174 if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
175 }
176 break;
177 case 'U':
178 upgrade=UPGRADE;
179 if(optarg!=NULL){
180 upgrade_opts=strdup(optarg);
181 if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
182 }
183 break;
184 case 'n':
185 upgrade=NO_UPGRADE;
186 break;
187 case 'u':
188 do_update=1;
189 if(optarg!=NULL){
190 update_opts=strdup(optarg);
191 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
192 }
193 break;
194 case 'i':
195 do_include=add_to_regexp(do_include, optarg);
196 break;
197 case 'e':
198 do_exclude=add_to_regexp(do_exclude, optarg);
199 break;
200 case 'c':
201 do_critical=add_to_regexp(do_critical, optarg);
202 break;
203 default:
204 /* print short usage statement if args not parsable */
205 usage5();
206 }
207 }
209 return OK;
210 }
213 /* run an apt-get upgrade */
214 int run_upgrade(int *pkgcount, int *secpkgcount){
215 int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
216 struct output chld_out, chld_err;
217 regex_t ireg, ereg, sreg;
218 char *cmdline=NULL, rerrbuf[64];
219 const char *include_ptr=NULL, *crit_ptr=NULL;
221 if(upgrade==NO_UPGRADE) return STATE_OK;
223 /* compile the regexps */
224 if(do_include!=NULL) include_ptr=do_include;
225 else include_ptr="^Inst";
226 if(do_critical!=NULL) crit_ptr=do_critical;
227 else crit_ptr=SECURITY_RE;
229 regres=regcomp(&ireg, include_ptr, REG_EXTENDED);
230 if(regres!=0) {
231 regerror(regres, &ireg, rerrbuf, 64);
232 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
233 }
235 if(do_exclude!=NULL){
236 regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
237 if(regres!=0) {
238 regerror(regres, &ereg, rerrbuf, 64);
239 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
240 progname, rerrbuf);
241 }
242 }
243 regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
244 if(regres!=0) {
245 regerror(regres, &ereg, rerrbuf, 64);
246 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
247 progname, rerrbuf);
248 }
250 cmdline=construct_cmdline(upgrade, upgrade_opts);
251 /* run the upgrade */
252 result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
253 /* apt-get upgrade only changes exit status if there is an
254 * internal error when run in dry-run mode. therefore we will
255 * treat such an error as UNKNOWN */
256 if(result != 0){
257 exec_warning=1;
258 result = STATE_UNKNOWN;
259 fprintf(stderr, _("'%s' exited with non-zero status.\n"),
260 cmdline);
261 }
263 /* parse the output, which should only consist of lines like
264 *
265 * Inst package ....
266 * Conf package ....
267 *
268 * so we'll filter based on "Inst" for the time being. later
269 * we may need to switch to the --print-uris output format,
270 * in which case the logic here will slightly change.
271 */
272 for(i = 0; i < chld_out.lines; i++) {
273 if(verbose){
274 printf("%s\n", chld_out.line[i]);
275 }
276 /* if it is a package we care about */
277 if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){
278 /* if we're not excluding, or it's not in the
279 * list of stuff to exclude */
280 if(do_exclude==NULL ||
281 regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
282 pc++;
283 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
284 spc++;
285 if(verbose) printf("*");
286 }
287 if(verbose){
288 printf("*%s\n", chld_out.line[i]);
289 }
290 }
291 }
292 }
293 *pkgcount=pc;
294 *secpkgcount=spc;
296 /* If we get anything on stderr, at least set warning */
297 if(chld_err.buflen){
298 stderr_warning=1;
299 result = max_state(result, STATE_WARNING);
300 if(verbose){
301 for(i = 0; i < chld_err.lines; i++) {
302 fprintf(stderr, "%s\n", chld_err.line[i]);
303 }
304 }
305 }
306 regfree(&ireg);
307 regfree(&sreg);
308 if(do_exclude!=NULL) regfree(&ereg);
309 free(cmdline);
310 return result;
311 }
313 /* run an apt-get update (needs root) */
314 int run_update(void){
315 int i=0, result=STATE_UNKNOWN;
316 struct output chld_out, chld_err;
317 char *cmdline;
319 /* run the upgrade */
320 cmdline = construct_cmdline(NO_UPGRADE, update_opts);
321 result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
322 /* apt-get update changes exit status if it can't fetch packages.
323 * since we were explicitly asked to do so, this is treated as
324 * a critical error. */
325 if(result != 0){
326 exec_warning=1;
327 result = STATE_CRITICAL;
328 fprintf(stderr, _("'%s' exited with non-zero status.\n"),
329 cmdline);
330 }
332 if(verbose){
333 for(i = 0; i < chld_out.lines; i++) {
334 printf("%s\n", chld_out.line[i]);
335 }
336 }
338 /* If we get anything on stderr, at least set warning */
339 if(chld_err.buflen){
340 stderr_warning=1;
341 result = max_state(result, STATE_WARNING);
342 if(verbose){
343 for(i = 0; i < chld_err.lines; i++) {
344 fprintf(stderr, "%s\n", chld_err.line[i]);
345 }
346 }
347 }
348 free(cmdline);
349 return result;
350 }
352 char* add_to_regexp(char *expr, const char *next){
353 char *re=NULL;
355 if(expr==NULL){
356 re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
357 if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
358 sprintf(re, "^Inst (%s) ", next);
359 } else {
360 /* resize it, adding an extra char for the new '|' separator */
361 re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1);
362 if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
363 /* append it starting at ')' in the old re */
364 sprintf((char*)(re+strlen(re)-2), "|%s) ", next);
365 }
367 return re;
368 }
370 char* construct_cmdline(upgrade_type u, const char *opts){
371 int len=0;
372 const char *opts_ptr=NULL, *aptcmd=NULL;
373 char *cmd=NULL;
375 switch(u){
376 case UPGRADE:
377 if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
378 else opts_ptr=opts;
379 aptcmd="upgrade";
380 break;
381 case DIST_UPGRADE:
382 if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
383 else opts_ptr=opts;
384 aptcmd="dist-upgrade";
385 break;
386 case NO_UPGRADE:
387 if(opts==NULL) opts_ptr=UPDATE_DEFAULT_OPTS;
388 else opts_ptr=opts;
389 aptcmd="update";
390 break;
391 }
393 len+=strlen(PATH_TO_APTGET)+1; /* "/usr/bin/apt-get " */
394 len+=strlen(opts_ptr)+1; /* "opts " */
395 len+=strlen(aptcmd)+1; /* "upgrade\0" */
397 cmd=(char*)malloc(sizeof(char)*len);
398 if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed");
399 sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
400 return cmd;
401 }
403 /* informative help message */
404 void
405 print_help (void)
406 {
407 print_revision(progname, revision);
409 printf(_(COPYRIGHT), copyright, email);
411 printf("%s\n", _("This plugin checks for software updates on systems that use"));
412 printf("%s\n", _("package management systems based on the apt-get(8) command"));
413 printf("%s\n", _("found in Debian GNU/Linux"));
415 printf ("\n\n");
417 print_usage();
419 printf(_(UT_HELP_VRSN));
420 printf(_(UT_EXTRA_OPTS));
422 printf(_(UT_TIMEOUT), timeout_interval);
424 printf (" %s\n", "-U, --upgrade=OPTS");
425 printf (" %s\n", _("[Default] Perform an upgrade. If an optional OPTS argument is provided,"));
426 printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
427 printf (" %s", _("default "));
428 printf ("(%s).\n", UPGRADE_DEFAULT_OPTS);
429 printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
430 printf (" %s\n", _("the default options."));
431 printf (" %s\n", "-d, --dist-upgrade=OPTS");
432 printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
433 printf (" %s\n", _("can be provided to override the default options."));
434 printf (" %s\n", " -n, --no-upgrade");
435 printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
436 printf (" %s\n", "-i, --include=REGEXP");
437 printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
438 printf (" %s\n", _("the values will be combined together. Any patches matching this list"));
439 printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
440 printf (" %s\n", _("Default is to include all packages."));
441 printf (" %s\n", "-e, --exclude=REGEXP");
442 printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
443 printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
444 printf (" %s\n", _("will be combined together. Default is to exclude no packages."));
445 printf (" %s\n", "-c, --critical=REGEXP");
446 printf (" %s\n", _("If the full package information of any of the upgradable packages match"));
447 printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
448 printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
449 printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
450 printf (" \t\%s\n", SECURITY_RE);
451 printf (" %s\n", _("Note that the package must first match the include list before its"));
452 printf (" %s\n\n", _("information is compared against the critical list."));
454 printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
455 printf (" %s\n", "-u, --update=OPTS");
456 printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
457 printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
458 printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
459 printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
461 #ifdef NP_EXTRA_OPTS
462 printf("\n");
463 printf("%s\n", _("Notes:"));
464 printf(_(UT_EXTRA_OPTS_NOTES));
465 #endif
467 printf(_(UT_SUPPORT));
468 }
471 /* simple usage heading */
472 void
473 print_usage(void)
474 {
475 printf (_("Usage:"));
476 printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname);
477 }