Back to index

nagios-plugins  1.4.16
Defines | Enumerations | Functions | Variables
check_apt.c File Reference
#include "common.h"
#include "runcmd.h"
#include "utils.h"
#include "regex.h"

Go to the source code of this file.

Defines

#define UPGRADE_DEFAULT_OPTS   "-o 'Debug::NoLocking=true' -s -qq"
#define UPDATE_DEFAULT_OPTS   "-q"
#define PATH_TO_APTGET   "/usr/bin/apt-get"
#define SECURITY_RE   "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"

Enumerations

enum  upgrade_type { UPGRADE, DIST_UPGRADE, NO_UPGRADE }

Functions

int process_arguments (int, char **)
void print_help (void)
void print_usage (void)
char * construct_cmdline (upgrade_type u, const char *opts)
int run_update (void)
int run_upgrade (int *pkgcount, int *secpkgcount)
char * add_to_regexp (char *expr, const char *next)
int main (int argc, char **argv)

Variables

const char * progname = "check_apt"
const char * copyright = "2006-2008"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
static int verbose = 0
static int do_update = 0
static upgrade_type upgrade = UPGRADE
static char * upgrade_opts = NULL
static char * update_opts = NULL
static char * do_include = NULL
static char * do_exclude = NULL
static char * do_critical = NULL
static int stderr_warning = 0
static int exec_warning = 0

Define Documentation

#define PATH_TO_APTGET   "/usr/bin/apt-get"

Definition at line 50 of file check_apt.c.

#define SECURITY_RE   "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"

Definition at line 53 of file check_apt.c.

#define UPDATE_DEFAULT_OPTS   "-q"

Definition at line 46 of file check_apt.c.

#define UPGRADE_DEFAULT_OPTS   "-o 'Debug::NoLocking=true' -s -qq"

Definition at line 45 of file check_apt.c.


Enumeration Type Documentation

Enumerator:
UPGRADE 
DIST_UPGRADE 
NO_UPGRADE 

Definition at line 42 of file check_apt.c.


Function Documentation

char * add_to_regexp ( char *  expr,
const char *  next 
)

Definition at line 347 of file check_apt.c.

                                                 {
       char *re=NULL;

       if(expr==NULL){
              re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
              if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
              sprintf(re, "^Inst (%s) ", next);
       } else {
              /* resize it, adding an extra char for the new '|' separator */
              re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1);
              if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
              /* append it starting at ')' in the old re */
              sprintf((char*)(re+strlen(re)-2), "|%s) ", next);
       }

       return re;
}

Here is the call graph for this function:

char * construct_cmdline ( upgrade_type  u,
const char *  opts 
)

Definition at line 365 of file check_apt.c.

                                                         {
       int len=0;
       const char *opts_ptr=NULL, *aptcmd=NULL;
       char *cmd=NULL;

       switch(u){
       case UPGRADE:
              if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
              else opts_ptr=opts;
              aptcmd="upgrade";
              break;
       case DIST_UPGRADE:
              if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
              else opts_ptr=opts;
              aptcmd="dist-upgrade";
              break;
       case NO_UPGRADE:
              if(opts==NULL) opts_ptr=UPDATE_DEFAULT_OPTS;
              else opts_ptr=opts;
              aptcmd="update";
              break;
       }

       len+=strlen(PATH_TO_APTGET)+1; /* "/usr/bin/apt-get " */
       len+=strlen(opts_ptr)+1;       /* "opts " */
       len+=strlen(aptcmd)+1;         /* "upgrade\0" */

       cmd=(char*)malloc(sizeof(char)*len);
       if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed");
       sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
       return cmd;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 83 of file check_apt.c.

                                 {
       int result=STATE_UNKNOWN, packages_available=0, sec_count=0;

       /* Parse extra opts if any */
       argv=np_extra_opts(&argc, argv, progname);

       if (process_arguments(argc, argv) == ERROR)
              usage_va(_("Could not parse arguments"));

       /* Set signal handling and alarm timeout */
       if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
              usage_va(_("Cannot catch SIGALRM"));
       }

       /* handle timeouts gracefully... */
       alarm (timeout_interval);

       /* if they want to run apt-get update first... */
       if(do_update) result = run_update();

       /* apt-get upgrade */
       result = max_state(result, run_upgrade(&packages_available, &sec_count));

       if(sec_count > 0){
              result = max_state(result, STATE_CRITICAL);
       } else if(packages_available > 0){
              result = max_state(result, STATE_WARNING);
       } else {
              result = max_state(result, STATE_OK);
       }

       printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"),
              state_text(result),
              packages_available,
              (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade",
                 sec_count,
              (stderr_warning)?" warnings detected":"",
              (stderr_warning && exec_warning)?",":"",
              (exec_warning)?" errors detected":"",
              (stderr_warning||exec_warning)?". run with -v for information.":""
              );

       return result;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int run_update ( void  )

Definition at line 309 of file check_apt.c.

                    {
       int i=0, result=STATE_UNKNOWN;
       struct output chld_out, chld_err;
       char *cmdline;

       /* run the upgrade */
       cmdline = construct_cmdline(NO_UPGRADE, update_opts);
       result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
       /* apt-get update changes exit status if it can't fetch packages.
        * since we were explicitly asked to do so, this is treated as
        * a critical error. */
       if(result != 0){
              exec_warning=1;
              result = STATE_CRITICAL;
              fprintf(stderr, _("'%s' exited with non-zero status.\n"),
                      cmdline);
       }

       if(verbose){
              for(i = 0; i < chld_out.lines; i++) {
                     printf("%s\n", chld_out.line[i]);
              }
       }

       /* If we get anything on stderr, at least set warning */
       if(chld_err.buflen){
              stderr_warning=1;
              result = max_state(result, STATE_WARNING);
              if(verbose){
                     for(i = 0; i < chld_err.lines; i++) {
                            fprintf(stderr, "%s\n", chld_err.line[i]);
                     }
              }
       }
       free(cmdline);
       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int run_upgrade ( int *  pkgcount,
int *  secpkgcount 
)

Definition at line 209 of file check_apt.c.

                                                {
       int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
       struct output chld_out, chld_err;
       regex_t ireg, ereg, sreg;
       char *cmdline=NULL, rerrbuf[64];
       const char *include_ptr=NULL, *crit_ptr=NULL;

       if(upgrade==NO_UPGRADE) return STATE_OK;

       /* compile the regexps */
       if(do_include!=NULL) include_ptr=do_include;
       else include_ptr="^Inst";
       if(do_critical!=NULL) crit_ptr=do_critical;
       else crit_ptr=SECURITY_RE;

       regres=regcomp(&ireg, include_ptr, REG_EXTENDED);
       if(regres!=0) {
              regerror(regres, &ireg, rerrbuf, 64);
              die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
       }

       if(do_exclude!=NULL){
              regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
              if(regres!=0) {
                     regerror(regres, &ereg, rerrbuf, 64);
                     die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
                         progname, rerrbuf);
              }
       }
       regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
       if(regres!=0) {
              regerror(regres, &ereg, rerrbuf, 64);
              die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
                  progname, rerrbuf);
       }

       cmdline=construct_cmdline(upgrade, upgrade_opts);
       /* run the upgrade */
       result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
       /* apt-get upgrade only changes exit status if there is an
        * internal error when run in dry-run mode.  therefore we will
        * treat such an error as UNKNOWN */
       if(result != 0){
              exec_warning=1;
              result = STATE_UNKNOWN;
              fprintf(stderr, _("'%s' exited with non-zero status.\n"),
                  cmdline);
       }

       /* parse the output, which should only consist of lines like
        *
        * Inst package ....
        * Conf package ....
        *
        * so we'll filter based on "Inst" for the time being.  later
        * we may need to switch to the --print-uris output format,
        * in which case the logic here will slightly change.
        */
       for(i = 0; i < chld_out.lines; i++) {
              if(verbose){
                     printf("%s\n", chld_out.line[i]);
              }
              /* if it is a package we care about */
              if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){
                     /* if we're not excluding, or it's not in the
                      * list of stuff to exclude */
                     if(do_exclude==NULL ||
                        regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
                            pc++;
                            if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
                                   spc++;
                                   if(verbose) printf("*");
                            }
                            if(verbose){
                                   printf("*%s\n", chld_out.line[i]);
                            }
                     }
              }
       }
       *pkgcount=pc;
       *secpkgcount=spc;

       /* If we get anything on stderr, at least set warning */
       if(chld_err.buflen){
              stderr_warning=1;
              result = max_state(result, STATE_WARNING);
              if(verbose){
                     for(i = 0; i < chld_err.lines; i++) {
                            fprintf(stderr, "%s\n", chld_err.line[i]);
                     }
              }
       }
       regfree(&ireg);
       regfree(&sreg);
       if(do_exclude!=NULL) regfree(&ereg);
       free(cmdline);
       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char* copyright = "2006-2008"

Definition at line 33 of file check_apt.c.

char* do_critical = NULL [static]

Definition at line 77 of file check_apt.c.

char* do_exclude = NULL [static]

Definition at line 76 of file check_apt.c.

char* do_include = NULL [static]

Definition at line 75 of file check_apt.c.

int do_update = 0 [static]

Definition at line 71 of file check_apt.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 34 of file check_apt.c.

int exec_warning = 0 [static]

Definition at line 81 of file check_apt.c.

const char* progname = "check_apt"

Definition at line 32 of file check_apt.c.

int stderr_warning = 0 [static]

Definition at line 80 of file check_apt.c.

char* update_opts = NULL [static]

Definition at line 74 of file check_apt.c.

Definition at line 72 of file check_apt.c.

char* upgrade_opts = NULL [static]

Definition at line 73 of file check_apt.c.

int verbose = 0 [static]

Definition at line 70 of file check_apt.c.