-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (128 loc) · 4.36 KB
/
Copy pathindex.js
File metadata and controls
139 lines (128 loc) · 4.36 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// ===================================
// External Libraries
// ===================================
require('dotenv').config();
const snmp = require ("net-snmp");
// ===================================
// Global variables
// ===================================
let activeMIB = {};
let tableCount = 1; // used for table id
// ===================================
// SNMP Server
// ===================================
class snmpServer {
constructor() {
this.ttl = process.env.SNMP_TTL || 900000
this.snmpAgent();
setInterval(this.housekeeper.bind(this), process.env.SNMP_HOUSE_KEEPER || 1800000)
}
/**
* Create a new SNMP Agent when the function is constructed
*/
snmpAgent() {
const options = {
port: process.env.SNMP_PORT || 161,
disableAuthorization: false,
accessControlModelType: snmp.AccessControlModelType.None,
//engineID: "8000B98380", // where the X's are random hex digits
address: null,
transport: "udp4",
version: snmp.Version2c
};
// callback
let callback = function (error, data) {
if ( error ) {
console.error (error);
}
};
// create agent
this.agent = snmp.createAgent (options, callback);
this.mib = this.agent.getMib();
this.authorizer = this.agent.getAuthorizer();
this.authorizer.addCommunity(process.env.SNMP_COMUNITY || "public");
}
/**
* Add data to MIB table
* @param {Object} data - Object with data to be added to the MIB
*/
setData(data) {
if (!activeMIB[data.name]) {
activeMIB[data.name] = { id: tableCount, entryList: {} };
tableCount ++;
// create new table
this.createTable(data);
}
// renew entry in activeMib table
activeMIB[data.name].entryList[data.entryID] = new Date();
// add data to table
let columns = [data.entryID];
for (let i = 0; i < data.columns.length; i++) {
columns.push(data.columns[i].value);
}
try {
this.mib.addTableRow(data.name, columns);
} catch (err) {
console.log(err.message);
};
};
/**
* Create a new table if it does not yet exsists
* @param {Object} data - Object with detail on the table that needs to be created
*/
createTable(data) {
var myTableProvider = {
name: data.name,
type: snmp.MibProviderType.Table,
oid: `1.3.6.1.4.1.54392.5.1464.${activeMIB[data.name].id}`,
maxAccess: snmp.MaxAccess[data.maxAccess],
tableColumns: [
{
number: 1,
name: "ifIndex",
type: snmp.ObjectType.OctetString,
maxAccess: snmp.MaxAccess["read-only"]
},
],
tableIndex: [
{
columnName: "ifIndex"
}
]
};
// genetate table columnts
for (let i = 0; i < data.columns.length; i++) {
myTableProvider.tableColumns.push({
number: i + 2,
name: data.columns[i].name,
type: snmp.ObjectType[data.columns[i].type],
maxAccess: snmp.MaxAccess[data.columns[i].maxAccess]
});
}
this.mib.registerProvider(myTableProvider);
}
/**
* Cleanup activeMib table from old data every 30 min
*/
housekeeper() {
let currentTime = new Date().getTime();
let currentTTL = currentTime - this.ttl;
Object.keys(activeMIB).forEach(key => {
let table = activeMIB[key].entryList;
Object.keys(table).forEach(entry => {
let entryTime = new Date(table[entry]).getTime();
if (entryTime < currentTTL) {
// remove entry from mib
try {
this.mib.deleteTableRow(key, [entry]);
// delete entry from activeMib table
delete activeMIB[key].entryList[entry];
} catch (err) {
console.log(err.message);
}
}
})
})
}
}
module.exports.snmpServer = snmpServer;