From 09f4a9c5b4a02e48a15438556d81525273d5b05f Mon Sep 17 00:00:00 2001 From: googol Date: Thu, 31 Jan 2019 22:51:25 +0200 Subject: [PATCH] Added base metrics --- init.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ mod.conf | 1 + 2 files changed, 52 insertions(+) create mode 100644 init.lua create mode 100644 mod.conf diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..f9f55f7 --- /dev/null +++ b/init.lua @@ -0,0 +1,51 @@ +local mt_stat ={} + +mt_stat.http = minetest.request_http_api() +mt_stat.url = minetest.settings:get("mt_stat.url") or "http://localhost:8086" +mt_stat.timeout = minetest.settings:get("mt_stat.timeout") or 10 +mt_stat.db = minetest.settings:get("mt_stat.db") or "minetest" +mt_stat.receive = minetest.settings:get("mt_stat.receive") or 30 +-- For secure connect set header value (Authorization: Basic bG9naW46cGFzc3dvcmQ=) +-- where bG9naW46cGFzc3dvcmQ= is the base64(login:password) +mt_stat.header = minetest.settings:get("mt_stat.header") or nil + +function mt_stat.send_metrics() + mt_stat.measurements = {} + local players = minetest.get_connected_players() + mt_stat.measurements.players_online = #players + mt_stat.measurements.max_lag = string.match(minetest.get_server_status(), "max_lag=(.-), cli") + mt_stat.measurements.game_time = minetest.get_timeofday() * 24000 + mt_stat.measurements.server_uptime = minetest.get_server_uptime() + local united_data = "mt_stat " + local t = "" + for key,value in pairs(mt_stat.measurements) do + united_data = united_data..t..string.format('%s=%02f',key,value) + t = "," + end + mt_stat.request_http("/write?db="..mt_stat.db, united_data) + minetest.after(mt_stat.timeout, mt_stat.send_metrics) +end + +function mt_stat.request_http(url,pd) + local req = {url = mt_stat.url..url, post_data = pd, extra_headers = {mt_stat.header}, timeout = mt_stat.interval} + mt_stat.http.fetch(req, function(result) + if result.succeeded and result.code == 200 then + if req.post_data.q == "SHOW DATABASES" and string.find(result.data, mt_stat.db) then + minetest.after(mt_stat.timeout, mt_stat.send_metrics) + minetest.log("mt_stat: Loaded... [OK]") + elseif req.post_data.q == "SHOW DATABASES" and not string.find(result.data, mt_stat.db) then + mt_stat.request_http("/query",{q="CREATE DATABASE "..mt_stat.db}) + minetest.log("mt_stat: Create database "..mt_stat.db.."!") + elseif req.post_data.q == "CREATE DATABASE "..mt_stat.db then + mt_stat.request_http("/query",{q="SHOW DATABASES"}) + minetest.log("mt_stat: Database created!") + end + end + end) +end + +if mt_stat.http then + mt_stat.request_http("/query",{q="SHOW DATABASES"}) +else + minetest.log("mt_stat: Please setup (secure.http_mod = mt_stat) in minetest.conf") +end \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..34a2503 --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +name = mt_stat \ No newline at end of file