-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnmp.mjs
More file actions
56 lines (40 loc) · 1.16 KB
/
snmp.mjs
File metadata and controls
56 lines (40 loc) · 1.16 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
import * as snmp from 'net-snmp';
export function subtree(session, oid) {
return new Promise((resolve, reject) => {
const results = {};
session.subtree(oid, varbinds => {
varbinds.forEach(element => {
if(snmp.isVarbindError(element)) {
reject(snmp.varbindError(element));
} else {
results[element.oid.toString().slice(oid.length+1)] = element.value;
}
});
}, () => {
resolve(results);
});
})
};
export async function table(session, oidRoot, mapping) {
const results = {};
for(var subOid in mapping) {
const data = await subtree(session, oidRoot + '.' + subOid);
if(Object.keys(data).length == 0)
throw new Error('Failed to get data.');
for(var i in data) {
if(i.includes('.'))
throw new Error('Table is not 1-dimensional.');
if(!results[i])
results[i] = {};
let property = mapping[subOid];
if(typeof property == 'object') {
if(property.type == 'string') {
data[i] = data[i].toString();
property = property.name;
}
}
results[i][property] = data[i];
}
}
return results;
}