check_apt.c 14 KB

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