diff --git a/nixstatsagent/nixstatsagent.py b/nixstatsagent/nixstatsagent.py index 5001def..b45ab34 100755 --- a/nixstatsagent/nixstatsagent.py +++ b/nixstatsagent/nixstatsagent.py @@ -27,7 +27,7 @@ import urllib2 -__version__ = '1.1.40' +__version__ = '1.1.41' __FILEABSDIRNAME__ = os.path.dirname(os.path.abspath(__file__)) diff --git a/nixstatsagent/plugins/minecraft.py b/nixstatsagent/plugins/minecraft.py index 7d25db3..bee5b61 100644 --- a/nixstatsagent/plugins/minecraft.py +++ b/nixstatsagent/plugins/minecraft.py @@ -11,47 +11,51 @@ def run(self, config): Fetch the amount of active and max players add to /etc/nixstats.ini [minecraft] - enabled=yes - host=minecraft_host_or_IP - port=minecraft_PORT + enabled=yes + hosts=127.0.0.1:8000,127.0.0.2:8000... ''' - try: - # Connect - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - host = config.get('minecraft', 'host') - port = int(config.get('minecraft', 'port')) - s.connect((host, port)) - - # Send handshake + status request - s.send(self.pack_data("\x00\x00" + self.pack_data(host.encode('utf8')) + self.pack_port(port) + "\x01")) - s.send(self.pack_data("\x00")) - - # Read response - self.unpack_varint(s) # Packet length - self.unpack_varint(s) # Packet ID - l = self.unpack_varint(s) # String length - - d = "" - while len(d) < l: - d += s.recv(1024) - - # Close our socket - s.close() - except: - return "Could not connect to server" - - results = {} - - try: - players = json.loads(d.decode('utf8'))['players'] - results['online'] = int(players['online']) - results['max'] = int(players['max']) - except: - results['online'] = 0 - results['max'] = 0 - - return results + my_hosts = config.get('minecraft', 'hosts').split(',') + result = {} + for connection_string in my_hosts: + try: + # Connect + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + hostname = connection_string.split(':')[0] + port = int(connection_string.split(':')[1]) + s.connect((hostname, port)) + + # Send handshake + status request + s.send(self.pack_data("\x00\x00" + self.pack_data(hostname.encode('utf8')) + self.pack_port(port) + "\x01")) + s.send(self.pack_data("\x00")) + + # Read response + self.unpack_varint(s) # Packet length + self.unpack_varint(s) # Packet ID + l = self.unpack_varint(s) # String length + + d = "" + while len(d) < l: + d += s.recv(1024) + + # Close our socket + s.close() + except: + pass + + results = {} + + try: + players = json.loads(d.decode('utf8'))['players'] + results['online'] = int(players['online']) + results['max'] = int(players['max']) + except: + results['online'] = 0 + results['max'] = 0 + result[str(connection_string.replace('.', '-'))] = results + + return result + def unpack_varint(self, s): d = 0