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
148
149
150
151
152
153
154
155
156
|
var nms = {
debug: true,
};
var ports_now;
var ports_5m_ago;
var ports_1h_ago;
var bandwidth = 0;
var sw;
var infra;
// var full=false;
/*
Sets $ports_now and $ports_5m_ago - both variables' structure:
obj(
<host> -> <port> -> <properties>,
<host> -> <port> -> <properties>,
[...]
)
Properties:
ifhcinoctets (counter64)
ifhcoutoctets (counter64)
ifhighbandwidth (???)
time (unix timestamp)
*/
function fetch_switch_data(){
mode = $('#switch').attr('data-mode');
$.ajax({
type: "GET",
url: "/port-state.pl",
dataType: "text",
success: function (data, textStatus, jqXHR) {
var switchdata = JSON.parse(data);
ports_now = switchdata['switches'];
var list = document.getElementById("switch-list");
if (list) {
var v = list.value;
var arry = new Array();
for (x in switchdata) {
arry.push(x);
}
arry.sort();
list.options.length = 0;
for (x in arry) {
list.add(new Option(arry[x]));
}
if (v)
list.value = v;
}
}
});
$.ajax({
type: "GET",
url: "/port-state.pl?time=5m",
dataType: "text",
success: function (data, textStatus, jqXHR) {
var switchdata = JSON.parse(data);
ports_5m_ago = switchdata['switches'];
}
})
// console.log(ports_now);
$.ajax({
type: "GET",
url: "/port-state.pl?time=1h",
dataType: "text",
success: function (data, textStatus, jqXHR) {
var switchdata = JSON.parse(data);
ports_1h_ago = switchdata['switches'];
}
})
}
/*
Get bandwidth average for last hour, in bytes
mode: either "tot_agg" or "edge_ports"
tot_agg gives total aggregation of all ports
edge_ports gives aggregation on all client facing ports on EX2200 switches (not 100% accurate, missing a few clients directly connected on EX3300)
*/
function get_bytes_avg_1h(mode){
var mode = $('#switch').attr('data-mode');
var bw_usage = 0;
for (sw in ports_now) {
for (port in ports_now[sw]["ports"]) {
if (!ports_now[sw]["ports"][port]) {
console.log('Error - should not happen (!ports_now[sw]["ports"][port])');
continue;
}
if (!ports_5m_ago[sw]["ports"][port]) {
console.log('Error - should not happen (!ports_5m_ago[sw]["ports"][port])');
continue;
}
var diff = parseInt(ports_now[sw]["ports"][port]["time"]) - parseInt(ports_1h_ago[sw]["ports"][port]["time"]);
var then = parseInt(ports_1h_ago[sw]["ports"][port]["ifhcinoctets"]);
var now = parseInt(ports_now[sw]["ports"][port]["ifhcinoctets"]);
var diffval = (now - then);
/*
Skips ports with no data (e.g. down)
*/
if (then == 0 || now == 0 || diffval == 0) {
continue;
}
if (mode == 'tot_agg'){
bw_usage += parseInt(diffval/diff);
}else if(mode == 'edge_ports' && /ge-0\/0\/4[4-7]$/.exec(port)){
bw_usage += parseInt(diffval/diff);
}
}
}
return Math.round(bw_usage);
}
function update_bandwidth() {
var mode = $('#switch').attr('data-mode');
var bandwidth_in = parseInt(0);
var bandwidth_kant = parseInt(0);
var counter=0;
var sw;
for (sw in ports_now) {
for (port in ports_now[sw]["ports"]) {
if (!ports_now[sw]["ports"][port]) {
console.log("ops");
continue;
}
if (!ports_5m_ago[sw]["ports"][port]) {
console.log("ops");
continue;
}
var diff = parseInt(ports_now[sw]["ports"][port]["time"]) - parseInt(ports_5m_ago[sw]["ports"][port]["time"]);
var then = parseInt(ports_5m_ago[sw]["ports"][port]["ifhcinoctets"]);
var now = parseInt(ports_now[sw]["ports"][port]["ifhcinoctets"]);
var diffval = (now - then);
if (then == 0 || now == 0 || diffval == 0 || diffval == NaN) {
continue;
}
if (mode == 'tot_agg'){
bandwidth_in += parseInt(diffval/diff) / 1024 ;
}else if(/e\d{1,2}-\d{1,2}/.exec(sw) || /sw\d-/.exec(sw)){
if(/ge-0\/0\/4[4-7]$/.exec(port)){
bandwidth_in += parseInt(diffval/diff) / 1024 ;
}
}
}
}
bandwidth = bandwidth_in;
$('#text-bandwidth').attr('data-used_bw', bandwidth);
}
|