4
0

check_apt.c 16 KB

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