Merge pull request #21 from freifunk-gluon/alfred-lua
gluon-alfred: replace announce.sh with lua script
This commit is contained in:
commit
c8c9d72df9
@ -11,7 +11,7 @@ include $(INCLUDE_DIR)/package.mk
|
|||||||
define Package/gluon-alfred
|
define Package/gluon-alfred
|
||||||
SECTION:=gluon
|
SECTION:=gluon
|
||||||
CATEGORY:=Gluon
|
CATEGORY:=Gluon
|
||||||
DEPENDS:=+gluon-core +gluon-node-info +gluon-cron +alfred +ethtool
|
DEPENDS:=+gluon-core +gluon-node-info +gluon-cron +alfred +ethtool +luci-lib-json +luci-lib-core
|
||||||
TITLE:=Configure alfred
|
TITLE:=Configure alfred
|
||||||
endef
|
endef
|
||||||
|
|
||||||
|
117
package/gluon-alfred/files/lib/gluon/alfred/announce.lua
Executable file
117
package/gluon-alfred/files/lib/gluon/alfred/announce.lua
Executable file
@ -0,0 +1,117 @@
|
|||||||
|
#!/usr/bin/lua
|
||||||
|
|
||||||
|
local json = require "luci.json"
|
||||||
|
local ltn12 = require "luci.ltn12"
|
||||||
|
local util = require "luci.util"
|
||||||
|
|
||||||
|
require "luci.model.uci"
|
||||||
|
local uci = luci.model.uci.cursor()
|
||||||
|
|
||||||
|
local alfred_data_type = tonumber(os.getenv("ALFRED_DATA_TYPE")) or 158
|
||||||
|
local net_if = os.getenv("NET_IF") or "br-client"
|
||||||
|
|
||||||
|
function readAll(file)
|
||||||
|
local f = io.open(file, "rb")
|
||||||
|
local content = f:read("*all")
|
||||||
|
f:close()
|
||||||
|
return content
|
||||||
|
end
|
||||||
|
|
||||||
|
function chomp(s)
|
||||||
|
return (s:gsub("^(.-)\n?$", "%1"))
|
||||||
|
end
|
||||||
|
|
||||||
|
function trim(s)
|
||||||
|
return (s:gsub("^%s*(.-)%s*$", "%1"))
|
||||||
|
end
|
||||||
|
|
||||||
|
output = {}
|
||||||
|
|
||||||
|
output["hostname"] = uci:get_first("system", "system", "hostname")
|
||||||
|
|
||||||
|
if uci:get_first("gluon-node-info", "location", "share_location", false) then
|
||||||
|
output["location"] =
|
||||||
|
{ latitude = tonumber(uci:get_first("gluon-node-info", "location", "latitude"))
|
||||||
|
, longitude = tonumber(uci:get_first("gluon-node-info", "location", "longitude"))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local contact = uci:get_first("gluon-node-info", "owner", "contact", "")
|
||||||
|
if contact ~= "" then
|
||||||
|
output["owner"] = { contact = contact }
|
||||||
|
end
|
||||||
|
|
||||||
|
output["software"] =
|
||||||
|
{ firmware = { base = "gluon-" .. chomp(readAll("/lib/gluon/gluon-version"))
|
||||||
|
, release = chomp(readAll("/lib/gluon/release"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local autoupdater = uci:get_all("autoupdater", "settings")
|
||||||
|
if autoupdater then
|
||||||
|
output["software"]["autoupdater"] =
|
||||||
|
{ branch = autoupdater["branch"]
|
||||||
|
, enabled = uci:get_bool("autoupdater", "settings", "enabled")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local fastd = uci:get_all("fastd", "mesh_vpn")
|
||||||
|
if fastd then
|
||||||
|
output["software"]["fastd"] =
|
||||||
|
{ enabled = uci:get_bool("fastd", "mesh_vpn", "enabled")
|
||||||
|
, version = chomp(util.exec("fastd -v | cut -d' ' -f2"))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
output["hardware"] =
|
||||||
|
{ model = chomp(util.exec(". /lib/gluon/functions/model.sh; get_model")) }
|
||||||
|
|
||||||
|
|
||||||
|
local addresses = {}
|
||||||
|
local tmp = util.exec("ip -o -6 addr show dev \"" .. net_if .. "\" | "
|
||||||
|
.. "grep -oE 'inet6 [0-9a-fA-F:]+' | cut -d' ' -f2")
|
||||||
|
|
||||||
|
for address in tmp:gmatch("[^\n]+") do
|
||||||
|
table.insert(addresses, address)
|
||||||
|
end
|
||||||
|
|
||||||
|
output["network"] =
|
||||||
|
{ mac = chomp(util.exec(". /lib/gluon/functions/sysconfig.sh; sysconfig primary_mac"))
|
||||||
|
, addresses = addresses
|
||||||
|
}
|
||||||
|
|
||||||
|
local gateway =
|
||||||
|
chomp(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'"))
|
||||||
|
|
||||||
|
if gateway ~= "" then
|
||||||
|
output["network"]["gateway"] = gateway
|
||||||
|
end
|
||||||
|
|
||||||
|
local traffic = {}
|
||||||
|
local ethtool = util.exec("ethtool -S bat0")
|
||||||
|
for k, v in ethtool:gmatch("([%a_]+): ([0-9]+)") do
|
||||||
|
traffic[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
for _,class in ipairs({"rx", "tx", "forward", "mgmt_rx", "mgmt_tx"}) do
|
||||||
|
traffic[class] =
|
||||||
|
{ bytes = traffic[class .. "_bytes"]
|
||||||
|
, packets = traffic[class]
|
||||||
|
}
|
||||||
|
|
||||||
|
if class == "tx" then
|
||||||
|
traffic[class]["dropped"] = traffic[class .. "_dropped"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
output["statistics"] =
|
||||||
|
{ uptime = chomp(util.exec("cut -d' ' -f1 /proc/uptime"))
|
||||||
|
, loadavg = chomp(util.exec("cut -d' ' -f1 /proc/loadavg"))
|
||||||
|
, traffic = traffic
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder = json.Encoder(output)
|
||||||
|
alfred = io.popen("alfred -s " .. tostring(alfred_data_type), "w")
|
||||||
|
ltn12.pump.all(encoder:source(), ltn12.sink.file(alfred))
|
||||||
|
|
||||||
|
|
@ -1,91 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
if [ -f /lib/functions/jshn.sh ]; then
|
|
||||||
. /lib/functions/jshn.sh
|
|
||||||
elif [ -f /usr/share/libubox/jshn.sh ]; then
|
|
||||||
. /usr/share/libubox/jshn.sh
|
|
||||||
else
|
|
||||||
echo "Error: jshn.sh not found!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
. /lib/gluon/functions/model.sh
|
|
||||||
. /lib/gluon/functions/sysconfig.sh
|
|
||||||
|
|
||||||
# set defaults
|
|
||||||
[ -z "$ALFRED_DATA_TYPE" ] && ALFRED_DATA_TYPE=158
|
|
||||||
[ -z "$NET_IF" ] && NET_IF=br-client
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
json_init
|
|
||||||
json_add_string "hostname" "$(uci get 'system.@system[0].hostname')"
|
|
||||||
|
|
||||||
if [ "$(uci -q get 'gluon-node-info.@location[0].share_location')" = 1 ]; then
|
|
||||||
json_add_object "location"
|
|
||||||
json_add_double "latitude" "$(uci get 'gluon-node-info.@location[0].latitude')"
|
|
||||||
json_add_double "longitude" "$(uci get 'gluon-node-info.@location[0].longitude')"
|
|
||||||
json_close_object # location
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$(uci -q get 'gluon-node-info.@owner[0].contact')" ]; then
|
|
||||||
json_add_object "owner"
|
|
||||||
json_add_string "contact" "$(uci get 'gluon-node-info.@owner[0].contact')"
|
|
||||||
json_close_object # owner
|
|
||||||
fi
|
|
||||||
|
|
||||||
json_add_object "software"
|
|
||||||
json_add_object "firmware"
|
|
||||||
json_add_string "base" "gluon-$(cat /lib/gluon/gluon-version)"
|
|
||||||
json_add_string "release" "$(cat /lib/gluon/release)"
|
|
||||||
json_close_object # firmware
|
|
||||||
|
|
||||||
if [ -x /usr/sbin/autoupdater ]; then
|
|
||||||
json_add_object "autoupdater"
|
|
||||||
json_add_string "branch" "$(uci -q get autoupdater.settings.branch)"
|
|
||||||
json_add_boolean "enabled" "$(uci -q get autoupdater.settings.enabled)"
|
|
||||||
json_close_object # autoupdater
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -x /usr/bin/fastd ]; then
|
|
||||||
json_add_object "fastd"
|
|
||||||
json_add_string "version" "$(fastd -v | cut -d' ' -f2)"
|
|
||||||
json_add_boolean "enabled" "$(uci -q get fastd.mesh_vpn.enabled)"
|
|
||||||
json_close_object # fastd
|
|
||||||
fi
|
|
||||||
json_close_object # software
|
|
||||||
|
|
||||||
json_add_object "hardware"
|
|
||||||
json_add_string "model" "$(get_model)"
|
|
||||||
json_close_object # hardware
|
|
||||||
|
|
||||||
json_add_object "network"
|
|
||||||
json_add_string "mac" "$(sysconfig primary_mac)"
|
|
||||||
json_add_array "addresses"
|
|
||||||
for addr in $(ip -o -6 addr show dev "$NET_IF" | grep -oE 'inet6 [0-9a-fA-F:]+' | cut -d' ' -f2); do
|
|
||||||
json_add_string "" "$addr"
|
|
||||||
done
|
|
||||||
json_close_array # adresses
|
|
||||||
|
|
||||||
GATEWAY="$(batctl -m bat0 gateways | awk '/^=>/ { print $2 }')"
|
|
||||||
[ -z "$GATEWAY" ] || json_add_string "gateway" "$GATEWAY"
|
|
||||||
json_close_object # network
|
|
||||||
|
|
||||||
json_add_object "statistics"
|
|
||||||
json_add_int "uptime" "$(cut -d' ' -f1 /proc/uptime)"
|
|
||||||
json_add_double "loadavg" "$(cut -d' ' -f1 /proc/loadavg)"
|
|
||||||
json_add_object "traffic"
|
|
||||||
TRAFFIC="$(ethtool -S bat0 | sed -e 's/^ *//')"
|
|
||||||
for class in rx tx forward mgmt_rx mgmt_tx; do
|
|
||||||
json_add_object "$class"
|
|
||||||
json_add_int "bytes" "$(echo "$TRAFFIC" | awk -F': ' "/^${class}_bytes:/ { print \$2 }")"
|
|
||||||
json_add_int "packets" "$(echo "$TRAFFIC" | awk -F': ' "/^${class}:/ { print \$2 }")"
|
|
||||||
if [ "$class" = "tx" ]; then
|
|
||||||
json_add_int "dropped" "$(echo "$TRAFFIC" | awk -F': ' "/^${class}_dropped:/ { print \$2 }")"
|
|
||||||
fi
|
|
||||||
json_close_object # $class
|
|
||||||
done
|
|
||||||
json_close_object # traffic
|
|
||||||
json_close_object # statistics
|
|
||||||
|
|
||||||
json_dump | tr -d '\n' | alfred -s "$ALFRED_DATA_TYPE"
|
|
@ -1 +1 @@
|
|||||||
* * * * * /lib/gluon/alfred/announce.sh
|
* * * * * /lib/gluon/alfred/announce.lua
|
||||||
|
Loading…
Reference in New Issue
Block a user