check_apt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #define APTGET_UPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq upgrade"
  27. #define APTGET_DISTUPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq dist-upgrade"
  28. #define APTGET_UPDATE "/usr/bin/apt-get update"
  29. int process_arguments(int, char **);
  30. void print_help(void);
  31. void print_usage(void);
  32. int run_upgrade(int *pkgcount);
  33. static int verbose = 0;
  34. int main (int argc, char **argv) {
  35. int result=STATE_UNKNOWN, packages_available=0;
  36. if (process_arguments(argc, argv) == ERROR)
  37. usage_va(_("Could not parse arguments"));
  38. /* Set signal handling and alarm timeout */
  39. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  40. usage_va(_("Cannot catch SIGALRM"));
  41. }
  42. /* handle timeouts gracefully... */
  43. alarm (timeout_interval);
  44. /* apt-get upgrade */
  45. result = run_upgrade(&packages_available);
  46. if(packages_available > 0){
  47. result = STATE_WARNING;
  48. printf("APT WARNING: ");
  49. } else {
  50. result = STATE_OK;
  51. printf("APT OK: ");
  52. }
  53. printf("%d packages available for upgrade\n", packages_available);
  54. return result;
  55. }
  56. /* process command-line arguments */
  57. int process_arguments (int argc, char **argv) {
  58. int c;
  59. static struct option longopts[] = {
  60. {"version", no_argument, 0, 'V'},
  61. {"help", no_argument, 0, 'h'},
  62. {"verbose", no_argument, 0, 'v'},
  63. {"timeout", required_argument, 0, 't'},
  64. {0, 0, 0, 0}
  65. };
  66. while(1) {
  67. c = getopt_long(argc, argv, "hVvt", longopts, NULL);
  68. if(c == -1 || c == EOF || c == 1) break;
  69. switch(c) {
  70. case 'h': /* help */
  71. print_help();
  72. exit(STATE_OK);
  73. case 'V': /* version */
  74. print_revision(progname, revision);
  75. exit(STATE_OK);
  76. case 'v':
  77. verbose++;
  78. break;
  79. case 't':
  80. timeout_interval=atoi(optarg);
  81. break;
  82. default:
  83. /* print short usage statement if args not parsable */
  84. usage_va(_("Unknown argument - %s"), optarg);
  85. }
  86. }
  87. return OK;
  88. }
  89. /* informative help message */
  90. void print_help(void){
  91. print_revision(progname, revision);
  92. printf(_(COPYRIGHT), copyright, email);
  93. printf(_("\
  94. This plugin checks for software updates on systems that use\n\
  95. package management systems based on the apt-get(8) command\n\
  96. found in Debian GNU/Linux\n\
  97. \n\n"));
  98. print_usage();
  99. printf(_(UT_HELP_VRSN));
  100. printf(_("\
  101. -t, --timeout=INTEGER\n\
  102. Seconds to wait for plugin execution to complete\n\
  103. "));
  104. }
  105. /* simple usage heading */
  106. void print_usage(void){
  107. printf ("Usage: %s [-u] [-t timeout]\n", progname);
  108. }
  109. /* run an apt-get upgrade */
  110. int run_upgrade(int *pkgcount){
  111. int i=0, result=STATE_UNKNOWN, pc=0;
  112. struct output chld_out, chld_err;
  113. /* run the upgrade */
  114. if((result = np_runcmd(APTGET_UPGRADE, &chld_out, &chld_err, 0)) != 0)
  115. result = STATE_WARNING;
  116. /* parse the output, which should only consist of lines like
  117. *
  118. * Inst package ....
  119. * Conf package ....
  120. *
  121. * so we'll filter based on "Inst"
  122. */
  123. for(i = 0; i < chld_out.lines; i++) {
  124. if(strncmp(chld_out.line[i], "Inst", 4)==0){
  125. if(verbose){
  126. printf("%s\n", chld_out.line[i]);
  127. }
  128. pc++;
  129. }
  130. }
  131. *pkgcount=pc;
  132. /* If we get anything on stderr, at least set warning */
  133. if(chld_err.buflen){
  134. fprintf(stderr, "warning, output detected on stderr\n");
  135. for(i = 0; i < chld_err.lines; i++) {
  136. printf("got this: %s\n", chld_err.line[i]);
  137. result = max_state (result, STATE_WARNING);
  138. }
  139. }
  140. return result;
  141. }