Subversion Repositories vnag

Rev

Rev 62 | Blame | Last modification | View Log | RSS feed

  1. <?php /* <ViaThinkSoftSignature>
  2. 2fWiqNt7gMw7rboV5l5dvB/ceP6jneWqHFYRNvge/NXbEoRDqossYdeNkXUGOZ1oQ
  3. PwqVwK/kkKISa1PTYeK33yJiiH8hBBc4LOTh4p08AEh2gBCrsS3J/Jk8+oiqg5yFi
  4. atnTF13ri4URJQTxC7AUNw3yDgWblNFaSSzMDIteljnDvMMb3EBCbeM87RTOxd8hw
  5. HqfcDqkgSRfBwDYi4NyNDSrk2ZZDkMlgH7XakxnP3GqsZgkujBVKYvhdLg86xu+1K
  6. qXBqfoQFhZBmL0l9iAbsFrLo0F+T6QsqsfVVyLZkuNJAGk0nRJudhaO+WwQmUealB
  7. ex4VsL3r18tyMri5fwft87GY1fLKjL2t3ABm03TczfGSMRQBNPdDMDziAbwBt7Vjy
  8. 2pnW42l4jv6gsM6FEfU0LcsfXZiZvEVOqWf4RaoyUj8KjsfZTLKxkDql3qB7NEpDr
  9. pDasGsKk5VfBS73zqVuBaNt425atLVhqQM4ufTyZTBr+rfcTJzxraMAH4NDzilXkP
  10. hxGgDpG8VcYDBohmoOD1MqjNXESHp915u4y+/41lNmPAk8wKN5OXzz2WgT/8JjvP/
  11. XBbpw03O+R95kTkNA6yhCDOs6FeO6962N1tmeYMp/+bw2CzPLtlUx4/vnLQk/A3Kd
  12. bXGlF2vKXgPLJckehzhU7Il4xbXbOL56R7DGLypuBcYTqRql/LlYKZwCctnboD0k9
  13. VsZK/kP6Lp5ra66qKSrg3vT84dJs51mLBS4pUiFZ3mpcyDUhAbYYH0gGWVU/+FvJv
  14. uIfBialrJXttcAZ+ntXF21j+m/hVSaUocyvOQ82DUgXmRrFTvqXA19c5EI3Kg5yPE
  15. UL6td9X1vmUjDTXDMidWSrgbU1bLUc7WRRxPO2OVKhvAW5SUQgD144OQCnHny5NfG
  16. Hv8Pn6e9c5INrM0PToqBP0co2GrSjCBJj2H8oHZrpUnOyIKKLFBBajxRBZuEVySC/
  17. AnKaSpJ/GgeBjQj8n+QzO+YTROnD0HPmhsPKFAXyQKp0AMGTeKEjvQ/BMY366+gnD
  18. tNhV5bW/Jg9HfmFCCc9HFl49mRODgfY/RPgXomkXXJoznI35gzuoEJ6oYa0l5/3x+
  19. CPQVEwQD27aZ16THSuo7B15PDqlDqFK6oBEIIQo0wiKvG3ZKJ4o+/8dEFpuL7iojN
  20. DF6090cihGns5gccj5uwrBkqswwkZjGb9Q3b7L5kivXuuUDXkYF98EbndhDAhMCxZ
  21. kLVD0Wuc87Jv9C5sb9jSv+KVPbcp6PtIGuYiKFv1QTaEkpiTHDfgxRf10DGo1Ferw
  22. tyc9Ot1W9I5Grq0bFQVenj/si5x+CmjONBOCnByNt8aRmhl8jp88LcDnyV5FwCLYE
  23. A==
  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.  *
  32.  * Revision 2023-06-12
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class MinecraftJavaVersionCheck 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_minecraft_java_version');
  46.                 $this->getHelpManager()->setVersion('1.0');
  47.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local Minecraft for Java server 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, 'serverPath', 'The local directory where your Minecraft for Java server.jar is located.'));
  54.         }
  55.  
  56.         protected function get_latest_minecraft_version() {
  57.                 $cont = @file_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json');
  58.                 if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request failed)");
  59.                 $json = @json_decode($cont, true);
  60.                 if (!$json) throw new Exception("Cannot detect latest available Minecraft version (JSON invalid data)");
  61.                 $version = $json['latest']['release'] ?? null;
  62.                 if (!$version) throw new Exception("Cannot detect latest available Minecraft version (JSON does not contain version)");
  63.                 return $version;
  64.         }
  65.  
  66.         protected function get_installed_minecraft_version($local_path) {
  67.                 if (substr($local_path,-4) == '.jar') {
  68.                         $files = glob($local_path);
  69.                 } else {
  70.                         $files = glob($local_path.'/server.jar');
  71.                 }
  72.  
  73.                 if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
  74.                 if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
  75.                 $server_jar = $files[0];
  76.  
  77.                 $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
  78.  
  79.                 $out = array();
  80.                 $ec = -1;
  81.                 exec($cmd, $out, $ec);
  82.                 if ($ec != 0) throw new Exception("Cannot unzip version.json");
  83.  
  84.                 $json = implode("\n",$out);
  85.  
  86.                 return (string)json_decode($json,true)['name'];
  87.         }
  88.  
  89.         protected function cbRun($optional_args=array()) {
  90.                 $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
  91.                 $cur_ver = $this->get_installed_minecraft_version($system_dir);
  92.  
  93.                 $new_ver = $this->get_latest_minecraft_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.