Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. b7BnMe8qeRUHdsJkqlNsMQYx99h5DOO+Wz1wymeJQ4whzOdN6COeaUVebw+prbhKo
  3. 5V4aWanR/7o7sXUic5s8WqyawlrejEOemH7KIHTec6C91T0IXjMOfUBRX4TucEkNi
  4. Ca5Ek4MCjrhk7jHAiTOtEKv3iRWIw1bPEoBwSLMfZ+jNDzhOakXKmCcgpKX47XwiZ
  5. qi+bsAgleBPx8a5Dw74PlMPTKMtAzXq/Qb1g+T+1L3K5kV8l9Cebkoi4GX6KhN+PF
  6. pkIF3c/1342hPlRjYcyE5lrDFelnit6vTTp4oy2ozh4ZkPBysTdFxWfIxRhiE0P59
  7. fuilhqUeyikmk+Q6Z2xbNXZS1OFMYOKEhJo1xa8b38AXys1GGebrQ4A/xYq5xtm2K
  8. Wvh3uW0tKQtTTd0AYt0RoDd8sJ8zFCmMk30NTqPlvZvwQIATc90tjtfGxqHzmYWOC
  9. itzBTMp89hN/xQY3lIWLO8vWxjD+Z9ULCdHiqhEYWgFTvSLLNIfN16oB2qF2kd3Sf
  10. n7Xj/DoMLAI3soU7vxznXcZabrbl4YQXoJQfod4AGzFSHnQJ1mH6/WFRIQsFKZeFN
  11. F3ud2yZFrKn7ee0e1GhieNU4fASa726N9ndxEGO8D9Rsgv8/0eApdV1ZQWbICN0JT
  12. A0XA6NEBKa6n/YYoh7wMrgUXI8DgQSDbqBslIKFgmPyfR7XvQlt2dxItwA5GYGQxs
  13. KJnaJqQfsXjNY6mAguMjK4DgHNqE7ulXSu7uSAkSBIfUR+4EvFXJbwwVy4EWJAXN0
  14. +9xfMMvwnoFYzt0dHyn0Y040KyVWMbx2bmSpkJSgxkUBba0NWosdlM4+wyDFy0FH3
  15. FkCY3GG/PuRJ3aYTTo9P+vPhysOFazBjoTPO8s62lgfgyizGOjVNlVFPe3PGEx2Xr
  16. Xl9cHFbheNCDRZ7wZMxX8fvl+8ipvZznJ+Kl1jjVSZBgDLqoxG08Ej9+gmFdRSlWp
  17. zwBQ/JGD2XU6gIR1Z241NikGNr0AVnB1hZXbBgMWD9ilXXm8dgXle3xcMsBKtP2y9
  18. fRRaBkdbNHuZhtlkAWxQumRtKh1b4jgorEsjgeT3FMNWACygFAQH2/Sb9YSnhbyf3
  19. scECLeyzxgi+S9cRusZj0nyTeepAwRUINkCVmQPjt5loZILc+caAoapLknuwAmKge
  20. d5NnwIBjO6v1nFhxNIw4wyQ0C+f33K96WxhnVRRAosw7q6JIVmy0QQ8DRWa3EtFvu
  21. MzEgPBZx6mKIhP4b2xK1KFOLpeYCz3gQ0DMg71ullD/5DWF8v/3RCFZFr3m76TmIj
  22. 3I/Jncg1pz2gUfxk+VxQatkbjXm72BcF/cc5+qJFhJzuYTA/9uLhpqW0sglRSYSQt
  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.                 $url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json';
  58.                 $max_cache_time = 1 * 60 * 60;
  59.                 $cache_file = $this->get_cache_dir().'/'.sha1($url);
  60.                 if (file_exists($cache_file) && (time()-filemtime($cache_file) < $max_cache_time)) {
  61.                         $cont = @file_get_contents($cache_file);
  62.                         if (!$cont) throw new Exception("Failed to get contents from $cache_file");
  63.                 } else {
  64.                         $cont = @file_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json');
  65.                         if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request to $url failed)");
  66.                         file_put_contents($cache_file, $cont);
  67.                 }
  68.  
  69.                 $json = @json_decode($cont, true);
  70.                 if (!$json) throw new Exception("Cannot detect latest available Minecraft version (JSON invalid data)");
  71.                 $version = $json['latest']['release'] ?? null;
  72.                 if (!$version) throw new Exception("Cannot detect latest available Minecraft version (JSON does not contain version)");
  73.                 return $version;
  74.         }
  75.  
  76.         protected function get_installed_minecraft_version($local_path) {
  77.                 if (strtolower(substr($local_path,-4)) == '.jar') {
  78.                         // Single JAR file specified. Search it.
  79.                         $files = glob($local_path);
  80.                 } else {
  81.                         // Directory specified. Search the server.jar
  82.                         /*$files = glob($local_path.'/server.jar');*/
  83.                         $files = glob($local_path.'/*');
  84.                         $files = preg_grep('/server\.jar$/i', $files); // case insensitive, for Windows
  85.                         if ($files === false) $files = [];
  86.                         $files = array_reverse(array_reverse($files, false), false); // make that array keys start with 0 again.
  87.                 }
  88.  
  89.                 if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
  90.                 if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
  91.                 $server_jar = $files[0];
  92.  
  93.                 $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
  94.  
  95.                 $out = array();
  96.                 $ec = -1;
  97.                 exec($cmd, $out, $ec);
  98.                 if ($ec != 0) throw new Exception("Cannot unzip version.json");
  99.  
  100.                 $json = implode("\n",$out);
  101.  
  102.                 return (string)json_decode($json,true)['name'];
  103.         }
  104.  
  105.         protected function cbRun($optional_args=array()) {
  106.                 $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
  107.                 $cur_ver = $this->get_installed_minecraft_version($system_dir);
  108.  
  109.                 $new_ver = $this->get_latest_minecraft_version();
  110.  
  111.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  112.                         $this->setStatus(VNag::STATUS_OK);
  113.                         $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
  114.                 } else {
  115.                         $this->setStatus(VNag::STATUS_WARNING);
  116.                         $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
  117.                 }
  118.         }
  119. }
  120.