check_apt.c 9.7 KB

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