Subversion Repositories vnag

Rev

Rev 77 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
58 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
58 daniel-mar 9
 */
10
 
11
declare(ticks=1);
12
 
13
class WebSvnVersionCheck 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_websvn_version');
79 daniel-mar 22
                $this->getHelpManager()->setVersion('2023-10-13');
59 daniel-mar 23
                $this->getHelpManager()->setShortDescription('This plugin checks if a local WebSVN system has the latest version installed.');
58 daniel-mar 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, 'websvnPath', 'The local directory where your WebSVN 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/include/version.php")) {
77 daniel-mar 36
                        throw new VNagException("Cannot find WebSVN settings file at $path");
58 daniel-mar 37
                }
38
 
39
                $cont = @file_get_contents("$path/include/version.php");
77 daniel-mar 40
                if ($cont === false) {
41
                        throw new VNagException("Cannot determine version for system $path (cannot read version.php)");
42
                }
58 daniel-mar 43
                if (!preg_match('@\\$version = \'(.+)\';@ismU', $cont, $m)) {
77 daniel-mar 44
                        throw new VNagException("Cannot determine version for system $path (cannot find version string)");
58 daniel-mar 45
                }
46
 
59 daniel-mar 47
                return $m[1]; // e.g. "2.8.1" or "2.8.1-DEV"
58 daniel-mar 48
        }
49
 
50
        protected function get_latest_version() {
76 daniel-mar 51
                $cont = $this->url_get_contents('https://api.github.com/repos/websvnphp/websvn/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)');
58 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)');
58 daniel-mar 59
                }
60
 
59 daniel-mar 61
                return $data['tag_name']; // e.g. "2.8.1"
58 daniel-mar 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 WebSVN installation.");
58 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.');
58 daniel-mar 73
                }
74
 
75
                $local_version = $this->get_local_version($system_dir);
76
 
77
                $latest_stable = $this->get_latest_version();
78
 
59 daniel-mar 79
                // Note: version_compare() correctly assumes that 2.8.1 is higher than 2.8.1-DEV
58 daniel-mar 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
}