Subversion Repositories vnag

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
40 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
2
oWlckS6PIRQ0p+i9C84P7Tm9qPWTQUJKoU/t1I+E0ripza58ZyM6QdU3kxSKkswmh
3
Gfk5BmdJ+8a8Q8mSnywxUcZbsJ6mXtAiaJ9VWIGHWuGAX2CtZrxpUVNDZ2ijoMz5W
4
rlh3aKEvzZ0V/lUVpZFWjE9MHYg4djoGbHW5Ql11iuDsjLO1YQBp0rAGzKquPBwaH
5
fIvRjdcpy1zLNBIZb8Pz5BuGD1IwDxxGZWeZP0WrNaiZVEaB21FBh8E3Ujr8AOJ76
6
WEd2k37tRWh0Yy02n3nLqKitFQYCE1KRmVzlGtH3/4N7X82UnYJR6I0jO7Y+6DjvJ
7
nhxdL76/EUlTeuXpF2vR7yjaxXW/3KRBALA7mP4Uq8OsAn+jt2zjZd8KNiC2IPnGq
8
aRl3BfLy+z5CyS6v+kTmSUd+a4o+d9D8y+oG2zEl9fCHvJOWJldtCVbNPztr7CrCh
9
SMG4kgf0xZtjjj0jPYFoVkYyy7UjnMbQwhrz97f3Sa/QihrnUnfOPrcBD6ebgkYdg
10
UKSBB3UWrJCUan8kuI4baf4UYrX8iRUitvNfbp69/t8sfSrJnywxsVgTdxKu/jJKQ
11
7LLVb7ivg5xcFpZp2IFeylK+fr/Z0I+T4X2HV624kTrLAzSz4MeorF5+Z8hwBbh1V
12
U02NM7HXZ3n9TStjWX+bf11BcOkB/mXXBdm+Pzu2um+xzElWh20vwINcMESLxVC3k
13
h4dAKMDqVd10DEoK0uLRHPeDeRz0Yc5GapuhfrpWf+mRhKbSYOzuMDzoLTZjt2xPE
14
lAH4I8CkPXXKB1DbHCnj8pEYPP9ul/GGZYVt5eGqJ3zB3oerqa79ftBFvTehJqpKx
15
T2lQ6u1r6Y9h/7GtURDFz0pOHqDBNHFvrmNhX8Lw+HRSMYFI6d3GzwdsBtw4Ke4YF
16
DWR8NvAzjYGIJJOh9RQHvz8saSNRjM9iMJ3HIi/YW1Gtb98a0xLeyzMtSfrFDWXPP
17
uLpZfbtorONbHbF5+ltauvmsDF++u69s80rItJtoggsw5uRXkj6NOGSN19Pw3pqtC
18
fF0iJ4/Ir/xcUdzbR7MBL85urAdza/Cim0vhfzTN3SpLjYmf50BrvAU0kAuyKrjHf
19
FAq+y8PrxiWRSF5dFt+lZvUBuxc2uau6gOzS6V7r/ZJwu/8wOtHguqqZVoiDrkhO4
20
cj09VQOzG3EqWUidlpiDwWKsDLDzrKYCKKSNZt4UjQ5qV8+WgPdh0XT4XSj5nMwt/
21
kcBwPCaIeCXMhpktVS8AYSskPaD5RYp8XZjCkiSOI8hFHOKYMl8iaUw7cM6j2YuBn
22
eHavCPB03FOK3YvsheEfIYYfNez+vsQqgTXOVWzuGtU1oMYL9fi15mRoQW44bbdxM
23
g==
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
 
75
                preg_match_all('@minecraft_server\\.(.+)\\.jar@U', $cont, $m);
76
 
77
                return $m[1][0];
78
        }
79
 
80
        protected function get_installed_minecraft_version($local_path) {
81
                if (substr($local_path,-4) == '.jar') {
82
                        $files = glob($local_path);
83
                } else {
84
                        $files = glob($local_path.'/server.jar');
85
                }
86
 
87
                if (count($files) == 0) throw new Exception("No server.jar found at $local_path");
88
                if (count($files) > 1) throw new Exception("More than one server.jar found at $local_path");
89
                $server_jar = $files[0];
90
 
91
                $cmd = "unzip -p ".escapeshellarg($server_jar)." version.json";
92
 
93
                $out = array();
94
                $ec = -1;
95
                exec($cmd, $out, $ec);
96
                if ($ec != 0) throw new Exception("Cannot unzip version.json");
97
 
98
                $json = implode("\n",$out);
99
 
100
                return (string)json_decode($json,true)['name'];
101
        }
102
 
103
        protected function cbRun($optional_args=array()) {
104
                $system_dir = $this->argSystemDir->getValue(); // note: can contain wildcards
105
                $cur_ver = $this->get_installed_minecraft_version($system_dir);
106
 
107
                $new_ver = $this->get_latest_minecraft_version();
108
 
109
                // TODO: also check/show if stable or not
110
                if ($cur_ver === $new_ver) {
111
                        $this->setStatus(VNag::STATUS_OK);
112
                        $this->setHeadline("Minecraft version $cur_ver is the latest available version for your Minecraft for Java server installation at $system_dir", true);
113
                } else {
114
                        $this->setStatus(VNag::STATUS_WARNING);
115
                        $this->setHeadline("Minecraft version $cur_ver is outdated. Newer version is $new_ver for installation at $system_dir", true);
116
                }
117
        }
118
}