Skip to content

Commit 0fd6d73

Browse files
committed
Show CPU and Memory usage
1 parent fd95b09 commit 0fd6d73

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

Diff for: lib/server.js

+35
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 processInterval = 2000
1114
var queryInterval = 5000
1215
var queryTypes = {
1316
arma1: 'arma',
@@ -69,6 +72,32 @@ Server.prototype.update = function (options) {
6972
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
7073
}
7174

75+
Server.prototype.queryProcess = function () {
76+
if (!this.instance) {
77+
return
78+
}
79+
80+
var self = this
81+
usage(self.instance.pid, function (err, stats) {
82+
if (!self.instance) {
83+
return
84+
}
85+
86+
if (err) {
87+
self.process = null
88+
} else {
89+
self.process = {
90+
cpu: stats.cpu,
91+
cpuFormatted: stats.cpu.toFixed(0) + ' %',
92+
memory: stats.memory,
93+
memoryFormatted: filesize(stats.memory)
94+
}
95+
}
96+
97+
self.emit('state')
98+
})
99+
}
100+
72101
Server.prototype.queryStatus = function () {
73102
if (!this.instance) {
74103
return
@@ -190,8 +219,10 @@ Server.prototype.start = function () {
190219
logStream.end()
191220
}
192221

222+
clearInterval(self.queryProcessInterval)
193223
clearInterval(self.queryStatusInterval)
194224
self.state = null
225+
self.process = null
195226
self.pid = null
196227
self.instance = null
197228

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

207238
this.pid = instance.pid
208239
this.instance = instance
240+
this.queryProcessInterval = setInterval(function () {
241+
self.queryProcess()
242+
}, processInterval)
209243
this.queryStatusInterval = setInterval(function () {
210244
self.queryStatus()
211245
}, queryInterval)
@@ -318,6 +352,7 @@ Server.prototype.toJSON = function () {
318352
persistent: this.persistent,
319353
pid: this.pid,
320354
port: this.port,
355+
process: this.process,
321356
state: this.state,
322357
title: this.title,
323358
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+
process: null,
2122
state: null,
2223
title: '',
2324
von: false,

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

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

42+
<% if (process) { %>
43+
<div class="form-group">
44+
<label class="col-sm-1 control-label">CPU</label>
45+
<div class="col-sm-11">
46+
<p class="form-control-static"><%= process.cpuFormatted %></p>
47+
</div>
48+
</div>
49+
50+
<div class="form-group">
51+
<label class="col-sm-1 control-label">RAM</label>
52+
<div class="col-sm-11">
53+
<p class="form-control-static"><%= process.memoryFormatted %></p>
54+
</div>
55+
</div>
56+
<% } %>
57+
4258
<% if (state) { %>
4359
<div class="form-group">
4460
<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 (process) { %>
31+
<%= process.cpuFormatted %>
32+
<% } %>
33+
</td>
34+
<td class="text-nowrap">
35+
<% if (process) { %>
36+
<%= process.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)