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.c70
1 files changed, 27 insertions, 43 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index f67ebf9..f72b9b5 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -43,6 +43,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
43#include "common.h" 43#include "common.h"
44#include "runcmd.h" 44#include "runcmd.h"
45#include "utils.h" 45#include "utils.h"
46#include "utils.h"
46#include "regex.h" 47#include "regex.h"
47 48
48/* some constants */ 49/* some constants */
@@ -74,7 +75,6 @@ int run_upgrade(int *pkgcount, int *secpkgcount);
74char* add_to_regexp(char *expr, const char *next); 75char* add_to_regexp(char *expr, const char *next);
75 76
76/* configuration variables */ 77/* configuration variables */
77static int verbose = 0; /* -v */
78static int do_update = 0; /* whether to call apt-get update */ 78static int do_update = 0; /* whether to call apt-get update */
79static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ 79static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
80static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ 80static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
@@ -90,6 +90,7 @@ static int exec_warning = 0; /* if a cmd exited non-zero */
90int main (int argc, char **argv) { 90int main (int argc, char **argv) {
91 int result=STATE_UNKNOWN, packages_available=0, sec_count=0; 91 int result=STATE_UNKNOWN, packages_available=0, sec_count=0;
92 92
93 np_set_mynames(argv[0], "APT");
93 if (process_arguments(argc, argv) == ERROR) 94 if (process_arguments(argc, argv) == ERROR)
94 usage_va(_("Could not parse arguments")); 95 usage_va(_("Could not parse arguments"));
95 96
@@ -115,8 +116,8 @@ int main (int argc, char **argv) {
115 result = max_state(result, STATE_OK); 116 result = max_state(result, STATE_OK);
116 } 117 }
117 118
118 printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"), 119 np_die(result,
119 state_text(result), 120 _("%d packages available for %s (%d critical updates). %s%s%s%s"),
120 packages_available, 121 packages_available,
121 (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade", 122 (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade",
122 sec_count, 123 sec_count,
@@ -125,8 +126,6 @@ int main (int argc, char **argv) {
125 (exec_warning)?" errors detected":"", 126 (exec_warning)?" errors detected":"",
126 (stderr_warning||exec_warning)?". run with -v for information.":"" 127 (stderr_warning||exec_warning)?". run with -v for information.":""
127 ); 128 );
128
129 return result;
130} 129}
131 130
132/* process command-line arguments */ 131/* process command-line arguments */
@@ -161,7 +160,7 @@ int process_arguments (int argc, char **argv) {
161 print_revision(progname, revision); 160 print_revision(progname, revision);
162 exit(STATE_OK); 161 exit(STATE_OK);
163 case 'v': 162 case 'v':
164 verbose++; 163 np_increase_verbosity(1);
165 break; 164 break;
166 case 't': 165 case 't':
167 timeout_interval=atoi(optarg); 166 timeout_interval=atoi(optarg);
@@ -170,14 +169,14 @@ int process_arguments (int argc, char **argv) {
170 upgrade=DIST_UPGRADE; 169 upgrade=DIST_UPGRADE;
171 if(optarg!=NULL){ 170 if(optarg!=NULL){
172 upgrade_opts=strdup(optarg); 171 upgrade_opts=strdup(optarg);
173 if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 172 if(upgrade_opts==NULL) np_die(STATE_UNKNOWN, "strdup failed: %m");
174 } 173 }
175 break; 174 break;
176 case 'U': 175 case 'U':
177 upgrade=UPGRADE; 176 upgrade=UPGRADE;
178 if(optarg!=NULL){ 177 if(optarg!=NULL){
179 upgrade_opts=strdup(optarg); 178 upgrade_opts=strdup(optarg);
180 if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 179 if(upgrade_opts==NULL) np_die(STATE_UNKNOWN, "strdup failed: %m");
181 } 180 }
182 break; 181 break;
183 case 'n': 182 case 'n':
@@ -187,7 +186,7 @@ int process_arguments (int argc, char **argv) {
187 do_update=1; 186 do_update=1;
188 if(optarg!=NULL){ 187 if(optarg!=NULL){
189 update_opts=strdup(optarg); 188 update_opts=strdup(optarg);
190 if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); 189 if(update_opts==NULL) np_die(STATE_UNKNOWN, "strdup failed: %m");
191 } 190 }
192 break; 191 break;
193 case 'i': 192 case 'i':
@@ -228,22 +227,22 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
228 regres=regcomp(&ireg, include_ptr, REG_EXTENDED); 227 regres=regcomp(&ireg, include_ptr, REG_EXTENDED);
229 if(regres!=0) { 228 if(regres!=0) {
230 regerror(regres, &ireg, rerrbuf, 64); 229 regerror(regres, &ireg, rerrbuf, 64);
231 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); 230 np_die(STATE_UNKNOWN, _("Error compiling regexp: %s"), rerrbuf);
232 } 231 }
233 232
234 if(do_exclude!=NULL){ 233 if(do_exclude!=NULL){
235 regres=regcomp(&ereg, do_exclude, REG_EXTENDED); 234 regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
236 if(regres!=0) { 235 if(regres!=0) {
237 regerror(regres, &ereg, rerrbuf, 64); 236 regerror(regres, &ereg, rerrbuf, 64);
238 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), 237 np_die(STATE_UNKNOWN, _("Error compiling regexp: %s"),
239 progname, rerrbuf); 238 rerrbuf);
240 } 239 }
241 } 240 }
242 regres=regcomp(&sreg, crit_ptr, REG_EXTENDED); 241 regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
243 if(regres!=0) { 242 if(regres!=0) {
244 regerror(regres, &ereg, rerrbuf, 64); 243 regerror(regres, &ereg, rerrbuf, 64);
245 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), 244 np_die(STATE_UNKNOWN, _("Error compiling regexp: %s"),
246 progname, rerrbuf); 245 rerrbuf);
247 } 246 }
248 247
249 cmdline=construct_cmdline(upgrade, upgrade_opts); 248 cmdline=construct_cmdline(upgrade, upgrade_opts);
@@ -255,8 +254,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
255 if(result != 0){ 254 if(result != 0){
256 exec_warning=1; 255 exec_warning=1;
257 result = STATE_UNKNOWN; 256 result = STATE_UNKNOWN;
258 fprintf(stderr, _("'%s' exited with non-zero status.\n"), 257 np_verbose(_("'%s' exited with non-zero status."), cmdline);
259 cmdline);
260 } 258 }
261 259
262 /* parse the output, which should only consist of lines like 260 /* parse the output, which should only consist of lines like
@@ -269,9 +267,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
269 * in which case the logic here will slightly change. 267 * in which case the logic here will slightly change.
270 */ 268 */
271 for(i = 0; i < chld_out.lines; i++) { 269 for(i = 0; i < chld_out.lines; i++) {
272 if(verbose){ 270 np_verbatim(chld_out.line[i]);
273 printf("%s\n", chld_out.line[i]);
274 }
275 /* if it is a package we care about */ 271 /* if it is a package we care about */
276 if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){ 272 if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){
277 /* if we're not excluding, or it's not in the 273 /* if we're not excluding, or it's not in the
@@ -281,11 +277,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
281 pc++; 277 pc++;
282 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){ 278 if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
283 spc++; 279 spc++;
284 if(verbose) printf("*"); 280 np_debug(1, "*");
285 }
286 if(verbose){
287 printf("*%s\n", chld_out.line[i]);
288 } 281 }
282 np_verbose("*%s", chld_out.line[i]);
289 } 283 }
290 } 284 }
291 } 285 }
@@ -296,11 +290,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount){
296 if(chld_err.buflen){ 290 if(chld_err.buflen){
297 stderr_warning=1; 291 stderr_warning=1;
298 result = max_state(result, STATE_WARNING); 292 result = max_state(result, STATE_WARNING);
299 if(verbose){ 293 for(i = 0; i < chld_err.lines; i++)
300 for(i = 0; i < chld_err.lines; i++) { 294 np_verbatim(chld_err.line[i]);
301 fprintf(stderr, "%s\n", chld_err.line[i]);
302 }
303 }
304 } 295 }
305 regfree(&ireg); 296 regfree(&ireg);
306 regfree(&sreg); 297 regfree(&sreg);
@@ -324,25 +315,18 @@ int run_update(void){
324 if(result != 0){ 315 if(result != 0){
325 exec_warning=1; 316 exec_warning=1;
326 result = STATE_CRITICAL; 317 result = STATE_CRITICAL;
327 fprintf(stderr, _("'%s' exited with non-zero status.\n"), 318 np_verbose(_("'%s' exited with non-zero status."), cmdline);
328 cmdline);
329 } 319 }
330 320
331 if(verbose){ 321 for(i = 0; i < chld_out.lines; i++)
332 for(i = 0; i < chld_out.lines; i++) { 322 np_verbatim(chld_out.line[i]);
333 printf("%s\n", chld_out.line[i]);
334 }
335 }
336 323
337 /* If we get anything on stderr, at least set warning */ 324 /* If we get anything on stderr, at least set warning */
338 if(chld_err.buflen){ 325 if(chld_err.buflen){
339 stderr_warning=1; 326 stderr_warning=1;
340 result = max_state(result, STATE_WARNING); 327 result = max_state(result, STATE_WARNING);
341 if(verbose){ 328 for(i = 0; i < chld_err.lines; i++)
342 for(i = 0; i < chld_err.lines; i++) { 329 np_verbatim(chld_err.line[i]);
343 fprintf(stderr, "%s\n", chld_err.line[i]);
344 }
345 }
346 } 330 }
347 free(cmdline); 331 free(cmdline);
348 return result; 332 return result;
@@ -353,12 +337,12 @@ char* add_to_regexp(char *expr, const char *next){
353 337
354 if(expr==NULL){ 338 if(expr==NULL){
355 re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1)); 339 re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
356 if(!re) die(STATE_UNKNOWN, "malloc failed!\n"); 340 if(!re) np_die(STATE_UNKNOWN, "malloc failed: %m");
357 sprintf(re, "^Inst (%s) ", next); 341 sprintf(re, "^Inst (%s) ", next);
358 } else { 342 } else {
359 /* resize it, adding an extra char for the new '|' separator */ 343 /* resize it, adding an extra char for the new '|' separator */
360 re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1); 344 re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1);
361 if(!re) die(STATE_UNKNOWN, "realloc failed!\n"); 345 if(!re) np_die(STATE_UNKNOWN, "realloc failed: %m");
362 /* append it starting at ')' in the old re */ 346 /* append it starting at ')' in the old re */
363 sprintf((char*)(re+strlen(re)-2), "|%s) ", next); 347 sprintf((char*)(re+strlen(re)-2), "|%s) ", next);
364 } 348 }
@@ -394,7 +378,7 @@ char* construct_cmdline(upgrade_type u, const char *opts){
394 len+=strlen(aptcmd)+1; /* "upgrade\0" */ 378 len+=strlen(aptcmd)+1; /* "upgrade\0" */
395 379
396 cmd=(char*)malloc(sizeof(char)*len); 380 cmd=(char*)malloc(sizeof(char)*len);
397 if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed"); 381 if(cmd==NULL) np_die(STATE_UNKNOWN, "malloc failed: %m");
398 sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd); 382 sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
399 return cmd; 383 return cmd;
400} 384}