forked from tombatossals/gps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_bandwidth.js
executable file
·147 lines (126 loc) · 4.97 KB
/
update_bandwidth.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
141
142
143
144
145
146
147
'use strict';
var mongoose = require('mongoose'),
Link = require('../models/link'),
Node = require('../models/node'),
getNodesByName = require('./common').getNodesByName,
getLinks = require('./common').getLinks,
exec = require('child_process').exec,
fs = require('fs'),
util = require('util'),
// sendpush = require('./pushover').sendpush,
Q = require('q');
function get_data_from_command_line(link) {
var deferred = Q.defer();
var result = { bandwidth: 0, traffic: 0 };
var iface = link.nodes[0].iface;
var s1 = link.nodes[0];
var s2 = link.nodes[1];
var f = util.format('/var/lib/collectd/%s/links/bandwidth-%s.rrd', s1.name, s2.name);
if (!fs.existsSync(f)) {
f = util.format('/var/lib/collectd/%s/links/bandwidth-%s.rrd', s2.name, s1.name);
} else {
var f2 = util.format('/var/lib/collectd/%s/links/bandwidth-%s.rrd', s2.name, s1.name);
if (fs.existsSync(f2)) {
deferred.reject(util.format('Duplicate RRD file: %s-%s', s1.name, s2.name));
return deferred.promise;
}
}
var command = util.format('/usr/bin/rrdtool lastupdate %s | tail -1', f);
exec(command, function(error, stdout, stderr) {
var bandwidth = stdout.trim().split(' ');
var lastupdated = new Date().getTime()/1000 - parseInt(bandwidth[0].split(':')[0], 10);
if (lastupdated < 4*3600 && bandwidth.length === 3 && bandwidth[1] > 0 && bandwidth[2] > 0) {
if (bandwidth[1] < result.bandwidth[2]) {
bandwidth = bandwidth[1];
} else {
bandwidth = bandwidth[2];
}
result.bandwidth = parseInt(bandwidth/(1024*1024), 10);
} else {
result.bandwidth = 0;
}
var command = util.format('/usr/bin/rrdtool graph xxx -s $(date -d yesterday +%%s) -e $(date +%%s) DEF:val1=/var/lib/collectd/%s/snmp/if_octets-%s.rrd:tx:MAX PRINT:val1:LAST:%lf DEF:val2=/var/lib/collectd/%s/snmp/if_octets-%s.rrd:tx:MAX PRINT:val2:LAST:%lf| tail -2', s1.name, iface, s1.name, iface);
exec(command, function(error, stdout, stderr) {
var traffic = stdout.trim().split('\n');
traffic = parseInt(traffic[0], 10) + parseInt(traffic[1], 10);
result.traffic = parseInt(traffic*8/(1024*1024), 10);
if (traffic === '-nan') {
result.traffic = 0;
}
deferred.resolve(result);
//sendpush(result, link, cb);
});
});
return deferred.promise;
}
function updateBandwidthLink(link) {
var deferred = Q.defer();
get_data_from_command_line(link).then(function(result) {
var traffic = result.traffic;
var bandwidth = result.bandwidth;
var saturation = 3;
if (bandwidth) {
var percentage = parseInt(traffic*100/bandwidth, 10);
if (percentage < 10) {
saturation = 0;
} else if (percentage < 25) {
saturation = 1;
} else if (percentage < 50) {
saturation = 2;
} else {
saturation = 3;
}
}
link.bandwidth = bandwidth;
link.saturation = saturation;
if (link.nodes[0].ospf.state !== 'Full' && link.nodes[1].ospf.state !== 'Full') {
link.bandwidth = 0;
link.saturation = 3;
}
link.save(function() {
deferred.resolve(util.format('Updated bandwidth %s-%s: saturation %s, bandwidth %s', link.nodes[0].name, link.nodes[1].name, saturation, bandwidth));
});
}).fail(function(result) {
deferred.reject(result);
});
return deferred.promise;
}
function execute(nodes) {
var deferred = Q.defer();
if (nodes && nodes.length > 0) {
getNodesByName(nodes).then(function(nodes) {
var nodesIds = [];
for (var i in nodes) {
var node = nodes[i];
nodesIds.push(node._id);
}
query = { 'active': true, 'nodes.id': { '$all': nodesIds } };
getLinks(query).then(function(links) {
var promises = [];
links.forEach(function(link) {
promises.push(updateBandwidthLink(link));
});
Q.allSettled(promises).then(function(results) {
deferred.resolve(results);
});
}).fail(function() {
deferred.reject();
});
});
} else {
var query = { active: true };
getLinks(query).then(function(links) {
var promises = [];
links.forEach(function(link) {
promises.push(updateBandwidthLink(link));
});
Q.allSettled(promises).then(function(results) {
deferred.resolve(results);
});
}).fail(function() {
deferred.reject();
});
}
return deferred.promise;
}
module.exports.execute = execute;