check_apt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /* for now define the various apt calls as constants. this may need
  28. * to change later. */
  29. #define APTGET_UPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq upgrade"
  30. #define APTGET_DISTUPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq dist-upgrade"
  31. #define APTGET_UPDATE "/usr/bin/apt-get -q update"
  32. #define SECURITY_RE "^[^\\(]*\\([^ ]* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
  33. /* some standard functions */
  34. int process_arguments(int, char **);
  35. void print_help(void);
  36. void print_usage(void);
  37. /* run an apt-get update */
  38. int run_update(void);
  39. /* run an apt-get upgrade */
  40. int run_upgrade(int *pkgcount, int *secpkgcount);
  41. /* add another clause to a regexp */
  42. char* add_to_regexp(char *expr, const char *next);
  43. /* configuration variables */
  44. static int verbose = 0; /* -v */
  45. static int do_update = 0; /* whether to call apt-get update */
  46. static int dist_upgrade = 0; /* whether to call apt-get dist-upgrade */
  47. static char* do_include = NULL; /* regexp to only include certain packages */
  48. static char* do_exclude = NULL; /* regexp to only exclude certain packages */
  49. /* other global variables */
  50. static int stderr_warning = 0; /* if a cmd issued output on stderr */
  51. static int exec_warning = 0; /* if a cmd exited non-zero */
  52. int main (int argc, char **argv) {
  53. int result=STATE_UNKNOWN, packages_available=0, sec_count=0;
  54. if (process_arguments(argc, argv) == ERROR)
  55. usage_va(_("Could not parse arguments"));
  56. /* Set signal handling and alarm timeout */
  57. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  58. usage_va(_("Cannot catch SIGALRM"));
  59. }
  60. /* handle timeouts gracefully... */
  61. alarm (timeout_interval);
  62. /* if they want to run apt-get update first... */
  63. if(do_update) result = run_update();
  64. /* apt-get upgrade */
  65. result = max_state(result, run_upgrade(&packages_available, &sec_count));
  66. if(sec_count > 0){
  67. result = max_state(result, STATE_CRITICAL);
  68. } else if(packages_available > 0){
  69. result = max_state(result, STATE_WARNING);
  70. } else {
  71. result = max_state(result, STATE_OK);
  72. }
  73. printf("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s\n",
  74. state_text(result),
  75. packages_available,
  76. (dist_upgrade)?"dist-upgrade":"upgrade",
  77. sec_count,
  78. (stderr_warning)?" warnings detected":"",
  79. (stderr_warning && exec_warning)?",":"",
  80. (exec_warning)?" errors detected":"",
  81. (stderr_warning||exec_warning)?". run with -v for information.":""
  82. );
  83. return result;
  84. }
  85. /* process command-line arguments */
  86. int process_arguments (int argc, char **argv) {
  87. int c;
  88. static struct option longopts[] = {
  89. {"version", no_argument, 0, 'V'},
  90. {"help", no_argument, 0, 'h'},
  91. {"verbose", no_argument, 0, 'v'},
  92. {"timeout", required_argument, 0, 't'},
  93. {"update", no_argument, 0, 'u'},
  94. {"dist-upgrade", no_argument, 0, 'd'},
  95. {"include", no_argument, 0, 'i'},
  96. {"exclude", no_argument, 0, 'e'},
  97. {0, 0, 0, 0}
  98. };
  99. while(1) {
  100. c = getopt_long(argc, argv, "hVvt:udi:e:", longopts, NULL);
  101. if(c == -1 || c == EOF || c == 1) break;
  102. switch(c) {
  103. case 'h':
  104. print_help();
  105. exit(STATE_OK);
  106. case 'V':
  107. print_revision(progname, revision);
  108. exit(STATE_OK);
  109. case 'v':
  110. verbose++;
  111. break;
  112. case 't':
  113. timeout_interval=atoi(optarg);
  114. break;
  115. case 'd':
  116. dist_upgrade=1;
  117. break;
  118. case 'u':
  119. do_update=1;
  120. break;
  121. case 'i':
  122. do_include=add_to_regexp(do_include, optarg);
  123. break;
  124. case 'e':
  125. do_exclude=add_to_regexp(do_exclude, optarg);
  126. break;
  127. default:
  128. /* print short usage statement if args not parsable */
  129. usage_va(_("Unknown argument - %s"), optarg);
  130. }
  131. }
  132. return OK;
  133. }
  134. /* informative help message */
  135. void print_help(void){
  136. print_revision(progname, revision);
  137. printf(_(COPYRIGHT), copyright, email);
  138. printf(_("\
  139. This plugin checks for software updates on systems that use\n\
  140. package management systems based on the apt-get(8) command\n\
  141. found in Debian GNU/Linux\n\
  142. \n\n"));
  143. print_usage();
  144. printf(_(UT_HELP_VRSN));
  145. printf(_(UT_TIMEOUT), timeout_interval);
  146. printf(_("\n\
  147. -d, --dist-upgrade\n\
  148. Perform a dist-upgrade instead of normal upgrade.\n\
  149. -i, --include=REGEXP\n\
  150. Include only packages matching REGEXP. Can be specified multiple times;\n\
  151. the values will be combined together. Default is to include all packages.\n\
  152. -e, --exclude=REGEXP\n\
  153. Exclude packages matching REGEXP from the list of packages that would\n\
  154. otherwise be excluded. Can be specified multiple times; the values\n\
  155. will be combined together. Default is to exclude no packages.\n\n"));
  156. printf(_("\
  157. The following options require root privileges and should be used with care: \
  158. \n\n"));
  159. printf(_("\
  160. -u, --update\n\
  161. First perform an 'apt-get update' (note: you may also need to use -t)\
  162. \n\n"));
  163. }
  164. /* simple usage heading */
  165. void print_usage(void){
  166. printf ("Usage: %s [-du] [-t timeout]\n", progname);
  167. }
  168. /* run an apt-get upgrade */
  169. int run_upgrade(int *pkgcount, int *secpkgcount){
  170. int i=0, result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
  171. struct output chld_out, chld_err;
  172. regex_t ireg, ereg, sreg;
  173. char rerrbuf[64];
  174. const char *default_include_expr="^Inst";
  175. /* compile the regexps */
  176. if(do_include!=NULL){
  177. regres=regcomp(&ireg, do_include, REG_EXTENDED);
  178. if(regres!=0) {
  179. regerror(regres, &ireg, rerrbuf, 64);
  180. die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
  181. progname, rerrbuf);
  182. }
  183. } else {
  184. regres=regcomp(&ireg, default_include_expr, REG_EXTENDED);
  185. if(regres!=0) {
  186. regerror(regres, &ireg, rerrbuf, 64);
  187. die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
  188. progname, rerrbuf);
  189. }
  190. }
  191. if(do_exclude!=NULL){
  192. regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
  193. if(regres!=0) {
  194. regerror(regres, &ereg, rerrbuf, 64);
  195. die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
  196. progname, rerrbuf);
  197. }
  198. }
  199. regres=regcomp(&sreg, SECURITY_RE, REG_EXTENDED);
  200. if(regres!=0) {
  201. regerror(regres, &ereg, rerrbuf, 64);
  202. die(STATE_UNKNOWN, "%s: Error compiling regexp: %s",
  203. progname, rerrbuf);
  204. }
  205. /* run the upgrade */
  206. if(dist_upgrade==0){
  207. result = np_runcmd(APTGET_UPGRADE, &chld_out, &chld_err, 0);
  208. } else {
  209. result = np_runcmd(APTGET_DISTUPGRADE, &chld_out, &chld_err, 0);
  210. }
  211. /* apt-get upgrade only changes exit status if there is an
  212. * internal error when run in dry-run mode. therefore we will
  213. * treat such an error as UNKNOWN */
  214. if(result != 0){
  215. exec_warning=1;
  216. result = STATE_UNKNOWN;
  217. fprintf(stderr, "'%s' exited with non-zero status.\n",
  218. APTGET_UPGRADE);
  219. }
  220. /* parse the output, which should only consist of lines like
  221. *
  222. * Inst package ....
  223. * Conf package ....
  224. *
  225. * so we'll filter based on "Inst" for the time being. later
  226. * we may need to switch to the --print-uris output format,
  227. * in which case the logic here will slightly change.
  228. */
  229. for(i = 0; i < chld_out.lines; i++) {
  230. if(verbose){
  231. printf("%s\n", chld_out.line[i]);
  232. }
  233. /* if it is a package we care about */
  234. if(regexec(&ireg, chld_out.line[i], 0, NULL, 0)==0){
  235. /* if we're not excluding, or it's not in the
  236. * list of stuff to exclude */
  237. if(do_exclude==NULL ||
  238. regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
  239. pc++;
  240. if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
  241. spc++;
  242. }
  243. if(verbose){
  244. printf("*%s\n", chld_out.line[i]);
  245. }
  246. }
  247. }
  248. }
  249. *pkgcount=pc;
  250. *secpkgcount=spc;
  251. /* If we get anything on stderr, at least set warning */
  252. if(chld_err.buflen){
  253. stderr_warning=1;
  254. result = max_state(result, STATE_WARNING);
  255. if(verbose){
  256. for(i = 0; i < chld_err.lines; i++) {
  257. fprintf(stderr, "%s\n", chld_err.line[i]);
  258. }
  259. }
  260. }
  261. return result;
  262. }
  263. /* run an apt-get update (needs root) */
  264. int run_update(void){
  265. int i=0, result=STATE_UNKNOWN;
  266. struct output chld_out, chld_err;
  267. /* run the upgrade */
  268. result = np_runcmd(APTGET_UPDATE, &chld_out, &chld_err, 0);
  269. /* apt-get update changes exit status if it can't fetch packages.
  270. * since we were explicitly asked to do so, this is treated as
  271. * a critical error. */
  272. if(result != 0){
  273. exec_warning=1;
  274. result = STATE_CRITICAL;
  275. fprintf(stderr, "'%s' exited with non-zero status.\n",
  276. APTGET_UPDATE);
  277. }
  278. if(verbose){
  279. for(i = 0; i < chld_out.lines; i++) {
  280. printf("%s\n", chld_out.line[i]);
  281. }
  282. }
  283. /* If we get anything on stderr, at least set warning */
  284. if(chld_err.buflen){
  285. stderr_warning=1;
  286. result = max_state(result, STATE_WARNING);
  287. if(verbose){
  288. for(i = 0; i < chld_err.lines; i++) {
  289. fprintf(stderr, "%s\n", chld_err.line[i]);
  290. }
  291. }
  292. }
  293. return result;
  294. }
  295. char* add_to_regexp(char *expr, const char *next){
  296. char *re=NULL;
  297. if(expr==NULL){
  298. re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
  299. if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
  300. sprintf(re, "^Inst (%s) ", next);
  301. } else {
  302. /* resize it, adding an extra char for the new '|' separator */
  303. re=realloc(expr, sizeof(char)*strlen(expr)+1+strlen(next)+1);
  304. if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
  305. /* append it starting at ')' in the old re */
  306. sprintf((char*)(re+strlen(re)-2), "|%s) ", next);
  307. }
  308. return re;
  309. }