From b26be28df9fd4db2106cc2a557966c9d2a7345d9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 16 Mar 2019 08:50:20 +0100 Subject: Adding upstream version 1.13.0. Signed-off-by: Daniel Baumann --- .../python.d.plugin/spigotmc/spigotmc.chart.py | 74 ++++++++++++++++------ 1 file changed, 53 insertions(+), 21 deletions(-) (limited to 'collectors/python.d.plugin/spigotmc/spigotmc.chart.py') diff --git a/collectors/python.d.plugin/spigotmc/spigotmc.chart.py b/collectors/python.d.plugin/spigotmc/spigotmc.chart.py index 09674f5c9..536fbe6a8 100644 --- a/collectors/python.d.plugin/spigotmc/spigotmc.chart.py +++ b/collectors/python.d.plugin/spigotmc/spigotmc.chart.py @@ -5,17 +5,22 @@ import socket import platform +import re from bases.FrameworkServices.SimpleService import SimpleService from third_party import mcrcon # Update only every 5 seconds because collection takes in excess of -# 100ms sometimes, and mos tpeople won't care about second-by-second data. +# 100ms sometimes, and most people won't care about second-by-second data. update_every = 5 PRECISION = 100 +COMMAND_TPS = 'tps' +COMMAND_LIST = 'list' +COMMAND_ONLINE = 'online' + ORDER = [ 'tps', 'users', @@ -39,6 +44,19 @@ CHARTS = { } +_TPS_REGEX = re.compile( + r'^.*: .*?' # Message lead-in + r'(\d{1,2}.\d+), .*?' # 1-minute TPS value + r'(\d{1,2}.\d+), .*?' # 5-minute TPS value + r'(\d{1,2}\.\d+).*$', # 15-minute TPS value + re.X +) +_LIST_REGEX = re.compile( + r'(\d+)', # Current user count. + re.X +) + + class Service(SimpleService): def __init__(self, configuration=None, name=None): SimpleService.__init__(self, configuration=configuration, name=name) @@ -60,7 +78,8 @@ class Service(SimpleService): self.error('Error connecting.') self.error(repr(err)) return False - return True + + return self._get_data() def connect(self): self.console.connect(self.host, self.port, self.password) @@ -80,44 +99,57 @@ class Service(SimpleService): return True def is_alive(self): - if (not self.alive) or \ - self.console.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_INFO, 0) != 1: + if not any( + [ + not self.alive, + self.console.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_INFO, 0) != 1 + ] + ): return self.reconnect() return True def _get_data(self): if not self.is_alive(): return None + data = {} + try: - raw = self.console.command('tps') - # The above command returns a string that looks like this: - # '§6TPS from last 1m, 5m, 15m: §a19.99, §a19.99, §a19.99\n' - # The values we care about are the three numbers after the : - tmp = raw.split(':')[1].split(',') - data['tps1'] = float(tmp[0].lstrip(u' §a*')) * PRECISION - data['tps5'] = float(tmp[1].lstrip(u' §a*')) * PRECISION - data['tps15'] = float(tmp[2].lstrip(u' §a*').rstrip()) * PRECISION + raw = self.console.command(COMMAND_TPS) + match = _TPS_REGEX.match(raw) + if match: + data['tps1'] = int(float(match.group(1)) * PRECISION) + data['tps5'] = int(float(match.group(2)) * PRECISION) + data['tps15'] = int(float(match.group(3)) * PRECISION) + else: + self.error('Unable to process TPS values.') + if not raw: + self.error("'{0}' command returned no value, make sure you set correct password".format(COMMAND_TPS)) except mcrcon.MCRconException: self.error('Unable to fetch TPS values.') except socket.error: self.error('Connection is dead.') self.alive = False return None - except (TypeError, LookupError): - self.error('Unable to process TPS values.') + try: - raw = self.console.command('list') - # The above command returns a string that looks like this: - # 'There are 0/20 players online:' - # We care about the first number here. - data['users'] = int(raw.split()[2].split('/')[0]) + raw = self.console.command(COMMAND_LIST) + match = _LIST_REGEX.search(raw) + if not match: + raw = self.console.command(COMMAND_ONLINE) + match = _LIST_REGEX.search(raw) + if match: + data['users'] = int(match.group(1)) + else: + if not raw: + self.error("'{0}' and '{1}' commands returned no value, make sure you set correct password".format( + COMMAND_LIST, COMMAND_ONLINE)) + self.error('Unable to process user counts.') except mcrcon.MCRconException: self.error('Unable to fetch user counts.') except socket.error: self.error('Connection is dead.') self.alive = False return None - except (TypeError, LookupError): - self.error('Unable to process user counts.') + return data -- cgit v1.2.3