Skip to content

Commit

Permalink
Show CPU and Memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dahlgren committed Jul 10, 2021
1 parent 7b6d0c6 commit dddbb46
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
52 changes: 52 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var _ = require('lodash')
var events = require('events')
var filesize = require('filesize')
var Gamedig = require('gamedig')
var usage = require('pidusage')
var slugify = require('slugify')

var ArmaServer = require('arma-server')

var processesInterval = 2000
var queryInterval = 5000
var queryTypes = {
arma1: 'arma',
Expand Down Expand Up @@ -66,6 +69,49 @@ Server.prototype.update = function (options) {
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
}

function processStats (stats) {
return {
cpu: stats.cpu,
cpuFormatted: stats.cpu.toFixed(0) + ' %',
memory: stats.memory,
memoryFormatted: filesize(stats.memory)
}
}

Server.prototype.queryProcesses = function () {
if (!this.instance) {
return
}

var self = this
var headlessPids = this.headlessClientInstances.map(function (instance) {
return instance.pid
})
var serverPid = self.instance.pid
var pids = [serverPid].concat(headlessPids)
usage(pids, function (err, stats) {
if (!self.instance) {
return
}

if (err) {
self.processes = null
} else {
self.processes = pids.map(function (pid, idx) {
var pidStats = processStats(stats[pid])
if (pid === serverPid) {
pidStats.name = 'Server'
} else {
pidStats.name = 'Headless ' + idx // First headless at idx 1
}
return pidStats
})
}

self.emit('state')
})
}

Server.prototype.queryStatus = function () {
if (!this.instance) {
return
Expand Down Expand Up @@ -165,8 +211,10 @@ Server.prototype.start = function () {
var self = this

instance.on('close', function (code) {
clearInterval(self.queryProcessesInterval)
clearInterval(self.queryStatusInterval)
self.state = null
self.processes = null
self.pid = null
self.instance = null

Expand All @@ -178,6 +226,9 @@ Server.prototype.start = function () {
this.pid = instance.pid
this.instance = instance
this.headlessClientInstances = []
this.queryProcessesInterval = setInterval(function () {
self.queryProcesses()
}, processesInterval)
this.queryStatusInterval = setInterval(function () {
self.queryStatus()
}, queryInterval)
Expand Down Expand Up @@ -274,6 +325,7 @@ Server.prototype.toJSON = function () {
persistent: this.persistent,
pid: this.pid,
port: this.port,
processes: this.processes,
state: this.state,
title: this.title,
von: this.von,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lodash": "^4.17.10",
"morgan": "^1.8.1",
"multer": "^1.3.0",
"pidusage": "2.0.17",
"raw-loader": "^0.5.1",
"serve-static": "^1.12.1",
"slugify": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions public/js/app/models/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = Backbone.Model.extend({
password: '',
persistent: false,
port: 2302,
processes: null,
state: null,
title: '',
von: false,
Expand Down
30 changes: 30 additions & 0 deletions public/js/tpl/servers/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@
</div>
</div>

<% if (processes) { %>
<div class="form-group">
<label class="col-sm-1 control-label">Processes</label>
<div class="col-sm-11">
<% processes.map(function (process) { %>
<div class="form-group">
<label class="col-sm-1 control-label">Name</label>
<div class="col-sm-11">
<p class="form-control-static"><%= process.name %></p>
</div>
</div>

<div class="form-group">
<label class="col-sm-1 control-label">CPU</label>
<div class="col-sm-11">
<p class="form-control-static"><%= process.cpuFormatted %></p>
</div>
</div>

<div class="form-group">
<label class="col-sm-1 control-label">RAM</label>
<div class="col-sm-11">
<p class="form-control-static"><%= process.memoryFormatted %></p>
</div>
</div>
<% }) %>
</div>
</div>
<% } %>

<% if (state) { %>
<div class="form-group">
<label class="col-sm-1 control-label">Name</label>
Expand Down
5 changes: 4 additions & 1 deletion public/js/tpl/servers/list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<table class="table table-striped">
<thead>
<tr>
<th colspan="2">Status</th>
<th>Status</th>
<th></th>
<th>Port</th>
<th>Title</th>
<th>CPU</th>
<th>RAM</th>
<th></th>
<th></th>
</tr>
Expand Down
12 changes: 10 additions & 2 deletions public/js/tpl/servers/list_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
<span class="glyphicon glyphicon-play"></span> Start
</button>
<% } %>


</td>
<td><%-port%></td>
<td style="width: 100%;">
<a href='#servers/<%-id%>'><%-title%></a>
</td>
<td class="text-nowrap">
<% if (processes && processes[0]) { %>
<%= processes[0].cpuFormatted %>
<% } %>
</td>
<td class="text-nowrap">
<% if (processes && processes[0]) { %>
<%= processes[0].memoryFormatted %></td>
<% } %>
</td>
<td>
<button type="button" class="btn btn-success btn-xs clone pull-right">
<span class="glyphicon glyphicon-plus-sign"></span> Clone
Expand Down

0 comments on commit dddbb46

Please sign in to comment.