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m8dgXle3xcMsBKtP2y9
fRRaBkdbNHuZhtlkAWxQumRtKh1b4jgorEsjgeT3FMNWACygFAQH2/Sb9YSnhbyf3
scECLeyzxgi+S9cRusZj0nyTeepAwRUINkCVmQPjt5loZILc+caAoapLknuwAmKge
d5NnwIBjO6v1nFhxNIw4wyQ0C+f33K96WxhnVRRAosw7q6JIVmy0QQ8DRWa3EtFvu
MzEgPBZx6mKIhP4b2xK1KFOLpeYCz3gQ0DMg71ullD/5DWF8v/3RCFZFr3m76TmIj
3I/Jncg1pz2gUfxk+VxQatkbjXm72BcF/cc5+qJFhJzuYTA/9uLhpqW0sglRSYSQt
A==
</ViaThinkSoftSignature> */ ?>
<?php
/*
* VNag - Nagios Framework for PHP
* Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
* Licensed under the terms of the Apache 2.0 license
*
* Revision 2023-06-12
*/
declare(ticks=1);
class MinecraftJavaVersionCheck extends VNag {
protected $argSystemDir = null;
public function __construct() {
parent::__construct();
$this->registerExpectedStandardArguments('Vvht');
$this->getHelpManager()->setPluginName('check_minecraft_java_version');
$this->getHelpManager()->setVersion('1.0');
$this->getHelpManager()->setShortDescription('This plugin checks if a local Minecraft for Java server has the latest version installed.');
$this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
$this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
$this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
// Individual (non-standard) arguments:
$this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'serverPath', 'The local directory where your Minecraft for Java server.jar is located.'));
}
protected function get_latest_minecraft_version() {
$url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json';
$max_cache_time = 1 * 60 * 60;
$cache_file = $this->get_cache_dir().'/'.sha1($url);
if (!$cont) throw new Exception("Failed to get contents from $cache_file");
} else {
$cont = @file_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json');
if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request to $url failed)");
}
if (!$json) throw new Exception("Cannot detect latest available Minecraft version (JSON invalid data)");
$version = $json['latest']['release'] ?? null;
if (!$version) throw new Exception("Cannot detect latest available Minecraft version (JSON does not contain version)");
return $version;
}
protected function get_installed_minecraft_version($local_path) {
// Single JAR file specified. Search it.
$files = glob($local_path);
} else {
// Directory specified. Search the server.jar
/*$files = glob($local_path.'/server.jar');*/
$files = glob($local_path.'/*');
$files = preg_grep('/server\.jar$/i', $files); // case insensitive, for Windows
if ($files === false) $files = [];
}
if (count($files) == 0) throw new Exception
("No server.jar found at $local_path");
if (count($files) > 1) throw new Exception
("More than one server.jar found at $local_path");
$server_jar = $files[0];
$ec = -1;
if ($ec != 0) throw new Exception("Cannot unzip version.json");
}
protected function cbRun
($optional_args=array()) {
$system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
$cur_ver = $this->get_installed_minecraft_version($system_dir);
$new_ver = $this->get_latest_minecraft_version();
$this->setStatus(VNag::STATUS_OK);
$this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
} else {
$this->setStatus(VNag::STATUS_WARNING);
$this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
}
}
}