diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-04-20 17:55:39 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-04-20 19:05:27 +0200 |
commit | fdce990b90f21f6925795d1f788f3e87bcdd0400 (patch) | |
tree | 754477c0046c74b1e23e0ce82c1551b6950f4fbd /applications/luci-app-nlbwmon/root | |
parent | 79814b2f5286b6956d555b796407e2394169669d (diff) |
luci-app-nlbwmon: convert to client side JS
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'applications/luci-app-nlbwmon/root')
3 files changed, 179 insertions, 0 deletions
diff --git a/applications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action b/applications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action new file mode 100755 index 000000000..89e51776e --- /dev/null +++ b/applications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action @@ -0,0 +1,117 @@ +#!/bin/sh + +case "$1" in + + backup) + dbdir=$(uci -q get nlbwmon.@nlbwmon[0].database_directory) + + if [ ! -d "${dbdir:-/var/lib/nlbwmon}" ]; then + echo "Unable to locate database directory" >&2 + exit 1 + fi + + exec /bin/tar -C "${dbdir:-/var/lib/nlbwmon}" -c -z . -f - + ;; + + commit) + exec /usr/sbin/nlbw -c commit + ;; + + download) + shift + + type=json + delim=, + period= + group_by= + order_by= + + while [ -n "$1" ]; do + case "$1" in + -f) + case "$2" in + csv|json) type=$2 ;; + *) echo "Invalid data format" >&2; exit 1 ;; + esac + shift + ;; + -s) + case "$2" in + ?) delim=$2 ;; + *) echo "Invalid delimitter" >&2; exit 1 ;; + esac + shift + ;; + -t) + case "$2" in + [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) period=$2 ;; + *) echo "Invalid period" >&2; exit 1 ;; + esac + shift + ;; + -g|-o) + case "$1:$2" in + -g:?*) group_by=$2 ;; + -o:?*) order_by=$2 ;; + *) echo "Argument required for $1" >&2; exit 1 ;; + esac + shift + ;; + *) + echo "Unknown option $1" >&2 + exit 1 + ;; + esac + shift + done + + exec /usr/sbin/nlbw -c $type -s$delim \ + ${period:+-t $period} \ + ${group_by:+-g "$group_by"} \ + ${order_by:+-o "$order_by"} + ;; + + periods) + for date in $(/usr/sbin/nlbw -c list); do + case "$date" in + [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) + res="${res:+$res, }\"$date\"" + ;; + esac + done + + printf '{ "periods": [ %s ] }' "$res" + ;; + + restore) + if [ ! -f /tmp/nlbw-restore.tar.gz ]; then + echo "Unable to locate archive to restore" >&2 + exit 1 + fi + + dbdir=$(uci -q get nlbwmon.@nlbwmon[0].database_directory) + files=$(/bin/tar -tzf /tmp/nlbw-restore.tar.gz | grep -E '^(\./)?[0-9]{8}\.db(\.gz)?$' | tr '\n' ' ') + + if [ -z "$files" ]; then + echo "Invalid or empty backup archive" >&2 + exit 1 + fi + + /etc/init.d/nlbwmon stop + /bin/mkdir -p "${dbdir:-/var/lib/nlbwmon}" + + for file in $(/bin/tar -C "${dbdir:-/var/lib/nlbwmon}" -vxzf /tmp/nlbw-restore.tar.gz $files); do + res="${res:+$res, }\"${file#./}\"" + done + + /bin/rm -f /tmp/nlbw-restore.tar.gz + /etc/init.d/nlbwmon start + + printf '{ "restored": [ %s ] }' "$res" + ;; + + *) + echo "Usage: $0 {commit|download|periods|restore}" >&2 + exit 1 + ;; +esac diff --git a/applications/luci-app-nlbwmon/root/usr/share/luci/menu.d/luci-app-nlbwmon.json b/applications/luci-app-nlbwmon/root/usr/share/luci/menu.d/luci-app-nlbwmon.json new file mode 100644 index 000000000..5b597c632 --- /dev/null +++ b/applications/luci-app-nlbwmon/root/usr/share/luci/menu.d/luci-app-nlbwmon.json @@ -0,0 +1,40 @@ +{ + "admin/nlbw": { + "title": "Bandwidth Monitor", + "order": 80, + "action": { + "type": "firstchild" + }, + "depends": { + "acl": [ "luci-app-nlbwmon" ], + "uci": { "nlbwmon": true } + } + }, + + "admin/nlbw/display": { + "title": "Display", + "order": 1, + "action": { + "type": "view", + "path": "nlbw/display" + } + }, + + "admin/nlbw/config": { + "title": "Configuration", + "order": 2, + "action": { + "type": "view", + "path": "nlbw/config" + } + }, + + "admin/nlbw/backup": { + "title": "Backup", + "order": 3, + "action": { + "type": "view", + "path": "nlbw/backup" + } + } +} diff --git a/applications/luci-app-nlbwmon/root/usr/share/rpcd/acl.d/luci-app-nlbwmon.json b/applications/luci-app-nlbwmon/root/usr/share/rpcd/acl.d/luci-app-nlbwmon.json index 0ccc5b1c3..20f05eef0 100644 --- a/applications/luci-app-nlbwmon/root/usr/share/rpcd/acl.d/luci-app-nlbwmon.json +++ b/applications/luci-app-nlbwmon/root/usr/share/rpcd/acl.d/luci-app-nlbwmon.json @@ -2,9 +2,31 @@ "luci-app-nlbwmon": { "description": "Grant UCI access for luci-app-nlbwmon", "read": { + "cgi-io": [ "exec" ], + "file": { + "/proc/sys/kernel/hostname": [ "read" ], + "/usr/libexec/nlbwmon-action backup": [ "exec" ], + "/usr/libexec/nlbwmon-action download *": [ "exec" ], + "/usr/libexec/nlbwmon-action periods": [ "exec" ], + "/usr/share/nlbwmon/protocols": [ "read" ] + }, + "ubus": { + "file": [ "read" ], + "luci-rpc": [ "getHostHints" ], + "network.rrdns": [ "lookup" ] + }, "uci": [ "nlbwmon" ] }, "write": { + "file": { + "/tmp/nlbw-restore.tar.gz": [ "write" ], + "/usr/libexec/nlbwmon-action commit": [ "exec" ], + "/usr/libexec/nlbwmon-action restore": [ "exec" ], + "/usr/share/nlbwmon/protocols": [ "write" ] + }, + "ubus": { + "file": [ "write" ] + }, "uci": [ "nlbwmon" ] } } |