diff options
-rw-r--r-- | plugins/check_apt.c | 504 |
1 files changed, 251 insertions, 253 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index 5e4021b..02af1a4 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -42,14 +42,14 @@ const char *email = "devel@monitoring-plugins.org"; | |||
42 | typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type; | 42 | typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type; |
43 | 43 | ||
44 | /* Character for hidden input file option (for testing). */ | 44 | /* Character for hidden input file option (for testing). */ |
45 | #define INPUT_FILE_OPT CHAR_MAX+1 | 45 | #define INPUT_FILE_OPT CHAR_MAX + 1 |
46 | /* the default opts can be overridden via the cmdline */ | 46 | /* the default opts can be overridden via the cmdline */ |
47 | #define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq" | 47 | #define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq" |
48 | #define UPDATE_DEFAULT_OPTS "-q" | 48 | #define UPDATE_DEFAULT_OPTS "-q" |
49 | /* until i commit the configure.in patch which gets this, i'll define | 49 | /* until i commit the configure.in patch which gets this, i'll define |
50 | * it here as well */ | 50 | * it here as well */ |
51 | #ifndef PATH_TO_APTGET | 51 | #ifndef PATH_TO_APTGET |
52 | # define PATH_TO_APTGET "/usr/bin/apt-get" | 52 | # define PATH_TO_APTGET "/usr/bin/apt-get" |
53 | #endif /* PATH_TO_APTGET */ | 53 | #endif /* PATH_TO_APTGET */ |
54 | /* String found at the beginning of the apt output lines we're interested in */ | 54 | /* String found at the beginning of the apt output lines we're interested in */ |
55 | #define PKGINST_PREFIX "Inst " | 55 | #define PKGINST_PREFIX "Inst " |
@@ -62,91 +62,84 @@ void print_help(void); | |||
62 | void print_usage(void); | 62 | void print_usage(void); |
63 | 63 | ||
64 | /* construct the appropriate apt-get cmdline */ | 64 | /* construct the appropriate apt-get cmdline */ |
65 | char* construct_cmdline(upgrade_type u, const char *opts); | 65 | char *construct_cmdline(upgrade_type u, const char *opts); |
66 | /* run an apt-get update */ | 66 | /* run an apt-get update */ |
67 | int run_update(void); | 67 | int run_update(void); |
68 | /* run an apt-get upgrade */ | 68 | /* run an apt-get upgrade */ |
69 | int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist); | 69 | int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist); |
70 | /* add another clause to a regexp */ | 70 | /* add another clause to a regexp */ |
71 | char* add_to_regexp(char *expr, const char *next); | 71 | char *add_to_regexp(char *expr, const char *next); |
72 | /* extract package name from Inst line */ | 72 | /* extract package name from Inst line */ |
73 | char* pkg_name(char *line); | 73 | char *pkg_name(char *line); |
74 | /* string comparison function for qsort */ | 74 | /* string comparison function for qsort */ |
75 | int cmpstringp(const void *p1, const void *p2); | 75 | int cmpstringp(const void *p1, const void *p2); |
76 | 76 | ||
77 | /* configuration variables */ | 77 | /* configuration variables */ |
78 | static int verbose = 0; /* -v */ | 78 | static int verbose = 0; /* -v */ |
79 | static bool list = false; /* list packages available for upgrade */ | 79 | static bool list = false; /* list packages available for upgrade */ |
80 | static bool do_update = false; /* whether to call apt-get update */ | 80 | static bool do_update = false; /* whether to call apt-get update */ |
81 | static bool only_critical = false; /* whether to warn about non-critical updates */ | 81 | static bool only_critical = false; /* whether to warn about non-critical updates */ |
82 | static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ | 82 | static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ |
83 | static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ | 83 | static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ |
84 | static char *update_opts = NULL; /* options to override defaults for update */ | 84 | static char *update_opts = NULL; /* options to override defaults for update */ |
85 | static char *do_include = NULL; /* regexp to only include certain packages */ | 85 | static char *do_include = NULL; /* regexp to only include certain packages */ |
86 | static char *do_exclude = NULL; /* regexp to only exclude certain packages */ | 86 | static char *do_exclude = NULL; /* regexp to only exclude certain packages */ |
87 | static char *do_critical = NULL; /* regexp specifying critical packages */ | 87 | static char *do_critical = NULL; /* regexp specifying critical packages */ |
88 | static char *input_filename = NULL; /* input filename for testing */ | 88 | static char *input_filename = NULL; /* input filename for testing */ |
89 | /* number of packages available for upgrade to return WARNING status */ | 89 | /* number of packages available for upgrade to return WARNING status */ |
90 | static int packages_warning = 1; | 90 | static int packages_warning = 1; |
91 | 91 | ||
92 | /* other global variables */ | 92 | /* other global variables */ |
93 | static int stderr_warning = 0; /* if a cmd issued output on stderr */ | 93 | static int stderr_warning = 0; /* if a cmd issued output on stderr */ |
94 | static int exec_warning = 0; /* if a cmd exited non-zero */ | 94 | static int exec_warning = 0; /* if a cmd exited non-zero */ |
95 | 95 | ||
96 | int main (int argc, char **argv) { | 96 | int main(int argc, char **argv) { |
97 | int result=STATE_UNKNOWN, packages_available=0, sec_count=0; | 97 | int result = STATE_UNKNOWN, packages_available = 0, sec_count = 0; |
98 | char **packages_list=NULL, **secpackages_list=NULL; | 98 | char **packages_list = NULL, **secpackages_list = NULL; |
99 | 99 | ||
100 | /* Parse extra opts if any */ | 100 | /* Parse extra opts if any */ |
101 | argv=np_extra_opts(&argc, argv, progname); | 101 | argv = np_extra_opts(&argc, argv, progname); |
102 | 102 | ||
103 | if (process_arguments(argc, argv) == ERROR) | 103 | if (process_arguments(argc, argv) == ERROR) |
104 | usage_va(_("Could not parse arguments")); | 104 | usage_va(_("Could not parse arguments")); |
105 | 105 | ||
106 | /* Set signal handling and alarm timeout */ | 106 | /* Set signal handling and alarm timeout */ |
107 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 107 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
108 | usage_va(_("Cannot catch SIGALRM")); | 108 | usage_va(_("Cannot catch SIGALRM")); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* handle timeouts gracefully... */ | 111 | /* handle timeouts gracefully... */ |
112 | alarm (timeout_interval); | 112 | alarm(timeout_interval); |
113 | 113 | ||
114 | /* if they want to run apt-get update first... */ | 114 | /* if they want to run apt-get update first... */ |
115 | if(do_update) result = run_update(); | 115 | if (do_update) |
116 | result = run_update(); | ||
116 | 117 | ||
117 | /* apt-get upgrade */ | 118 | /* apt-get upgrade */ |
118 | result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); | 119 | result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); |
119 | 120 | ||
120 | if(sec_count > 0){ | 121 | if (sec_count > 0) { |
121 | result = max_state(result, STATE_CRITICAL); | 122 | result = max_state(result, STATE_CRITICAL); |
122 | } else if(packages_available >= packages_warning && only_critical == false){ | 123 | } else if (packages_available >= packages_warning && only_critical == false) { |
123 | result = max_state(result, STATE_WARNING); | 124 | result = max_state(result, STATE_WARNING); |
124 | } else if(result > STATE_UNKNOWN){ | 125 | } else if (result > STATE_UNKNOWN) { |
125 | result = STATE_UNKNOWN; | 126 | result = STATE_UNKNOWN; |
126 | } | 127 | } |
127 | 128 | ||
128 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), | 129 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), |
129 | state_text(result), | 130 | state_text(result), packages_available, (upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, |
130 | packages_available, | 131 | (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", |
131 | (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade", | 132 | (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); |
132 | sec_count, | 133 | |
133 | (stderr_warning)?" warnings detected":"", | 134 | if (list) { |
134 | (stderr_warning && exec_warning)?",":"", | 135 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); |
135 | (exec_warning)?" errors detected":"", | 136 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); |
136 | (stderr_warning||exec_warning)?".":"", | 137 | |
137 | packages_available, | 138 | for (int i = 0; i < sec_count; i++) |
138 | sec_count | ||
139 | ); | ||
140 | |||
141 | if(list) { | ||
142 | qsort(secpackages_list, sec_count, sizeof(char*), cmpstringp); | ||
143 | qsort(packages_list, packages_available-sec_count, sizeof(char*), cmpstringp); | ||
144 | |||
145 | for(int i = 0; i < sec_count; i++) | ||
146 | printf("%s (security)\n", secpackages_list[i]); | 139 | printf("%s (security)\n", secpackages_list[i]); |
147 | 140 | ||
148 | if (only_critical == false) { | 141 | if (only_critical == false) { |
149 | for(int i = 0; i < packages_available - sec_count; i++) | 142 | for (int i = 0; i < packages_available - sec_count; i++) |
150 | printf("%s\n", packages_list[i]); | 143 | printf("%s\n", packages_list[i]); |
151 | } | 144 | } |
152 | } | 145 | } |
@@ -155,34 +148,33 @@ int main (int argc, char **argv) { | |||
155 | } | 148 | } |
156 | 149 | ||
157 | /* process command-line arguments */ | 150 | /* process command-line arguments */ |
158 | int process_arguments (int argc, char **argv) { | 151 | int process_arguments(int argc, char **argv) { |
159 | int c; | 152 | int c; |
160 | 153 | ||
161 | static struct option longopts[] = { | 154 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
162 | {"version", no_argument, 0, 'V'}, | 155 | {"help", no_argument, 0, 'h'}, |
163 | {"help", no_argument, 0, 'h'}, | 156 | {"verbose", no_argument, 0, 'v'}, |
164 | {"verbose", no_argument, 0, 'v'}, | 157 | {"timeout", required_argument, 0, 't'}, |
165 | {"timeout", required_argument, 0, 't'}, | 158 | {"update", optional_argument, 0, 'u'}, |
166 | {"update", optional_argument, 0, 'u'}, | 159 | {"upgrade", optional_argument, 0, 'U'}, |
167 | {"upgrade", optional_argument, 0, 'U'}, | 160 | {"no-upgrade", no_argument, 0, 'n'}, |
168 | {"no-upgrade", no_argument, 0, 'n'}, | 161 | {"dist-upgrade", optional_argument, 0, 'd'}, |
169 | {"dist-upgrade", optional_argument, 0, 'd'}, | 162 | {"list", no_argument, false, 'l'}, |
170 | {"list", no_argument, false, 'l'}, | 163 | {"include", required_argument, 0, 'i'}, |
171 | {"include", required_argument, 0, 'i'}, | 164 | {"exclude", required_argument, 0, 'e'}, |
172 | {"exclude", required_argument, 0, 'e'}, | 165 | {"critical", required_argument, 0, 'c'}, |
173 | {"critical", required_argument, 0, 'c'}, | 166 | {"only-critical", no_argument, 0, 'o'}, |
174 | {"only-critical", no_argument, 0, 'o'}, | 167 | {"input-file", required_argument, 0, INPUT_FILE_OPT}, |
175 | {"input-file", required_argument, 0, INPUT_FILE_OPT}, | 168 | {"packages-warning", required_argument, 0, 'w'}, |
176 | {"packages-warning", required_argument, 0, 'w'}, | 169 | {0, 0, 0, 0}}; |
177 | {0, 0, 0, 0} | 170 | |
178 | }; | 171 | while (1) { |
179 | |||
180 | while(1) { | ||
181 | c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); | 172 | c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); |
182 | 173 | ||
183 | if(c == -1 || c == EOF || c == 1) break; | 174 | if (c == -1 || c == EOF || c == 1) |
175 | break; | ||
184 | 176 | ||
185 | switch(c) { | 177 | switch (c) { |
186 | case 'h': | 178 | case 'h': |
187 | print_help(); | 179 | print_help(); |
188 | exit(STATE_UNKNOWN); | 180 | exit(STATE_UNKNOWN); |
@@ -193,46 +185,49 @@ int process_arguments (int argc, char **argv) { | |||
193 | verbose++; | 185 | verbose++; |
194 | break; | 186 | break; |
195 | case 't': | 187 | case 't': |
196 | timeout_interval=atoi(optarg); | 188 | timeout_interval = atoi(optarg); |
197 | break; | 189 | break; |
198 | case 'd': | 190 | case 'd': |
199 | upgrade=DIST_UPGRADE; | 191 | upgrade = DIST_UPGRADE; |
200 | if(optarg!=NULL){ | 192 | if (optarg != NULL) { |
201 | upgrade_opts=strdup(optarg); | 193 | upgrade_opts = strdup(optarg); |
202 | if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); | 194 | if (upgrade_opts == NULL) |
195 | die(STATE_UNKNOWN, "strdup failed"); | ||
203 | } | 196 | } |
204 | break; | 197 | break; |
205 | case 'U': | 198 | case 'U': |
206 | upgrade=UPGRADE; | 199 | upgrade = UPGRADE; |
207 | if(optarg!=NULL){ | 200 | if (optarg != NULL) { |
208 | upgrade_opts=strdup(optarg); | 201 | upgrade_opts = strdup(optarg); |
209 | if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); | 202 | if (upgrade_opts == NULL) |
203 | die(STATE_UNKNOWN, "strdup failed"); | ||
210 | } | 204 | } |
211 | break; | 205 | break; |
212 | case 'n': | 206 | case 'n': |
213 | upgrade=NO_UPGRADE; | 207 | upgrade = NO_UPGRADE; |
214 | break; | 208 | break; |
215 | case 'u': | 209 | case 'u': |
216 | do_update=true; | 210 | do_update = true; |
217 | if(optarg!=NULL){ | 211 | if (optarg != NULL) { |
218 | update_opts=strdup(optarg); | 212 | update_opts = strdup(optarg); |
219 | if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed"); | 213 | if (update_opts == NULL) |
214 | die(STATE_UNKNOWN, "strdup failed"); | ||
220 | } | 215 | } |
221 | break; | 216 | break; |
222 | case 'l': | 217 | case 'l': |
223 | list=true; | 218 | list = true; |
224 | break; | 219 | break; |
225 | case 'i': | 220 | case 'i': |
226 | do_include=add_to_regexp(do_include, optarg); | 221 | do_include = add_to_regexp(do_include, optarg); |
227 | break; | 222 | break; |
228 | case 'e': | 223 | case 'e': |
229 | do_exclude=add_to_regexp(do_exclude, optarg); | 224 | do_exclude = add_to_regexp(do_exclude, optarg); |
230 | break; | 225 | break; |
231 | case 'c': | 226 | case 'c': |
232 | do_critical=add_to_regexp(do_critical, optarg); | 227 | do_critical = add_to_regexp(do_critical, optarg); |
233 | break; | 228 | break; |
234 | case 'o': | 229 | case 'o': |
235 | only_critical=true; | 230 | only_critical = true; |
236 | break; | 231 | break; |
237 | case INPUT_FILE_OPT: | 232 | case INPUT_FILE_OPT: |
238 | input_filename = optarg; | 233 | input_filename = optarg; |
@@ -249,46 +244,44 @@ int process_arguments (int argc, char **argv) { | |||
249 | return OK; | 244 | return OK; |
250 | } | 245 | } |
251 | 246 | ||
252 | |||
253 | /* run an apt-get upgrade */ | 247 | /* run an apt-get upgrade */ |
254 | int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist){ | 248 | int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) { |
255 | int result=STATE_UNKNOWN, regres=0, pc=0, spc=0; | 249 | int result = STATE_UNKNOWN, regres = 0, pc = 0, spc = 0; |
256 | struct output chld_out, chld_err; | 250 | struct output chld_out, chld_err; |
257 | regex_t ireg, ereg, sreg; | 251 | regex_t ireg, ereg, sreg; |
258 | char *cmdline=NULL, rerrbuf[64]; | 252 | char *cmdline = NULL, rerrbuf[64]; |
259 | 253 | ||
260 | /* initialize ereg as it is possible it is printed while uninitialized */ | 254 | /* initialize ereg as it is possible it is printed while uninitialized */ |
261 | memset(&ereg, '\0', sizeof(ereg.buffer)); | 255 | memset(&ereg, '\0', sizeof(ereg.buffer)); |
262 | 256 | ||
263 | if(upgrade==NO_UPGRADE) return STATE_OK; | 257 | if (upgrade == NO_UPGRADE) |
258 | return STATE_OK; | ||
264 | 259 | ||
265 | /* compile the regexps */ | 260 | /* compile the regexps */ |
266 | if (do_include != NULL) { | 261 | if (do_include != NULL) { |
267 | regres=regcomp(&ireg, do_include, REG_EXTENDED); | 262 | regres = regcomp(&ireg, do_include, REG_EXTENDED); |
268 | if (regres!=0) { | 263 | if (regres != 0) { |
269 | regerror(regres, &ireg, rerrbuf, 64); | 264 | regerror(regres, &ireg, rerrbuf, 64); |
270 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); | 265 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); |
271 | } | 266 | } |
272 | } | 267 | } |
273 | 268 | ||
274 | if(do_exclude!=NULL){ | 269 | if (do_exclude != NULL) { |
275 | regres=regcomp(&ereg, do_exclude, REG_EXTENDED); | 270 | regres = regcomp(&ereg, do_exclude, REG_EXTENDED); |
276 | if(regres!=0) { | 271 | if (regres != 0) { |
277 | regerror(regres, &ereg, rerrbuf, 64); | 272 | regerror(regres, &ereg, rerrbuf, 64); |
278 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), | 273 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); |
279 | progname, rerrbuf); | ||
280 | } | 274 | } |
281 | } | 275 | } |
282 | 276 | ||
283 | const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE; | 277 | const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE; |
284 | regres=regcomp(&sreg, crit_ptr, REG_EXTENDED); | 278 | regres = regcomp(&sreg, crit_ptr, REG_EXTENDED); |
285 | if(regres!=0) { | 279 | if (regres != 0) { |
286 | regerror(regres, &ereg, rerrbuf, 64); | 280 | regerror(regres, &ereg, rerrbuf, 64); |
287 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), | 281 | die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); |
288 | progname, rerrbuf); | ||
289 | } | 282 | } |
290 | 283 | ||
291 | cmdline=construct_cmdline(upgrade, upgrade_opts); | 284 | cmdline = construct_cmdline(upgrade, upgrade_opts); |
292 | if (input_filename != NULL) { | 285 | if (input_filename != NULL) { |
293 | /* read input from a file for testing */ | 286 | /* read input from a file for testing */ |
294 | result = cmd_file_read(input_filename, &chld_out, 0); | 287 | result = cmd_file_read(input_filename, &chld_out, 0); |
@@ -300,17 +293,18 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
300 | /* apt-get upgrade only changes exit status if there is an | 293 | /* apt-get upgrade only changes exit status if there is an |
301 | * internal error when run in dry-run mode. therefore we will | 294 | * internal error when run in dry-run mode. therefore we will |
302 | * treat such an error as UNKNOWN */ | 295 | * treat such an error as UNKNOWN */ |
303 | if(result != 0){ | 296 | if (result != 0) { |
304 | exec_warning=1; | 297 | exec_warning = 1; |
305 | result = STATE_UNKNOWN; | 298 | result = STATE_UNKNOWN; |
306 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), | 299 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
307 | cmdline); | ||
308 | } | 300 | } |
309 | 301 | ||
310 | *pkglist=malloc(sizeof(char *) * chld_out.lines); | 302 | *pkglist = malloc(sizeof(char *) * chld_out.lines); |
311 | if(!pkglist) die(STATE_UNKNOWN, "malloc failed!\n"); | 303 | if (!pkglist) |
312 | *secpkglist=malloc(sizeof(char *) * chld_out.lines); | 304 | die(STATE_UNKNOWN, "malloc failed!\n"); |
313 | if(!secpkglist) die(STATE_UNKNOWN, "malloc failed!\n"); | 305 | *secpkglist = malloc(sizeof(char *) * chld_out.lines); |
306 | if (!secpkglist) | ||
307 | die(STATE_UNKNOWN, "malloc failed!\n"); | ||
314 | 308 | ||
315 | /* parse the output, which should only consist of lines like | 309 | /* parse the output, which should only consist of lines like |
316 | * | 310 | * |
@@ -321,54 +315,56 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
321 | * we may need to switch to the --print-uris output format, | 315 | * we may need to switch to the --print-uris output format, |
322 | * in which case the logic here will slightly change. | 316 | * in which case the logic here will slightly change. |
323 | */ | 317 | */ |
324 | for(size_t i = 0; i < chld_out.lines; i++) { | 318 | for (size_t i = 0; i < chld_out.lines; i++) { |
325 | if(verbose){ | 319 | if (verbose) { |
326 | printf("%s\n", chld_out.line[i]); | 320 | printf("%s\n", chld_out.line[i]); |
327 | } | 321 | } |
328 | /* if it is a package we care about */ | 322 | /* if it is a package we care about */ |
329 | if (strncmp(PKGINST_PREFIX, chld_out.line[i], strlen(PKGINST_PREFIX)) == 0 && | 323 | if (strncmp(PKGINST_PREFIX, chld_out.line[i], strlen(PKGINST_PREFIX)) == 0 && |
330 | (do_include == NULL || regexec(&ireg, chld_out.line[i], 0, NULL, 0) == 0)) { | 324 | (do_include == NULL || regexec(&ireg, chld_out.line[i], 0, NULL, 0) == 0)) { |
331 | /* if we're not excluding, or it's not in the | 325 | /* if we're not excluding, or it's not in the |
332 | * list of stuff to exclude */ | 326 | * list of stuff to exclude */ |
333 | if(do_exclude==NULL || | 327 | if (do_exclude == NULL || regexec(&ereg, chld_out.line[i], 0, NULL, 0) != 0) { |
334 | regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){ | ||
335 | pc++; | 328 | pc++; |
336 | if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){ | 329 | if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) { |
337 | spc++; | 330 | spc++; |
338 | if(verbose) printf("*"); | 331 | if (verbose) |
339 | (*secpkglist)[spc-1] = pkg_name(chld_out.line[i]); | 332 | printf("*"); |
333 | (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]); | ||
340 | } else { | 334 | } else { |
341 | (*pkglist)[pc-spc-1] = pkg_name(chld_out.line[i]); | 335 | (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]); |
342 | } | 336 | } |
343 | if(verbose){ | 337 | if (verbose) { |
344 | printf("*%s\n", chld_out.line[i]); | 338 | printf("*%s\n", chld_out.line[i]); |
345 | } | 339 | } |
346 | } | 340 | } |
347 | } | 341 | } |
348 | } | 342 | } |
349 | *pkgcount=pc; | 343 | *pkgcount = pc; |
350 | *secpkgcount=spc; | 344 | *secpkgcount = spc; |
351 | 345 | ||
352 | /* If we get anything on stderr, at least set warning */ | 346 | /* If we get anything on stderr, at least set warning */ |
353 | if (input_filename == NULL && chld_err.buflen) { | 347 | if (input_filename == NULL && chld_err.buflen) { |
354 | stderr_warning=1; | 348 | stderr_warning = 1; |
355 | result = max_state(result, STATE_WARNING); | 349 | result = max_state(result, STATE_WARNING); |
356 | if(verbose){ | 350 | if (verbose) { |
357 | for(size_t i = 0; i < chld_err.lines; i++) { | 351 | for (size_t i = 0; i < chld_err.lines; i++) { |
358 | fprintf(stderr, "%s\n", chld_err.line[i]); | 352 | fprintf(stderr, "%s\n", chld_err.line[i]); |
359 | } | 353 | } |
360 | } | 354 | } |
361 | } | 355 | } |
362 | if (do_include != NULL) regfree(&ireg); | 356 | if (do_include != NULL) |
357 | regfree(&ireg); | ||
363 | regfree(&sreg); | 358 | regfree(&sreg); |
364 | if(do_exclude!=NULL) regfree(&ereg); | 359 | if (do_exclude != NULL) |
360 | regfree(&ereg); | ||
365 | free(cmdline); | 361 | free(cmdline); |
366 | return result; | 362 | return result; |
367 | } | 363 | } |
368 | 364 | ||
369 | /* run an apt-get update (needs root) */ | 365 | /* run an apt-get update (needs root) */ |
370 | int run_update(void){ | 366 | int run_update(void) { |
371 | int result=STATE_UNKNOWN; | 367 | int result = STATE_UNKNOWN; |
372 | struct output chld_out, chld_err; | 368 | struct output chld_out, chld_err; |
373 | char *cmdline; | 369 | char *cmdline; |
374 | 370 | ||
@@ -378,25 +374,24 @@ int run_update(void){ | |||
378 | /* apt-get update changes exit status if it can't fetch packages. | 374 | /* apt-get update changes exit status if it can't fetch packages. |
379 | * since we were explicitly asked to do so, this is treated as | 375 | * since we were explicitly asked to do so, this is treated as |
380 | * a critical error. */ | 376 | * a critical error. */ |
381 | if(result != 0){ | 377 | if (result != 0) { |
382 | exec_warning=1; | 378 | exec_warning = 1; |
383 | result = STATE_CRITICAL; | 379 | result = STATE_CRITICAL; |
384 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), | 380 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
385 | cmdline); | ||
386 | } | 381 | } |
387 | 382 | ||
388 | if(verbose){ | 383 | if (verbose) { |
389 | for(size_t i = 0; i < chld_out.lines; i++) { | 384 | for (size_t i = 0; i < chld_out.lines; i++) { |
390 | printf("%s\n", chld_out.line[i]); | 385 | printf("%s\n", chld_out.line[i]); |
391 | } | 386 | } |
392 | } | 387 | } |
393 | 388 | ||
394 | /* If we get anything on stderr, at least set warning */ | 389 | /* If we get anything on stderr, at least set warning */ |
395 | if(chld_err.buflen){ | 390 | if (chld_err.buflen) { |
396 | stderr_warning=1; | 391 | stderr_warning = 1; |
397 | result = max_state(result, STATE_WARNING); | 392 | result = max_state(result, STATE_WARNING); |
398 | if(verbose){ | 393 | if (verbose) { |
399 | for(size_t i = 0; i < chld_err.lines; i++) { | 394 | for (size_t i = 0; i < chld_err.lines; i++) { |
400 | fprintf(stderr, "%s\n", chld_err.line[i]); | 395 | fprintf(stderr, "%s\n", chld_err.line[i]); |
401 | } | 396 | } |
402 | } | 397 | } |
@@ -405,158 +400,161 @@ int run_update(void){ | |||
405 | return result; | 400 | return result; |
406 | } | 401 | } |
407 | 402 | ||
408 | char* pkg_name(char *line){ | 403 | char *pkg_name(char *line) { |
409 | char *start=NULL, *space=NULL, *pkg=NULL; | 404 | char *start = NULL, *space = NULL, *pkg = NULL; |
410 | int len=0; | 405 | int len = 0; |
411 | 406 | ||
412 | start = line + strlen(PKGINST_PREFIX); | 407 | start = line + strlen(PKGINST_PREFIX); |
413 | len = strlen(start); | 408 | len = strlen(start); |
414 | 409 | ||
415 | space = index(start, ' '); | 410 | space = index(start, ' '); |
416 | if(space!=NULL){ | 411 | if (space != NULL) { |
417 | len = space - start; | 412 | len = space - start; |
418 | } | 413 | } |
419 | 414 | ||
420 | pkg=malloc(sizeof(char)*(len+1)); | 415 | pkg = malloc(sizeof(char) * (len + 1)); |
421 | if(!pkg) die(STATE_UNKNOWN, "malloc failed!\n"); | 416 | if (!pkg) |
417 | die(STATE_UNKNOWN, "malloc failed!\n"); | ||
422 | 418 | ||
423 | strncpy(pkg, start, len); | 419 | strncpy(pkg, start, len); |
424 | pkg[len]='\0'; | 420 | pkg[len] = '\0'; |
425 | 421 | ||
426 | return pkg; | 422 | return pkg; |
427 | } | 423 | } |
428 | 424 | ||
429 | int cmpstringp(const void *p1, const void *p2){ | 425 | int cmpstringp(const void *p1, const void *p2) { return strcmp(*(char *const *)p1, *(char *const *)p2); } |
430 | return strcmp(* (char * const *) p1, * (char * const *) p2); | ||
431 | } | ||
432 | 426 | ||
433 | char* add_to_regexp(char *expr, const char *next){ | 427 | char *add_to_regexp(char *expr, const char *next) { |
434 | char *re=NULL; | 428 | char *re = NULL; |
435 | 429 | ||
436 | if(expr==NULL){ | 430 | if (expr == NULL) { |
437 | re=malloc(sizeof(char)*(strlen("()")+strlen(next)+1)); | 431 | re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); |
438 | if(!re) die(STATE_UNKNOWN, "malloc failed!\n"); | 432 | if (!re) |
433 | die(STATE_UNKNOWN, "malloc failed!\n"); | ||
439 | sprintf(re, "(%s)", next); | 434 | sprintf(re, "(%s)", next); |
440 | } else { | 435 | } else { |
441 | /* resize it, adding an extra char for the new '|' separator */ | 436 | /* resize it, adding an extra char for the new '|' separator */ |
442 | re=realloc(expr, sizeof(char)*(strlen(expr)+1+strlen(next)+1)); | 437 | re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); |
443 | if(!re) die(STATE_UNKNOWN, "realloc failed!\n"); | 438 | if (!re) |
439 | die(STATE_UNKNOWN, "realloc failed!\n"); | ||
444 | /* append it starting at ')' in the old re */ | 440 | /* append it starting at ')' in the old re */ |
445 | sprintf((char*)(re+strlen(re)-1), "|%s)", next); | 441 | sprintf((char *)(re + strlen(re) - 1), "|%s)", next); |
446 | } | 442 | } |
447 | 443 | ||
448 | return re; | 444 | return re; |
449 | } | 445 | } |
450 | 446 | ||
451 | char* construct_cmdline(upgrade_type u, const char *opts){ | 447 | char *construct_cmdline(upgrade_type u, const char *opts) { |
452 | int len=0; | 448 | int len = 0; |
453 | const char *opts_ptr=NULL, *aptcmd=NULL; | 449 | const char *opts_ptr = NULL, *aptcmd = NULL; |
454 | char *cmd=NULL; | 450 | char *cmd = NULL; |
455 | 451 | ||
456 | switch(u){ | 452 | switch (u) { |
457 | case UPGRADE: | 453 | case UPGRADE: |
458 | if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS; | 454 | if (opts == NULL) |
459 | else opts_ptr=opts; | 455 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
460 | aptcmd="upgrade"; | 456 | else |
457 | opts_ptr = opts; | ||
458 | aptcmd = "upgrade"; | ||
461 | break; | 459 | break; |
462 | case DIST_UPGRADE: | 460 | case DIST_UPGRADE: |
463 | if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS; | 461 | if (opts == NULL) |
464 | else opts_ptr=opts; | 462 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
465 | aptcmd="dist-upgrade"; | 463 | else |
464 | opts_ptr = opts; | ||
465 | aptcmd = "dist-upgrade"; | ||
466 | break; | 466 | break; |
467 | case NO_UPGRADE: | 467 | case NO_UPGRADE: |
468 | if(opts==NULL) opts_ptr=UPDATE_DEFAULT_OPTS; | 468 | if (opts == NULL) |
469 | else opts_ptr=opts; | 469 | opts_ptr = UPDATE_DEFAULT_OPTS; |
470 | aptcmd="update"; | 470 | else |
471 | opts_ptr = opts; | ||
472 | aptcmd = "update"; | ||
471 | break; | 473 | break; |
472 | } | 474 | } |
473 | 475 | ||
474 | len+=strlen(PATH_TO_APTGET)+1; /* "/usr/bin/apt-get " */ | 476 | len += strlen(PATH_TO_APTGET) + 1; /* "/usr/bin/apt-get " */ |
475 | len+=strlen(opts_ptr)+1; /* "opts " */ | 477 | len += strlen(opts_ptr) + 1; /* "opts " */ |
476 | len+=strlen(aptcmd)+1; /* "upgrade\0" */ | 478 | len += strlen(aptcmd) + 1; /* "upgrade\0" */ |
477 | 479 | ||
478 | cmd=(char*)malloc(sizeof(char)*len); | 480 | cmd = (char *)malloc(sizeof(char) * len); |
479 | if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed"); | 481 | if (cmd == NULL) |
482 | die(STATE_UNKNOWN, "malloc failed"); | ||
480 | sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd); | 483 | sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd); |
481 | return cmd; | 484 | return cmd; |
482 | } | 485 | } |
483 | 486 | ||
484 | /* informative help message */ | 487 | /* informative help message */ |
485 | void | 488 | void print_help(void) { |
486 | print_help (void) | 489 | print_revision(progname, NP_VERSION); |
487 | { | 490 | |
488 | print_revision(progname, NP_VERSION); | 491 | printf(_(COPYRIGHT), copyright, email); |
489 | 492 | ||
490 | printf(_(COPYRIGHT), copyright, email); | 493 | printf("%s\n", _("This plugin checks for software updates on systems that use")); |
491 | 494 | printf("%s\n", _("package management systems based on the apt-get(8) command")); | |
492 | printf("%s\n", _("This plugin checks for software updates on systems that use")); | 495 | printf("%s\n", _("found in Debian GNU/Linux")); |
493 | printf("%s\n", _("package management systems based on the apt-get(8) command")); | 496 | |
494 | printf("%s\n", _("found in Debian GNU/Linux")); | 497 | printf("\n\n"); |
495 | 498 | ||
496 | printf ("\n\n"); | 499 | print_usage(); |
497 | 500 | ||
498 | print_usage(); | 501 | printf(UT_HELP_VRSN); |
499 | 502 | printf(UT_EXTRA_OPTS); | |
500 | printf(UT_HELP_VRSN); | 503 | |
501 | printf(UT_EXTRA_OPTS); | 504 | printf(UT_PLUG_TIMEOUT, timeout_interval); |
502 | 505 | ||
503 | printf(UT_PLUG_TIMEOUT, timeout_interval); | 506 | printf(" %s\n", "-n, --no-upgrade"); |
504 | 507 | printf(" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least).")); | |
505 | printf (" %s\n", "-n, --no-upgrade"); | 508 | printf(" %s\n", "-l, --list"); |
506 | printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least).")); | 509 | printf(" %s\n", _("List packages available for upgrade. Packages are printed sorted by")); |
507 | printf (" %s\n", "-l, --list"); | 510 | printf(" %s\n", _("name with security packages listed first.")); |
508 | printf (" %s\n", _("List packages available for upgrade. Packages are printed sorted by")); | 511 | printf(" %s\n", "-i, --include=REGEXP"); |
509 | printf (" %s\n", _("name with security packages listed first.")); | 512 | printf(" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times")); |
510 | printf (" %s\n", "-i, --include=REGEXP"); | 513 | printf(" %s\n", _("the values will be combined together. Any packages matching this list")); |
511 | printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times")); | 514 | printf(" %s\n", _("cause the plugin to return WARNING status. Others will be ignored.")); |
512 | printf (" %s\n", _("the values will be combined together. Any packages matching this list")); | 515 | printf(" %s\n", _("Default is to include all packages.")); |
513 | printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored.")); | 516 | printf(" %s\n", "-e, --exclude=REGEXP"); |
514 | printf (" %s\n", _("Default is to include all packages.")); | 517 | printf(" %s\n", _("Exclude packages matching REGEXP from the list of packages that would")); |
515 | printf (" %s\n", "-e, --exclude=REGEXP"); | 518 | printf(" %s\n", _("otherwise be included. Can be specified multiple times; the values")); |
516 | printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would")); | 519 | printf(" %s\n", _("will be combined together. Default is to exclude no packages.")); |
517 | printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values")); | 520 | printf(" %s\n", "-c, --critical=REGEXP"); |
518 | printf (" %s\n", _("will be combined together. Default is to exclude no packages.")); | 521 | printf(" %s\n", _("If the full package information of any of the upgradable packages match")); |
519 | printf (" %s\n", "-c, --critical=REGEXP"); | 522 | printf(" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified")); |
520 | printf (" %s\n", _("If the full package information of any of the upgradable packages match")); | 523 | printf(" %s\n", _("multiple times like above. Default is a regexp matching security")); |
521 | printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified")); | 524 | printf(" %s\n", _("upgrades for Debian and Ubuntu:")); |
522 | printf (" %s\n", _("multiple times like above. Default is a regexp matching security")); | 525 | printf(" \t%s\n", SECURITY_RE); |
523 | printf (" %s\n", _("upgrades for Debian and Ubuntu:")); | 526 | printf(" %s\n", _("Note that the package must first match the include list before its")); |
524 | printf (" \t%s\n", SECURITY_RE); | 527 | printf(" %s\n", _("information is compared against the critical list.")); |
525 | printf (" %s\n", _("Note that the package must first match the include list before its")); | 528 | printf(" %s\n", "-o, --only-critical"); |
526 | printf (" %s\n", _("information is compared against the critical list.")); | 529 | printf(" %s\n", _("Only warn about upgrades matching the critical list. The total number")); |
527 | printf (" %s\n", "-o, --only-critical"); | 530 | printf(" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause")); |
528 | printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number")); | 531 | printf(" %s\n", _("the plugin to return WARNING status.")); |
529 | printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause")); | 532 | printf(" %s\n", "-w, --packages-warning"); |
530 | printf (" %s\n", _("the plugin to return WARNING status.")); | 533 | printf(" %s\n", _("Minimum number of packages available for upgrade to return WARNING status.")); |
531 | printf (" %s\n", "-w, --packages-warning"); | 534 | printf(" %s\n\n", _("Default is 1 package.")); |
532 | printf (" %s\n", _("Minimum number of packages available for upgrade to return WARNING status.")); | 535 | |
533 | printf (" %s\n\n", _("Default is 1 package.")); | 536 | printf("%s\n\n", _("The following options require root privileges and should be used with care:")); |
534 | 537 | printf(" %s\n", "-u, --update=OPTS"); | |
535 | printf ("%s\n\n", _("The following options require root privileges and should be used with care:")); | 538 | printf(" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides")); |
536 | printf (" %s\n", "-u, --update=OPTS"); | 539 | printf(" %s\n", _("the default options. Note: you may also need to adjust the global")); |
537 | printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides")); | 540 | printf(" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get")); |
538 | printf (" %s\n", _("the default options. Note: you may also need to adjust the global")); | 541 | printf(" %s\n", _("upgrade is expected to take longer than the default timeout.")); |
539 | printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get")); | 542 | printf(" %s\n", "-U, --upgrade=OPTS"); |
540 | printf (" %s\n", _("upgrade is expected to take longer than the default timeout.")); | 543 | printf(" %s\n", _("Perform an upgrade. If an optional OPTS argument is provided,")); |
541 | printf (" %s\n", "-U, --upgrade=OPTS"); | 544 | printf(" %s\n", _("apt-get will be run with these command line options instead of the")); |
542 | printf (" %s\n", _("Perform an upgrade. If an optional OPTS argument is provided,")); | 545 | printf(" %s", _("default ")); |
543 | printf (" %s\n", _("apt-get will be run with these command line options instead of the")); | 546 | printf("(%s).\n", UPGRADE_DEFAULT_OPTS); |
544 | printf (" %s", _("default ")); | 547 | printf(" %s\n", _("Note that you may be required to have root privileges if you do not use")); |
545 | printf ("(%s).\n", UPGRADE_DEFAULT_OPTS); | 548 | printf(" %s\n", _("the default options, which will only run a simulation and NOT perform the upgrade")); |
546 | printf (" %s\n", _("Note that you may be required to have root privileges if you do not use")); | 549 | printf(" %s\n", "-d, --dist-upgrade=OPTS"); |
547 | printf (" %s\n", _("the default options, which will only run a simulation and NOT perform the upgrade")); | 550 | printf(" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS")); |
548 | printf (" %s\n", "-d, --dist-upgrade=OPTS"); | 551 | printf(" %s\n", _("can be provided to override the default options.")); |
549 | printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS")); | 552 | |
550 | printf (" %s\n", _("can be provided to override the default options.")); | 553 | printf(UT_SUPPORT); |
551 | |||
552 | printf(UT_SUPPORT); | ||
553 | } | 554 | } |
554 | 555 | ||
555 | |||
556 | /* simple usage heading */ | 556 | /* simple usage heading */ |
557 | void | 557 | void print_usage(void) { |
558 | print_usage(void) | 558 | printf("%s\n", _("Usage:")); |
559 | { | 559 | printf("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname); |
560 | printf ("%s\n", _("Usage:")); | ||
561 | printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname); | ||
562 | } | 560 | } |