UUID-fix: empty usernames from the Mojang API (request response status code 204) no longer get passed up to collector

This commit is contained in:
Brian 2021-03-16 13:53:46 +01:00
parent 5432fed77b
commit c502e7194f

View file

@ -1,4 +1,3 @@
from prometheus_client import start_http_server, REGISTRY, Metric
import time import time
import requests import requests
import json import json
@ -9,7 +8,7 @@ import schedule
from mcrcon import MCRcon from mcrcon import MCRcon
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
from prometheus_client import start_http_server, REGISTRY, Metric
class MinecraftCollector(object): class MinecraftCollector(object):
def __init__(self): def __init__(self):
self.statsdirectory = "/world/stats" self.statsdirectory = "/world/stats"
@ -36,9 +35,12 @@ class MinecraftCollector(object):
if uuid in self.map: if uuid in self.map:
return self.map[uuid] return self.map[uuid]
else: else:
result = requests.get('https://api.mojang.com/user/profiles/'+uuid+'/names') try:
result = requests.get('https://api.mojang.com/user/profiles/' + uuid + '/names')
self.map[uuid] = result.json()[-1]['name'] self.map[uuid] = result.json()[-1]['name']
return(result.json()[-1]['name']) return(result.json()[-1]['name'])
except:
return False
def rcon_command(self,command): def rcon_command(self,command):
if self.rcon == None: if self.rcon == None:
@ -149,8 +151,11 @@ class MinecraftCollector(object):
return data return data
def update_metrics_for_player(self,uuid): def update_metrics_for_player(self,uuid):
data = self.get_player_stats(uuid)
name = self.uuid_to_player(uuid) name = self.uuid_to_player(uuid)
if not name: return
data = self.get_player_stats(uuid)
blocks_mined = Metric('blocks_mined','Blocks a Player mined',"counter") blocks_mined = Metric('blocks_mined','Blocks a Player mined',"counter")
blocks_picked_up = Metric('blocks_picked_up','Blocks a Player picked up',"counter") blocks_picked_up = Metric('blocks_picked_up','Blocks a Player picked up',"counter")
player_deaths = Metric('player_deaths','How often a Player died',"counter") player_deaths = Metric('player_deaths','How often a Player died',"counter")
@ -288,11 +293,14 @@ class MinecraftCollector(object):
def collect(self): def collect(self):
for player in self.get_players(): for player in self.get_players():
for metric in self.update_metrics_for_player(player): metrics = self.update_metrics_for_player(player)
yield metric if not metrics: continue
for metric in self.get_server_stats():
for metric in metrics:
yield metric yield metric
for metric in self.get_server_stats():
yield metric
if __name__ == '__main__': if __name__ == '__main__':
if all(x in os.environ for x in ['RCON_HOST','RCON_PASSWORD']): if all(x in os.environ for x in ['RCON_HOST','RCON_PASSWORD']):
@ -300,7 +308,7 @@ if __name__ == '__main__':
start_http_server(8000) start_http_server(8000)
REGISTRY.register(MinecraftCollector()) REGISTRY.register(MinecraftCollector())
print("Exporter started on Port 8000") print("\nExporter started on Port 8000\n")
while True: while True:
time.sleep(1) time.sleep(1)
schedule.run_pending() schedule.run_pending()