-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
117 lines (104 loc) · 2.56 KB
/
index.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
113
114
115
116
117
const promCli = require('prom-client')
const { HttpServer } = require('./HttpServer')
const logger = require('../../../lib/logger.js').module('Prometheus')
exports.logger = logger
let instance = null // the singleton instance
/**
* Registry and Gauge settings for Prometheus
*/
const PromCliRegistry = promCli.Registry
const customRegistry = new PromCliRegistry()
const gauge = new promCli.Gauge({
registers: [customRegistry],
name: 'zj2m',
help: 'zwavejs2mqtt gauges from metrics',
labelNames: [
'nodeId',
'location',
'name',
'commandClass',
'property',
'propertyKey',
'label',
'type',
'endpoint',
'id'
]
})
/**
* Function to initiate the Client (plugin)
**/
function PromClient (zwave) {
if (!(this instanceof PromClient)) {
return new PromClient(zwave)
}
if (instance) {
instance.destroy()
} else {
// start http server
HttpServer(customRegistry, logger)
}
instance = this
this.zwave = zwave
this.start()
}
PromClient.prototype.start = async function () {
logger.info('Event caller')
if (this.zwave) {
this.zwave.on('valueChanged', onValueChanged.bind(this))
this.zwave.on('nodeRemoved', onNodeRemoved.bind(this))
}
// this is async but doesn't need to be awaited
// this.zwave.connect()
}
// Implements the Payload for gauge, and registers/upgrade gauge
function gaugePayload (payload) {
// Ignore CCs not making sense to monitor
switch (payload.commandClass) {
case 112:
case 114:
case 134:
return
}
let metricValue = 0
switch (typeof payload.value) {
case 'number':
metricValue = payload.value
break
case 'boolean':
if (payload.value) {
metricValue = 1
}
break
default:
return
}
logger.info(`Adding value to metric ${payload.id}`)
const gaugeLabels = {
nodeId: payload.nodeId,
name: payload.nodeName,
location: payload.nodeLocation,
commandClass: payload.commandClassName,
property: payload.propertyName,
propertyKey: payload.propertyKey,
label: payload.label,
type: payload.type,
endpoint: payload.endpoint,
id: payload.id
}
// set gauge
gauge.set(gaugeLabels, metricValue)
logger.debug(`Registered ${metricValue} under ${payload.id}`)
}
// TODO: Placeholder for removal
function onNodeRemoved (node) {
logger.debug(`Node data ${node}`)
}
/**
* Value changes calls for change
**/
function onValueChanged (valueId) {
logger.debug(`Value ${valueId.value} is typeof ${typeof valueId.value}`)
gaugePayload(valueId)
}
module.exports = PromClient