Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. xhl/K7K2NBjuGDzJ8SDyDfjnqVYQGqJYY8y/rSj2nQ2qB1UaPExnZbVeYXmkuuNHn
  3. 0FYtsd797Xuz5VJrqTGE8FjIwYxW4wjuOn/AKdzI7EJIVah9inU52Nh9i/t1ZBBSj
  4. l6M6qn0Hr0gImkW4aPagZ5PVTW9D/kGyI01TiNY5lmXnDYnOIGwVTriOqEl1KWVaY
  5. Fy350Ji1UN5dh4EeA6UyvTCtTPHV7SwVOMTMSBnQSE1/6Tkr0ETMfUsSE/HA2pnp8
  6. V9DKDj5US43AXTBZf7SUhLbsESzgVJBIX1LIUCSRWZC067GRhYpyOqzzb8Jx0GduQ
  7. 5+cnZSckAsv4sjDra0blFjnfYS1mVO0vHY0n3wRANS4bqgodaBhmyN3lvzTK+Hf2N
  8. Ki1MAi91Pd8K81mrQ5EbB+TOVAH3w0p3DejtJrdLTQpYuq+pofQRvE8Ua3zBdBu+3
  9. GWazom0drmFfdOrcWOLp9nMqJmF/GYXB1zbuSsi83amZFtaexZePsPjfFq/2Hx2Tb
  10. iO9Ivao5VKayqeUUzjb40/+9UYfNqz38BKp25SJELQBCyJjUQ4KVa3ugZPFu/79xD
  11. NAs33H1qEMOu99KFayelWamGg44th+MQuRQyF8JWYehYEPNA9TGHQoWKeH7E8GJkr
  12. qP291YkDhxSvmGjVIsgypSHrN29tleKE9RahtFzAFE8gpQT0xDLTirut8ecdHLqkc
  13. dmm15VS2X2oGCvrCkTCNhIucD+wweR0EJ1cOYUW0ET0hOlWgxEohCy65d4GXRvIf3
  14. zgSf91dyv/Of2c6vtx+JEs8bGofkYNxrTGF6mUTb7NDW08xs4SiXbL91KB83kjLxX
  15. ATbeS+t6wTcm9op+82CCnb/VFt5TPeFhoCv1U2ETsAf4SvAsYumHfc+GggZi2wtUK
  16. JMpV41FgPUv51XiYfcCLUyCF2nxenr8sAOzVA8L3gGiqJQ3jf8rUTbp35eMIvAgU2
  17. ut0w/gdtHym44ipjArNVYFS7X8OkEq6bMZ4P/wUBVm8t+WTNf7ra4Upybv5YX1QpT
  18. D6jmyGmb1ggDNstqj7rVRT6dYrbuNBPBhJV8As8oBUcW5BAeUWqCIJj44pyXOEH9Z
  19. pkgSanTgFUEqHAkg/NW/ah2/6CgPNb7xKdosgHSgXTvaROUBqBQ8AiQBnTjdduJTp
  20. 1GYXcGOsix70/+DBH8MZW1ToP5FU2oAkiin+iEKe/XE6Ek506hhd0CvyLuIXtrScz
  21. 4EYmjCEoXYyZb7f3l9yidIWz/HkmUIWc2RIq8bv9ygzF+6t6PZktBrztMC2jRkFbe
  22. vTtpxQWS485kKJO0s84IBNQXPUb+4Dpfy01OjJa8PXcYK4Ajp7DBt+jgLsFUMpCoV
  23. A==
  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];
  70.         }
  71.  
  72.         protected function get_latest_version() {
  73.                 $url = 'https://api.github.com/repos/websvnphp/websvn/releases/latest';
  74.                 $options = array(
  75.                   'http'=>array(
  76.                     'method'=>"GET",
  77.                     'header'=>"Accept-language: en\r\n" .
  78.                               "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"
  79.                   )
  80.                 );
  81.                 $context = stream_context_create($options);
  82.                 $cont = file_get_contents($url, false, $context);
  83.                 if (!$cont) {
  84.                         throw new Exception('Cannot parse version from GitHub API. The plugin probably needs to be updated. A');
  85.                 }
  86.  
  87.                 $data = @json_decode($cont, true);
  88.                 if (!$data) {
  89.                         throw new Exception('Cannot parse version from GitHub API. The plugin probably needs to be updated. B');
  90.                 }
  91.  
  92.                 return $data['name']; // e.g. "2.8.1"
  93.         }
  94.  
  95.         protected function cbRun($optional_args=array()) {
  96.                 $system_dir = $this->argSystemDir->getValue();
  97.                 if (empty($system_dir)) {
  98.                         throw new Exception("Please specify the directory of the WebSVN installation.");
  99.                 }
  100.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  101.  
  102.                 if (!is_dir($system_dir)) {
  103.                         throw new Exception('Directory "'.$system_dir.'" not found.');
  104.                 }
  105.  
  106.                 $local_version = $this->get_local_version($system_dir);
  107.  
  108.                 $latest_stable = $this->get_latest_version();
  109.  
  110.                 if (version_compare($local_version,$latest_stable,'>')) {
  111.                         $this->setStatus(VNag::STATUS_OK);
  112.                         $this->setHeadline("Version $local_version (Latest stable version $latest_stable) at $system_dir", true);
  113.                 } else if (version_compare($local_version,$latest_stable,'=')) {
  114.                         $this->setStatus(VNag::STATUS_OK);
  115.                         $this->setHeadline("Version $local_version (Latest stable version) at $system_dir", true);
  116.                 } else {
  117.                         $this->setStatus(VNag::STATUS_WARNING);
  118.                         $this->setHeadline("Version $local_version is outdated (Latest version is $latest_stable) at $system_dir", true);
  119.                 }
  120.         }
  121. }
  122.