Subversion Repositories vnag

Rev

Rev 40 | 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>
47 daniel-mar 2
AGm/a5N2RKsITjiz0KnYh35+TQ7ya4jusXkbmTSo9tutK49ouGx05yBncsc3kC92Z
3
VU1rk2Nau7wWDRL942pupi1q/hSDkQjslkvp4j4P093stHeI8QnGAYSyn3F4DJcRi
4
AEemHB3cGDkWVR0AK4kW4b9VzD6r1id93T9aCG7eWFWMd4wWE7z/wqVr2cOQrPkrr
5
ka7tChyplVFrtq7qeekxSvfWqNu1/lQBXFhVyH85J7ttJrggOvD0WKNCGUCpJm+Lq
6
MqbLjBEGa8I5g334BADhJI572aUjhLxBwqh9oL5UDFHLuzFecHbH6PO1EoUfX6716
7
eQoX00kWsZi99L7xbRKWKw8Cx+Kf96/Q8aJf31w5wjBi5X/rwODnAXIjxIIZXUH8r
8
a6TY2RSmrKlKfxdzmKVlEP6mCPSz1CZQhCJq80/pv8fmSIE9yyIpSMc2acUn62vWm
9
ICrDZd/j19o798vHxDZiKurJn+B69yzFdWuR5mpS2M4chXoVeGVofSOJqzRNwgeX1
10
r/LUYE1P5Vsy5t2CpzEg2ibcHP+0VNcA6zXvdIN0Cie9Yq6c6X5/nJKDajVjXKBJK
11
ytXN7ANThK7FC9SASZZ1aQSKsuj6NNccNp0pWYRuPlidALr9q0+G/8Ea6VYiDNFwp
12
Wr0Hk/xGlWBT4n7fGZKwN7ZPd0vwzSWdGtwBLjPWAN9Iu+qzQutAhcJ5LVWbdBsKE
13
xQY2C2GDPWkmX5rmETlQ7eaJLrbYqGE+tKTH9nDmExMkecVM+KHbqMhkCS3Fh3ygz
14
p+KSgCUB4xkm4KTeLMlb+xoD6G+JsapR3GJ2Ejgn57Vc5ehU49BeI+Sv8X230Cu9T
15
C2h/81XipPdrJ7uXkDx2rTBWjQ4wwEQweCFiVUnXQbAGHjkJqtj823Itzi/Si+1Jt
16
vOP9L2ByepU82pM3O0arnSJwkJG4LLs2OIYLw0UD6K9rwRPzBM4fODHgIbXmZeH37
17
x3m7f3DzuLSLlxqIXNIcP8t5AXpe4YlWs8yczickR2oAqx4TJbaMVhu/SpVm34ot9
18
9ryzDnEyfRAIH7cXby0wNaLhHrfGd+mlR1Bgd6bSdd3W9nSLkwqbpVYpC4bB/HgIl
19
MY0A4x/nikcHjCh5hpgdRb0AkZsc5K4m5AyWnPloS7/cVytQ/g4DfDSobteOdOkhU
20
o9lTyfyDsmAxuxA1JNqVLOYZfcwrtgPBX/V/WphAmrgZGsEwS4PGlGfaKEMMkxxI7
21
1zcmxYoNQgVpS2GmET6UOYcRP6FCjNMhp4pSaaQ27GejA5Mc7d0VgkbaNLFW8KaiO
22
xTK8JurMkIVeWqLY990VRuJCxk0ncOJ1xqpFXtLXWeeTzallHU3R3UoN5MRuhLsqE
23
w==
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
 *
32
 * Revision 2021-12-13
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
                );
63
                // curl 'https://www.minecraft.net/en-us/download/server' \
64
                //      -H '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' \
65
                //      -H 'accept-language: de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6' \
66
                //      --compressed
67
                $ch = curl_init();
68
                curl_setopt($ch, CURLOPT_URL,"https://www.minecraft.net/en-us/download/server"); // TODO: make locale configurable?
69
                curl_setopt($ch, CURLOPT_POST, 0);
70
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
71
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
72
                $cont = curl_exec($ch);
73
                curl_close($ch);
74
 
47 daniel-mar 75
                if (!$cont) throw new Exception("Cannot detect latest available Minecraft version (HTTPS request failed)");
76
 
40 daniel-mar 77
                preg_match_all('@minecraft_server\\.(.+)\\.jar@U', $cont, $m);
78
 
47 daniel-mar 79
                if (!isset($m[1][0])) throw new Exception("Cannot detect latest available Minecraft version (regex pattern mismatch)");
80
 
40 daniel-mar 81
                return $m[1][0];
82
        }
83
 
84
        protected function get_installed_minecraft_version($local_path) {
85
                if (substr($local_path,-4) == '.jar') {
86
                        $files = glob($local_path);
87
                } else {
88
                        $files = glob($local_path.'/server.jar');
89
                }
90
 
91
                if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
92
                if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
93
                $server_jar = $files[0];
94
 
95
                $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
96
 
97
                $out = array();
98
                $ec = -1;
99
                exec($cmd, $out, $ec);
100
                if ($ec != 0) throw new Exception("Cannot unzip version.json");
101
 
102
                $json = implode("\n",$out);
103
 
104
                return (string)json_decode($json,true)['name'];
105
        }
106
 
107
        protected function cbRun($optional_args=array()) {
108
                $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
109
                $cur_ver = $this->get_installed_minecraft_version($system_dir);
110
 
111
                $new_ver = $this->get_latest_minecraft_version();
112
 
113
                // TODO: also check/show if stable or not
114
                if ($cur_ver === $new_ver) {
115
                        $this->setStatus(VNag::STATUS_OK);
116
                        $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
117
                } else {
118
                        $this->setStatus(VNag::STATUS_WARNING);
119
                        $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
120
                }
121
        }
122
}