summaryrefslogtreecommitdiffstats
path: root/health-networkstats-html.lsp
blob: 888a73ea6abce5420d3ed754abbeb76eab5cb222 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<% local view, viewlibrary, page_info = ... %>
<% require("viewfunctions") %>
<% require("json") %>
<%
	-- Table of colors
	local rgb = {
		{"rgb(0,192,128)","rgb(64,255,192)"},
		{"rgb(128,0,192)","rgb(192,64,255)"},
		{"rgb(0,128,192)","rgb(64,192,255)"},
		{"rgb(192,0,128)","rgb(255,64,192)"},
		{"rgb(128,192,0)","rgb(192,255,64)"},
		{"rgb(192,128,0)","rgb(255,192,64)"},
		{"rgb(0,0,0)","rgb(128,128,128)"},
		{"rgb(128,0,0)","rgb(192,64,64)"},
		{"rgb(0,128,0)","rgb(64,192,64)"},
		{"rgb(0,0,128)","rgb(64,64,192)"},
		{"rgb(128,128,0)","rgb(192,192,64)"},
		{"rgb(0,128,128)","rgb(64,192,192)"},
		{"rgb(128,0,128)","rgb(192,64,192)"},
		{"rgb(192,192,192)","rgb(255,255,255)"},
		{"rgb(192,128,128)","rgb(255,192,192)"},
		{"rgb(128,192,128)","rgb(192,255,192)"},
		{"rgb(128,128,192)","rgb(192,192,255)"},
		{"rgb(192,128,192)","rgb(255,192,255)"},
		{"rgb(128,192,192)","rgb(192,255,255)"},
		{"rgb(192,192,128)","rgb(255,255,192)"},
	}

	local interfaces = {}
	for intf in pairs(view.value) do table.insert(interfaces, intf) end
	table.sort(interfaces)
%>

<style type="text/css">
	#chart table {
		width: auto;
	}
</style>
<!--[if IE]><script type="text/javascript" src="/js/excanvas.js"></script><![endif]-->
<script type="text/javascript" src="/js/jquery-latest.js"></script>
<script type="text/javascript" src="/js/jquery.flot.js"></script>
<script type="text/javascript">
	var interval = 1000;
	var duration = 60000;
	var lastdata = <%= json.encode(view) %>;
	var chartdata = <% -- Generate the data structure in Lua and then convert to json
			local chartdata = {}
			for i,intf in ipairs(interfaces) do
				chartdata[intf.."RX"] = {label=intf.." RX", data={}, color=rgb[i][1]}
				chartdata[intf.."TX"] = {label=intf.." TX", data={}, color=rgb[i][2]}
			end
			io.write( json.encode(chartdata) ) %>;
	var ID
	function DrawChart(){
		var data = [];
		$("body").find("input:checked").each(function() {
			data.push(chartdata[$(this).attr("name")]);
		});
		var timestamp = 0;
		$.each(chartdata, function(key,val){
			if (val.data.length > 0){
				timestamp=val.data[0][0];
				return false;
			}
		});
		if (timestamp == 0 && lastdata != null) timestamp = lastdata.timestamp*1000;
		$.plot(
			$("#chart"), data, {legend:{show:false}, xaxis:{mode:"time", timeformat:"%H:%M:%S", min:timestamp, max:timestamp+duration}, yaxis:{min:0}});
	}
	function Update(){
		$.ajaxSetup({cache:false});
		$.getJSON(
			'<%= html.html_escape(page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action) %>',
			{viewtype:'json'},
			function(data) {
				if (lastdata != null){
					if (data.timestamp <= lastdata.timestamp) return false;
					var timestamp = data.timestamp * 1000;
					var multiplier = 1 / (data.timestamp - lastdata.timestamp);
					var shiftcount = null;
					$.each(lastdata.value, function(key,val){
						chartdata[key+"RX"].data.push([timestamp, (data.value[key].RX.bytes - lastdata.value[key].RX.bytes)*multiplier]);
						chartdata[key+"TX"].data.push([timestamp, (data.value[key].TX.bytes - lastdata.value[key].TX.bytes)*multiplier]);
						if (shiftcount == null) {
							shiftcount = 0;
							$.each(chartdata[key+"RX"].data, function(key,val){
								if (val[0] < timestamp-duration)
									shiftcount += 1;
								else
									return false;
							});
						}
						for (i=0; i<shiftcount; i++){
							chartdata[key+"RX"].data.shift();
							chartdata[key+"TX"].data.shift();
						}
					});
				}
				lastdata = data;
				DrawChart();
			}
		);
	}
	function Start(){
		lastdata = null;
		$.each(chartdata, function(key,val){
			val.data = [];
		});
		Update();
		ID = window.setInterval("Update()", interval);
		$("#Start").attr("disabled","disabled");
		$("#Stop").removeAttr("disabled");
	}
	function Stop(){
		window.clearInterval(ID);
		$("#Stop").attr("disabled","disabled");
		$("#Start").removeAttr("disabled");
	}
	$(function (){
	   	$(":checkbox").click(DrawChart);
		$("#Start").click(Start);
		$("#Stop").click(Stop);
		DrawChart();
		Start();
	});
</script>

<H1>Network Statistics</H1>
Network traffic in bytes/second
<div id="chart" style="width:680px; height:300px;"></div>

<DL>
<DT>Display Options</DT>
<DD><TABLE style="width:auto;">
<TR><TD><B>Interface</B></TD><TD><B>IP Address</B></TD><TD colspan="2" style="text-align:center;"><B>RX</B></TD><TD colspan="2" style="text-align:center;"><B>TX</B></TD></TR>
<% for i,intf in ipairs(interfaces) do %>
	<TR><TD><%= html.html_escape(intf) %></TD><TD><%= html.html_escape(view.value[intf].ipaddr) %></TD>
	<TD><INPUT type="checkbox" name="<%= html.html_escape(intf) %>RX" checked="checked"></TD>
	<TD><DIV style="border:1px solid #ccc;padding:1px"><DIV style="width:14px;height:10px;background-color:<%= rgb[i][1] %>;overflow:hidden"></DIV></DIV></TD>
	<TD><INPUT type="checkbox" name=<%= html.html_escape(intf) %>TX checked="checked"></TD>
	<TD><DIV style="border:1px solid #ccc;padding:1px"><DIV style="width:14px;height:10px;background-color:<%= rgb[i][2] %>;overflow:hidden"></DIV></DIV></TD></TR>
<% end %>
</TABLE></DD>
<DT>Start / Stop</DT>
<DD>
<input TYPE="button" ID="Start" VALUE="Start">
<input TYPE="button" ID="Stop" VALUE="Stop">
</DD>
</DL>

<% --[[ -- display table of colors %>
<TABLE style="width:auto;">
<% for i=1,#rgb do %>
<TR><TD><DIV style="border:1px solid #ccc;padding:1px"><DIV style="width:14px;height:10px;background-color:<%= rgb[i][1] %>;overflow:hidden"></DIV></DIV></TD>
<TD><DIV style="border:1px solid #ccc;padding:1px"><DIV style="width:14px;height:10px;background-color:<%= rgb[i][2] %>;overflow:hidden"></DIV></DIV></TD></TR>
<% end %>
</TABLE>
<% --]] %>