Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. CmjFiZLK2FpfPQ5dciAKEBihjx7LF9tbFssLowaSzj20S0kXMd/vQEZTVivanUr2+
  3. sCRxMjiFbCZT2X7YurXKAvvr2i/Rdh5lyedBddXrwrbhrGhomKtE/iKGHsJuUIyNs
  4. 1GEOY8m+mQD5Dxf7GFE4xdGUkr8wRc/whKv/f4VTVm3S1PCBWlEfv3RHqfkOw0x1d
  5. g5Yo30yTk5nEGZuoxRjCQPkXgvPLHoiRcsKq22xzZdCREL0ujlK7AGWuPIVOxk7q8
  6. qmtzF48LIfsewV3O6U4uW1yfJyqwbHFtMCpePxHKeuTkkOs7ZLFwEL7vOaKvMwKcf
  7. F70BW62yltBl02IliqsCIluQ177KXf2RiXduj19sU71Gb483J1JjK2/wu2SNJg6oH
  8. GEcaQ2bwOA+Ymj4EmZsczTJLmKuf0fPYVdUFY1gzpmAg7tyT6HO5r9SgIw9Yfcbbq
  9. ExcCGFMYPVMWG4ahddZDDjGhbDlXbFhRikzO7EOvea0K1IWb3moavg2GOCtDkQy7B
  10. YnErR/3sL2q7bzaAW68Rlzg6jVm/DGH1apx1+FLFr+DEoFs6TS//JW8sDiqCcoU8S
  11. K72tyWsN3wz1qAVoFn5inm/uqk8mV+rlht4jBoTnYedz1xbdequkPCX2Y2UCMEp00
  12. PGf4oP/oUJ5UwT3/AC3XX+kDNDIDEOHQtzIhTg4CPnno2Y21uULgVb94RPr8zJ4gV
  13. LtWZd6B5vso9LJJaOrYgL5SjkVxLfMEJvOQV97S0ZTsbZ4gYjBzRnBOARmsz5858V
  14. YFdvv6i3xNdPbF6NH3icQIZeVScKEdBPd48d8nSBx4DSJQlzuSYv4ANc+YH+adDv+
  15. 52jk1o4d73vvraqcwSWHWIE1DB3ma1DgDfCiCUqubboyh3OxgNIshEZqClNE6xDTa
  16. v1iViJbS4j76tpGmPyFFba/tzIoJigBfECYAIDEmQlKLui/18tSeZizymQDHySLpj
  17. yTQir2F2HyVFYE/CH9RV0QigP+Vx+g4uXEN0OPGCE7Lh6V/d6Yg5mBuYWHd0z53+Y
  18. +HoE0bPGcAkRKSPLWQe8Nk5yDnJPyz7sG9XHpvoRl2NxDhW78Xr6yFhhGuHu0W92K
  19. 4xiVRCd8UDWdmrv8uwUdubP8bPtDsTdll6MGVZj5zr1n/tI7dKj8M0FITpW0o0o8u
  20. rpc0NiH0iu/NRTzeqrGehA14SOrM/q63+jpX76iCBIWJG2QXvyd/lBdfNI/SASN6o
  21. Pat9Itt0QC7YH1g9/15d1rcPxVEZua3m+rjMtdqnyRgLQqEH6DWkDW1ixnX7EdVnP
  22. 0kiVuMKY38aSkTfEAcG6utYsDY2LY/AkOvOItfXBMuTxiddC4VffmissJHHP+Y3jl
  23. w==
  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 2022-12-18
  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 Exception("Cannot find ViewVC settings file at $path");
  61.                 }
  62.  
  63.                 $cont = @file_get_contents("$path/lib/viewvc.py");
  64.  
  65.                 if (!preg_match('@__version__\\s*=\\s*([\'"])(.+)\\1@ismU', $cont, $m)) {
  66.                         throw new Exception("Cannot determine version for system $path");
  67.                 }
  68.  
  69.                 return $m[2]; // e.g. "1.3.0-dev"
  70.         }
  71.  
  72.         protected function get_latest_version() {
  73.                 $url = 'https://api.github.com/repos/viewvc/viewvc/releases/latest';
  74.                 $max_cache_time = 24 * 60 * 60;
  75.                 $cache_file = $this->get_cache_dir().'/'.sha1($url);
  76.                 if (file_exists($cache_file) && (time()-filemtime($cache_file) < $max_cache_time)) {
  77.                         $cont = @file_get_contents($cache_file);
  78.                         if (!$cont) throw new Exception("Failed to get contents from $cache_file");
  79.                 } else {
  80.                         $options = array(
  81.                           'http'=>array(
  82.                             'method'=>"GET",
  83.                             'header'=>"Accept-language: en\r\n" .
  84.                                       "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"
  85.                           )
  86.                         );
  87.                         $context = stream_context_create($options);
  88.                         $cont = @file_get_contents($url, false, $context);
  89.                         if (!$cont) throw new Exception("Failed to get contents from $url");
  90.                         file_put_contents($cache_file, $cont);
  91.                 }
  92.  
  93.                 $data = @json_decode($cont, true);
  94.                 if (!$data) {
  95.                         throw new Exception('Cannot parse version from GitHub API. The plugin probably needs to be updated. B');
  96.                 }
  97.  
  98.                 return $data['tag_name']; // e.g. "1.2.1"
  99.         }
  100.  
  101.         protected function cbRun($optional_args=array()) {
  102.                 $system_dir = $this->argSystemDir->getValue();
  103.                 if (empty($system_dir)) {
  104.                         throw new Exception("Please specify the directory of the ViewVC installation.");
  105.                 }
  106.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  107.  
  108.                 if (!is_dir($system_dir)) {
  109.                         throw new Exception('Directory "'.$system_dir.'" not found.');
  110.                 }
  111.  
  112.                 $local_version = $this->get_local_version($system_dir);
  113.  
  114.                 $latest_stable = $this->get_latest_version();
  115.  
  116.                 // Note: version_compare() correctly assumes that 1.3.0-dev is higher than 1.3.0
  117.                 if (version_compare($local_version,$latest_stable,'>')) {
  118.                         $this->setStatus(VNag::STATUS_OK);
  119.                         $this->setHeadline("Version $local_version (Latest stable version $latest_stable) at $system_dir", true);
  120.                 } else if (version_compare($local_version,$latest_stable,'=')) {
  121.                         $this->setStatus(VNag::STATUS_OK);
  122.                         $this->setHeadline("Version $local_version (Latest stable version) at $system_dir", true);
  123.                 } else {
  124.                         $this->setStatus(VNag::STATUS_WARNING);
  125.                         $this->setHeadline("Version $local_version is outdated (Latest version is $latest_stable) at $system_dir", true);
  126.                 }
  127.         }
  128. }
  129.