check_apt.c 14 KB

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