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
Ilya Mashchenko committed
Mar 11, 2019 at 00:02 UTC
e88794c934a717e806e2a6d97aa6173d5c559c45
1 file changed
+37
-14
collectors/python.d.plugin/spigotmc/spigotmc.chart.py
+37
-14
@@ -12,11 +12,15 @@ from bases.FrameworkServices.SimpleService import SimpleService
12
from third_party import mcrcon
13
14
# Update only every 5 seconds because collection takes in excess of
15
-# 100ms sometimes, and mos tpeople won't care about second-by-second data.
15
+# 100ms sometimes, and most people won't care about second-by-second data.
16
update_every = 5
17
18
PRECISION = 100
19
20
+COMMAND_TPS = 'tps'
21
+COMMAND_LIST = 'list'
22
+COMMAND_ONLINE = 'online'
23
+
24
ORDER = [
25
'tps',
26
'users',
@@ -38,16 +42,18 @@ CHARTS = {
42
]
43
}
44
}
45
+
46
+
47
_TPS_REGEX = re.compile(
42
- r'^.*: .*?' # Message lead-in
43
- r'(\d{1,2}.\d+), .*?' # 1-minute TPS value
44
- r'(\d{1,2}.\d+), .*?' # 5-minute TPS value
45
- r'(\d{1,2}\.\d+).*$', # 15-minute TPS value
46
- re.X | re.A
48
+ r'^.*: .*?' # Message lead-in
49
+ r'(\d{1,2}.\d+), .*?' # 1-minute TPS value
50
+ r'(\d{1,2}.\d+), .*?' # 5-minute TPS value
51
+ r'(\d{1,2}\.\d+).*$', # 15-minute TPS value
52
+ re.X
53
)
54
_LIST_REGEX = re.compile(
49
- r'(\d+)', # Current user count.
50
- re.X | re.A
55
+ r'(\d+)', # Current user count.
56
+ re.X
57
)
58
59
@@ -72,7 +78,8 @@ class Service(SimpleService):
78
self.error('Error connecting.')
79
self.error(repr(err))
80
return False
75
- return True
81
+
82
+ return self._get_data()
83
84
def connect(self):
85
self.console.connect(self.host, self.port, self.password)
@@ -92,17 +99,24 @@ class Service(SimpleService):
99
return True
100
101
def is_alive(self):
95
- if (not self.alive) or \
96
- self.console.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_INFO, 0) != 1:
102
+ if not any(
103
+ [
104
+ not self.alive,
105
+ self.console.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_INFO, 0) != 1
106
+ ]
107
+ ):
108
return self.reconnect()
109
return True
110
111
def _get_data(self):
112
if not self.is_alive():
113
return None
114
+
115
data = {}
116
+
117
try:
105
- raw = self.console.command('tps')
118
+ raw = self.console.command(COMMAND_TPS)
119
+ self.debug("'{0}' command output : {1}".format(COMMAND_TPS, raw))
120
match = _TPS_REGEX.match(raw)
121
if match:
122
data['tps1'] = int(float(match.group(1)) * PRECISION)
@@ -110,21 +124,29 @@ class Service(SimpleService):
124
data['tps15'] = int(float(match.group(3)) * PRECISION)
125
else:
126
self.error('Unable to process TPS values.')
127
+ if not raw:
128
+ self.error("'{0}' command returned no value, make sure you set correct password".format(COMMAND_TPS))
129
except mcrcon.MCRconException:
130
self.error('Unable to fetch TPS values.')
131
except socket.error:
132
self.error('Connection is dead.')
133
self.alive = False
134
return None
135
+
136
try:
120
- raw = self.console.command('list')
137
+ raw = self.console.command(COMMAND_LIST)
138
+ self.debug("'{0}' command output : {1}".format(COMMAND_LIST, raw))
139
match = _LIST_REGEX.search(raw)
140
if not match:
123
- raw = self.console.command('online')
141
+ raw = self.console.command(COMMAND_ONLINE)
142
+ self.debug("'{0}' command output : {1}".format(COMMAND_ONLINE, raw))
143
match = _LIST_REGEX.search(raw)
144
if match:
145
data['users'] = int(match.group(1))
146
else:
147
+ if not raw:
148
+ self.error("'{0}' and '{1}' commands returned no value, make sure you set correct password".format(
149
+ COMMAND_LIST, COMMAND_ONLINE))
150
self.error('Unable to process user counts.')
151
except mcrcon.MCRconException:
152
self.error('Unable to fetch user counts.')
@@ -132,4 +154,5 @@ class Service(SimpleService):
154
self.error('Connection is dead.')
155
self.alive = False
156
return None
157
+
158
return data