-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
198 lines (152 loc) · 4.8 KB
/
Copy pathapp.js
File metadata and controls
198 lines (152 loc) · 4.8 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
var pmx = require('pmx');
var stats = {};
stats.connections = {};
stats.network = {};
stats.opcounters = {};
var config;
pmx.initModule({
widget : {
// Logo displayed
logo : 'https://raw.githubusercontent.com/jodacame/pm2-mongodb-stats/master/logo.png',
// Module colors
// 0 = main element
// 1 = secondary
// 2 = main border
// 3 = secondary border
theme : ['#141A1F', '#222222', '#000', '#000'],
// Section to show / hide
el : {
probes : true,
actions : true
},
// Main block to show / hide
block : {
actions : true,
issues : true,
meta : false,
// Custom metrics to put in BIG
main_probes : ['MongoDB','Uptime','Connections','Connections AVG','Network']
}
}
}, function(err, conf) {
config = conf;
var Probe = pmx.probe();
var cache = {};
var value_to_inspect = 0;
Probe.metric({
name : 'MongoDB',
value : function() {
return "v."+stats.version;
}
});
Probe.metric({
name : 'Uptime',
value : function() {
return formatSeconds(stats.uptime);
}
});
Probe.metric({
name : 'Connections',
value : function() {
return stats.connections.current+" ("+((stats.connections.current*100)/stats.connections.available).toFixed(2)+"%) ["+stats.connections.avg+"/sec]";
},
alert : {
mode : 'threshold',
value : 1000,
msg : '> 1000 connections', // optional
action: function() { //optional
console.error('MongoDB 1000+ connections');
},
cmp : function(value, threshold) { //optional
return (parseFloat(stats.connections.current) > threshold); // default check
}
}
});
var histogramConnections = Probe.histogram({
name : 'Connections AVG',
measurement : 'mean'
});
Probe.metric({
name : 'Network',
value : function() {
return "⬇ "+bytesToSize(stats.networkIn)+"/sec ⬆ "+bytesToSize(stats.networkOut)+"/sec";
}
});
Probe.metric({
name : 'Insert',
value : function() {
return stats.insert+"/sec";
}
});
Probe.metric({
name : 'Update',
value : function() {
return stats.update+"/sec";
}
});
Probe.metric({
name : 'Query',
value : function() {
return stats.query+"/sec";
}
});
Probe.metric({
name : 'Delete',
value : function() {
return stats.deleted+"/sec";
}
});
var refresh_rate = 1000;
setInterval(function() {
getStats(function(data){
if (typeof stats.lastInsert != 'undefined') {
stats.insert = Math.round(data.opcounters.insert - stats.lastInsert);
stats.query = Math.round(data.opcounters.query - stats.lastQuery);
stats.update = Math.round(data.opcounters.update - stats.lastUpdate);
stats.deleted = Math.round(data.opcounters.delete - stats.lastDelete);
stats.command = Math.round(data.opcounters.command - stats.lastCommand);
stats.networkIn = Math.round(data.network.bytesIn - stats.lastBytesIn);
stats.networkOut = Math.round(data.network.bytesOut - stats.lastBytesOut);
stats.connections.avg = Math.round(data.connections.totalCreated - stats.connections.totalCreated);
}
stats.lastInsert = data.opcounters.insert;
stats.lastQuery = data.opcounters.query;
stats.lastUpdate = data.opcounters.update;
stats.lastDelete = data.opcounters.delete;
stats.lastCommand = data.opcounters.command;
stats.lastBytesIn = data.network.bytesIn;
stats.lastBytesOut = data.network.bytesOut;
stats.version = data.version;
stats.uptime = data.uptime;
stats.connections.current = data.connections.current;
stats.connections.totalCreated = data.connections.totalCreated;
stats.connections.available = data.connections.available;
histogramConnections.update(stats.connections.current);
});
}, refresh_rate);
});
var getStats = function(callback){
var url = process.env.MONGO || config.mongo;
var MongoClient = require('mongodb').MongoClient;
var conn = MongoClient.connect(url, function(err, db) {
var adminDb = db.admin();
adminDb.serverStatus(function(err, info) {
//stats = info;
db.close();
if(callback)
callback(info);
})
})
}
function formatSeconds(sec) {
return [(sec / 3600), ((sec % 3600) / 60), ((sec % 3600) % 60)]
.map(v => v < 10 ? "0" + parseInt(v) : parseInt(v))
.filter((i, j) => i !== "00" || j > 0)
.join(":");
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};