forked from mmarchand/fireping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinger.js
140 lines (122 loc) · 3.73 KB
/
pinger.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var util = require('util');
var ping = require ("net-ping");
var sqlite3 = require ("sqlite3");
var influx = require ("influx");
var colors = require('colors');
var global = require('./conf.js');
var targets = [];
var hosts = [];
var ival = global.ival; //ms
var cping = global.cping; //count
var tping = global.tping; //ms
var pingtimeout = global.pingtimeout; //ms
var pinghost = global.srchost;
var results = [];
var losses = [];
var pingerItv;
var db = new sqlite3.Database('fireping.db');
var infdb = influx ({ host : global.infhost, username: global.infuser, password: global.infpass, database: global.infdb } );
// Default options
var options = {
networkProtocol: ping.NetworkProtocol.IPv4,
packetSize: 16,
retries: 0,
sessionId: (process.pid % 65535),
timeout: pingtimeout,
ttl: 128
};
var session = ping.createSession (options);
function average (arr) {
var sum = 0, j = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i]; ++j;
}
return j ? sum / j : 0;
}
function doOnePing (target, count) {
session.pingHost (target, function (error, target, sent, rcvd) {
var ms = rcvd - sent;
//ignore first ping (arp resolutions and stuff can be laggy)
if ( count === 1 )
return;
if (error) {
//console.log (target + "(" + count + ") :" + error.toString ());
losses[target].push(count);
} else {
results[target].push(ms);
}
});
}
function dumpResults (ip) {
if (losses[ip].length) {
console.error(ip + " ("+hosts[ip]+") : " + average(results[ip]) + " ms : " + (losses[ip].length + " losses").red);
} else {
console.log(ip + " ("+hosts[ip]+") : " + average(results[ip]) + " ms : " + losses[ip].length + " losses");
}
//var points = [ { 'ttl': average(results[ip]), 'loss': losses[ip].length*100/20, time: new Date() } ];
//infdb.writePoints("fireping."+hosts[ip].replace(/\./g, "_"), points, function(err) {
// if (err)
// console.error(err);
//});
var dt = new Date();
ttlip = average(results[ip]);
lossip = losses[ip].length*100/20;
var points = [ [ { value: ttlip, time: dt}, { srchost: pinghost, host: hosts[ip].replace(/\./g, "_") } ] ];
var points2 = [ [ { value: lossip, time: dt}, { srchost: pinghost, host: hosts[ip].replace(/\./g, "_") } ] ];
var series = {
"ttl": points,
"loss": points2
};
infdb.writeSeries(series, function(err) {
if (err)
console.error(err);
});
//reset for next scan
results[ip]=[];
losses[ip]=[];
}
function doAllPing () {
for (i in targets) {
var ip = targets[i];
for (var j = 1 ; j <= (cping+1) ; j++) {
setTimeout( doOnePing, tping*j, ip , j );
}
setTimeout( dumpResults, (tping*(cping+2))+pingtimeout, ip );
}
}
function Pinger() {
}
Pinger.addTarget = function(target, hostname) {
console.log("Add target: " + target + "(" + hostname + ")");
if (hosts[target]) {
console.error ( "Entry " + target + " already exists");
} else {
var stmt = db.prepare("INSERT INTO targets ('target', 'hostname') VALUES (?,?)");
stmt.run(target,hostname);
stmt.finalize(Pinger.loadTargets);
}
};
Pinger.loadTargets = function() {
//force init arrays
losses=[];
results=[];
targets=[];
hosts=[];
if (pingerItv)
clearInterval(pingerItv);
db.each("SELECT * FROM targets", function (err,row) {
console.log("Read target from DB : " + row.id + " : " + row.target + " (" + row.hostname + ")");
results[row.target] = [];
losses[row.target] = [];
targets.push(row.target);
hosts[row.target] = row.hostname;
}, startPing); //db.each
};
function startPing (err,rows) {
console.log("Targets: " + rows);
doAllPing();
pingerItv = setInterval (doAllPing, ival);
}
//init and start
Pinger.loadTargets();
module.exports = Pinger;