Subversion Repositories vnag

Rev

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

Rev Author Line No. Line
34 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
76 daniel-mar 2
xIodU7lBP0sOx3e6CFtClkfp/bVAPoqGDfH9rytWnT4tezmk0METjFW5UfkfEhftq
3
60M3zCwBU7WMH+RrYyyzTLpbya28xsj7VQTnI4QTNfawMGEUrHBRtRtG/Eq/B2HLn
4
sJ/ZpLwpipTVAnt9MWsKmkz5I/7BfOJwjrH2BlLTDxmHk9SoelvAyR29n2kmfsRty
5
J5miiW628U/lKRI2N53K22DFKBYVwvN8YQzhbfZrwnyNzrfOdvfGZ6DINe/CtjYGi
6
+u06hJF0q/hceMw3zln3lKNz2bhW9XJ2K8CGtimj1OVjZlhc+Pw9RF46X/SDeaz5Q
7
nlNoAde16v9GP2cwTEXjnicMR3g7PJdQjiHxj/fTCg3C1BIUQb7kJXsYKe9HlYijD
8
ZQGDMout6gg1oc34ZCwWttYjo54FlB6t/BKdvZCgPjKqC6DcP0JE/29a+Nb3XhYV7
9
+juV3nnDAGv9vHtRgZoBlTuLiSRKurhECMhnQO4IAHxSC7pdUiglEG/LJ7CMEyh5c
10
VJQkq9LyVC8koExrzGiMUBCXh8QC64UWL5LTX/diSn3AyLXX6ZNLXBtV2mmHwGAl/
11
d+H8Sw8IganZPfNwFcKT2hvj05GalidbNgFMXqOkJBGT7gOrVux6jmUfFuOzeXjHG
12
qNpPF6e4D3SDv7iCsEOKmM1ueWeawFLnz2tub3NINgRqt91GAULsA53N2tv7qd3ek
13
1ko5NLT2NFIxkcz0BlDSyhhMu8etDQBQtY+D4g7z879LLREgdrw58MAAhrk/f5np7
14
+eXVh4qTuGIsJyYoXwCzgDZ9RcZmXfDZ7x9CLJTgpp5YhRzutYiqrgxw3Nkn3R3Jk
15
9GpVuaRymb0fz/FT/km0rsqKChSaazTeCUxhZF0+vAbLDHurkw5G34IkbypJHdbKp
16
ny+/83ktrxwLG+2ICHh4WLFDKWmbwepPePqH8fqKy+oi+7xsUJB0Z1joasD9WYxme
17
SoTWBC9js7r9KpXnuvD6DaTHoYaowLZlo44guo0O++6gFT/Y1Cv6YXqNbNJeOQ8Nc
18
yxVYXzdTeKO4lixezSI/K8/OeeHEyLhjHMb4kUcEOCuXFzQcO9jPd9pucOZVdwSvB
19
M2pDAOcrhFWiSFQiwx826zEyY5J+MwTuewOJrJKXNIaMxzu55ZTPKy8qfPma9jvrY
20
fwDy9ZnDvb7XKHgJIdBdrPfq7fkPVvd2pYYozBvQaYDdrZvapixhy4300Iqx8fup6
21
pW8xfN+HvKLUWt4NFWojvmIpSp5egb8DGRzBChgLVktI+2Q64cXGXOrfhR1sIK2me
22
XMs33vPVTK5t1obSksMDCf/9dJmjRQ+XtS0+D2AsWdkD2MUJhoHLJE6spl+h5dQ+7
23
Q==
34 daniel-mar 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
 *
76 daniel-mar 32
 * Revision 2023-10-13
34 daniel-mar 33
 */
34
 
35
declare(ticks=1);
36
 
37
class GitLabVersionCheck extends VNag {
38
        protected $argSystemDir = null;
39
 
40
        public function __construct() {
41
                parent::__construct();
42
 
43
                $this->registerExpectedStandardArguments('Vvht');
44
 
45
                $this->getHelpManager()->setPluginName('check_gitlab_version');
46
                $this->getHelpManager()->setVersion('1.0');
47
                $this->getHelpManager()->setShortDescription('This plugin checks if a local GitLab 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, 'gitLabPath', 'The local directory where your GitLab installation (version-manifest.txt) is located.'));
54
        }
55
 
56
        protected function getInstalledVersion($dir) {
57
                $version_manifest = $dir.'/version-manifest.json';
58
 
59
                if (!file_exists($version_manifest)) {
60
                        throw new Exception('This is not a valid GitLab installation in "'.$dir.'" (version-manifest.json is missing).');
61
                }
62
 
63
                $json = json_decode(file_get_contents($version_manifest),true);
64
                return $json['build_version'];
65
        }
66
 
67
        protected function getServerResponse($version) {
68
                $opts = array(
69
                  'http'=>array(
70
                    'method'=>"GET",
76 daniel-mar 71
                    'header'=>"Referer: http://example.org/\r\n" // Important!!!
34 daniel-mar 72
                  )
73
                );
74
                $context = stream_context_create($opts);
75
                $url = "https://version.gitlab.com/check.svg?gitlab_info=".
76
                       urlencode(base64_encode('{"version":"'.$version.'"}'));
76 daniel-mar 77
                $cont = $this->url_get_contents($url, 1*60*60, $context);
34 daniel-mar 78
 
76 daniel-mar 79
                if ($cont === false) {
34 daniel-mar 80
                        throw new Exception('Cannot query version.gitlab.com for version check (Version '.$version.')');
81
                }
82
 
76 daniel-mar 83
                if (!preg_match('@>([^<]+)</text>@ismU', $cont, $m)) {
34 daniel-mar 84
                        throw new Exception('Server version.gitlab.com sent an unexpected reply (Version '.$version.')');
85
                }
86
 
87
                return $m[1];
88
        }
89
 
90
        protected function cbRun($optional_args=array()) {
91
                $system_dir = $this->argSystemDir->getValue();
92
                if (empty($system_dir)) {
93
                        throw new Exception("Please specify the directory of the GitLab installation.");
94
                }
95
                $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
96
 
97
                if (!is_dir($system_dir)) {
98
                        throw new Exception('Directory "'.$system_dir.'" not found.');
99
                }
100
 
101
                $cur_ver = $this->getInstalledVersion($system_dir);
102
                $status = $this->getServerResponse($cur_ver);
103
 
104
                if ($status == 'up-to-date') {
105
                        $this->setStatus(VNag::STATUS_OK);
106
                } else if ($status == 'update available') {
107
                        $this->setStatus(VNag::STATUS_WARNING);
35 daniel-mar 108
                } else if ($status == 'update asap') {
109
                        $this->setStatus(VNag::STATUS_CRITICAL);
34 daniel-mar 110
                } else {
111
                        $this->setStatus(VNag::STATUS_UNKNOWN);
112
                }
35 daniel-mar 113
                $this->setHeadline("GitLab currently installed version $cur_ver [$status] at $system_dir", true);
34 daniel-mar 114
        }
115
}