-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
112 lines (89 loc) · 2.44 KB
/
server.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
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
'use strict';
const express = require('express');
const cluster = require('cluster');
const server = express();
const client = require('prom-client');
const register = client.register;
const DNAC = require('./DNAC');
const CONFIG = require('./DNAC_USER_CONFIG');
console.log(CONFIG.ip);
console.log(CONFIG.user);
console.log(CONFIG.passwd);
let dnac = new DNAC({
host: 'https://' + CONFIG.ip,
username: CONFIG.user,
password: CONFIG.passwd
});
// Test Code to enable for troubleshooting DNAC interface helper
//dnac.getSites(function(sites) {
//console.log(JSON.stringify(sites));
//});
const Histogram = client.Histogram;
const h = new Histogram({
name: 'test_histogram',
help: 'Example of a histogram',
labelNames: ['code']
});
const Counter = client.Counter;
const c = new Counter({
name: 'test_counter',
help: 'Example of a counter',
labelNames: ['code']
});
const Gauge = client.Gauge;
const nd = new client.Gauge({
name: 'dnac_network_device_count',
help: 'Network Device Count'
});
setInterval(() => {
dnac.getNetworkDevicesCount(function(networkDevices) {
// console.log(JSON.stringify(networkDevices));
console.log("Collected network device count: " + networkDevices.response);
nd.set(networkDevices.response);
});
}, 3000);
const st = new client.Gauge({
name: 'dnac_site_count',
help: 'Site Count'
});
setInterval(() => {
dnac.getSitesCount(function(siteCountResp) {
console.log("Collected site count: " + siteCountResp.response);
st.set(siteCountResp.response);
});
}, 3000);
const g = new Gauge({
name: 'test_gauge',
help: 'Example of a gauge',
labelNames: ['method', 'code']
});
setTimeout(() => {
h.labels('200').observe(Math.random());
h.labels('300').observe(Math.random());
}, 10);
setInterval(() => {
c.inc({ code: 200 });
}, 5000);
setInterval(() => {
c.inc({ code: 400 });
}, 2000);
setInterval(() => {
c.inc();
}, 2000);
setInterval(() => {
g.set({ method: 'get', code: 200 }, Math.random());
g.set(Math.random());
g.labels('post', '300').inc();
}, 100);
server.get('/metrics', (req, res) => {
res.set('Content-Type', register.contentType);
res.end(register.metrics());
});
server.get('/metrics/counter', (req, res) => {
res.set('Content-Type', register.contentType);
res.end(register.getSingleMetricAsString('test_counter'));
});
//Enable collection of default metrics
client.collectDefaultMetrics();
console.log('Server listening to 9000, metrics exposed on /metrics endpoint');
server.listen(9000);