check_apt.c 14 KB

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