modules/admin-full: implement load graph
This commit is contained in:
parent
45cedb0fbd
commit
750b023494
4 changed files with 541 additions and 54 deletions
17
modules/admin-full/htdocs/luci-static/resources/load.svg
Normal file
17
modules/admin-full/htdocs/luci-static/resources/load.svg
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" standalone="no"?>
|
||||||
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||||
|
|
||||||
|
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<line x1="0" y1="25%" x2="100%" y2="25%" style="stroke:black;stroke-width:0.1" />
|
||||||
|
<text id="label_75" x="20" y="24%" style="fill:#999999; font-size:9pt"> </text>
|
||||||
|
|
||||||
|
<line x1="0" y1="50%" x2="100%" y2="50%" style="stroke:black;stroke-width:0.1" />
|
||||||
|
<text id="label_50" x="20" y="49%" style="fill:#999999; font-size:9pt"> </text>
|
||||||
|
|
||||||
|
<line x1="0" y1="75%" x2="100%" y2="75%" style="stroke:black;stroke-width:0.1" />
|
||||||
|
<text id="label_25" x="20" y="74%" style="fill:#999999; font-size:9pt"> </text>
|
||||||
|
|
||||||
|
<polyline id="load01" points="" style="fill:#ff0000;fill-opacity:0.4;stroke:#ff0000;stroke-width:1" />
|
||||||
|
<polyline id="load05" points="" style="fill:#ff6600;fill-opacity:0.4;stroke:#ff6600;stroke-width:1" />
|
||||||
|
<polyline id="load15" points="" style="fill:#ffaa00;fill-opacity:0.4;stroke:#ffaa00;stroke-width:1" />
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1 KiB |
|
@ -25,7 +25,10 @@ function index()
|
||||||
entry({"admin", "status", "syslog"}, call("action_syslog"), i18n("System Log"), 5)
|
entry({"admin", "status", "syslog"}, call("action_syslog"), i18n("System Log"), 5)
|
||||||
entry({"admin", "status", "dmesg"}, call("action_dmesg"), i18n("Kernel Log"), 6)
|
entry({"admin", "status", "dmesg"}, call("action_dmesg"), i18n("Kernel Log"), 6)
|
||||||
|
|
||||||
entry({"admin", "status", "bandwidth"}, template("admin_status/bandwidth"), i18n("Realtime Traffic"), 7).leaf = true
|
entry({"admin", "status", "load"}, template("admin_status/load"), i18n("Realtime Load"), 7).leaf = true
|
||||||
|
entry({"admin", "status", "load_status"}, call("action_load")).leaf = true
|
||||||
|
|
||||||
|
entry({"admin", "status", "bandwidth"}, template("admin_status/bandwidth"), i18n("Realtime Traffic"), 8).leaf = true
|
||||||
entry({"admin", "status", "bandwidth_status"}, call("action_bandwidth")).leaf = true
|
entry({"admin", "status", "bandwidth_status"}, call("action_bandwidth")).leaf = true
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -61,10 +64,10 @@ function action_bandwidth()
|
||||||
local iface = path[#path]
|
local iface = path[#path]
|
||||||
|
|
||||||
local fs = require "luci.fs"
|
local fs = require "luci.fs"
|
||||||
if fs.access("/var/lib/luci-bwc/%s" % iface) then
|
if fs.access("/var/lib/luci-bwc/if/%s" % iface) then
|
||||||
luci.http.prepare_content("application/json")
|
luci.http.prepare_content("application/json")
|
||||||
|
|
||||||
local bwc = io.popen("luci-bwc -p %q 2>/dev/null" % iface)
|
local bwc = io.popen("luci-bwc -i %q 2>/dev/null" % iface)
|
||||||
if bwc then
|
if bwc then
|
||||||
luci.http.write("[")
|
luci.http.write("[")
|
||||||
|
|
||||||
|
@ -81,5 +84,30 @@ function action_bandwidth()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
luci.http.status(404, "No such interface")
|
luci.http.status(404, "No data available")
|
||||||
|
end
|
||||||
|
|
||||||
|
function action_load()
|
||||||
|
local fs = require "luci.fs"
|
||||||
|
if fs.access("/var/lib/luci-bwc/load") then
|
||||||
|
luci.http.prepare_content("application/json")
|
||||||
|
|
||||||
|
local bwc = io.popen("luci-bwc -l 2>/dev/null")
|
||||||
|
if bwc then
|
||||||
|
luci.http.write("[")
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local ln = bwc:read("*l")
|
||||||
|
if not ln then break end
|
||||||
|
luci.http.write(ln)
|
||||||
|
end
|
||||||
|
|
||||||
|
luci.http.write("]")
|
||||||
|
bwc:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
luci.http.status(404, "No data available")
|
||||||
end
|
end
|
||||||
|
|
293
modules/admin-full/luasrc/view/admin_status/load.htm
Normal file
293
modules/admin-full/luasrc/view/admin_status/load.htm
Normal file
|
@ -0,0 +1,293 @@
|
||||||
|
<%#
|
||||||
|
LuCI - Lua Configuration Interface
|
||||||
|
Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
|
||||||
|
-%>
|
||||||
|
|
||||||
|
<%+header%>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
|
||||||
|
<script type="text/javascript">//<![CDATA[
|
||||||
|
var bwxhr = new XHR();
|
||||||
|
|
||||||
|
var G;
|
||||||
|
var TIME = 0;
|
||||||
|
var L01 = 1;
|
||||||
|
var L05 = 2;
|
||||||
|
var L15 = 3;
|
||||||
|
|
||||||
|
var width = 760;
|
||||||
|
var height = 300;
|
||||||
|
var step = 5;
|
||||||
|
|
||||||
|
var data_wanted = Math.floor(width / step);
|
||||||
|
var data_fill = 0;
|
||||||
|
var data_stamp = 0;
|
||||||
|
|
||||||
|
var data_01 = [ ];
|
||||||
|
var data_05 = [ ];
|
||||||
|
var data_15 = [ ];
|
||||||
|
|
||||||
|
var line_01;
|
||||||
|
var line_05;
|
||||||
|
var line_15;
|
||||||
|
|
||||||
|
var label_25;
|
||||||
|
var label_050;
|
||||||
|
var label_75;
|
||||||
|
|
||||||
|
var label_01_cur;
|
||||||
|
var label_01_avg;
|
||||||
|
var label_01_peak;
|
||||||
|
|
||||||
|
var label_05_cur;
|
||||||
|
var label_05_avg;
|
||||||
|
var label_05_peak;
|
||||||
|
|
||||||
|
var label_15_cur;
|
||||||
|
var label_15_avg;
|
||||||
|
var label_15_peak;
|
||||||
|
|
||||||
|
var label_scale;
|
||||||
|
|
||||||
|
|
||||||
|
function update_graph()
|
||||||
|
{
|
||||||
|
bwxhr.get('<%=build_url("admin/status/load_status")%>', null,
|
||||||
|
function(x, data)
|
||||||
|
{
|
||||||
|
var data_max = 0;
|
||||||
|
var data_scale = 0;
|
||||||
|
|
||||||
|
var data_01_avg = 0;
|
||||||
|
var data_05_avg = 0;
|
||||||
|
var data_15_avg = 0;
|
||||||
|
|
||||||
|
var data_01_peak = 0;
|
||||||
|
var data_05_peak = 0;
|
||||||
|
var data_15_peak = 0;
|
||||||
|
|
||||||
|
for (var i = data_stamp ? 0 : 1; i < data.length; i++)
|
||||||
|
{
|
||||||
|
/* skip overlapping entries */
|
||||||
|
if (data[i][TIME] <= data_stamp)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
data_01.push(data[i][L01]);
|
||||||
|
data_05.push(data[i][L05]);
|
||||||
|
data_15.push(data[i][L15]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* cut off outdated entries */
|
||||||
|
data_01 = data_01.slice(data_01.length - data_wanted, data_01.length);
|
||||||
|
data_05 = data_05.slice(data_05.length - data_wanted, data_05.length);
|
||||||
|
data_15 = data_15.slice(data_15.length - data_wanted, data_15.length);
|
||||||
|
|
||||||
|
/* find peak */
|
||||||
|
for (var i = 0; i < data_01.length; i++)
|
||||||
|
{
|
||||||
|
data_max = Math.max(data_max, data_01[i]);
|
||||||
|
data_max = Math.max(data_max, data_05[i]);
|
||||||
|
data_max = Math.max(data_max, data_15[i]);
|
||||||
|
|
||||||
|
data_01_peak = Math.max(data_01_peak, data_01[i]);
|
||||||
|
data_05_peak = Math.max(data_05_peak, data_05[i]);
|
||||||
|
data_15_peak = Math.max(data_15_peak, data_15[i]);
|
||||||
|
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
data_01_avg = (data_01_avg + data_01[i]) / 2;
|
||||||
|
data_05_avg = (data_05_avg + data_05[i]) / 2;
|
||||||
|
data_15_avg = (data_15_avg + data_15[i]) / 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
data_01_avg = data_01[i];
|
||||||
|
data_05_avg = data_05[i];
|
||||||
|
data_15_avg = data_15[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* remember current timestamp, calculate horizontal scale */
|
||||||
|
data_stamp = data[data.length-1][TIME];
|
||||||
|
data_scale = height / (data_max * 1.1);
|
||||||
|
|
||||||
|
|
||||||
|
/* plot data */
|
||||||
|
var pt_01 = '0,' + height;
|
||||||
|
var pt_05 = '0,' + height;
|
||||||
|
var pt_15 = '0,' + height;
|
||||||
|
|
||||||
|
var y_01 = 0;
|
||||||
|
var y_05 = 0;
|
||||||
|
var y_15 = 0;
|
||||||
|
|
||||||
|
for (var i = 0; i < data_01.length; i++)
|
||||||
|
{
|
||||||
|
var x = i * step;
|
||||||
|
|
||||||
|
y_01 = height - Math.floor(data_01[i] * data_scale);
|
||||||
|
y_05 = height - Math.floor(data_05[i] * data_scale);
|
||||||
|
y_15 = height - Math.floor(data_15[i] * data_scale);
|
||||||
|
|
||||||
|
pt_01 += ' ' + x + ',' + y_01;
|
||||||
|
pt_05 += ' ' + x + ',' + y_05;
|
||||||
|
pt_15 += ' ' + x + ',' + y_15;
|
||||||
|
}
|
||||||
|
|
||||||
|
pt_01 += ' ' + width + ',' + y_01 + ' ' + width + ',' + height;
|
||||||
|
pt_05 += ' ' + width + ',' + y_05 + ' ' + width + ',' + height;
|
||||||
|
pt_15 += ' ' + width + ',' + y_15 + ' ' + width + ',' + height;
|
||||||
|
|
||||||
|
|
||||||
|
line_01.setAttribute('points', pt_01);
|
||||||
|
line_05.setAttribute('points', pt_05);
|
||||||
|
line_15.setAttribute('points', pt_15);
|
||||||
|
|
||||||
|
label_25.firstChild.data = (1.1 * 0.25 * data_max / 100).toFixed(2);
|
||||||
|
label_50.firstChild.data = (1.1 * 0.50 * data_max / 100).toFixed(2);
|
||||||
|
label_75.firstChild.data = (1.1 * 0.75 * data_max / 100).toFixed(2);
|
||||||
|
|
||||||
|
label_01_cur.innerHTML = (data_01[data_01.length-1] / 100).toFixed(2);
|
||||||
|
label_05_cur.innerHTML = (data_05[data_05.length-1] / 100).toFixed(2);
|
||||||
|
label_15_cur.innerHTML = (data_01[data_15.length-1] / 100).toFixed(2);
|
||||||
|
|
||||||
|
label_01_avg.innerHTML = (data_01_avg / 100).toFixed(2);
|
||||||
|
label_05_avg.innerHTML = (data_05_avg / 100).toFixed(2);
|
||||||
|
label_15_avg.innerHTML = (data_15_avg / 100).toFixed(2);
|
||||||
|
|
||||||
|
label_01_peak.innerHTML = (data_01_peak / 100).toFixed(2);
|
||||||
|
label_05_peak.innerHTML = (data_05_peak / 100).toFixed(2);
|
||||||
|
label_15_peak.innerHTML = (data_15_peak / 100).toFixed(2);
|
||||||
|
|
||||||
|
/* reset timer */
|
||||||
|
window.setTimeout(update_graph, 1000);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* wait for SVG */
|
||||||
|
window.setTimeout(
|
||||||
|
function() {
|
||||||
|
G = document.embeds["bwsvg"].getSVGDocument();
|
||||||
|
|
||||||
|
if (!G)
|
||||||
|
{
|
||||||
|
window.setTimeout(arguments.callee, 1000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* find sizes */
|
||||||
|
width = document.embeds["bwsvg"].offsetWidth - 2;
|
||||||
|
height = document.embeds["bwsvg"].offsetHeight - 2;
|
||||||
|
data_wanted = Math.ceil(width / step);
|
||||||
|
|
||||||
|
/* prefill datasets */
|
||||||
|
for (var i = 0; i < data_wanted; i++)
|
||||||
|
{
|
||||||
|
data_01[i] = 0;
|
||||||
|
data_05[i] = 0;
|
||||||
|
data_15[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* find svg elements */
|
||||||
|
line_01 = G.getElementById('load01');
|
||||||
|
line_05 = G.getElementById('load05');
|
||||||
|
line_15 = G.getElementById('load15');
|
||||||
|
|
||||||
|
label_25 = G.getElementById('label_25');
|
||||||
|
label_50 = G.getElementById('label_50');
|
||||||
|
label_75 = G.getElementById('label_75');
|
||||||
|
|
||||||
|
label_01_cur = document.getElementById('lb_load01_cur');
|
||||||
|
label_01_avg = document.getElementById('lb_load01_avg');
|
||||||
|
label_01_peak = document.getElementById('lb_load01_peak');
|
||||||
|
|
||||||
|
label_05_cur = document.getElementById('lb_load05_cur');
|
||||||
|
label_05_avg = document.getElementById('lb_load05_avg');
|
||||||
|
label_05_peak = document.getElementById('lb_load05_peak');
|
||||||
|
|
||||||
|
label_15_cur = document.getElementById('lb_load15_cur');
|
||||||
|
label_15_avg = document.getElementById('lb_load15_avg');
|
||||||
|
label_15_peak = document.getElementById('lb_load15_peak');
|
||||||
|
|
||||||
|
label_scale = document.getElementById('scale');
|
||||||
|
|
||||||
|
|
||||||
|
/* plot horizontal time interval lines */
|
||||||
|
for (var i = step * 60; i < width; i += step * 60)
|
||||||
|
{
|
||||||
|
var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
|
||||||
|
line.setAttribute('x1', i);
|
||||||
|
line.setAttribute('y1', 0);
|
||||||
|
line.setAttribute('x2', i);
|
||||||
|
line.setAttribute('y2', '100%');
|
||||||
|
line.setAttribute('style', 'stroke:black;stroke-width:0.1');
|
||||||
|
|
||||||
|
var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
|
||||||
|
text.setAttribute('x', i + 5);
|
||||||
|
text.setAttribute('y', 15);
|
||||||
|
text.setAttribute('style', 'fill:#999999; font-size:9pt');
|
||||||
|
text.appendChild(G.createTextNode(Math.round(i / step / 60) + 'm'));
|
||||||
|
|
||||||
|
label_25.parentNode.appendChild(line);
|
||||||
|
label_25.parentNode.appendChild(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 1);
|
||||||
|
|
||||||
|
/* render datasets, start update interval */
|
||||||
|
update_graph();
|
||||||
|
}
|
||||||
|
}, 1000
|
||||||
|
);
|
||||||
|
//]]></script>
|
||||||
|
|
||||||
|
<h2><a id="content" name="content"><%:Realtime Load%></a></h2>
|
||||||
|
|
||||||
|
<embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/load.svg" />
|
||||||
|
<div style="text-align:right"><small id="scale">-</small></div>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<table style="width:100%; table-layout:fixed" cellspacing="5">
|
||||||
|
<tr>
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid #ff0000"><%:1 Minute Load:%></strong></td>
|
||||||
|
<td id="lb_load01_cur">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
|
||||||
|
<td id="lb_load01_avg">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
|
||||||
|
<td id="lb_load01_peak">0</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid #ff6600"><%:5 Minute Load:%></strong></td>
|
||||||
|
<td id="lb_load05_cur">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
|
||||||
|
<td id="lb_load05_avg">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
|
||||||
|
<td id="lb_load05_peak">0</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid #ffaa00"><%:15 Minute Load:%></strong></td>
|
||||||
|
<td id="lb_load15_cur">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
|
||||||
|
<td id="lb_load15_avg">0</td>
|
||||||
|
|
||||||
|
<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
|
||||||
|
<td id="lb_load15_peak">0</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<%+footer%>
|
|
@ -35,13 +35,17 @@
|
||||||
#define STEP_TIME 1
|
#define STEP_TIME 1
|
||||||
|
|
||||||
#define DB_PATH "/var/lib/luci-bwc"
|
#define DB_PATH "/var/lib/luci-bwc"
|
||||||
#define DB_FILE DB_PATH "/%s"
|
#define DB_IF_FILE DB_PATH "/if/%s"
|
||||||
|
#define DB_LD_FILE DB_PATH "/load"
|
||||||
|
|
||||||
#define SCAN_PATTERN \
|
#define IF_SCAN_PATTERN \
|
||||||
" %[^ :]:%" SCNu64 " %" SCNu64 \
|
" %[^ :]:%" SCNu64 " %" SCNu64 \
|
||||||
" %*d %*d %*d %*d %*d %*d" \
|
" %*d %*d %*d %*d %*d %*d" \
|
||||||
" %" SCNu64 " %" SCNu64
|
" %" SCNu64 " %" SCNu64
|
||||||
|
|
||||||
|
#define LD_SCAN_PATTERN \
|
||||||
|
"%f %f %f"
|
||||||
|
|
||||||
|
|
||||||
struct traffic_entry {
|
struct traffic_entry {
|
||||||
uint64_t time;
|
uint64_t time;
|
||||||
|
@ -51,6 +55,14 @@ struct traffic_entry {
|
||||||
uint64_t txp;
|
uint64_t txp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct load_entry {
|
||||||
|
uint64_t time;
|
||||||
|
uint16_t load1;
|
||||||
|
uint16_t load5;
|
||||||
|
uint16_t load15;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
static uint64_t htonll(uint64_t value)
|
static uint64_t htonll(uint64_t value)
|
||||||
{
|
{
|
||||||
int num = 1;
|
int num = 1;
|
||||||
|
@ -65,14 +77,9 @@ static uint64_t htonll(uint64_t value)
|
||||||
#define ntohll htonll
|
#define ntohll htonll
|
||||||
|
|
||||||
|
|
||||||
static int init_minutely(const char *ifname)
|
static int init_directory(char *path)
|
||||||
{
|
{
|
||||||
int i, file;
|
char *p = path;
|
||||||
char path[1024];
|
|
||||||
char *p;
|
|
||||||
struct traffic_entry e = { 0 };
|
|
||||||
|
|
||||||
snprintf(path, sizeof(path), DB_FILE, ifname);
|
|
||||||
|
|
||||||
for (p = &path[1]; *p; p++)
|
for (p = &path[1]; *p; p++)
|
||||||
{
|
{
|
||||||
|
@ -87,6 +94,48 @@ static int init_minutely(const char *ifname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int update_file(const char *path, void *entry, int esize)
|
||||||
|
{
|
||||||
|
int rv = -1;
|
||||||
|
int file;
|
||||||
|
char *map;
|
||||||
|
|
||||||
|
if ((file = open(path, O_RDWR)) >= 0)
|
||||||
|
{
|
||||||
|
map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED | MAP_LOCKED, file, 0);
|
||||||
|
|
||||||
|
if ((map != NULL) && (map != MAP_FAILED))
|
||||||
|
{
|
||||||
|
memmove(map, map + esize, esize * (STEP_COUNT-1));
|
||||||
|
memcpy(map + esize * (STEP_COUNT-1), entry, esize);
|
||||||
|
|
||||||
|
munmap(map, esize * STEP_COUNT);
|
||||||
|
|
||||||
|
rv = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int init_ifstat(const char *ifname)
|
||||||
|
{
|
||||||
|
int i, file;
|
||||||
|
char path[1024];
|
||||||
|
struct traffic_entry e = { 0 };
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
|
||||||
|
|
||||||
|
if (init_directory(path))
|
||||||
|
return -1;
|
||||||
|
|
||||||
if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
|
if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
|
||||||
{
|
{
|
||||||
for (i = 0; i < STEP_COUNT; i++)
|
for (i = 0; i < STEP_COUNT; i++)
|
||||||
|
@ -103,65 +152,96 @@ static int init_minutely(const char *ifname)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int update_minutely(
|
static int update_ifstat(
|
||||||
const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
|
const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
|
||||||
) {
|
) {
|
||||||
int rv = -1;
|
|
||||||
|
|
||||||
int file;
|
|
||||||
int entrysize = sizeof(struct traffic_entry);
|
|
||||||
int mapsize = STEP_COUNT * entrysize;
|
|
||||||
|
|
||||||
char path[1024];
|
char path[1024];
|
||||||
char *map;
|
|
||||||
|
|
||||||
struct stat s;
|
struct stat s;
|
||||||
struct traffic_entry e;
|
struct traffic_entry e;
|
||||||
|
|
||||||
snprintf(path, sizeof(path), DB_FILE, ifname);
|
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
|
||||||
|
|
||||||
if (stat(path, &s))
|
if (stat(path, &s))
|
||||||
{
|
{
|
||||||
if (init_minutely(ifname))
|
if (init_ifstat(ifname))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to init %s: %s\n",
|
fprintf(stderr, "Failed to init %s: %s\n",
|
||||||
path, strerror(errno));
|
path, strerror(errno));
|
||||||
|
|
||||||
return rv;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((file = open(path, O_RDWR)) >= 0)
|
|
||||||
{
|
|
||||||
map = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
|
|
||||||
MAP_SHARED | MAP_LOCKED, file, 0);
|
|
||||||
|
|
||||||
if ((map != NULL) && (map != MAP_FAILED))
|
|
||||||
{
|
|
||||||
e.time = htonll(time(NULL));
|
e.time = htonll(time(NULL));
|
||||||
e.rxb = htonll(rxb);
|
e.rxb = htonll(rxb);
|
||||||
e.rxp = htonll(rxp);
|
e.rxp = htonll(rxp);
|
||||||
e.txb = htonll(txb);
|
e.txb = htonll(txb);
|
||||||
e.txp = htonll(txp);
|
e.txp = htonll(txp);
|
||||||
|
|
||||||
memmove(map, map + entrysize, mapsize - entrysize);
|
return update_file(path, &e, sizeof(struct traffic_entry));
|
||||||
memcpy(map + mapsize - entrysize, &e, entrysize);
|
}
|
||||||
|
|
||||||
munmap(map, mapsize);
|
static int init_ldstat(void)
|
||||||
|
{
|
||||||
|
int i, file;
|
||||||
|
char path[1024];
|
||||||
|
struct load_entry e = { 0 };
|
||||||
|
|
||||||
rv = 0;
|
snprintf(path, sizeof(path), DB_LD_FILE);
|
||||||
|
|
||||||
|
if (init_directory(path))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
|
||||||
|
{
|
||||||
|
for (i = 0; i < STEP_COUNT; i++)
|
||||||
|
{
|
||||||
|
if (write(file, &e, sizeof(struct load_entry)) < 0)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(file);
|
close(file);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
|
||||||
|
{
|
||||||
|
char path[1024];
|
||||||
|
|
||||||
|
struct stat s;
|
||||||
|
struct load_entry e;
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), DB_LD_FILE);
|
||||||
|
|
||||||
|
if (stat(path, &s))
|
||||||
|
{
|
||||||
|
if (init_ldstat())
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to init %s: %s\n",
|
||||||
|
path, strerror(errno));
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e.time = htonll(time(NULL));
|
||||||
|
e.load1 = htons(load1);
|
||||||
|
e.load5 = htons(load5);
|
||||||
|
e.load15 = htons(load15);
|
||||||
|
|
||||||
|
return update_file(path, &e, sizeof(struct load_entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int run_daemon(int nofork)
|
static int run_daemon(int nofork)
|
||||||
{
|
{
|
||||||
FILE *info;
|
FILE *info;
|
||||||
uint64_t rxb, txb, rxp, txp;
|
uint64_t rxb, txb, rxp, txp;
|
||||||
|
float lf1, lf5, lf15;
|
||||||
char line[1024];
|
char line[1024];
|
||||||
char ifname[16];
|
char ifname[16];
|
||||||
|
|
||||||
|
@ -202,21 +282,33 @@ static int run_daemon(int nofork)
|
||||||
if (strchr(line, '|'))
|
if (strchr(line, '|'))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (sscanf(line, SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
|
if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
|
||||||
{
|
{
|
||||||
if (strncmp(ifname, "lo", sizeof(ifname)))
|
if (strncmp(ifname, "lo", sizeof(ifname)))
|
||||||
update_minutely(ifname, rxb, rxp, txb, txp);
|
update_ifstat(ifname, rxb, rxp, txb, txp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(info);
|
fclose(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((info = fopen("/proc/loadavg", "r")) != NULL)
|
||||||
|
{
|
||||||
|
if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
|
||||||
|
{
|
||||||
|
update_ldstat((uint16_t)(lf1 * 100),
|
||||||
|
(uint16_t)(lf5 * 100),
|
||||||
|
(uint16_t)(lf15 * 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(info);
|
||||||
|
}
|
||||||
|
|
||||||
sleep(STEP_TIME);
|
sleep(STEP_TIME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int run_dump(const char *ifname)
|
static int run_dump_ifname(const char *ifname)
|
||||||
{
|
{
|
||||||
int rv = 1;
|
int rv = 1;
|
||||||
|
|
||||||
|
@ -229,7 +321,7 @@ static int run_dump(const char *ifname)
|
||||||
|
|
||||||
struct traffic_entry *e;
|
struct traffic_entry *e;
|
||||||
|
|
||||||
snprintf(path, sizeof(path), DB_FILE, ifname);
|
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
|
||||||
|
|
||||||
if ((file = open(path, O_RDONLY)) >= 0)
|
if ((file = open(path, O_RDONLY)) >= 0)
|
||||||
{
|
{
|
||||||
|
@ -266,16 +358,65 @@ static int run_dump(const char *ifname)
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int run_dump_load(void)
|
||||||
|
{
|
||||||
|
int rv = 1;
|
||||||
|
|
||||||
|
int i, file;
|
||||||
|
int entrysize = sizeof(struct load_entry);
|
||||||
|
int mapsize = STEP_COUNT * entrysize;
|
||||||
|
|
||||||
|
char path[1024];
|
||||||
|
char *map;
|
||||||
|
|
||||||
|
struct load_entry *e;
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), DB_LD_FILE);
|
||||||
|
|
||||||
|
if ((file = open(path, O_RDONLY)) >= 0)
|
||||||
|
{
|
||||||
|
map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
|
||||||
|
|
||||||
|
if ((map != NULL) && (map != MAP_FAILED))
|
||||||
|
{
|
||||||
|
for (i = 0; i < mapsize; i += entrysize)
|
||||||
|
{
|
||||||
|
e = (struct load_entry *) &map[i];
|
||||||
|
|
||||||
|
if (!e->time)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
|
||||||
|
ntohll(e->time),
|
||||||
|
ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
|
||||||
|
((i + entrysize) < mapsize) ? "," : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
munmap(map, mapsize);
|
||||||
|
rv = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
int daemon = 0;
|
int daemon = 0;
|
||||||
int nofork = 0;
|
int nofork = 0;
|
||||||
int dprint = 0;
|
int iprint = 0;
|
||||||
|
int lprint = 0;
|
||||||
char *ifname = NULL;
|
char *ifname = NULL;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "dfp:")) > -1)
|
while ((opt = getopt(argc, argv, "dfi:l")) > -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
|
@ -287,11 +428,15 @@ int main(int argc, char *argv[])
|
||||||
nofork = 1;
|
nofork = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'p':
|
case 'i':
|
||||||
dprint = 1;
|
iprint = 1;
|
||||||
ifname = optarg;
|
ifname = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'l':
|
||||||
|
lprint = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -300,15 +445,19 @@ int main(int argc, char *argv[])
|
||||||
if (daemon)
|
if (daemon)
|
||||||
return run_daemon(nofork);
|
return run_daemon(nofork);
|
||||||
|
|
||||||
else if (dprint && ifname)
|
else if (iprint && ifname)
|
||||||
return run_dump(ifname);
|
return run_dump_ifname(ifname);
|
||||||
|
|
||||||
|
else if (lprint)
|
||||||
|
return run_dump_load();
|
||||||
|
|
||||||
else
|
else
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Usage:\n"
|
"Usage:\n"
|
||||||
" %s -d [-f]\n"
|
" %s -d [-f]\n"
|
||||||
" %s -p ifname\n",
|
" %s -i ifname\n"
|
||||||
argv[0], argv[0]
|
" %s -l\n",
|
||||||
|
argv[0], argv[0], argv[0]
|
||||||
);
|
);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue