summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-04-20 17:55:39 +0200
committerJo-Philipp Wich <jo@mein.io>2020-04-20 19:05:27 +0200
commitfdce990b90f21f6925795d1f788f3e87bcdd0400 (patch)
tree754477c0046c74b1e23e0ce82c1551b6950f4fbd /applications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action
parent79814b2f5286b6956d555b796407e2394169669d (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/usr/libexec/nlbwmon-action')
-rwxr-xr-xapplications/luci-app-nlbwmon/root/usr/libexec/nlbwmon-action117
1 files changed, 117 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 0000000000..89e51776ef
--- /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