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