Fixes for the spigotmc plugin:
* Wired up the remaining missing stuff for the auto-reconnect code (still doesn't work correctly). * Made the data extraction a bit quicker and more robust.
Austin S. Hemmelgarn committed
Apr 30, 2018 at 10:52 UTC
4a1fadcef64877e529973630e92521e57f7c38d0
1 file changed
+22
-5
python.d/spigotmc.chart.py
+22
-5
@@ -38,6 +38,7 @@ class Service(SimpleService):
38
self.port = self.configuration.get('port', 25575)
39
self.password = self.configuration.get('password', '')
40
self.console = mcrcon.MCRcon()
41
+ self.alive = True
42
43
def check(self):
44
try:
@@ -56,11 +57,19 @@ class Service(SimpleService):
57
except mcrcon.MCRconException:
58
pass
59
self.console.connect(self.host, self.port, self.password)
60
+ self.alive = True
61
except (mcrcon.MCRconException, socket.error):
62
return False
63
return True
64
65
+ def is_alive(self):
66
+ if not self.alive:
67
+ return self.reconnect()
68
+ return True
69
+
70
def _get_data(self):
71
+ if not self.is_alive():
72
+ return None
73
data = {}
74
try:
75
raw = self.console.command('tps')
@@ -68,17 +77,25 @@ class Service(SimpleService):
77
# '§6TPS from last 1m, 5m, 15m: §a19.99, §a19.99, §a19.99\n'
78
# The values we care about are the three numbers after the :
79
tmp = raw.split(':')[1].split(',')
71
- data['tps1'] = float(tmp[0].lstrip()[2:]) * PRECISION
72
- data['tps5'] = float(tmp[1].lstrip()[2:]) * PRECISION
73
- data['tps15'] = float(tmp[2].lstrip().rstrip()[2:]) * PRECISION
74
- except (mcrcon.MCRconException, socket.error):
80
+ data['tps1'] = float(tmp[0].lstrip(u' §a*')) * PRECISION
81
+ data['tps5'] = float(tmp[1].lstrip(u' §a*')) * PRECISION
82
+ data['tps15'] = float(tmp[2].lstrip(u' §a*').rstrip()) * PRECISION
83
+ except mcrcon.MCRconException:
84
self.error('Unable to fetch TPS values.')
85
+ except socket.error:
86
+ self.error('Connection is dead.')
87
+ self.alive = False
88
+ return None
89
try:
90
raw = self.console.command('list')
91
# The above command returns a string that looks like this:
92
# 'There are 0/20 players online:'
93
# We care about the first number here.
94
data['users'] = int(raw.split()[2].split('/')[0])
82
- except (mcrcon.MCRconException, socket.error):
95
+ except mcrcon.MCRconException:
96
self.error('Unable to fetch user counts.')
97
+ except socket.error:
98
+ self.error('Connection is dead.')
99
+ self.alive = False
100
+ return None
101
return data