11var _ = require ( 'lodash' )
22var events = require ( 'events' )
3+ var filesize = require ( 'filesize' )
34var Gamedig = require ( 'gamedig' )
5+ var usage = require ( 'pidusage' )
46var slugify = require ( 'slugify' )
57
68var ArmaServer = require ( 'arma-server' )
79
10+ var processesInterval = 2000
811var queryInterval = 5000
912var 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+
69115Server . 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 ,
0 commit comments