1 /******************************************************************************
2 * check_apt.c: check for available updates in apt package management systems
3 * original author: sean finney <seanius@seanius.net>
4 * (with some common bits stolen from check_nagios.c)
5 ******************************************************************************
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 $Id$
23 ******************************************************************************/
25 const char *progname = "check_apt";
26 const char *revision = "$Revision$";
27 const char *copyright = "2006";
28 const char *email = "nagiosplug-devel@lists.sourceforge.net";
30 #include "common.h"
31 #include "runcmd.h"
32 #include "utils.h"
33 #include <regex.h>
35 /* for now define the various apt calls as constants. this may need
36 * to change later. */
37 #define APTGET_UPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq upgrade"
38 #define APTGET_DISTUPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq dist-upgrade"
39 #define APTGET_UPDATE "/usr/bin/apt-get -q update"
41 /* some standard functions */
42 int process_arguments(int, char **);
43 void print_help(void);
44 void print_usage(void);
46 /* run an apt-get update */
47 int run_update(void);
48 /* run an apt-get upgrade */
49 int run_upgrade(int *pkgcount);
50 /* add another clause to a regexp */
51 char* add_to_regexp(char *expr, const char *next);
53 /* configuration variables */
54 static int verbose = 0; /* -v */
55 static int do_update = 0; /* whether to call apt-get update */
56 static int dist_upgrade = 0; /* whether to call apt-get dist-upgrade */
57 static char* do_include = NULL; /* regexp to only include certain packages */
58 static char* do_exclude = NULL; /* regexp to only exclude certain packages */
60 /* other global variables */
61 static int stderr_warning = 0; /* if a cmd issued output on stderr */
62 static int exec_warning = 0; /* if a cmd exited non-zero */
64 int main (int argc, char **argv) {
65 int result=STATE_UNKNOWN, packages_available=0;
67 if (process_arguments(argc, argv) == ERROR)
68 usage_va(_("Could not parse arguments"));
70 /* Set signal handling and alarm timeout */
71 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
72 usage_va(_("Cannot catch SIGALRM"));
73 }
75 /* handle timeouts gracefully... */
76 alarm (timeout_interval);
78 /* if they want to run apt-get update first... */
79 if(do_update) result = run_update();
81 /* apt-get upgrade */
82 result = max_state(result, run_upgrade(&packages_available));
84 if(packages_available > 0){
85 result = max_state(result, STATE_WARNING);
86 } else {
87 result = max_state(result, STATE_OK);
88 }
90 printf("APT %s: %d packages available for %s.%s%s%s%s\n",
91 state_text(result),
92 packages_available,
93 (dist_upgrade)?"dist-upgrade":"upgrade",
94 (stderr_warning)?" warnings detected":"",
95 (stderr_warning && exec_warning)?",":"",
96 (exec_warning)?" errors detected":"",
97 (stderr_warning||exec_warning)?". run with -v for information.":""
98 );
100 return result;
101 }
103 /* process command-line arguments */
104 int process_arguments (int argc, char **argv) {
105 int c;
107 static struct option longopts[] = {
108 {"version", no_argument, 0, 'V'},
109 {"help", no_argument, 0, 'h'},
110 {"verbose", no_argument, 0, 'v'},
111 {"timeout", required_argument, 0, 't'},
112 {"update", no_argument, 0, 'u'},
113 {"dist-upgrade", no_argument, 0, 'd'},
114 {"include", no_argument, 0, 'i'},
115 {"exclude", no_argument, 0, 'e'},
116 {0, 0, 0, 0}
117 };
119 while(1) {
120 c = getopt_long(argc, argv, "hVvt:udi:e:", longopts, NULL);
122 if(c == -1 || c == EOF || c == 1) break;
124 switch(c) {
125 case 'h':
126 print_help();
127 exit(STATE_OK);
128 case 'V':
129 print_revision(progname, revision);
130 exit(STATE_OK);
131 case 'v':
132 verbose++;
133 break;
134 case 't':
135 timeout_interval=atoi(optarg);
136 break;
137 case 'd':
138 dist_upgrade=1;
139 break;
140 case 'u':
141 do_update=1;
142 break;
143 case 'i':
144 do_include=add_to_regexp(do_include, optarg);
145 break;
146 case 'e':
147 do_exclude=add_to_regexp(do_exclude, optarg);
148 break;
149 default:
150 /* print short usage statement if args not parsable */
151 usage_va(_("Unknown argument - %s"), optarg);
152 }
153 }
155 return OK;
156 }
159 /* informative help message */
160 void print_help(void){
161 print_revision(progname, revision);
162 printf(_(COPYRIGHT), copyright, email);
163 printf(_("\
164 This plugin checks for software updates on systems that use\n\
165 package management systems based on the apt-get(8) command\n\
166 found in Debian GNU/Linux\n\
167 \n\n"));
168 print_usage();
169 printf(_(UT_HELP_VRSN));
170 printf(_(UT_TIMEOUT), timeout_interval);
171 printf(_("\n\
172 -d, --dist-upgrade\n\
173 Perform a dist-upgrade instead of normal upgrade.\n\
174 -i, --include=REGEXP\n\
175 Include only packages matching REGEXP. Can be specified multiple times;\n\
176 the values will be combined together. Default is to include all packages.\n\
177 -e, --exclude=REGEXP\n\
178 Exclude packages matching REGEXP from the list of packages that would\n\
179 otherwise be excluded. Can be specified multiple times; the values\n\
180 will be combined together. Default is to exclude no packages.\n\n"));
181 printf(_("\
182 The following options require root privileges and should be used with care: \
183 \n\n"));
184 printf(_("\
185 -u, --update\n\
186 First perform an 'apt-get update' (note: you may also need to use -t)\
187 \n\n"));
188 }
190 /* simple usage heading */
191 void print_usage(void){
192 printf ("Usage: %s [-du] [-t timeout]\n", progname);
193 }
195 /* run an apt-get upgrade */
196 int run_upgrade(int *pkgcount){
197 int i=0, result=STATE_UNKNOWN, regres=0, pc=0;
198 struct output chld_out, chld_err;
199 regex_t ireg, ereg;
200 char rerrbuf[64];
201 const char *default_include_expr="^Inst";
203 /* compile the regexps */
204 if(do_include!=NULL){
205 regres=regcomp(&ireg, do_include, REG_EXTENDED);
206 if(regres!=0) {
207 regerror(regres, &ireg, rerrbuf, 64);
208 die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
209 progname, rerrbuf);
210 }
211 } else {
212 regres=regcomp(&ireg, default_include_expr, REG_EXTENDED);
213 if(regres!=0) {
214 regerror(regres, &ireg, rerrbuf, 64);
215 die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
216 progname, rerrbuf);
217 }
218 }
219 if(do_exclude!=NULL){
220 regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
221 if(regres!=0) {
222 regerror(regres, &ereg, rerrbuf, 64);
223 die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
224 progname, rerrbuf);
225 }
226 }
229 /* run the upgrade */
230 if(dist_upgrade==0){
231 result = np_runcmd(APTGET_UPGRADE, &chld_out, &chld_err, 0);
232 } else {
233 result = np_runcmd(APTGET_DISTUPGRADE, &chld_out, &chld_err, 0);
234 }
235 /* apt-get upgrade only changes exit status if there is an
236 * internal error when run in dry-run mode. therefore we will
237 * treat such an error as UNKNOWN */
238 if(result != 0){
239 exec_warning=1;
240 result = STATE_UNKNOWN;
241 fprintf(stderr, "'%s' exited with non-zero status.\n",
242 APTGET_UPGRADE);
243 }
245 /* parse the output, which should only consist of lines like
246 *
247 * Inst package ....
248 * Conf package ....
249 *
250 * so we'll filter based on "Inst" for the time being. later
251 * we may need to switch to the --print-uris output format,
252 * in which case the logic here will slightly change.
253 */
254 for(i = 0; i < chld_out.lines; i++) {
255 if(verbose){
256 printf("%s\n", chld_out.line[i]);
257 }
258 /* if it is a package we care about */
259 if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){
260 /* if we're not excluding, or it's not in the
261 * list of stuff to exclude */
262 if(do_exclude==NULL ||
263 regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
264 pc++;
265 if(verbose){
266 printf("*%s\n", chld_out.line[i]);
267 }
268 }
269 }
270 }
271 *pkgcount=pc;
273 /* If we get anything on stderr, at least set warning */
274 if(chld_err.buflen){
275 stderr_warning=1;
276 result = max_state(result, STATE_WARNING);
277 if(verbose){
278 for(i = 0; i < chld_err.lines; i++) {
279 fprintf(stderr, "%s\n", chld_err.line[i]);
280 }
281 }
282 }
283 return result;
284 }
286 /* run an apt-get update (needs root) */
287 int run_update(void){
288 int i=0, result=STATE_UNKNOWN;
289 struct output chld_out, chld_err;
291 /* run the upgrade */
292 result = np_runcmd(APTGET_UPDATE, &chld_out, &chld_err, 0);
293 /* apt-get update changes exit status if it can't fetch packages.
294 * since we were explicitly asked to do so, this is treated as
295 * a critical error. */
296 if(result != 0){
297 exec_warning=1;
298 result = STATE_CRITICAL;
299 fprintf(stderr, "'%s' exited with non-zero status.\n",
300 APTGET_UPDATE);
301 }
303 if(verbose){
304 for(i = 0; i < chld_out.lines; i++) {
305 printf("%s\n", chld_out.line[i]);
306 }
307 }
309 /* If we get anything on stderr, at least set warning */
310 if(chld_err.buflen){
311 stderr_warning=1;
312 result = max_state(result, STATE_WARNING);
313 if(verbose){
314 for(i = 0; i < chld_err.lines; i++) {
315 fprintf(stderr, "%s\n", chld_err.line[i]);
316 }
317 }
318 }
319 return result;
320 }
322 char* add_to_regexp(char *expr, const char *next){
323 char *re=NULL;
325 if(expr==NULL){
326 re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
327 if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
328 sprintf(re, "^Inst (%s) ", next);
329 } else {
330 /* resize it, adding an extra char for the new '|' separator */
331 re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1);
332 if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
333 /* append it starting at ')' in the old re */
334 sprintf((char*)(re+strlen(re)-2), "|%s) ", next);
335 }
337 return re;
338 }