diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2010-11-28 06:41:45 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2010-11-28 06:41:45 +0000 |
commit | ae3825387bd9b47813e0b862db38959e7000cb02 (patch) | |
tree | d7219f49ce60f72abbcbfab29d780e551cc51bc0 /modules | |
parent | f45d5fb952aecd48080c0b48580d0b2113769e2b (diff) |
modules/admin-full: add SVG based realtime bandwidth status
Diffstat (limited to 'modules')
-rw-r--r-- | modules/admin-full/Makefile | 24 | ||||
-rw-r--r-- | modules/admin-full/htdocs/luci-static/resources/bandwidth.svg | 16 | ||||
-rwxr-xr-x | modules/admin-full/ipkg/postinst | 6 | ||||
-rw-r--r-- | modules/admin-full/luasrc/controller/admin/status.lua | 31 | ||||
-rw-r--r-- | modules/admin-full/luasrc/view/admin_status/bandwidth.htm | 310 | ||||
-rwxr-xr-x | modules/admin-full/root/etc/init.d/luci_bwc | 14 | ||||
-rw-r--r-- | modules/admin-full/src/luci-bwc.c | 315 |
7 files changed, 715 insertions, 1 deletions
diff --git a/modules/admin-full/Makefile b/modules/admin-full/Makefile index 81a96f6a83..347a34286a 100644 --- a/modules/admin-full/Makefile +++ b/modules/admin-full/Makefile @@ -1,2 +1,24 @@ include ../../build/config.mk -include ../../build/module.mk
\ No newline at end of file +include ../../build/module.mk +include ../../build/gccconfig.mk + +BWC_LDFLAGS = +BWC_CFLAGS = +BWC_BIN = luci-bwc +BWC_OBJ = src/luci-bwc.o + +%.o: %.c + $(COMPILE) $(BWC_CFLAGS) $(LUA_CFLAGS) $(FPIC) -c -o $@ $< + +compile: build-clean $(BWC_OBJ) + $(LINK) $(BWC_LDFLAGS) -o src/$(BWC_BIN) $(BWC_OBJ) + mkdir -p dist/usr/bin + cp src/$(BWC_BIN) dist/usr/bin/$(BWC_BIN) + +install: build + cp -pR dist/usr/bin/$(BWC_BIN) /usr/bin/$(BWC_BIN) + +clean: build-clean + +build-clean: + rm -f src/*.o src/$(BWC_BIN) diff --git a/modules/admin-full/htdocs/luci-static/resources/bandwidth.svg b/modules/admin-full/htdocs/luci-static/resources/bandwidth.svg new file mode 100644 index 0000000000..4f9148833a --- /dev/null +++ b/modules/admin-full/htdocs/luci-static/resources/bandwidth.svg @@ -0,0 +1,16 @@ +<?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="rx" points="" style="fill:blue;fill-opacity:0.4;stroke:blue;stroke-width:1" /> + <polyline id="tx" points="" style="fill:green;fill-opacity:0.4;stroke:green;stroke-width:1" /> +</svg> diff --git a/modules/admin-full/ipkg/postinst b/modules/admin-full/ipkg/postinst new file mode 100755 index 0000000000..a36479ba71 --- /dev/null +++ b/modules/admin-full/ipkg/postinst @@ -0,0 +1,6 @@ +#!/bin/sh + +[ -n "${IPKG_INSTROOT}" ] || { + /etc/init.d/luci_bwc enabled || /etc/init.d/luci_bwc enable + exit 0 +} diff --git a/modules/admin-full/luasrc/controller/admin/status.lua b/modules/admin-full/luasrc/controller/admin/status.lua index 55bba2f381..d283627ebf 100644 --- a/modules/admin-full/luasrc/controller/admin/status.lua +++ b/modules/admin-full/luasrc/controller/admin/status.lua @@ -25,6 +25,9 @@ function index() 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", "bandwidth"}, template("admin_status/bandwidth"), i18n("Realtime Traffic"), 7).leaf = true + entry({"admin", "status", "bandwidth_status"}, call("action_bandwidth")).leaf = true + end function action_syslog() @@ -52,3 +55,31 @@ function action_iptables() luci.template.render("admin_status/iptables") end end + +function action_bandwidth() + local path = luci.dispatcher.context.requestpath + local iface = path[#path] + + local fs = require "luci.fs" + if fs.access("/var/lib/luci-bwc/%s" % iface) then + luci.http.prepare_content("application/json") + + local bwc = io.popen("luci-bwc -p %q 2>/dev/null" % iface) + 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 such interface") +end diff --git a/modules/admin-full/luasrc/view/admin_status/bandwidth.htm b/modules/admin-full/luasrc/view/admin_status/bandwidth.htm new file mode 100644 index 0000000000..f12a222511 --- /dev/null +++ b/modules/admin-full/luasrc/view/admin_status/bandwidth.htm @@ -0,0 +1,310 @@ +<%# +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$ + +-%> + +<%- + local dev + local devices = { } + for _, dev in luci.util.kspairs(luci.sys.net.devices()) do + if dev ~= "lo" then + devices[#devices+1] = dev + end + end + + local curdev = luci.dispatcher.context.requestpath + curdev = curdev[#curdev] ~= "bandwidth" and curdev[#curdev] or devices[1] +-%> + +<%+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 RXB = 1; + var RXP = 2; + var TXB = 3; + var TXP = 4; + + 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_rx = [ ]; + var data_tx = [ ]; + + var line_rx; + var line_tx; + + var label_25; + var label_50; + var label_75; + + var label_rx_cur; + var label_rx_avg; + var label_rx_peak; + + var label_tx_cur; + var label_tx_avg; + var label_tx_peak; + + var label_scale; + + + function bandwidth_label(bytes, br) + { + var uby = 'KByte'; + var kby = (bytes / 1024); + + if (kby >= 1024) + { + uby = 'MByte'; + kby = kby / 1024; + } + + var ubi = 'KBit'; + var kbi = (bytes * 8 / 1024); + + if (kbi >= 1024) + { + ubi = 'MBit'; + kbi = kbi / 1024; + } + + return String.format("%f %s/s%s(%f %s/s)", + kbi.toFixed(2), ubi, + br ? '<br />' : ' ', + kby.toFixed(2), uby + ); + } + + function update_graph() + { + bwxhr.get('<%=build_url("admin/status/bandwidth_status", curdev)%>', null, + function(x, data) + { + var data_max = 0; + var data_scale = 0; + + var data_rx_avg = 0; + var data_tx_avg = 0; + + var data_rx_peak = 0; + var data_tx_peak = 0; + + for (var i = data_stamp ? 0 : 1; i < data.length; i++) + { + /* skip overlapping entries */ + if (data[i][TIME] <= data_stamp) + continue; + + /* normalize difference against time interval */ + var time_delta = data[i][TIME] - data[i-1][TIME]; + if (time_delta) + { + data_rx.push((data[i][RXB] - data[i-1][RXB]) / time_delta); + data_tx.push((data[i][TXB] - data[i-1][TXB]) / time_delta); + } + } + + /* cut off outdated entries */ + data_rx = data_rx.slice(data_rx.length - data_wanted, data_rx.length); + data_tx = data_tx.slice(data_tx.length - data_wanted, data_tx.length); + + /* find peak */ + for (var i = 0; i < data_rx.length; i++) + { + data_max = Math.max(data_max, data_rx[i]); + data_max = Math.max(data_max, data_tx[i]); + + data_rx_peak = Math.max(data_rx_peak, data_rx[i]); + data_tx_peak = Math.max(data_tx_peak, data_tx[i]); + + if (i > 0) + { + data_rx_avg = (data_rx_avg + data_rx[i]) / 2; + data_tx_avg = (data_tx_avg + data_tx[i]) / 2; + } + else + { + data_rx_avg = data_rx[i]; + data_tx_avg = data_tx[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_rx = '0,' + height; + var pt_tx = '0,' + height; + + var y_rx = 0; + var y_tx = 0; + + for (var i = 0; i < data_rx.length; i++) + { + var x = i * step; + + y_rx = height - Math.floor(data_rx[i] * data_scale); + y_tx = height - Math.floor(data_tx[i] * data_scale); + + pt_rx += ' ' + x + ',' + y_rx; + pt_tx += ' ' + x + ',' + y_tx; + } + + pt_rx += ' ' + width + ',' + y_rx + ' ' + width + ',' + height; + pt_tx += ' ' + width + ',' + y_tx + ' ' + width + ',' + height; + + + line_rx.setAttribute('points', pt_rx); + line_tx.setAttribute('points', pt_tx); + + label_25.firstChild.data = bandwidth_label(1.1 * 0.25 * data_max); + label_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max); + label_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max); + + label_rx_cur.innerHTML = bandwidth_label(data_rx[data_rx.length-1], true); + label_tx_cur.innerHTML = bandwidth_label(data_tx[data_tx.length-1], true); + + label_rx_avg.innerHTML = bandwidth_label(data_rx_avg, true); + label_tx_avg.innerHTML = bandwidth_label(data_tx_avg, true); + + label_rx_peak.innerHTML = bandwidth_label(data_rx_peak, true); + label_tx_peak.innerHTML = bandwidth_label(data_tx_peak, true); + + /* 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_rx[i] = 0; + data_tx[i] = 0; + } + + /* find svg elements */ + line_rx = G.getElementById('rx'); + line_tx = G.getElementById('tx'); + + label_25 = G.getElementById('label_25'); + label_50 = G.getElementById('label_50'); + label_75 = G.getElementById('label_75'); + + label_rx_cur = document.getElementById('rx_bw_cur'); + label_rx_avg = document.getElementById('rx_bw_avg'); + label_rx_peak = document.getElementById('rx_bw_peak'); + + label_tx_cur = document.getElementById('tx_bw_cur'); + label_tx_avg = document.getElementById('tx_bw_avg'); + label_tx_peak = document.getElementById('tx_bw_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 Traffic%></a></h2> + +<ul class="cbi-tabmenu"> + <% for _, dev in ipairs(devices) do %> + <li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li> + <% end %> +</ul> + +<embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/bandwidth.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 blue"><%:Inbound:%></strong></td> + <td id="rx_bw_cur">0 kbit/s<br />(0 KB/s)</td> + + <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td> + <td id="rx_bw_avg">0 kbit/s<br />(0 KB/s)</td> + + <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td> + <td id="rx_bw_peak">0 kbit/s<br />(0 KB/s)</td> + </tr> + <tr> + <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Outbound:%></strong></td> + <td id="tx_bw_cur">0 kbit/s<br />(0 KB/s)</td> + + <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td> + <td id="tx_bw_avg">0 kbit/s<br />(0 KB/s)</td> + + <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td> + <td id="tx_bw_peak">0 kbit/s<br />(0 KB/s)</td> + </tr> +</table> + + +<pre id="debug"></pre> + + +<%+footer%> diff --git a/modules/admin-full/root/etc/init.d/luci_bwc b/modules/admin-full/root/etc/init.d/luci_bwc new file mode 100755 index 0000000000..fe246073f0 --- /dev/null +++ b/modules/admin-full/root/etc/init.d/luci_bwc @@ -0,0 +1,14 @@ +#!/bin/sh /etc/rc.common + +START=95 +STOP=95 + +BWC=/usr/bin/luci-bwc + +start() { + $BWC -d +} + +stop() { + killall ${BWC##*/} +} diff --git a/modules/admin-full/src/luci-bwc.c b/modules/admin-full/src/luci-bwc.c new file mode 100644 index 0000000000..6b6c0c7101 --- /dev/null +++ b/modules/admin-full/src/luci-bwc.c @@ -0,0 +1,315 @@ +/* + * luci-bwc - Very simple bandwidth collector cache for LuCI realtime graphs + * + * Copyright (C) 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <inttypes.h> +#include <fcntl.h> +#include <time.h> +#include <errno.h> +#include <unistd.h> + +#include <sys/stat.h> +#include <sys/mman.h> +#include <arpa/inet.h> + + +#define STEP_COUNT 60 +#define STEP_TIME 1 + +#define DB_PATH "/var/lib/luci-bwc" +#define DB_FILE DB_PATH "/%s" + +#define SCAN_PATTERN \ + " %[^ :]:%" SCNu64 " %" SCNu64 \ + " %*d %*d %*d %*d %*d %*d" \ + " %" SCNu64 " %" SCNu64 + + +struct traffic_entry { + uint64_t time; + uint64_t rxb; + uint64_t rxp; + uint64_t txb; + uint64_t txp; +}; + +static uint64_t htonll(uint64_t value) +{ + int num = 1; + + if (*(char *)&num == 1) + return htonl((uint32_t)(value & 0xFFFFFFFF)) | + htonl((uint32_t)(value >> 32)); + + return value; +} + +#define ntohll htonll + + +static int init_minutely(const char *ifname) +{ + int i, file; + char path[1024]; + char *p; + struct traffic_entry e = { 0 }; + + snprintf(path, sizeof(path), DB_FILE, ifname); + + for (p = &path[1]; *p; p++) + { + if (*p == '/') + { + *p = 0; + + if (mkdir(path, 0700) && (errno != EEXIST)) + return -1; + + *p = '/'; + } + } + + if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0) + { + for (i = 0; i < STEP_COUNT; i++) + { + if (write(file, &e, sizeof(struct traffic_entry)) < 0) + break; + } + + close(file); + + return 0; + } + + return -1; +} + +static int update_minutely( + 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 *map; + + struct stat s; + struct traffic_entry e; + + snprintf(path, sizeof(path), DB_FILE, ifname); + + if (stat(path, &s)) + { + if (init_minutely(ifname)) + { + fprintf(stderr, "Failed to init %s: %s\n", + path, strerror(errno)); + + return rv; + } + } + + 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.rxb = htonll(rxb); + e.rxp = htonll(rxp); + e.txb = htonll(txb); + e.txp = htonll(txp); + + memmove(map, map + entrysize, mapsize - entrysize); + memcpy(map + mapsize - entrysize, &e, entrysize); + + munmap(map, mapsize); + + rv = 0; + } + + close(file); + } + + return rv; +} + +static int run_daemon(int nofork) +{ + FILE *info; + uint64_t rxb, txb, rxp, txp; + char line[1024]; + char ifname[16]; + + + if (!nofork) + { + switch (fork()) + { + case -1: + perror("fork()"); + return -1; + + case 0: + if (chdir("/") < 0) + { + perror("chdir()"); + exit(1); + } + + close(0); + close(1); + close(2); + break; + + default: + exit(0); + } + } + + + /* go */ + while (1) + { + if ((info = fopen("/proc/net/dev", "r")) != NULL) + { + while (fgets(line, sizeof(line), info)) + { + if (strchr(line, '|')) + continue; + + if (sscanf(line, SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp)) + { + if (strncmp(ifname, "lo", sizeof(ifname))) + update_minutely(ifname, rxb, rxp, txb, txp); + } + } + + fclose(info); + } + + sleep(STEP_TIME); + } +} + +static int run_dump(const char *ifname) +{ + int rv = 1; + + int i, file; + int entrysize = sizeof(struct traffic_entry); + int mapsize = STEP_COUNT * entrysize; + + char path[1024]; + char *map; + + struct traffic_entry *e; + + snprintf(path, sizeof(path), DB_FILE, ifname); + + 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 traffic_entry *) &map[i]; + + if (!e->time) + continue; + + printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64 + ", %" PRIu64 ", %" PRIu64 " ]%s\n", + ntohll(e->time), + ntohll(e->rxb), ntohll(e->rxp), + ntohll(e->txb), ntohll(e->txp), + ((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 opt; + int daemon = 0; + int nofork = 0; + int dprint = 0; + char *ifname = NULL; + + while ((opt = getopt(argc, argv, "dfp:")) > -1) + { + switch (opt) + { + case 'd': + daemon = 1; + break; + + case 'f': + nofork = 1; + break; + + case 'p': + dprint = 1; + ifname = optarg; + break; + + default: + break; + } + } + + if (daemon) + return run_daemon(nofork); + + else if (dprint && ifname) + return run_dump(ifname); + + else + fprintf(stderr, + "Usage:\n" + " %s -d [-f]\n" + " %s -p ifname\n", + argv[0], argv[0] + ); + + return 1; +} |