-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathstats.js
More file actions
243 lines (207 loc) · 5.93 KB
/
stats.js
File metadata and controls
243 lines (207 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*!
* Cluster - stats
* Copyright (c) 2011 LearnBoost <dev@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var fs = require('fs')
// , Log = require('log')
, repl = require('./repl')
, utils = require('../utils')
, os;
// COMPAT:
try {
os = require('os');
} catch (err) {
// ignore
}
/**
* Enable stat tracking with the given `options`.
*
* Options:
*
* - `connections` enable connection statistics
* - `requests` enable request statistics
*
* @param {Object} options
* @return {Function}
* @api public
*/
module.exports = function(options){
options = options || {};
stats.enableInWorker = options.connections || options.requests;
function stats(master){
var server = master.server;
master.connectionStats = options.connections;
master.requestStats = options.requests;
// worker stats
if (master.isWorker) {
var id = 0;
// connections
if (options.connections) {
server.on('connection', function(sock){
var data = { remoteAddress: sock.remoteAddress };
master.call('reportStats', 'connection', data);
sock.on('close', function(){
master.call('reportStats', 'disconnection', data);
});
});
}
// requests
if (options.requests) {
// force cluster's callback first
if (Array.isArray(server._events.request)) {
server._events.request.unshift(request);
} else {
server._events.request = [request, server._events.request];
}
function request(req, res){
var data = {
remoteAddress: req.socket.remoteAddress
, headers: req.headers
, httpVersion: req.httpVersion
, method: req.method
, url: req.url
, id: ++id
};
master.call('reportStats', 'request', data);
var end = res.end;
res.end = function(str, encoding){
res.end = end;
res.end(str, encoding);
master.call('reportStats', 'request complete', data);
}
};
}
// master stats
} else {
master.stats = {
start: new Date
, restarts: 0
, workersSpawned: 0
, workersKilled: 0
};
// 0.4.x
if (os) {
master.stats.totalmem = os.totalmem();
master.stats.freemem = os.freemem();
}
// worker stats
master.reportStats = function(worker, type, data){
master.emit('client ' + type, worker, data);
switch (type) {
case 'connection':
worker.stats.connectionsTotal++;
worker.stats.connectionsActive++;
break;
case 'disconnection':
worker.stats.connectionsActive--;
break;
case 'request':
worker.stats.requestsTotal++;
}
};
// total workers spawned
master.on('worker', function(worker){
++master.stats.workersSpawned;
worker.stats = {
start: new Date
, connectionsTotal: 0
, connectionsActive: 0
, requestsTotal: 0
};
});
// total worker deaths
master.on('worker killed', function(worker){
++master.stats.workersKilled;
});
// restarting
master.on('restarting', function(data){
++master.stats.restarts;
data.stats = master.stats;
});
// restart
master.on('restart', function(data){
master.stats = data.stats;
master.stats.start = new Date(master.stats.start);
});
}
}
return stats;
};
/**
* REPL statistics command.
*/
repl.define('stats', function(master, sock){
var active = master.children.length
, total = master.stats.workersSpawned
, deaths = master.stats.workersKilled
, restarts = master.stats.restarts;
// master stats
sock.title('Master');
if (os) sock.row('os', os.type() + ' ' + os.release());
sock.row('state', master.state);
sock.row('started', master.stats.start.toUTCString());
sock.row('uptime', utils.formatDateRange(new Date, master.stats.start));
sock.row('restarts', restarts);
sock.row('workers', active);
sock.row('deaths', deaths);
// resources
if (os) {
sock.title('Resources');
sock.row('load average', os.loadavg().map(function(n){ return n.toFixed(2); }).join(' '));
sock.row('cores utilized', active + ' / ' + os.cpus().length);
var free = utils.formatBytes(master.stats.freemem);
var total = utils.formatBytes(master.stats.totalmem);
sock.row('memory at boot (free / total)', free + ' / ' + total);
var free = utils.formatBytes(os.freemem());
var total = utils.formatBytes(os.totalmem());
sock.row('memory now (free / total)', free + ' / ' + total);
}
// worker stats
sock.title('Workers');
// connections
if (master.connectionStats) {
sock.row('connections total', sum(master.children, 'connectionsTotal'));
sock.row('connections active', sum(master.children, 'connectionsActive'));
}
// requests
if (master.requestStats) {
sock.row('requests total', sum(master.children, 'requestsTotal'));
}
master.children.forEach(function(worker){
var stats = ''
, piped = [];
// uptime
stats += utils.formatDateRange(new Date, worker.stats.start);
// connections
if (master.connectionStats) {
piped.push(worker.stats.connectionsActive);
piped.push(worker.stats.connectionsTotal);
}
// requests
if (master.requestStats) {
piped.push(worker.stats.requestsTotal);
}
if (piped.length) {
stats += ' ' + piped.join('\033[90m|\033[0m');
}
sock.row(worker.id, stats);
});
sock.write('\n');
}, 'Display server statistics');
/**
* Return sum of each `prop` in `arr`.
*
* @param {Array} arr
* @param {String} prop
* @return {Number}
* @api private
*/
function sum(arr, prop){
return arr.reduce(function(sum, obj){
return sum + obj.stats[prop];
}, 0);
};