Subversion Repositories vnag

Rev

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

  1. <?php
  2.  
  3. /*
  4.  * VNag - Nagios Framework for PHP
  5.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  6.  * Licensed under the terms of the Apache 2.0 license
  7.  *
  8.  * Revision 2023-10-13
  9.  */
  10.  
  11. declare(ticks=1);
  12.  
  13. class ViewVCVersionCheck extends VNag {
  14.         protected $argSystemDir = null;
  15.  
  16.         public function __construct() {
  17.                 parent::__construct();
  18.  
  19.                 $this->registerExpectedStandardArguments('Vht');
  20.  
  21.                 $this->getHelpManager()->setPluginName('check_viewvc_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local ViewVC system has the latest version installed.');
  24.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  25.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  26.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  27.  
  28.                 // Individual (non-standard) arguments:
  29.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'viewvcPath', 'The local directory where your ViewVC installation is located.'));
  30.         }
  31.  
  32.         protected function get_local_version($path) {
  33.                 $path = realpath($path) === false ? $path : realpath($path);
  34.  
  35.                 if (!file_exists("$path/lib/viewvc.py")) {
  36.                         throw new VNagException("Cannot find ViewVC settings file at $path");
  37.                 }
  38.  
  39.                 $cont = @file_get_contents("$path/lib/viewvc.py");
  40.                 if ($cont === false) {
  41.                         throw new VNagException("Cannot determine version for system $path (cannot read viewvc.py)");
  42.                 }
  43.                 if (!preg_match('@__version__\\s*=\\s*([\'"])(.+)\\1@ismU', $cont, $m)) {
  44.                         throw new VNagException("Cannot determine version for system $path (cannot find version string)");
  45.                 }
  46.  
  47.                 return $m[2]; // e.g. "1.3.0-dev"
  48.         }
  49.  
  50.         protected function get_latest_version() {
  51.                 $cont = $this->url_get_contents('https://api.github.com/repos/viewvc/viewvc/releases/latest');
  52.                 if ($cont === false) {
  53.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Cannot access api.github.com)');
  54.                 }
  55.  
  56.                 $data = @json_decode($cont, true);
  57.                 if ($data === false) {
  58.                         throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Invalid JSON data downloaded from api.github.com)');
  59.                 }
  60.  
  61.                 return $data['tag_name']; // e.g. "1.2.1"
  62.         }
  63.  
  64.         protected function cbRun($optional_args=array()) {
  65.                 $system_dir = $this->argSystemDir->getValue();
  66.                 if (empty($system_dir)) {
  67.                         throw new VNagException("Please specify the directory of the ViewVC installation.");
  68.                 }
  69.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  70.  
  71.                 if (!is_dir($system_dir)) {
  72.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  73.                 }
  74.  
  75.                 $local_version = $this->get_local_version($system_dir);
  76.  
  77.                 $latest_stable = $this->get_latest_version();
  78.  
  79.                 // Note: version_compare() correctly assumes that 1.3.0-dev is higher than 1.3.0
  80.                 if (version_compare($local_version,$latest_stable,'>')) {
  81.                         $this->setStatus(VNag::STATUS_OK);
  82.                         $this->setHeadline("Version $local_version (Latest stable version $latest_stable) at $system_dir", true);
  83.                 } else if (version_compare($local_version,$latest_stable,'=')) {
  84.                         $this->setStatus(VNag::STATUS_OK);
  85.                         $this->setHeadline("Version $local_version (Latest stable version) at $system_dir", true);
  86.                 } else {
  87.                         $this->setStatus(VNag::STATUS_WARNING);
  88.                         $this->setHeadline("Version $local_version is outdated (Latest version is $latest_stable) at $system_dir", true);
  89.                 }
  90.         }
  91. }
  92.