-
Notifications
You must be signed in to change notification settings - Fork 1
/
printerusage-snmpv1.js
47 lines (43 loc) · 1.69 KB
/
printerusage-snmpv1.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
#!/usr/bin/node
var snmp = require('net-snmp');
var printerData = require('./printers-config');
var pd = new printerData();
var host = process.argv[1].replace(/.*\/printerusage_/, "");
if (!host.match(/\//)) {
if (process.argv[2] && process.argv[2] == 'autoconf') {
console.log('yes');
process.exit();
}
var session = snmp.createSession(host, "public", {
version: snmp.Version1
});
session.get([ pd.deviceID.join('.') ], function (error, varbind) {
if (!error) {
var device = varbind[0].value.toString('UTF-8');
var oids = {};
if (pd.select(device)) {
session.subtree(pd.devices[device].usageTypes.base.join('.'), function (varbind) {
for (var v in varbind) {
oids[varbind[v].oid] = varbind[v].type == 4 ? varbind[v].value.toString('UTF-8') : varbind[v].value;
}
}, function done(result) {
if (process.argv[2] && process.argv[2] == 'config') {
console.log('graph_title ' + device + ' counters on ' + host + '\ngraph_args --units-exponent 0 --upper-limit 100 --lower-limit 0\ngraph_vlabel counter\ngraph_category printers');
for (c in pd.devices[device].counters) {
var unit = 'u' + pd.devices[device].counters[c].join('') + '.';
console.log(unit + 'label ' + c);
console.log(unit + 'type GAUGE');
}
} else {
for (c in pd.devices[device].counters) {
var oidCounter = pd.devices[device].usageTypes.base.join('.') + '.' + pd.devices[device].usageTypes.counter + '.' + pd.devices[device].counters[c].join('.');
console.log('u' + pd.devices[device].counters[c].join('') + '.value ' + oids[oidCounter]);
}
}
});
}
}
});
} else {
console.log('filename convention: printerusage_<hostname>');
}