Subversion Repositories vnag

Rev

Rev 78 | 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 MinecraftJavaVersionCheck 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_minecraft_java_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local Minecraft for Java server 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, 'serverPath', 'The local directory where your Minecraft for Java server.jar is located.'));
  30.         }
  31.  
  32.         protected function get_local_version($local_path) {
  33.                 if (strtolower(substr($local_path,-4)) == '.jar') {
  34.                         // Single JAR file specified. Search it.
  35.                         $files = glob($local_path);
  36.                 } else {
  37.                         // Directory specified. Search the server.jar
  38.                         /*$files = glob($local_path.'/server.jar');*/
  39.                         $files = glob($local_path.'/'.'*');
  40.                         $files = preg_grep('/server\.jar$/i', $files); // case insensitive, for Windows
  41.                         if ($files === false) $files = [];
  42.                         $files = array_reverse(array_reverse($files, false), false); // make that array keys start with 0 again.
  43.                 }
  44.  
  45.                 if (count($files) == 0) throw new VNagException("No server.jar found at $local_path");
  46.                 if (count($files) > 1) throw new VNagException("More than one server.jar found at $local_path");
  47.                 $server_jar = $files[0];
  48.  
  49.  
  50.  
  51.                 $cache_id = 'MinecraftJavaVersionCheck:server.jar('.filemtime($server_jar).'/'.filesize($server_jar).'):version.json';
  52.                 $cache_file = $this->get_cache_dir().'/'.hash('sha256',$cache_id);
  53.                 if (file_exists($cache_file)) {
  54.                         $cont = @file_get_contents($cache_file);
  55.                 } else {
  56.                         $cont = false;
  57.                 }
  58.  
  59.                 if ($cont === false) {
  60.                         $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
  61.  
  62.                         $out = array();
  63.                         $ec = -1;
  64.                         exec($cmd, $out, $ec);
  65.                         if ($ec != 0) throw new VNagException("Cannot unzip version.json");
  66.  
  67.                         $cont = implode("\n",$out);
  68.  
  69.                         assert(@json_decode($cont,true) !== false);
  70.  
  71.                         @file_put_contents($cache_file, $cont);
  72.                 }
  73.  
  74.                 $json = @json_decode($cont,true);
  75.                 if ($json === false) throw new VNagException("version.json has invalid JSON data");
  76.                 return (string)$json['name'];
  77.         }
  78.  
  79.         protected function get_latest_version() {
  80.                 $cont = $this->url_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json');
  81.                 if ($cont === false) throw new VNagException("Cannot detect latest available Minecraft version (GET request failed)");
  82.                 $json = @json_decode($cont, true);
  83.                 if ($json === false) throw new VNagException("Cannot detect latest available Minecraft version (JSON invalid data)");
  84.                 $version = $json['latest']['release'] ?? null;
  85.                 if (!$version) throw new VNagException("Cannot detect latest available Minecraft version (JSON does not contain version)");
  86.                 return $version;
  87.         }
  88.  
  89.         protected function cbRun($optional_args=array()) {
  90.                 $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
  91.                 $cur_ver = $this->get_local_version($system_dir);
  92.  
  93.                 $new_ver = $this->get_latest_version();
  94.  
  95.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  96.                         $this->setStatus(VNag::STATUS_OK);
  97.                         $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
  98.                 } else {
  99.                         $this->setStatus(VNag::STATUS_WARNING);
  100.                         $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
  101.                 }
  102.         }
  103. }
  104.