Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. R1byw31mTTINoOFpCUaDZk9PM8lgAl7cFUGkK3R+yrE+NnO5O3OwOyY3XQcOV2kSd
  3. d21IoZozk4drHLRhiDzodcjBas3+QfWpKVwfxjX0vkgtgIO38a/AhR4inB/dLiLQx
  4. Wi8xTyFJdmfOKP5QH0mlIAhhM5RccMTV14klhw8g+l4VPMlS9jjKbSIX/1T3+EGnk
  5. iAs8SWn5YiK91gMLCpQwvkDeGZjk9usuUnplW7UmhEs1fRXU0byNC3VX2sdSMIoPP
  6. OziyMTzuJIsOm1ihFY34V2093JAWKWLgS9pK4jhXZ0amqIjwh+4PAnNXK8My9QkCr
  7. 7z7WB4r1ZuVGCRMW5q1DD8hdyzuo4+Yn8wmrzuYsnFbcZtpUtxSedTRQyoFgQLMWx
  8. oq9txiiqjbNUeRCRJ2SJvzvP6niDK+Yqb8ElkcjzN7HButAok3DQDhXdfCqbAeWpQ
  9. z/4vP9ruYoo3YHIxnXXLJyrSSbklsCPHTEDqpeSq2zty+/1Z+YCpY1YRTTlzpv9V6
  10. csChl5vJmXdyJrpKTHEOjbyJPhhW4MJoJMjyIQ7zFVZTssLhMPGuYx5VRcUJ4PBhO
  11. iSQilZlSG8NR3wxnAXP1xEFNtZbbBW+C3062/+tvwWATIFuhBuIdQyAmDr30mSaKm
  12. PmgWft5w9khqRlM4nJe5g+P2p5GG5JgrzoVM6FnhKRH+XAjlHavZAPlucTZ1X3S07
  13. 5ZkED9IQzOREIE73mshS/j1bATWrEQFdiWJCohN4L2QhIyVORzXJQztAkvOzjSqVE
  14. s1vMsVLTmJAB6pzt8RLxqE0NAlzyF11l/9v1/LdxfScmPFt0CDcz2etBVM1yFWEtz
  15. 9RDguHEytq4z5dXvojoOUHvhLFVQGC4jH+k0iaooc+EtaU3/R34knaJGJppOsA03y
  16. dkxDtv/T2iKwFCXTB3vF58fl2wAJXxnubc6AT1UgQJSHMNRvppkhUpFUcxdYc4e7Y
  17. lkG7fnbxz/Fvxof1p/H1lPfTPSNxoJqn1Ogeb4DcW3XhKbe51R9X6FgMUeOE4NmoG
  18. CJx9Bfs2gXhe+iU62GDUOzgAp+IMoE7Vjw8vQqTvfWqvIvSDzIR194o1ajZ0I2yEy
  19. B3VbLvLP12ppwTI2MkhMipU3k+VlPo8Wxs0FKMSNlRxIJ5rUspnspgPfsgX4Iz7b+
  20. SCIejj5xCDZb/EbMzoAaE+rzOavtf+uhPZv+/HecrSyVff4VxJsfbNueLRgaxdyBm
  21. 2FHJHaaZfGxFBUdsV70C4zHNFItmkgiqXO4JGgJH2T6rd+0uRkutLs9ko8MSjwCvi
  22. QkRrAii8ODm2ryWJXahOAVWERPIcvyahd2gjPeseOUZgKBnSFh3lxG3bBdhuIG9d8
  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 (substr($local_path,-4) == '.jar') {
  78.                         $files = glob($local_path);
  79.                 } else {
  80.                         $files = glob($local_path.'/server.jar');
  81.                 }
  82.  
  83.                 if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
  84.                 if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
  85.                 $server_jar = $files[0];
  86.  
  87.                 $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
  88.  
  89.                 $out = array();
  90.                 $ec = -1;
  91.                 exec($cmd, $out, $ec);
  92.                 if ($ec != 0) throw new Exception("Cannot unzip version.json");
  93.  
  94.                 $json = implode("\n",$out);
  95.  
  96.                 return (string)json_decode($json,true)['name'];
  97.         }
  98.  
  99.         protected function cbRun($optional_args=array()) {
  100.                 $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
  101.                 $cur_ver = $this->get_installed_minecraft_version($system_dir);
  102.  
  103.                 $new_ver = $this->get_latest_minecraft_version();
  104.  
  105.                 if (version_compare($cur_ver,$new_ver) >= 0) {
  106.                         $this->setStatus(VNag::STATUS_OK);
  107.                         $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
  108.                 } else {
  109.                         $this->setStatus(VNag::STATUS_WARNING);
  110.                         $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
  111.                 }
  112.         }
  113. }
  114.