11var _ = require ( 'lodash' )
22var events = require ( 'events' )
33var fs = require ( 'fs' )
4+ var filesize = require ( 'filesize' )
45var Gamedig = require ( 'gamedig' )
6+ var usage = require ( 'pidusage' )
57var slugify = require ( 'slugify' )
68
79var ArmaServer = require ( 'arma-server' )
810
911var config = require ( '../config.js' )
1012
13+ var processesInterval = 2000
1114var queryInterval = 5000
1215var 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+
72118Server . 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 ,
0 commit comments