Rev 78 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
40 | daniel-mar | 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 | * |
||
76 | daniel-mar | 8 | * Revision 2023-10-13 |
40 | daniel-mar | 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'); |
||
79 | daniel-mar | 22 | $this->getHelpManager()->setVersion('2023-10-13'); |
40 | daniel-mar | 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 | |||
76 | daniel-mar | 32 | protected function get_local_version($local_path) { |
65 | daniel-mar | 33 | if (strtolower(substr($local_path,-4)) == '.jar') { |
34 | // Single JAR file specified. Search it. |
||
40 | daniel-mar | 35 | $files = glob($local_path); |
36 | } else { |
||
65 | daniel-mar | 37 | // Directory specified. Search the server.jar |
38 | /*$files = glob($local_path.'/server.jar');*/ |
||
77 | daniel-mar | 39 | $files = glob($local_path.'/'.'*'); |
65 | daniel-mar | 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. |
||
40 | daniel-mar | 43 | } |
44 | |||
77 | daniel-mar | 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"); |
||
40 | daniel-mar | 47 | $server_jar = $files[0]; |
48 | |||
49 | |||
50 | |||
78 | daniel-mar | 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); |
||
66 | daniel-mar | 53 | if (file_exists($cache_file)) { |
77 | daniel-mar | 54 | $cont = @file_get_contents($cache_file); |
66 | daniel-mar | 55 | } else { |
77 | daniel-mar | 56 | $cont = false; |
57 | } |
||
58 | |||
59 | if ($cont === false) { |
||
66 | daniel-mar | 60 | $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json"; |
40 | daniel-mar | 61 | |
66 | daniel-mar | 62 | $out = array(); |
63 | $ec = -1; |
||
64 | exec($cmd, $out, $ec); |
||
77 | daniel-mar | 65 | if ($ec != 0) throw new VNagException("Cannot unzip version.json"); |
66 | daniel-mar | 66 | |
77 | daniel-mar | 67 | $cont = implode("\n",$out); |
66 | daniel-mar | 68 | |
77 | daniel-mar | 69 | assert(@json_decode($cont,true) !== false); |
70 | |||
71 | @file_put_contents($cache_file, $cont); |
||
66 | daniel-mar | 72 | } |
73 | |||
77 | daniel-mar | 74 | $json = @json_decode($cont,true); |
75 | if ($json === false) throw new VNagException("version.json has invalid JSON data"); |
||
76 | return (string)$json['name']; |
||
40 | daniel-mar | 77 | } |
78 | |||
76 | daniel-mar | 79 | protected function get_latest_version() { |
80 | $cont = $this->url_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json'); |
||
77 | daniel-mar | 81 | if ($cont === false) throw new VNagException("Cannot detect latest available Minecraft version (GET request failed)"); |
76 | daniel-mar | 82 | $json = @json_decode($cont, true); |
77 | daniel-mar | 83 | if ($json === false) throw new VNagException("Cannot detect latest available Minecraft version (JSON invalid data)"); |
76 | daniel-mar | 84 | $version = $json['latest']['release'] ?? null; |
77 | daniel-mar | 85 | if (!$version) throw new VNagException("Cannot detect latest available Minecraft version (JSON does not contain version)"); |
76 | daniel-mar | 86 | return $version; |
87 | } |
||
88 | |||
40 | daniel-mar | 89 | protected function cbRun($optional_args=array()) { |
90 | $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards |
||
76 | daniel-mar | 91 | $cur_ver = $this->get_local_version($system_dir); |
40 | daniel-mar | 92 | |
76 | daniel-mar | 93 | $new_ver = $this->get_latest_version(); |
40 | daniel-mar | 94 | |
57 | daniel-mar | 95 | if (version_compare($cur_ver,$new_ver) >= 0) { |
40 | daniel-mar | 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 | } |