check_apt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*****************************************************************************
  2. *
  3. * Nagios check_apt plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2006-2008 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-2008";
  33. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  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 upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
  70. static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
  71. static char *update_opts = NULL; /* options to override defaults for update */
  72. static char *do_include = NULL; /* regexp to only include certain packages */
  73. static char *do_exclude = NULL; /* regexp to only exclude certain packages */
  74. static char *do_critical = NULL; /* regexp specifying critical packages */
  75. static char *input_filename = NULL; /* input filename for testing */
  76. /* other global variables */
  77. static int stderr_warning = 0; /* if a cmd issued output on stderr */
  78. static int exec_warning = 0; /* if a cmd exited non-zero */
  79. int main (int argc, char **argv) {
  80. int result=STATE_UNKNOWN, packages_available=0, sec_count=0;
  81. /* Parse extra opts if any */
  82. argv=np_extra_opts(&argc, argv, progname);
  83. if (process_arguments(argc, argv) == ERROR)
  84. usage_va(_("Could not parse arguments"));
  85. /* Set signal handling and alarm timeout */
  86. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  87. usage_va(_("Cannot catch SIGALRM"));
  88. }
  89. /* handle timeouts gracefully... */
  90. alarm (timeout_interval);
  91. /* if they want to run apt-get update first... */
  92. if(do_update) result = run_update();
  93. /* apt-get upgrade */
  94. result = max_state(result, run_upgrade(&packages_available, &sec_count));
  95. if(sec_count > 0){
  96. result = max_state(result, STATE_CRITICAL);
  97. } else if(packages_available > 0){
  98. result = max_state(result, STATE_WARNING);
  99. } else if(result > STATE_UNKNOWN){
  100. result = STATE_UNKNOWN;
  101. }
  102. printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n"),
  103. state_text(result),
  104. packages_available,
  105. (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade",
  106. sec_count,
  107. (stderr_warning)?" warnings detected":"",
  108. (stderr_warning && exec_warning)?",":"",
  109. (exec_warning)?" errors detected":"",
  110. (stderr_warning||exec_warning)?". run with -v for information.":""
  111. );
  112. return result;
  113. }
  114. /* process command-line arguments */
  115. int process_arguments (int argc, char **argv) {
  116. int c;
  117. static struct option longopts[] = {
  118. {"version", no_argument, 0, 'V'},
  119. {"help", no_argument, 0, 'h'},
  120. {"verbose", no_argument, 0, 'v'},
  121. {"timeout", required_argument, 0, 't'},
  122. {"update", optional_argument, 0, 'u'},
  123. {"upgrade", optional_argument, 0, 'U'},
  124. {"no-upgrade", no_argument, 0, 'n'},
  125. {"dist-upgrade", optional_argument, 0, 'd'},
  126. {"include", required_argument, 0, 'i'},
  127. {"exclude", required_argument, 0, 'e'},
  128. {"critical", required_argument, 0, 'c'},
  129. {"input-file", required_argument, 0, INPUT_FILE_OPT},
  130. {0, 0, 0, 0}
  131. };
  132. while(1) {
  133. c = getopt_long(argc, argv, "hVvt:u::U::d::ni:e:c:", longopts, NULL);
  134. if(c == -1 || c == EOF || c == 1) break;
  135. switch(c) {
  136. case 'h':
  137. print_help();
  138. exit(STATE_OK);
  139. case 'V':
  140. print_revision(progname, NP_VERSION);
  141. exit(STATE_OK);
  142. case 'v':
  143. verbose++;
  144. break;
  145. case 't':
  146. timeout_interval=atoi(optarg);
  147. break;
  148. case 'd':
  149. upgrade=DIST_UPGRADE;
  150. if(optarg!=NULL){
  151. upgrade_opts=strdup(optarg);
  152. if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
  153. }
  154. break;
  155. case 'U':
  156. upgrade=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 'n':
  163. upgrade=NO_UPGRADE;
  164. break;
  165. case 'u':
  166. do_update=1;
  167. if(optarg!=NULL){
  168. update_opts=strdup(optarg);
  169. if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
  170. }
  171. break;
  172. case 'i':
  173. do_include=add_to_regexp(do_include, optarg);
  174. break;
  175. case 'e':
  176. do_exclude=add_to_regexp(do_exclude, optarg);
  177. break;
  178. case 'c':
  179. do_critical=add_to_regexp(do_critical, optarg);
  180. break;
  181. case INPUT_FILE_OPT:
  182. input_filename = optarg;
  183. break;
  184. default:
  185. /* print short usage statement if args not parsable */
  186. usage5();
  187. }
  188. }
  189. return OK;
  190. }
  191. /* run an apt-get upgrade */
  192. int run_upgrade(int *pkgcount, int *secpkgcount){
  193. int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
  194. struct output chld_out, chld_err;
  195. regex_t ireg, ereg, sreg;
  196. char *cmdline=NULL, rerrbuf[64];
  197. if(upgrade==NO_UPGRADE) return STATE_OK;
  198. /* compile the regexps */
  199. if (do_include != NULL) {
  200. regres=regcomp(&ireg, do_include, REG_EXTENDED);
  201. if (regres!=0) {
  202. regerror(regres, &ireg, rerrbuf, 64);
  203. die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
  204. }
  205. }
  206. if(do_exclude!=NULL){
  207. regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
  208. if(regres!=0) {
  209. regerror(regres, &ereg, rerrbuf, 64);
  210. die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
  211. progname, rerrbuf);
  212. }
  213. }
  214. const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE;
  215. regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
  216. if(regres!=0) {
  217. regerror(regres, &ereg, rerrbuf, 64);
  218. die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
  219. progname, rerrbuf);
  220. }
  221. cmdline=construct_cmdline(upgrade, upgrade_opts);
  222. if (input_filename != NULL) {
  223. /* read input from a file for testing */
  224. result = cmd_file_read(input_filename, &chld_out, 0);
  225. } else {
  226. /* run the upgrade */
  227. result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
  228. }
  229. /* apt-get upgrade only changes exit status if there is an
  230. * internal error when run in dry-run mode. therefore we will
  231. * treat such an error as UNKNOWN */
  232. if(result != 0){
  233. exec_warning=1;
  234. result = STATE_UNKNOWN;
  235. fprintf(stderr, _("'%s' exited with non-zero status.\n"),
  236. cmdline);
  237. }
  238. /* parse the output, which should only consist of lines like
  239. *
  240. * Inst package ....
  241. * Conf package ....
  242. *
  243. * so we'll filter based on "Inst" for the time being. later
  244. * we may need to switch to the --print-uris output format,
  245. * in which case the logic here will slightly change.
  246. */
  247. for(i = 0; i < chld_out.lines; i++) {
  248. if(verbose){
  249. printf("%s\n", chld_out.line[i]);
  250. }
  251. /* if it is a package we care about */
  252. if (strncmp(PKGINST_PREFIX, chld_out.line[i], strlen(PKGINST_PREFIX)) == 0 &&
  253. (do_include == NULL || regexec(&ireg, chld_out.line[i], 0, NULL, 0) == 0)) {
  254. /* if we're not excluding, or it's not in the
  255. * list of stuff to exclude */
  256. if(do_exclude==NULL ||
  257. regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
  258. pc++;
  259. if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
  260. spc++;
  261. if(verbose) printf("*");
  262. }
  263. if(verbose){
  264. printf("*%s\n", chld_out.line[i]);
  265. }
  266. }
  267. }
  268. }
  269. *pkgcount=pc;
  270. *secpkgcount=spc;
  271. /* If we get anything on stderr, at least set warning */
  272. if (input_filename == NULL && chld_err.buflen) {
  273. stderr_warning=1;
  274. result = max_state(result, STATE_WARNING);
  275. if(verbose){
  276. for(i = 0; i < chld_err.lines; i++) {
  277. fprintf(stderr, "%s\n", chld_err.line[i]);
  278. }
  279. }
  280. }
  281. if (do_include != NULL) regfree(&ireg);
  282. regfree(&sreg);
  283. if(do_exclude!=NULL) regfree(&ereg);
  284. free(cmdline);
  285. return result;
  286. }
  287. /* run an apt-get update (needs root) */
  288. int run_update(void){
  289. int i=0, result=STATE_UNKNOWN;
  290. struct output chld_out, chld_err;
  291. char *cmdline;
  292. /* run the upgrade */
  293. cmdline = construct_cmdline(NO_UPGRADE, update_opts);
  294. result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
  295. /* apt-get update changes exit status if it can't fetch packages.
  296. * since we were explicitly asked to do so, this is treated as
  297. * a critical error. */
  298. if(result != 0){
  299. exec_warning=1;
  300. result = STATE_CRITICAL;
  301. fprintf(stderr, _("'%s' exited with non-zero status.\n"),
  302. cmdline);
  303. }
  304. if(verbose){
  305. for(i = 0; i < chld_out.lines; i++) {
  306. printf("%s\n", chld_out.line[i]);
  307. }
  308. }
  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. free(cmdline);
  320. return result;
  321. }
  322. char* add_to_regexp(char *expr, const char *next){
  323. char *re=NULL;
  324. if(expr==NULL){
  325. re=malloc(sizeof(char)*(strlen("()")+strlen(next)+1));
  326. if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
  327. sprintf(re, "(%s)", next);
  328. } else {
  329. /* resize it, adding an extra char for the new '|' separator */
  330. re=realloc(expr, sizeof(char)*(strlen(expr)+1+strlen(next)+1));
  331. if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
  332. /* append it starting at ')' in the old re */
  333. sprintf((char*)(re+strlen(re)-1), "|%s)", next);
  334. }
  335. return re;
  336. }
  337. char* construct_cmdline(upgrade_type u, const char *opts){
  338. int len=0;
  339. const char *opts_ptr=NULL, *aptcmd=NULL;
  340. char *cmd=NULL;
  341. switch(u){
  342. case UPGRADE:
  343. if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
  344. else opts_ptr=opts;
  345. aptcmd="upgrade";
  346. break;
  347. case DIST_UPGRADE:
  348. if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
  349. else opts_ptr=opts;
  350. aptcmd="dist-upgrade";
  351. break;
  352. case NO_UPGRADE:
  353. if(opts==NULL) opts_ptr=UPDATE_DEFAULT_OPTS;
  354. else opts_ptr=opts;
  355. aptcmd="update";
  356. break;
  357. }
  358. len+=strlen(PATH_TO_APTGET)+1; /* "/usr/bin/apt-get " */
  359. len+=strlen(opts_ptr)+1; /* "opts " */
  360. len+=strlen(aptcmd)+1; /* "upgrade\0" */
  361. cmd=(char*)malloc(sizeof(char)*len);
  362. if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed");
  363. sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
  364. return cmd;
  365. }
  366. /* informative help message */
  367. void
  368. print_help (void)
  369. {
  370. print_revision(progname, NP_VERSION);
  371. printf(_(COPYRIGHT), copyright, email);
  372. printf("%s\n", _("This plugin checks for software updates on systems that use"));
  373. printf("%s\n", _("package management systems based on the apt-get(8) command"));
  374. printf("%s\n", _("found in Debian GNU/Linux"));
  375. printf ("\n\n");
  376. print_usage();
  377. printf(UT_HELP_VRSN);
  378. printf(UT_EXTRA_OPTS);
  379. printf(UT_TIMEOUT, timeout_interval);
  380. printf (" %s\n", "-U, --upgrade=OPTS");
  381. printf (" %s\n", _("[Default] Perform an upgrade. If an optional OPTS argument is provided,"));
  382. printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
  383. printf (" %s", _("default "));
  384. printf ("(%s).\n", UPGRADE_DEFAULT_OPTS);
  385. printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
  386. printf (" %s\n", _("the default options."));
  387. printf (" %s\n", "-d, --dist-upgrade=OPTS");
  388. printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
  389. printf (" %s\n", _("can be provided to override the default options."));
  390. printf (" %s\n", " -n, --no-upgrade");
  391. printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
  392. printf (" %s\n", "-i, --include=REGEXP");
  393. printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
  394. printf (" %s\n", _("the values will be combined together. Any packages matching this list"));
  395. printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
  396. printf (" %s\n", _("Default is to include all packages."));
  397. printf (" %s\n", "-e, --exclude=REGEXP");
  398. printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
  399. printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
  400. printf (" %s\n", _("will be combined together. Default is to exclude no packages."));
  401. printf (" %s\n", "-c, --critical=REGEXP");
  402. printf (" %s\n", _("If the full package information of any of the upgradable packages match"));
  403. printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
  404. printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
  405. printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
  406. printf (" \t\%s\n", SECURITY_RE);
  407. printf (" %s\n", _("Note that the package must first match the include list before its"));
  408. printf (" %s\n\n", _("information is compared against the critical list."));
  409. printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
  410. printf (" %s\n", "-u, --update=OPTS");
  411. printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
  412. printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
  413. printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
  414. printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
  415. printf(UT_SUPPORT);
  416. }
  417. /* simple usage heading */
  418. void
  419. print_usage(void)
  420. {
  421. printf ("%s\n", _("Usage:"));
  422. printf ("%s [[-d|-u|-U]opts] [-n] [-t timeout]\n", progname);
  423. }