-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx_njs_prometheus.js
62 lines (42 loc) · 2.11 KB
/
nginx_njs_prometheus.js
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
/*
Nginx njs application that mimics a prometheus exporter.
Copyright (c) Pavel Kim
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var VERSION = "0.0.0";
function index(req) {
var result = "";
result += `# HELP Currently open connections\n`;
result += `# TYPE gauge\n`;
result += `nginx_current_active{instance="${req.variables.hostname}"} ${req.variables.connections_active}\n`;
result += `# HELP Number of request nginx is reading headers from\n`;
result += `# TYPE gauge\n`;
result += `nginx_current_reading{instance="${req.variables.hostname}"} ${req.variables.connections_reading}\n`;
result += `# HELP Number of request body readings, request processings, or response writings\n`;
result += `# TYPE gauge\n`;
result += `nginx_current_writing{instance="${req.variables.hostname}"} ${req.variables.connections_writing}\n`;
result += `# HELP Number of connections that are Keep-Alive'ing\n`;
result += `# TYPE gauge\n`;
result += `nginx_current_waiting{instance="${req.variables.hostname}"} ${req.variables.connections_waiting}\n`;
req.subrequest('/nginx_stats/', { method: 'GET' }, function(res) {
if (res.status != 200) {
req.return(res.status);
return;
}
var response_lines = res.responseBody.split("\n");
var counters = response_lines[2].split(" ");
result += `# HELP Total accepted connectons\n`;
result += `# TYPE counter\n`;
result += `nginx_accepted_connectons{instance="${req.variables.hostname}"} ${counters[1]}\n`;
result += `# HELP Total handled connections\n`;
result += `# TYPE counter\n`;
result += `nginx_handled_connections{instance="${req.variables.hostname}"} ${counters[2]}\n`;
result += `# HELP Total handled requests\n`;
result += `# TYPE counter\n`;
result += `nginx_handled_requests{instance="${req.variables.hostname}"} ${counters[3]}\n`;
req.return(200, result);
});
}
export default {index};