From 6bc07ad1b3ce9c304ac371c2bfb53ecfa9fc6079 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 6 Apr 2020 17:03:19 +0200 Subject: [PATCH] Add Factorio CPU and Memory usage statistics Add per instance metrics for CPU and memory usage with the pidusage library. --- CHANGELOG.md | 1 + lib/factorio/server.js | 10 ++++++++++ package.json | 1 + slave.js | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5bbfb88..19f2d8c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Version 2.0.0 allowing it to be proxied behind web-server. - Added WebSocket usage statistics. - Added commands sent statistic. +- Added Factorio server CPU and memory usage statistic. ### Changes diff --git a/lib/factorio/server.js b/lib/factorio/server.js index 0a765ddc..e8414fac 100644 --- a/lib/factorio/server.js +++ b/lib/factorio/server.js @@ -590,6 +590,16 @@ class FactorioServer extends events.EventEmitter { return this._version; } + /** + * The pid of the server process, or null if not running + */ + get pid() { + if (!this._server) { + return null; + } + return this._server.pid; + } + _attachStdio() { this._server.stdout.on('data', chunk => { this._stdout.data(chunk); }); this._server.stdout.on('close', () => { this._stdout.end(); }); diff --git a/package.json b/package.json index 8646ff7b..b0693d9a 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "needle": "^2.4.0", "node-factorio-api": "^0.3.8", "node-forge": "^0.7.1", + "pidusage": "^2.0.18", "rcon-client": "^4.2.0", "request": "^2.83.0", "sanitizer": "^0.1.3", diff --git a/slave.js b/slave.js index 6e466615..7aee2935 100644 --- a/slave.js +++ b/slave.js @@ -2,6 +2,7 @@ const fs = require('fs-extra'); const path = require('path'); const yargs = require("yargs"); const events = require("events"); +const pidusage = require("pidusage"); const version = require("./package").version; // internal libraries @@ -22,6 +23,18 @@ const instanceRconCommandsCounter = new prometheus.Counter( { labels: ["instance_id"] } ); +const instanceFactorioCpuTime = new prometheus.Gauge( + "clusterio_instance_factorio_cpu_time_total", + "Factorio CPU time spent in seconds.", + { labels: ["instance_id"] } +); + +const instanceFactorioMemoryUsage = new prometheus.Gauge( + "clusterio_instance_factorio_resident_memory_bytes", + "Factorio resident memory size in bytes.", + { labels: ["instance_id"] } +); + /** * Keeps track of the runtime parameters of an instance */ @@ -311,6 +324,13 @@ class Instance extends link.Link{ } } + let pid = this.server.pid; + if (pid) { + let stats = await pidusage(pid); + instanceFactorioCpuTime.labels(String(this.config.get("instance.id"))).set(stats.ctime / 1000); + instanceFactorioMemoryUsage.labels(String(this.config.get("instance.id"))).set(stats.memory); + } + return { results }; } @@ -702,6 +722,9 @@ class Slave extends link.Link { } this.connector.close(1001, "Slave Shutdown"); await events.once(this.connector, "close"); + + // Clear silly interval in pidfile library. + pidusage.clear(); } }