-
Notifications
You must be signed in to change notification settings - Fork 0
/
half-duplex.js
119 lines (118 loc) · 3.84 KB
/
half-duplex.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
118
119
var config = require('./config.js');
var snmp = require('snmp-native');
var ip = require('ip');
var ipRegex = require('ip-regex');
var session = new snmp.Session({ community: config.community });
var polledHosts = config.ignoreDevices;
var pollingFailed = [];
var pollingSucceeded = [];
var uniqueHosts = [];
var cbCount = 0;
var halfDuplexTotal = 0;
var duplexReportBody = [];
var duplexReportHeader = '';
var SMTPConnection = require('smtp-connection');
var connection = new SMTPConnection({host: config.smtpServer});
function filterHost (host) {
if(polledHosts.indexOf(host) == -1 && ipRegex({exact: true}).test(host)){
polledHosts.push(host);
getNeighbors(host);
}
}
function getNeighbors (host) {
++cbCount;
session.getSubtree({ host: host, oid: [1,3,6,1,4,1,9,9,23,1,2,1,1,4] }, function (error, varbinds) {
if (error) {
if (pollingFailed.indexOf(host) == -1) {
pollingFailed.push(host);
}
console.log('Fail :(');
} else {
getName(host);
varbinds.forEach(function (vb) {
filterHost(ip.toString(vb.valueRaw));
});
}
countdown();
});
}
function getName (host) {
++cbCount;
session.get({ host: host, oid: [1,3,6,1,2,1,1,5,0] }, function (error, varbinds) {
if (error) {
console.log('Fail :(');
} else {
filterName(varbinds[0].value,host);
}
countdown();
});
}
function filterName (name,host) {
if (uniqueHosts.indexOf(name) == -1) {
uniqueHosts.push(name);
pollingSucceeded.push(name.split('.')[0]);
getDuplex(name.split('.')[0],host);
}
}
function getDuplex (name,host) {
++cbCount;
session.getSubtree({ host: host, oid: [1,3,6,1,2,1,10,7,2,1,19] }, function (error, varbinds) {
if (error) {
console.log('Fail :(');
} else {
varbinds.forEach(function (vb) {
filterDuplex(name,host,vb.oid.pop(),vb.value)
});
}
countdown();
});
}
function filterDuplex (name,host,ifIndex,duplex) {
if (duplex == 2) {
getIntStatus(name,host,ifIndex)
}
}
function getIntStatus (name,host,ifIndex) {
++cbCount;
session.get({ host: host, oid: [1,3,6,1,2,1,2,2,1,8,ifIndex] }, function (error, varbinds) {
if (error) {
console.log('Fail :(');
} else {
filterIntStatus(name,host,ifIndex,varbinds[0].value);
}
countdown();
});
}
function filterIntStatus (name,host,ifIndex,intStatus) {
if (intStatus==1) {
getIntDetails(name,host,ifIndex);
}
}
function getIntDetails (name,host,ifIndex) {
++cbCount;
session.getAll({ host: host, oids: [[1,3,6,1,2,1,31,1,1,1,1,ifIndex],[1,3,6,1,2,1,31,1,1,1,18,ifIndex]] }, function (error, varbinds) {
if (error) {
console.log('Fail :(');
} else {
duplexReportBody.push( name+' '+varbinds[0].value+' '+varbinds[1].value );
++halfDuplexTotal;
}
countdown();
});
}
function countdown () {
if (--cbCount===0) {
duplexReportHeader = 'From: '+config.emailFrom+'\r\nTo: '+config.emailTo+'\r\nSubject: '+halfDuplexTotal+' Half Duplex Interfaces\r\n\r\nHalf Duplex Interfaces:\r\n';
duplexReportFooter = '\r\n\r\nPolling failed:\r\n' + pollingFailed.sort().join('\r\n') + '\r\n\r\nPolling succeeded:\r\n' + pollingSucceeded.sort().join('\r\n');
connection.connect(function () {
connection.send({from: config.emailFrom, to:config.emailTo}, duplexReportHeader+duplexReportBody.sort().join('\r\n')+duplexReportFooter, function () {
connection.quit();
session.close();
process.exit();
});
});
}
}
config.seedDevices.forEach(function (device) {
filterHost(device);
});