Skip to content

Commit d39cac3

Browse files
committed
Show CPU and Memory usage
1 parent f5eb892 commit d39cac3

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,13 +1,16 @@
11
var _ = require('lodash')
22
var events = require('events')
33
var fs = require('fs')
4+
var filesize = require('filesize')
45
var Gamedig = require('gamedig')
6+
var usage = require('pidusage')
57
var slugify = require('slugify')
68

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

911
var config = require('../config.js')
1012

13+
var processesInterval = 2000
1114
var queryInterval = 5000
1215
var queryTypes = {
1316
arma1: 'arma',
@@ -69,6 +72,49 @@ Server.prototype.update = function (options) {
6972
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
7073
}
7174

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

239+
clearInterval(self.queryProcessesInterval)
193240
clearInterval(self.queryStatusInterval)
194241
self.state = null
242+
self.processes = null
195243
self.pid = null
196244
self.instance = null
197245

@@ -206,6 +254,9 @@ Server.prototype.start = function () {
206254

207255
this.pid = instance.pid
208256
this.instance = instance
257+
this.queryProcessesInterval = setInterval(function () {
258+
self.queryProcesses()
259+
}, processesInterval)
209260
this.queryStatusInterval = setInterval(function () {
210261
self.queryStatus()
211262
}, queryInterval)
@@ -318,6 +369,7 @@ Server.prototype.toJSON = function () {
318369
persistent: this.persistent,
319370
pid: this.pid,
320371
port: this.port,
372+
processes: this.processes,
321373
state: this.state,
322374
title: this.title,
323375
von: this.von,

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"lodash": "^4.17.10",
3838
"morgan": "^1.8.1",
3939
"multer": "^1.3.0",
40+
"pidusage": "2.0.17",
4041
"raw-loader": "^0.5.1",
4142
"serve-static": "^1.12.1",
4243
"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)