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 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');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  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)) {
  36.                         throw new VNagException('This is not a valid GitLab installation in "'.$dir.'" (version-manifest.json is missing).');
  37.                 }
  38.  
  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);
  44.                 if ($json === false) {
  45.                         throw new VNagException('This is not a valid GitLab installation in "'.$dir.'" (version-manifest.json has invalid JSON data).');
  46.                 }
  47.  
  48.                 return $json['build_version'];
  49.         }
  50.  
  51.         protected function getServerResponse($version) {
  52.                 $opts = array(
  53.                   'http'=>array(
  54.                     'method'=>"GET",
  55.                     'header'=>"Referer: http://example.org/\r\n" // Important!!!
  56.                   )
  57.                 );
  58.                 $context = stream_context_create($opts);
  59.                 $url = "https://version.gitlab.com/check.svg?gitlab_info=".
  60.                        urlencode(base64_encode('{"version":"'.$version.'"}'));
  61.                 $cont = $this->url_get_contents($url, 1*60*60, $context);
  62.  
  63.                 if ($cont === false) {
  64.                         throw new VNagException('Cannot query version.gitlab.com for version check (Version '.$version.')');
  65.                 }
  66.  
  67.                 if (!preg_match('@>([^<]+)</text>@ismU', $cont, $m)) {
  68.                         throw new VNagException('Server version.gitlab.com sent an unexpected reply (Version '.$version.')');
  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.                         throw new VNagException("Please specify the directory of the GitLab installation.");
  78.                 }
  79.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  80.  
  81.                 if (!is_dir($system_dir)) {
  82.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  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);
  92.                 } else if ($status == 'update asap') {
  93.                         $this->setStatus(VNag::STATUS_CRITICAL);
  94.                 } else {
  95.                         $this->setStatus(VNag::STATUS_UNKNOWN);
  96.                 }
  97.                 $this->setHeadline("GitLab currently installed version $cur_ver [$status] at $system_dir", true);
  98.         }
  99. }
  100.