Subversion Repositories vnag

Rev

Rev 76 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php /* <ViaThinkSoftSignature>
  2. TDF5pajkBmVclzZlc0Mqz3y4QzLmlLQH7/1OwuO0mDvrBIICenmgyTBAOxMXYrdCC
  3. F36A/6YxsrSNws2EgNU4FWm1LZuMeo4vRbT7Vz8tgxnaBmc2uxBW84QChRFIF6VSO
  4. TYG2ebNkcbUuYaCm36ryk5zyygeDzwop4msrd8yrQ3HqLW2/Xas3Rjs/1hBOxxwSh
  5. bsZ6deXEAO71BCxA8QsmHjJbMH9x0iaiF+7wbvXeFL2iepw3tpSE9n2HA6rGfd3xn
  6. gZtzMDoN8RMUfBbbx0txj9S26rgnQsmwHcYz+D+TNO/0CMplLNpZJgXyE5W2RaR5M
  7. JX4uF4859t26Btc4A1muXCydMlWUHSjDURpagoOPCGuK7SyiD5Iucly2Z+JCkbiFW
  8. hR84YmPvCKo4rIIA0M2NnNEtSxngRvB39Cnu86e0MSvFPUFfrSkU/94nv92pKw053
  9. crPDsmAPSkeqkRPHM/muuNhWHWFpxxM9RuLZHVNAovQDh+rHzLGn7qmgzNsJ/f2+H
  10. Z/2Xo5b2VjyfB8pQZQIXukA4Ikrtl1R4ah0z3fyeufuMiVcXBUOIdmtBIle5SlrsH
  11. Eq8hpVL/qvxjhNgtD/tXNGKn9ib43FAnknE+mJi1hUDCduvOVCd4IaZgCvIbIhSEt
  12. Q8Xx5nw4EWNl7AOYEYh1jzOn04/lZCFmjM0EyufrMj93Um6WPyXvROOwH6GHEKaSo
  13. cavkpMYBsrb1YFHv0+RF7Z7PoGQR6XEM/3XKH+jTsVVQaVI8DoC/JFY6WPACcq9Y4
  14. ykCW3b0MioLKXs/rU7NYEcJ360j20XnxXgR/GrMCLWwCdXJTDIOK0BdGiwpN6i2m2
  15. n1zEAp0Kn/lKeKHf6GY0g2kT6oXBXuflm+rfDBoSU0hd0Ux0dYCSlcqg2GdfpcDfo
  16. e99plg1NahSqUA4DWTz0ChlBv6GTtDhJ0QWaq+dQvUuW9bhoOPi/FNwe1xv3OuBf+
  17. opz6e51fFEpaeHG5Y+Qa9D7CrHk0z88QJWXCgCdWNboGwNaJ7pCleL9AOz5RR53Y2
  18. Fk9neknuhFv/PUfePhQZd8mhxBgbzlRpN1v8enDY4bY+A695Vmz6Lx3XWvb8os1aO
  19. CIkperWfF336r+lj3fDGDHnlqXYVrOKgiBSFBqtQZ8l4xmcxE/On8el2B95LTY4hE
  20. D4KKRPTBqanx75Ajgv0o5FVHtNripTFA2PXOv9NaEjqkvckBCHnLOcUUSfT4GCqe/
  21. EZwmgNGc8eQ6Jf09Lr4B7ppmUFD/mEx/wsaQrVwFNclFtOhJi6Y1KDBqq/I9tmB0P
  22. 31b1a4OfVuclmipIyFphLOhAkKYD7l4kpuaJ2i4ij+OAfBikVcZr+sVliDrKQhWvG
  23. Q==
  24. </ViaThinkSoftSignature> */ ?>
  25. <?php
  26.  
  27. /*
  28.  * VNag - Nagios Framework for PHP
  29.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  30.  * Licensed under the terms of the Apache 2.0 license
  31.  *
  32.  * Revision 2023-10-13
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class ViewVCVersionCheck extends VNag {
  38.         protected $argSystemDir = null;
  39.  
  40.         public function __construct() {
  41.                 parent::__construct();
  42.  
  43.                 $this->registerExpectedStandardArguments('Vht');
  44.  
  45.                 $this->getHelpManager()->setPluginName('check_viewvc_version');
  46.                 $this->getHelpManager()->setVersion('1.0');
  47.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local ViewVC system has the latest version installed.');
  48.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  49.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  50.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  51.  
  52.                 // Individual (non-standard) arguments:
  53.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'viewvcPath', 'The local directory where your ViewVC installation is located.'));
  54.         }
  55.  
  56.         protected function get_local_version($path) {
  57.                 $path = realpath($path) === false ? $path : realpath($path);
  58.  
  59.                 if (!file_exists("$path/lib/viewvc.py")) {
  60.                         throw new VNagException("Cannot find ViewVC settings file at $path");
  61.                 }
  62.  
  63.                 $cont = @file_get_contents("$path/lib/viewvc.py");
  64.                 if ($cont === false) {
  65.                         throw new VNagException("Cannot determine version for system $path (cannot read viewvc.py)");
  66.                 }
  67.                 if (!preg_match('@__version__\\s*=\\s*([\'"])(.+)\\1@ismU', $cont, $m)) {
  68.                         throw new VNagException("Cannot determine version for system $path (cannot find version string)");
  69.                 }
  70.  
  71.                 return $m[2]; // e.g. "1.3.0-dev"
  72.         }
  73.  
  74.         protected function get_latest_version() {
  75.                 $cont = $this->url_get_contents('https://api.github.com/repos/viewvc/viewvc/releases/latest');
  76.                 if ($cont === false) {
  77.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Cannot access api.github.com)');
  78.                 }
  79.  
  80.                 $data = @json_decode($cont, true);
  81.                 if ($data === false) {
  82.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Invalid JSON data downloaded from api.github.com)');
  83.                 }
  84.  
  85.                 return $data['tag_name']; // e.g. "1.2.1"
  86.         }
  87.  
  88.         protected function cbRun($optional_args=array()) {
  89.                 $system_dir = $this->argSystemDir->getValue();
  90.                 if (empty($system_dir)) {
  91.                         throw new VNagException("Please specify the directory of the ViewVC installation.");
  92.                 }
  93.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  94.  
  95.                 if (!is_dir($system_dir)) {
  96.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  97.                 }
  98.  
  99.                 $local_version = $this->get_local_version($system_dir);
  100.  
  101.                 $latest_stable = $this->get_latest_version();
  102.  
  103.                 // Note: version_compare() correctly assumes that 1.3.0-dev is higher than 1.3.0
  104.                 if (version_compare($local_version,$latest_stable,'>')) {
  105.                         $this->setStatus(VNag::STATUS_OK);
  106.                         $this->setHeadline("Version $local_version (Latest stable version $latest_stable) at $system_dir", true);
  107.                 } else if (version_compare($local_version,$latest_stable,'=')) {
  108.                         $this->setStatus(VNag::STATUS_OK);
  109.                         $this->setHeadline("Version $local_version (Latest stable version) at $system_dir", true);
  110.                 } else {
  111.                         $this->setStatus(VNag::STATUS_WARNING);
  112.                         $this->setHeadline("Version $local_version is outdated (Latest version is $latest_stable) at $system_dir", true);
  113.                 }
  114.         }
  115. }
  116.