Subversion Repositories vnag

Rev

Rev 61 | 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 /* <ViaThinkSoftSignature>
62 daniel-mar 2
VoLrukd2+1rHWc8YwnYyT7d2R4OI9x3gYpI9oiFWRkU32Yv9zQ9Yl2iHQD/2eq1vU
3
HYHjdpEAWrMkSrpo9MNZ+mbyRwXXd0gK/cLQiLlYy5BCMypYdNJ54Yy/7jNto7O1u
4
Wt3iBPVZeLhgmdoM3UCWSJW1c0Ngj8zv8hg7vrgU5tCqwucEvfDARDNfZs/wyQj0Z
5
n8Kdj3FxhzBuM4Jm7BUgz5YJnPuKF3HVnnAMp/F68zr1ouQwzA67xPr02vd8Apbu5
6
WU2gpJzhxMvmAvNnWz5HgkSqSQb0W4ABi0M2HNzTx0f6Xr57phn8MkL6fPqGpiNE+
7
U/3IhdaSIGBqmhcg6O14MNHQzvdMcowSyEPEAz4nyGNjmobDjhSN9sYeW5zwotOfd
8
Kwrz/0UlgGMhWeW3dwgxd0lUDgjvYjJtaru7Z0mkyyZNcTwKnE+BOFTZfADYe0PUm
9
zY3CSSftjsfuUBap1aF1Jxy4/xVctgTVBWdKiK1Lk2C/iGEkeIFxWIk5wFVhJecr0
10
QCDzCc88s7HHNRmZwiGYGzN/2jjswOHc9KpgOczQWd1BLgX53b8rRmU1CT9u1EXli
11
2ePh/QHB89fo9iaGaC5uWmFYpGdLh8CSI4evdPT4gC2hhjIpl3+t9LoR4T7i1wHKZ
12
Uk1SVNvQdQUeDBZNkoqFMW8iwJcU1VgrMmr0tCqu88bxaNFAPnvOZq3W5YbPMwOvQ
13
Q+G9W8dPafu0x8VjlcREkd3oHDMZFmRULPgOLhzj6Q9Ms/aBoKHRZH7T+2nzhJ1w3
14
OL7MY33Jr9WBZszPfcerOAPil65w4dS9LVzUq1mdufZ4udRMqr0DCARgk637KT7Tj
15
tLntP877IW/ZaveUaLIEmFUPrK097tNi9d6og6Gb26aEeH6bd3KsVfmvgYAw7FSnM
16
v9QvxjMYXrAp7X7mZ1DTad4lwAZBml8k1uQhNfeMxGXCF9Hs8wlnLCyQXM9HqhVof
17
71yLzIGfS8DPIRoI6Mv8MZ0LLWqn8spNOyoazOPcNAcJaxz21J8f20F6cTjquhiaG
18
oDpLH5pqjnJptdPJmZGCfzocHpAHqSsZ2zlghcoL/25iVa3sCwSVlW3/SXdrh7OIz
19
smIG02GjCFa4n9/kAGewSb/UJvZmB+Z2UbMfbGKqRUoUmZvh99+qfnFHfH2cG59+b
20
uviYgBLY37MEbnJ4rdyO8KtlZjcBsHzf8AyqDPthJS9hN9KGd/dq4/EneIlm+nnbo
21
zQuo6Q7W2IahVsrI5Wnr9GYl9mZLBoZ1X2kn7xrprMT5K8RwohjKv2zeQaOlsBX7+
22
NkeaktqUt69FIza1BNJ5irvqGbWdkVLeeVv7W6oZYZkaQCLtgy3sujDDxCs+mIMrE
57 daniel-mar 23
g==
40 daniel-mar 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
 *
61 daniel-mar 32
 * Revision 2023-05-02
40 daniel-mar 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
                $headers = array(
58
                        // These headers are important! Otherwise the request will be blocked by AkamaiGhost
59
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
60
                        "Accept-Language: de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6",
61
                        "Accept-Encoding: none"
62
                );
61 daniel-mar 63
 
64
                // TODO: Version is currently not shown anymore: https://bugs.mojang.com/browse/WEB-6497
65
                /*
40 daniel-mar 66
                $ch = curl_init();
67
                curl_setopt($ch, CURLOPT_URL,"https://www.minecraft.net/en-us/download/server"); // TODO: make locale configurable?
68
                curl_setopt($ch, CURLOPT_POST, 0);
69
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
70
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
71
                $cont = curl_exec($ch);
72
                curl_close($ch);
47 daniel-mar 73
                if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request failed)");
40 daniel-mar 74
                preg_match_all('@minecraft_server\\.(.+)\\.jar@U', $cont, $m);
47 daniel-mar 75
                if (!isset($m[1][0])) throw new Exception("Cannot detect latest available Minecraft version (regex pattern mismatch)");
61 daniel-mar 76
                return $m[1][0];
77
                */
47 daniel-mar 78
 
61 daniel-mar 79
                for ($page=1; $page<=2; $page++) {
80
                        $url = 'https://feedback.minecraft.net/hc/en-us/sections/360001186971-Release-Changelogs';
81
                        if ($page > 1) $url .= '?page='.$page;
82
                        $url = 'https://webcache.googleusercontent.com/search?q=cache:'.urlencode($url); // Bypass CloudFlare...
83
 
84
                        $ch = curl_init();
85
                        curl_setopt($ch, CURLOPT_URL, $url);
86
                        curl_setopt($ch, CURLOPT_POST, 0);
87
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
88
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
89
                        $cont = curl_exec($ch);
90
                        curl_close($ch);
91
                        if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request failed)");
92
                        preg_match_all('@>Minecraft: Java Edition \\- (.+)</@U', $cont, $m);
93
                        if (isset($m[1][0])) return $m[1][0];
94
                }
95
                throw new Exception("Cannot detect latest available Minecraft version (regex pattern mismatch)");
40 daniel-mar 96
        }
97
 
98
        protected function get_installed_minecraft_version($local_path) {
99
                if (substr($local_path,-4) == '.jar') {
100
                        $files = glob($local_path);
101
                } else {
102
                        $files = glob($local_path.'/server.jar');
103
                }
104
 
105
                if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
106
                if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
107
                $server_jar = $files[0];
108
 
109
                $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
110
 
111
                $out = array();
112
                $ec = -1;
113
                exec($cmd, $out, $ec);
114
                if ($ec != 0) throw new Exception("Cannot unzip version.json");
115
 
116
                $json = implode("\n",$out);
117
 
118
                return (string)json_decode($json,true)['name'];
119
        }
120
 
121
        protected function cbRun($optional_args=array()) {
122
                $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
123
                $cur_ver = $this->get_installed_minecraft_version($system_dir);
124
 
125
                $new_ver = $this->get_latest_minecraft_version();
126
 
57 daniel-mar 127
                if (version_compare($cur_ver,$new_ver) >= 0) {
40 daniel-mar 128
                        $this->setStatus(VNag::STATUS_OK);
129
                        $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
130
                } else {
131
                        $this->setStatus(VNag::STATUS_WARNING);
132
                        $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
133
                }
134
        }
135
}