Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. O3yOuJ0uY8SaZtsrvzay90oQ6sp1M+CxL4k53lThK/mXA4x6XNuTX1K8OygUl0swc
  3. +yV3RRKNVNaLXMxKBOhRVC1SqJi5jDM+/AN0vJ2j0fjYcc0mXbuy4amScMq0wFBBq
  4. 0Kf2mqWu4QtZiZ6PQo+SoUuoR5QsN4IQkOKdj7cdluNFClt6uar2ZPNEGeHFGt0eT
  5. ddCEJf13rjkpta4FUjjPr1eN2uWE7qFIAqgYbx1tW7+fX3iFOOcnd9F85hbvGQhpY
  6. LJgFwwsFbfgH26E54Za6ynVJ5yvZ3FBFItO/UMtuiAqjFgsv6tmU49B7NxzzFeAln
  7. xTPhhF5SILLJr/aKkMhNd1vWup4s6ZW+HWlwxd5kRdNedGPiWExZZuGDfdorycLLo
  8. kiP2xEHVhMFlOFtBsprOwF8ZjkQ8Jhak+MmUyXIPyhPUPWIyow8kttnBhw2QNxwI3
  9. 9l/7Z/HNj+XYcJq+lRPLG7CGzmD6xBDgjhLMVGTb2ZSqR6Y0YVpd8Wsq7zhok5538
  10. YhE0jtLjaX8U99Y5l9hwJnLZJH7POHbmR5GFAyxZdH08E+nboIwlV5LJrFYb6odSC
  11. S2zWC1PCr3fvSErp9ikEE+GKvsvDviYymcR3rd8xxlsUFi8N/gOKgLY1cobY2Ic2s
  12. jtdinhvnoKU8Fmmc9qUUNJcFS2dyM3uv1CSvYJp0iFcMqEZ3G/FcM3/octYhl1hl1
  13. vPJRe+0Xm9O0T6VqhWNvAf6MjJ2FQGMRAKWkP4NGuP1jOkN3yU0GjP0nc3ZXY92r/
  14. wGnYqLTFh4c3WiGRjNJOmWHqSy1QCVKz13lqVTa4/g6RK0dNVphvTjCcpB6/VCAtw
  15. II0K/y1ZJ0JmhWUbN7JXULyB1Ghy00WUZa3XPZCfCGWqVT/KtPkZgL/8zBzg+NFkF
  16. ZNINJsjM3aFebt2yeUn5E13WCOCFsm6fqhhtNBe14tn2b/AEew4gZfuitFuvbPtY/
  17. gPSqg8EnJ2qmoGJEQhgmkLdmklBLQ/Xtyf68kYFGeoRU/IPVnsODqytvS/J23v0vF
  18. f//VIjpE2vmrRi7aE1QmnT+JIKqq1HZsUPnuEjpIWhdH1W+sztLSbYHTmVG89QOeT
  19. cQzGPitGrPgqqSuVJo0q/zYdXDjE+Y6kbIj8QrK05N6MfhCeund5JvTab7AQlg51n
  20. BSNXZFMdena5C5QTXGwU3BQwBpvr92YWCj7EXzGU8N5XTDULiyInNNZ0xOULA+AXP
  21. aV79sQdctyrWEkll9ykl00641y3jdJl0b3FzFy87v9OoRLj8vAsvy2cUmRykRjyJm
  22. Uc8V1D7xjhvuxa5APwrpcaULIpOSiC3upgmNYlZIODV2CKixY5csoo7Ka2wnAttss
  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 2022-12-18
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class WebSvnVersionCheck 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_websvn_version');
  46.                 $this->getHelpManager()->setVersion('1.0');
  47.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local WebSVN 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, 'websvnPath', 'The local directory where your WebSVN 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/include/version.php")) {
  60.                         throw new Exception("Cannot find WebSVN settings file at $path");
  61.                 }
  62.  
  63.                 $cont = @file_get_contents("$path/include/version.php");
  64.  
  65.                 if (!preg_match('@\\$version = \'(.+)\';@ismU', $cont, $m)) {
  66.                         throw new Exception("Cannot determine version for system $path");
  67.                 }
  68.  
  69.                 return $m[1]; // e.g. "2.8.1" or "2.8.1-DEV"
  70.         }
  71.  
  72.         protected function get_latest_version() {
  73.                 $url = 'https://api.github.com/repos/websvnphp/websvn/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. "2.8.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 WebSVN 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 2.8.1 is higher than 2.8.1-DEV
  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.