check_apt.c 14 KB

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