Add Factorio CPU and Memory usage statistics

Add per instance metrics for CPU and memory usage with the pidusage
library.
This commit is contained in:
Hornwitser committed 2020-04-09 16:17:38 +02:00
1 parent 56ee1fe955
commit 6bc07ad1b3
4 files changed
+35

No files matched your search

+1
View File
@@ -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
+10
View File
@@ -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(); });
+1
View File
@@ -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",
+23
View File
@@ -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();
}
}