Rev 77 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
59 | daniel-mar | 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 | * |
||
76 | daniel-mar | 8 | * Revision 2023-10-13 |
59 | daniel-mar | 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'); |
||
79 | daniel-mar | 22 | $this->getHelpManager()->setVersion('2023-10-13'); |
59 | daniel-mar | 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")) { |
||
77 | daniel-mar | 36 | throw new VNagException("Cannot find ViewVC settings file at $path"); |
59 | daniel-mar | 37 | } |
38 | |||
39 | $cont = @file_get_contents("$path/lib/viewvc.py"); |
||
77 | daniel-mar | 40 | if ($cont === false) { |
41 | throw new VNagException("Cannot determine version for system $path (cannot read viewvc.py)"); |
||
42 | } |
||
59 | daniel-mar | 43 | if (!preg_match('@__version__\\s*=\\s*([\'"])(.+)\\1@ismU', $cont, $m)) { |
77 | daniel-mar | 44 | throw new VNagException("Cannot determine version for system $path (cannot find version string)"); |
59 | daniel-mar | 45 | } |
46 | |||
47 | return $m[2]; // e.g. "1.3.0-dev" |
||
48 | } |
||
49 | |||
50 | protected function get_latest_version() { |
||
76 | daniel-mar | 51 | $cont = $this->url_get_contents('https://api.github.com/repos/viewvc/viewvc/releases/latest'); |
52 | if ($cont === false) { |
||
77 | daniel-mar | 53 | throw new VNagException('Cannot parse version from GitHub API. The plugin probably needs to be updated. (Cannot access api.github.com)'); |
59 | daniel-mar | 54 | } |
55 | |||
56 | $data = @json_decode($cont, true); |
||
76 | daniel-mar | 57 | if ($data === false) { |
77 | daniel-mar | 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 | daniel-mar | 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)) { |
||
77 | daniel-mar | 67 | throw new VNagException("Please specify the directory of the ViewVC installation."); |
59 | daniel-mar | 68 | } |
69 | $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir); |
||
70 | |||
71 | if (!is_dir($system_dir)) { |
||
77 | daniel-mar | 72 | throw new VNagException('Directory "'.$system_dir.'" not found.'); |
59 | daniel-mar | 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 | } |