Skip to content

Commit dddbb46

Browse files
committed
Show CPU and Memory usage
1 parent 7b6d0c6 commit dddbb46

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

Diff for: lib/server.js

+52
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
var _ = require('lodash')
22
var events = require('events')
3+
var filesize = require('filesize')
34
var Gamedig = require('gamedig')
5+
var usage = require('pidusage')
46
var slugify = require('slugify')
57

68
var ArmaServer = require('arma-server')
79

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

72+
function processStats (stats) {
73+
return {
74+
cpu: stats.cpu,
75+
cpuFormatted: stats.cpu.toFixed(0) + ' %',
76+
memory: stats.memory,
77+
memoryFormatted: filesize(stats.memory)
78+
}
79+
}
80+
81+
Server.prototype.queryProcesses = function () {
82+
if (!this.instance) {
83+
return
84+
}
85+
86+
var self = this
87+
var headlessPids = this.headlessClientInstances.map(function (instance) {
88+
return instance.pid
89+
})
90+
var serverPid = self.instance.pid
91+
var pids = [serverPid].concat(headlessPids)
92+
usage(pids, function (err, stats) {
93+
if (!self.instance) {
94+
return
95+
}
96+
97+
if (err) {
98+
self.processes = null
99+
} else {
100+
self.processes = pids.map(function (pid, idx) {
101+
var pidStats = processStats(stats[pid])
102+
if (pid === serverPid) {
103+
pidStats.name = 'Server'
104+
} else {
105+
pidStats.name = 'Headless ' + idx // First headless at idx 1
106+
}
107+
return pidStats
108+
})
109+
}
110+
111+
self.emit('state')
112+
})
113+
}
114+
69115
Server.prototype.queryStatus = function () {
70116
if (!this.instance) {
71117
return
@@ -165,8 +211,10 @@ Server.prototype.start = function () {
165211
var self = this
166212

167213
instance.on('close', function (code) {
214+
clearInterval(self.queryProcessesInterval)
168215
clearInterval(self.queryStatusInterval)
169216
self.state = null
217+
self.processes = null
170218
self.pid = null
171219
self.instance = null
172220

@@ -178,6 +226,9 @@ Server.prototype.start = function () {
178226
this.pid = instance.pid
179227
this.instance = instance
180228
this.headlessClientInstances = []
229+
this.queryProcessesInterval = setInterval(function () {
230+
self.queryProcesses()
231+
}, processesInterval)
181232
this.queryStatusInterval = setInterval(function () {
182233
self.queryStatus()
183234
}, queryInterval)
@@ -274,6 +325,7 @@ Server.prototype.toJSON = function () {
274325
persistent: this.persistent,
275326
pid: this.pid,
276327
port: this.port,
328+
processes: this.processes,
277329
state: this.state,
278330
title: this.title,
279331
von: this.von,

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"lodash": "^4.17.10",
3939
"morgan": "^1.8.1",
4040
"multer": "^1.3.0",
41+
"pidusage": "2.0.17",
4142
"raw-loader": "^0.5.1",
4243
"serve-static": "^1.12.1",
4344
"slugify": "^1.1.0",

Diff for: public/js/app/models/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = Backbone.Model.extend({
1818
password: '',
1919
persistent: false,
2020
port: 2302,
21+
processes: null,
2122
state: null,
2223
title: '',
2324
von: false,

Diff for: public/js/tpl/servers/info.html

+30
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@
3939
</div>
4040
</div>
4141

42+
<% if (processes) { %>
43+
<div class="form-group">
44+
<label class="col-sm-1 control-label">Processes</label>
45+
<div class="col-sm-11">
46+
<% processes.map(function (process) { %>
47+
<div class="form-group">
48+
<label class="col-sm-1 control-label">Name</label>
49+
<div class="col-sm-11">
50+
<p class="form-control-static"><%= process.name %></p>
51+
</div>
52+
</div>
53+
54+
<div class="form-group">
55+
<label class="col-sm-1 control-label">CPU</label>
56+
<div class="col-sm-11">
57+
<p class="form-control-static"><%= process.cpuFormatted %></p>
58+
</div>
59+
</div>
60+
61+
<div class="form-group">
62+
<label class="col-sm-1 control-label">RAM</label>
63+
<div class="col-sm-11">
64+
<p class="form-control-static"><%= process.memoryFormatted %></p>
65+
</div>
66+
</div>
67+
<% }) %>
68+
</div>
69+
</div>
70+
<% } %>
71+
4272
<% if (state) { %>
4373
<div class="form-group">
4474
<label class="col-sm-1 control-label">Name</label>

Diff for: public/js/tpl/servers/list.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<table class="table table-striped">
22
<thead>
33
<tr>
4-
<th colspan="2">Status</th>
4+
<th>Status</th>
5+
<th></th>
56
<th>Port</th>
67
<th>Title</th>
8+
<th>CPU</th>
9+
<th>RAM</th>
710
<th></th>
811
<th></th>
912
</tr>

Diff for: public/js/tpl/servers/list_item.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
<span class="glyphicon glyphicon-play"></span> Start
2222
</button>
2323
<% } %>
24-
25-
2624
</td>
2725
<td><%-port%></td>
2826
<td style="width: 100%;">
2927
<a href='#servers/<%-id%>'><%-title%></a>
3028
</td>
29+
<td class="text-nowrap">
30+
<% if (processes && processes[0]) { %>
31+
<%= processes[0].cpuFormatted %>
32+
<% } %>
33+
</td>
34+
<td class="text-nowrap">
35+
<% if (processes && processes[0]) { %>
36+
<%= processes[0].memoryFormatted %></td>
37+
<% } %>
38+
</td>
3139
<td>
3240
<button type="button" class="btn btn-success btn-xs clone pull-right">
3341
<span class="glyphicon glyphicon-plus-sign"></span> Clone

0 commit comments

Comments
 (0)