summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlya Mashchenko <ilyamaschenko@gmail.com>2019-03-11 00:02:44 +0300
committerGitHub <noreply@github.com>2019-03-11 00:02:44 +0300
commite88794c934a717e806e2a6d97aa6173d5c559c45 (patch)
tree34a1026d2b767f776deb3e2991e7174a347eeb5c
parent8b4f4dd68668b6c8ecc99b2578d572347ef271b5 (diff)
spigotmc: py2 compatibility fix (#5593)
* py2 compatibility fix: do not use re.A (re.ASCII) * debug and minor changes * more debug output and check fix
-rw-r--r--collectors/python.d.plugin/spigotmc/spigotmc.chart.py51
1 files changed, 37 insertions, 14 deletions
diff --git a/collectors/python.d.plugin/spigotmc/spigotmc.chart.py b/collectors/python.d.plugin/spigotmc/spigotmc.chart.py
index 00ba5b03db..16b9d12ea2 100644
--- a/collectors/python.d.plugin/spigotmc/spigotmc.chart.py
+++ b/collectors/python.d.plugin/spigotmc/spigotmc.chart.py
@@ -12,11 +12,15 @@ 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',
@@ -38,16 +42,18 @@ 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 | re.A
+ 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 | re.A
+ r'(\d+)', # Current user count.
+ re.X
)
@@ -72,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)
@@ -92,17 +99,24 @@ 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')
+ raw = self.console.command(COMMAND_TPS)
+ self.debug("'{0}' command output : {1}".format(COMMAND_TPS, raw))
match = _TPS_REGEX.match(raw)
if match:
data['tps1'] = int(float(match.group(1)) * PRECISION)
@@ -110,21 +124,29 @@ class Service(SimpleService):
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
+
try:
- raw = self.console.command('list')
+ raw = self.console.command(COMMAND_LIST)
+ self.debug("'{0}' command output : {1}".format(COMMAND_LIST, raw))
match = _LIST_REGEX.search(raw)
if not match:
- raw = self.console.command('online')
+ raw = self.console.command(COMMAND_ONLINE)
+ self.debug("'{0}' command output : {1}".format(COMMAND_ONLINE, raw))
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.')
@@ -132,4 +154,5 @@ class Service(SimpleService):
self.error('Connection is dead.')
self.alive = False
return None
+
return data