aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms.gathering.org/nms2/js/nms2.js
blob: 29dc3df1ea74b885f4e3341f933b4fbf8f7af5fa (plain)
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
var nms = {
	debug: true,
};

var ports_now;
var ports_then;
var speed = 0;
var sw;
var infra;


function updateSwitches()
{
	$.ajax({
		type: "GET",
		url: "/switches-json2.pl",
		dataType: "text",
		success: function (data, textStatus, jqXHR) {
			infra = JSON.parse(data);
		}
	});
}

function updatePorts()
{
	$.ajax({
		type: "GET",
		url: "/port-state.pl",
		dataType: "text",
		success: function (data, textStatus, jqXHR) {
			var  switchdata = JSON.parse(data);
			var list = document.getElementById("switch-list");
			var v = list.value;
			var arry = new Array();
			ports_now = switchdata;
			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_then = switchdata;
		}
	})
}

function switchChange()
{
	var sw = document.getElementById("switch-list").value;
	var list = document.getElementById("port-list");
	var v = list.value;
	var array = new Array();
	for (x in ports_now[sw]["ports"]) {
		array.push(x);
	}
	array.sort();
	list.options.length = 0;
	for (x in array) {
		list.add(new Option(array[x]));
	}
	if (v)
		list.value = v;
	var info = document.getElementById("switch-info");
	info.value = "Temp: " +  infra["switches"][sw]["temp"] + " °C";	
	
}

function portChange()
{
	var sw = document.getElementById("switch-list").value;
	var port = document.getElementById("port-list").value;
	var out = document.getElementById("foo");
	var diff = parseInt(parseInt(ports_now[sw]["ports"][port]["time"]) - parseInt(ports_then[sw]["ports"][port]["time"]));
	var tmp2 = "time diff: " + diff + "s\n";
	for (x in ports_now[sw]["ports"][port]) {
		then = parseInt(ports_then[sw]["ports"][port][x]);
		now = parseInt(ports_now[sw]["ports"][port][x]);
		diffval = (now - then);
		if (diffval<0) {
			diffval = (now + Math.pow(2,32)) - then;
		}
		tmp2 +=  x + ": " + now;
		tmp2 +=  "      (" + then + ")\n";
		tmp2 +=  "diff: " + x + ": " + (diffval) + " : ";
		tmp2 +=  parseInt(((diffval)/diff)/1024) + " k/s\n";
		tmp2 += "---------\n";

	}
	out.innerHTML = tmp2;
}

function updateSpeed()
{
	var speed_in = 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_then[sw]["ports"][port]) {
				console.log("ops");
				continue;
			}
			var diff = parseInt(parseInt(ports_now[sw]["ports"][port]["time"]) - parseInt(ports_then[sw]["ports"][port]["time"]));
			var then = parseInt(ports_then[sw]["ports"][port]["ifhcinoctets"]) / 1024;
			var now =  parseInt( ports_now[sw]["ports"][port]["ifhcinoctets"]) / 1024;
			var diffval = (now - then);
			if (then == 0 || now == 0 || diffval == 0 || diffval == NaN) {
				continue;
			}
			speed_in += parseInt(diffval/diff) / 1024 ;
			counter++;
		}
	}
	var out = document.getElementById("speed");
	speed = speed_in;
	out.innerHTML = "Aggregated speed: " + ((speed_in) / 1024).toPrecision(5) + " GB/s";

}
updateSwitches();
setInterval(function(){updateSwitches()},5000);
setInterval(function(){updatePorts()},2000);
setInterval(function(){updateSpeed()},2000);