From f69a06f4ecedd8a553dcd27dde15a1232a28ebb3 Mon Sep 17 00:00:00 2001 From: Caspar Chou Date: Sat, 7 Mar 2020 19:09:24 +0800 Subject: [PATCH] fix 1.15 issues --- Dockerfile | 7 +++---- docker-compose.yaml | 12 ++++++++++++ minecraft_exporter.py | 14 +++++++++----- requirements.txt | 4 ++++ 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yaml create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile index 2b3b557..548189f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,7 @@ -from python:3 +FROM python:3-alpine -RUN pip install nbt mcrcon prometheus_client requests - -COPY minecraft_exporter.py / +COPY requirements.txt minecraft_exporter.py / +RUN pip install --no-cache-dir -r requirements.txt EXPOSE 8000 diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..cb439b4 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3.1' +services: + minecraft-exporter: + image: 'joshi425/minecraft_exporter' + environment: + RCON_HOST: + RCON_PORT: + RCON_PASSWORD: + ports: + - 9700:8000 + volumes: + - /path/to/minecraft/world:/world:ro diff --git a/minecraft_exporter.py b/minecraft_exporter.py index 11df32a..d817c28 100644 --- a/minecraft_exporter.py +++ b/minecraft_exporter.py @@ -2,7 +2,7 @@ from prometheus_client import start_http_server, REGISTRY, Metric import time import requests import json -import nbt +import nbt import re import os from mcrcon import MCRcon @@ -26,7 +26,7 @@ class MinecraftCollector(object): def uuid_to_player(self,uuid): uuid = uuid.replace('-','') if uuid in self.map: - return self.map[uuid] + return self.map[uuid] else: result = requests.get('https://api.mojang.com/user/profiles/'+uuid+'/names') self.map[uuid] = result.json()[0]['name'] @@ -50,7 +50,7 @@ class MinecraftCollector(object): for dimid, dimname, meanticktime, meantps in dimtpsregex.findall(resp): dim_tps.add_sample('dim_tps',value=meantps,labels={'dimension_id':dimid,'dimension_name':dimname}) dim_ticktime.add_sample('dim_ticktime',value=meanticktime,labels={'dimension_id':dimid,'dimension_name':dimname}) - overallregex = re.compile("Overall : Mean tick time: (.*) ms. Mean TPS: (.*)") + overallregex = re.compile("Overall\s?: Mean tick time: (.*) ms. Mean TPS: (.*)") overall_tps.add_sample('overall_tps',value=overallregex.findall(resp)[0][1],labels={}) overall_ticktime.add_sample('overall_ticktime',value=overallregex.findall(resp)[0][0],labels={}) @@ -95,7 +95,9 @@ class MinecraftCollector(object): count = 0 advancements = json.load(json_file) for key, value in advancements.items(): - if value["done"] == True: + if key in ("DataVersion"): + continue + if value["done"] == True: count += 1 data["stat.advancements"] = count if self.questsEnabled: @@ -125,6 +127,8 @@ class MinecraftCollector(object): player_quests_finished = Metric('player_quests_finished', 'Number of quests a Player has finished', 'counter') player_used_crafting_table = Metric('player_used_crafting_table',"Times a Player used a Crafting Table","counter") for key, value in data.items(): + if key in ("stats", "DataVersion"): + continue stat = key.split(".")[1] # entityKilledBy if stat == "mineBlock": blocks_mined.add_sample("blocks_mined",value=value,labels={'player':name,'block':'.'.join((key.split(".")[2],key.split(".")[3]))}) @@ -199,4 +203,4 @@ if __name__ == '__main__': REGISTRY.register(MinecraftCollector()) print("Exporter started on Port 8000") while True: - time.sleep(1) + time.sleep(1) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cf73475 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +mcrcon==0.5.2 +NBT==1.5.0 +prometheus-client==0.7.1 +requests==2.5.1