From bd29915e5550af575039f5a569a74e07db7ed2f9 Mon Sep 17 00:00:00 2001 From: Joshua Grimm Date: Wed, 30 Oct 2019 17:37:33 +0000 Subject: [PATCH] add player-online metric via RCON --- README.md | 1 + dashboard/server-stats.json | 69 ++++++++++++++++++++++++++++++++++--- minecraft_exporter.py | 9 ++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 20b0a2c..bfa629a 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ dim_tps dim_ticktime overall_tps overall_ticktime +player_online ``` # Dashboards diff --git a/dashboard/server-stats.json b/dashboard/server-stats.json index 977cf21..1861194 100644 --- a/dashboard/server-stats.json +++ b/dashboard/server-stats.json @@ -27,6 +27,12 @@ "id": "prometheus", "name": "Prometheus", "version": "1.0.0" + }, + { + "type": "panel", + "id": "table", + "name": "Table", + "version": "" } ], "annotations": { @@ -212,8 +218,8 @@ "format": "short", "label": null, "logBase": 1, - "max": null, - "min": null, + "max": "20", + "min": "0", "show": true }, { @@ -401,8 +407,63 @@ "align": false, "alignLevel": null } + }, + { + "columns": [], + "datasource": "${DS_PROMETHEUS}", + "fontSize": "100%", + "gridPos": { + "h": 6, + "w": 3, + "x": 0, + "y": 17 + }, + "id": 9, + "options": {}, + "pageSize": null, + "showHeader": true, + "sort": { + "col": 0, + "desc": true + }, + "styles": [ + { + "alias": "Time", + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "pattern": "Time", + "type": "hidden" + }, + { + "alias": "", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "decimals": 2, + "pattern": "/Value/", + "thresholds": [], + "type": "hidden", + "unit": "short" + } + ], + "targets": [ + { + "expr": "player_online", + "instant": true, + "legendFormat": "{{player}}", + "refId": "A" + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Players Online", + "transform": "timeseries_to_rows", + "type": "table" } ], + "refresh": "5s", "schemaVersion": 20, "style": "dark", "tags": [], @@ -410,12 +471,12 @@ "list": [] }, "time": { - "from": "now-6h", + "from": "now-30m", "to": "now" }, "timepicker": {}, "timezone": "", "title": "Minecraft Server", "uid": "LhW0bV0Wz", - "version": 3 + "version": 7 } diff --git a/minecraft_exporter.py b/minecraft_exporter.py index d357990..a37649c 100644 --- a/minecraft_exporter.py +++ b/minecraft_exporter.py @@ -34,6 +34,7 @@ class MinecraftCollector(object): dim_ticktime = Metric('dim_ticktime',"Time a Tick took in a Dimension","counter") overall_tps = Metric('overall_tps','overall TPS',"counter") overall_ticktime = Metric('overall_ticktime',"overall Ticktime","counter") + player_online = Metric('player_online',"is 1 if player is online","counter") mcr = MCRcon(os.environ['RCON_HOST'],os.environ['RCON_PASSWORD'],port=int(os.environ['RCON_PORT'])) mcr.connect() resp = mcr.command("forge tps") @@ -44,7 +45,13 @@ class MinecraftCollector(object): overallregex = re.compile("Overall : 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={}) - return[dim_tps,dim_ticktime,overall_tps,overall_ticktime] + resp = mcr.command("list") + playerregex = re.compile("There are \d*\/20 players online:(.*)") + if playerregex.findall(resp): + for player in playerregex.findall(resp)[0].split(","): + if player: + player_online.add_sample('player_online',value=1,labels={'player':player.lstrip()}) + return[dim_tps,dim_ticktime,overall_tps,overall_ticktime,player_online] def get_player_stats(self,uuid):