UNPKG

3.78 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Dmitri Melikyan
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25var os = require('os');
26
27
28var nt;
29var lastCpuTime;
30
31exports.init = function() {
32 nt = global.nodetime;
33
34 nt.on("call", function(point, time) {
35 if(!nt.history && nt.paused) return;
36
37 if(point === "done") {
38 nt.metric(time.scope, 'Requests per minute', 1, undefined, 'sum');
39 nt.metric(time.scope, 'Average response time', time.ms, 'ms', 'avg');
40 nt.metric(time.scope, 'Response time histogram', time.ms, 'ms', 'hist');
41 if(time.cputime) nt.metric(time.scope, 'Average CPU time', time.cputime, 'ms', 'avg');
42 }
43 });
44
45 setInterval(function() {
46 try {
47 collect();
48 }
49 catch(e) {
50 nt.error(e);
51 }
52 }, 60000);
53
54 collect();
55};
56
57
58var collect = function() {
59
60 var info = {};
61 info._ts = nt.millis();
62 info._ns = 'info';
63 info['Application name'] = nt.appName;
64 try { info['Hostname'] = os.hostname() } catch(err) { nt.error(err) }
65 try { info['OS type'] = os.type() } catch(err) { nt.error(err) }
66 try { info['Platform'] = os.platform() } catch(err) { nt.error(err) }
67 try { info['Total memory (MB)'] = os.totalmem() / 1000000 } catch(err) { nt.error(err) }
68 try { var cpus = os.cpus(); info['CPU'] = {architecture: os.arch(), model: cpus[0].model, speed: cpus[0].speed, cores: cpus.length} } catch(err) { nt.error(err) }
69 try { info['Interfaces'] = os.networkInterfaces() } catch(err) { nt.error(err) }
70 try { info['OS uptime (Hours)'] = Math.floor(os.uptime() / 3600) } catch(err) { nt.error(err) }
71 try { info['Node arguments'] = process.argv } catch(err) { nt.error(err) }
72 try { info['Node PID'] = process.pid; } catch(err) { nt.error(err) }
73 try { info['Node uptime (Hours)'] = Math.floor(process.uptime() / 3600); } catch(err) { nt.error(err) }
74 try { info['Node versions'] = process.versions } catch(err) { nt.error(err) }
75 info['Nodetime version'] = nt.version;
76
77 try {
78 nt.emit('info', info);
79 }
80 catch(err) {
81 nt.error(err);
82 }
83
84
85 try { nt.metric('OS', 'Load average', os.loadavg()[0]); } catch(err) { nt.error(err); }
86 try { nt.metric('OS', 'Free memory', os.freemem() / 1000000, 'MB'); } catch(err) { nt.error(err); }
87
88
89 try {
90 var mem = process.memoryUsage();
91 nt.metric('Process', 'Node RSS', mem.rss / 1000000, 'MB');
92 nt.metric('Process', 'V8 heap used', mem.heapUsed / 1000000, 'MB');
93 nt.metric('Process', 'V8 heap total', mem.heapTotal / 1000000, 'MB');
94 }
95 catch(err) {
96 nt.error(err);
97 }
98
99 var cpuTime = nt.cputime();
100 if(cpuTime !== undefined && lastCpuTime !== undefined)
101 nt.metric('Process', 'CPU time', (cpuTime - lastCpuTime) / 1000, 'ms');
102 if(cpuTime !== undefined)
103 lastCpuTime = cpuTime;
104};
105