Rev 80 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
34 | 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 |
34 | daniel-mar | 9 | */ |
10 | |||
11 | declare(ticks=1); |
||
12 | |||
13 | class GitLabVersionCheck extends VNag { |
||
14 | protected $argSystemDir = null; |
||
15 | |||
16 | public function __construct() { |
||
17 | parent::__construct(); |
||
18 | |||
19 | $this->registerExpectedStandardArguments('Vvht'); |
||
20 | |||
21 | $this->getHelpManager()->setPluginName('check_gitlab_version'); |
||
79 | daniel-mar | 22 | $this->getHelpManager()->setVersion('2023-10-13'); |
34 | daniel-mar | 23 | $this->getHelpManager()->setShortDescription('This plugin checks if a local GitLab 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, 'gitLabPath', 'The local directory where your GitLab installation (version-manifest.txt) is located.')); |
||
30 | } |
||
31 | |||
32 | protected function getInstalledVersion($dir) { |
||
33 | $version_manifest = $dir.'/version-manifest.json'; |
||
34 | |||
35 | if (!file_exists($version_manifest)) { |
||
77 | daniel-mar | 36 | throw new VNagException('This is not a valid GitLab installation in "'.$dir.'" (version-manifest.json is missing).'); |
34 | daniel-mar | 37 | } |
38 | |||
77 | daniel-mar | 39 | $cont = @file_get_contents($version_manifest); |
40 | if ($cont === false) { |
||
41 | throw new VNagException('Cannot read version-manifest.json from GitLab installation in "'.$dir.'".'); |
||
42 | } |
||
43 | $json = @json_decode($cont,true); |
||
88 | daniel-mar | 44 | if ($json === null) { |
77 | daniel-mar | 45 | throw new VNagException('This is not a valid GitLab installation in "'.$dir.'" (version-manifest.json has invalid JSON data).'); |
46 | } |
||
47 | |||
34 | daniel-mar | 48 | return $json['build_version']; |
49 | } |
||
50 | |||
51 | protected function getServerResponse($version) { |
||
52 | $opts = array( |
||
53 | 'http'=>array( |
||
54 | 'method'=>"GET", |
||
76 | daniel-mar | 55 | 'header'=>"Referer: http://example.org/\r\n" // Important!!! |
34 | daniel-mar | 56 | ) |
57 | ); |
||
58 | $context = stream_context_create($opts); |
||
59 | $url = "https://version.gitlab.com/check.svg?gitlab_info=". |
||
60 | urlencode(base64_encode('{"version":"'.$version.'"}')); |
||
76 | daniel-mar | 61 | $cont = $this->url_get_contents($url, 1*60*60, $context); |
34 | daniel-mar | 62 | |
76 | daniel-mar | 63 | if ($cont === false) { |
77 | daniel-mar | 64 | throw new VNagException('Cannot query version.gitlab.com for version check (Version '.$version.')'); |
34 | daniel-mar | 65 | } |
66 | |||
76 | daniel-mar | 67 | if (!preg_match('@>([^<]+)</text>@ismU', $cont, $m)) { |
77 | daniel-mar | 68 | throw new VNagException('Server version.gitlab.com sent an unexpected reply (Version '.$version.')'); |
34 | daniel-mar | 69 | } |
70 | |||
71 | return $m[1]; |
||
72 | } |
||
73 | |||
74 | protected function cbRun($optional_args=array()) { |
||
75 | $system_dir = $this->argSystemDir->getValue(); |
||
76 | if (empty($system_dir)) { |
||
77 | daniel-mar | 77 | throw new VNagException("Please specify the directory of the GitLab installation."); |
34 | daniel-mar | 78 | } |
79 | $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir); |
||
80 | |||
81 | if (!is_dir($system_dir)) { |
||
77 | daniel-mar | 82 | throw new VNagException('Directory "'.$system_dir.'" not found.'); |
34 | daniel-mar | 83 | } |
84 | |||
85 | $cur_ver = $this->getInstalledVersion($system_dir); |
||
86 | $status = $this->getServerResponse($cur_ver); |
||
87 | |||
88 | if ($status == 'up-to-date') { |
||
89 | $this->setStatus(VNag::STATUS_OK); |
||
90 | } else if ($status == 'update available') { |
||
91 | $this->setStatus(VNag::STATUS_WARNING); |
||
35 | daniel-mar | 92 | } else if ($status == 'update asap') { |
93 | $this->setStatus(VNag::STATUS_CRITICAL); |
||
34 | daniel-mar | 94 | } else { |
95 | $this->setStatus(VNag::STATUS_UNKNOWN); |
||
96 | } |
||
35 | daniel-mar | 97 | $this->setHeadline("GitLab currently installed version $cur_ver [$status] at $system_dir", true); |
34 | daniel-mar | 98 | } |
99 | } |