diff --git a/luci/luci-app-falter-owm/Makefile b/luci/luci-app-falter-owm/Makefile index 08c08888..a739a88f 100644 --- a/luci/luci-app-falter-owm/Makefile +++ b/luci/luci-app-falter-owm/Makefile @@ -72,7 +72,7 @@ endef define Package/luci-app-falter-owm-cmd/install $(INSTALL_DIR) $(1)/usr/sbin/ - $(CP) files/owm.lua $(1)/usr/sbin/owm.lua + $(CP) files/owm.sh $(1)/usr/sbin/owm.sh $(INSTALL_DIR) $(1)/etc/uci-defaults $(CP) files/owm-defaults $(1)/etc/uci-defaults/owm endef diff --git a/luci/luci-app-falter-owm/files/owm-defaults b/luci/luci-app-falter-owm/files/owm-defaults index eb30ccf9..a1a0afdb 100644 --- a/luci/luci-app-falter-owm/files/owm-defaults +++ b/luci/luci-app-falter-owm/files/owm-defaults @@ -2,4 +2,4 @@ test -f /etc/crontabs/root || touch /etc/crontabs/root SEED="$( dd if=/dev/urandom bs=2 count=1 2>&- | hexdump | if read line; then echo 0x${line#* }; fi )" MIN="$(( $SEED % 59 ))" -crontab -l | grep -q "owm.lua" || crontab -l | { cat; echo "$MIN * * * * test -e /usr/sbin/owm.lua && /usr/sbin/owm.lua"; } | crontab - +crontab -l | grep -q "owm.sh" || crontab -l | { cat; echo "$MIN * * * * test -e /usr/sbin/owm.sh && /usr/sbin/owm.sh"; } | crontab - diff --git a/luci/luci-app-falter-owm/files/owm.lua b/luci/luci-app-falter-owm/files/owm.lua deleted file mode 100755 index d687e120..00000000 --- a/luci/luci-app-falter-owm/files/owm.lua +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/lua - -require("luci.util") -require("luci.model.uci") -require("luci.sys") -require("nixio.fs") -require("luci.httpclient") - --- Print help text -function print_help() - print("owm.lua - Tool for registering routers at openwifimap.net\n") - print("Options:\ - -\-help|-h:\tprint this text\ - \ - -\-dry-run:\tcheck if owm.lua is working (does not paste any data).\ - \t\tWith this option you can check for errors in your\ - \t\tconfiguration and test the transmission of data to\ - \t\tthe map.") - print("\nIf invoked without any options, this tool\ -will try to register your node at the community-map,\ -but will execute silently. To work correctly, this tool\ -will need at least the geo-location of the node (check\ -with -\-dry-run).\ -\ -To override the server used by this script, set freifunk.community.owm_api.\ -") -end - --- check for arguments -if (#arg) > 0 and arg[1]~="--dry-run" then - print_help() - return -end - --- Init state session -local uci = luci.model.uci.cursor_state() -local owm = require "luci.owm" -local json = require "luci.json" -local lockfile = "/var/run/owm.lock" -local hostname - ---function db_put(uri,body) -function db_put(owm_api,hostname,suffix,body) - local httpc = luci.httpclient - local uri_update = owm_api.."/update_node/"..hostname.."."..suffix - - local options = { - method = "PUT", - body = body, - headers = { - ["Content-Type"] = "application/json", - }, - } - - local response, code, msg = httpc.request_to_buffer(uri_update, options) - - if code == 201 then - print("update Doc Statuscode: "..code.." "..uri_update.." ("..msg..")") - elseif code then - print("fail Doc Statuscode: "..code.." "..uri_update.." ("..msg..")") - end -end - - -function lock() - if nixio.fs.access(lockfile) then - local timediff = os.time() - nixio.fs.stat(lockfile, "mtime") - if timediff < 3600 then - print(lockfile.." exists, time since lock: "..timediff) - os.exit() - end - else - os.execute("lock "..lockfile) - end -end - -function unlock() - os.execute("lock -u "..lockfile) - os.execute("rm -f "..lockfile) -end - --- location for the node is required -if owm.get_position()==nil then - if arg[1]=="--dry-run" then - print("no latitude/longitude specified for node.") - end - unlock() - return -end - -lock() - -uci:foreach("system", "system", function(s) - hostname = s.hostname -end) - -local owm_api = uci:get("freifunk", "community", "owm_api") or "http://util.berlin.freifunk.net" -local cname = uci:get("freifunk", "community", "name") or "freifunk" -local suffix = uci:get("freifunk", "community", "suffix") or uci:get("profile_" .. cname, "profile", "suffix") or "olsr" -local body = json.encode(owm.get()) - -if arg[1]=="--dry-run" then - print(body) - unlock() - return -end - -if type(owm_api)=="table" then - for i,v in ipairs(owm_api) do - local owm_api = v - db_put(owm_api,hostname,suffix,body) - end -else - db_put(owm_api,hostname,suffix,body) -end - -unlock() - diff --git a/luci/luci-app-falter-owm/files/owm.sh b/luci/luci-app-falter-owm/files/owm.sh index 9ec1f523..b7f377ab 100755 --- a/luci/luci-app-falter-owm/files/owm.sh +++ b/luci/luci-app-falter-owm/files/owm.sh @@ -3,15 +3,41 @@ . /lib/functions.sh . /usr/share/libubox/jshn.sh -#Divide by 65536.0 and round to 2 dec to get 1.00 -int2float() { - local val=$1 - reell="$((val/65536))" - ratio="$((val*100/65536))" - rest="$((reell*100))" - ratio="$((ratio-rest))" - printf "%d.%02d" $reell $ratio +OWM_API_VER="1.0" + +printhelp() { + printf "owm.sh - Tool for registering routers at openwifimap.net\n +Options: +\t--help|-h:\tprint this text + +\t--dry-run:\tcheck if owm.lua is working (does not paste any data). +\t\t\tWith this option you can check for errors in your +\t\t\tconfiguration and test the transmission of data to +\t\t\tthe map.\n\n +If invoked without any options, this tool will try to register +your node at the community-map and print the servers response. +To work correctly, this tool will need at least the geo-location +of the node (check correct execution with --dry-run). + +To override the server used by this script, set freifunk.community.owm_api. +" } + +# save positional argument, as it would get overwritten otherwise. +CMD_1="$1" +if [ -n "$CMD_1" ] && [ "$CMD_1" != "--dry-run" ]; then + [ "$CMD_1" != "-h" ] && [ "$CMD_1" != "--help" ] && printf "Unrecognized argument %s.\n\n" "$CMD_1" + printhelp + exit 1 +fi + + +###################### +# # +# Collect OWM-Data # +# # +###################### + olsr2_links() { json_select $2 json_get_var localIP link_bindto @@ -24,6 +50,7 @@ olsr2_links() { json_select .. olsr2links="$olsr2links$localIP $remoteIP $remotehost $linkQuality $ifName;" } + olsr4_links() { json_select $2 json_get_var localIP localIP @@ -35,6 +62,7 @@ olsr4_links() { json_select .. olsr4links="$olsr4links$localIP $remoteIP $remotehost $linkQuality $ifName;" } + olsr6_links() { json_select $2 json_get_var localIP localIP @@ -46,6 +74,25 @@ olsr6_links() { json_select .. olsr6links="$olsr6links$localIP $remoteIP $remotehost $linkQuality $ifName;" } + +# This section is relevant for hopglass statistics feature (isUplink/isHotspot) +OLSRCONFIG=$(printf "/config" | nc localhost 9090) + +# collect nodes location +uci_load system +longitude="$(uci_get system @system[-1] longitude)" +latitude="$(uci_get system @system[-1] latitude)" + +# +# Stop execution if lat/lon is not set. +# +if [ -z "$latitude" ] || [ -z "$longitude" ]; then + printf "latitude/longitude is not set.\nStopping now...\n" + exit 2 +fi + + +# collect data on OLSR-links json_load "$(echo /nhdpinfo json link | nc ::1 2009 2>/dev/null)" 2>/dev/null olsr2links="" if json_is_a link array;then @@ -66,6 +113,9 @@ if json_is_a links array;then json_for_each_item olsr6_links links fi json_cleanup + + +# collect board info json_load "$(ubus call system board)" json_get_var model model json_get_var hostname hostname @@ -78,122 +128,187 @@ json_select .. json_load "$(ubus call system info)" json_get_var uptime uptime json_get_values loads load -#Divide by 65536.0 and round to 2 dec to get 1.00 -set -- $loads -load1=$(int2float $1) -load5=$(int2float $2) -load15=$(int2float $3) -uci_load system -longitude="$(uci_get system @system[-1] longitude "13.4")" -latitude="$(uci_get system @system[-1] latitude "52.5")" +# if file freifunk_release is available, override version and revision +if [ -f /etc/freifunk_release ]; then + . /etc/freifunk_release + distribution="$FREIFUNK_DISTRIB_ID" + version="$FREIFUNK_RELEASE" + revision="$FREIFUNK_REVISION" +fi + +# Get Sysload +sysload=$(uptime | sed -e 's/average: /;/g' | cut -d';' -f2 | tr ',' ' ') +load1=$(echo "$sysload" | cut -d' ' -f1) +load5=$(echo "$sysload" | cut -d' ' -f3) +load15=$(echo "$sysload" | cut -d' ' -f5) + +# contact information uci_load freifunk +name="$(uci_get freifunk contact name)" +nick="$(uci_get freifunk contact nickname)" mail="$(uci_get freifunk contact mail)" -nick="$(uci_get freifunk contact nick)" +phone="$(uci_get freifunk contact phone)" +homepage="$(uci_get freifunk contact homepage)" # whitespace-separated, with single quotes, if string contains whitspace +note="$(uci_get freifunk contact note)" + +# community info ssid="$(uci_get freifunk community ssid)" mesh_network="$(uci_get freifunk community mesh_network)" uci_owm_apis="$(uci_get freifunk community owm_api)" -name="$(uci_get freifunk community name)" -homepage="$(uci_get freifunk community homepage)" +com_name="$(uci_get freifunk community name)" +com_homepage="$(uci_get freifunk community homepage)" com_longitude="$(uci_get freifunk community longitude)" com_latitude="$(uci_get freifunk community latitude)" +com_ssid_scheme=$(uci_get freifunk community ssid_scheme) +com_splash_network=$(uci_get freifunk community splash_network) +com_splash_prefix=$(uci_get freifunk community splash_prefix) + + + +########################### +# # +# Construct JSON-string # +# # +########################### + json_init json_add_object freifunk -json_add_object contact -json_add_string mail "$mail" -json_add_string nickname "$nickname" -json_close_object -json_add_object community -json_add_string ssid "$ssid" -json_add_string mesh_network "$mesh_network" -json_add_array owm_api -for uci_owm_api in "$uci_owm_apis";do - json_add_string "" "$uci_owm_api" -done -json_close_array -json_add_string name "$name" -json_add_string homepage "$homepage" -json_add_string longitude "$com_longitude" -json_add_string latitude "$com_latitude" -json_close_object + + json_add_object contact + if [ -n "$name" ]; then json_add_string name "$name"; fi + if [ -n "$mail" ]; then json_add_string mail "$mail"; fi + if [ -n "$nick" ]; then json_add_string nickname "$nick"; fi + if [ -n "$phone" ]; then json_add_string phone "$phone"; fi + if [ -n "$homepage" ]; then json_add_string homepage "$homepage"; fi #ToDo: list of homepages + if [ -n "$note" ]; then json_add_string note "$note"; fi + # ToDo: add location-string + json_close_object + + json_add_object community + json_add_string ssid "$ssid" + json_add_string mesh_network "$mesh_network" + json_add_string owm_api "$uci_owm_api" + json_add_string name "$com_name" + json_add_string homepage "$com_homepage" + json_add_string longitude "$com_longitude" + json_add_string latitude "$com_latitude" + json_add_string ssid_scheme "$com_ssid_scheme" + json_add_string splash_network "$com_splash_network" + json_add_int splash_prefix $com_splash_prefix + json_close_object json_close_object + +# script infos json_add_string type "node" json_add_string script "owm.sh" -json_add_string api_rev "1.0" +json_add_double api_rev $OWM_API_VER + json_add_object system -#FIXME -json_add_array sysinfo -json_add_string "" "system is deprecated" -json_add_string "" "$model" -json_close_array -#FIXME -json_add_array uptime -json_add_int "" $uptime -json_close_array -json_add_array loadavg -#BUG -#json_add_double "" $load1 -#json_add_double "" $load5 -#json_add_double "" $load15 -json_add_string "" $load1 -json_add_string "" $load5 -json_add_string "" $load15 -json_close_array + json_add_array sysinfo + json_add_string "" "system is deprecated" + json_add_string "" "$model" + json_close_array + json_add_array + json_add_int "" $uptime + json_close_array + json_add_array loadavg + json_add_double "" $load5 + json_close_array json_close_object -#TODO + +# OLSR-Config +# That string gets substituted by the olsrd-config-string afterwards json_add_object olsr + json_add_string ipv4Config '$OLSRCONFIG' json_close_object + json_add_array links -IFSORIG="$IFS" -IFS=';' -for i in ${olsr2links} ; do - IFS="$IFSORIG" - set -- $i - json_add_object - json_add_string sourceAddr6 "$1" - json_add_string destAddr6 "$2" - json_add_string id "$3" - #json_add_string quality "$4" - json_add_double quality "$4" - json_close_object - IFS=';' -done -for i in ${olsr4links} ; do - IFS="$IFSORIG" - set -- $i - json_add_object - json_add_string sourceAddr4 "$1" - json_add_string destAddr4 "$2" - json_add_string id "$3" - #json_add_string quality "$4" - json_add_double quality "$4" - json_close_object + IFSORIG="$IFS" IFS=';' -done -for i in ${olsr6links} ; do + for i in ${olsr2links} ; do + IFS="$IFSORIG" + set -- $i + json_add_object + json_add_string sourceAddr6 "$1" + json_add_string destAddr6 "$2" + json_add_string id "$3" + #json_add_string quality "$4" + json_add_double quality "$4" + json_close_object + IFS=';' + done + for i in ${olsr4links} ; do + IFS="$IFSORIG" + set -- $i + json_add_object + json_add_string sourceAddr4 "$1" + json_add_string destAddr4 "$2" + json_add_string id "$3" + #json_add_string quality "$4" + json_add_double quality "$4" + json_close_object + IFS=';' + done + for i in ${olsr6links} ; do + IFS="$IFSORIG" + set -- $i + json_add_object + json_add_string sourceAddr6 "$1" + json_add_string destAddr6 "$2" + json_add_string id "$3" + #json_add_string quality "$4" + json_add_double quality "$4" + json_close_object + IFS=';' + done IFS="$IFSORIG" - set -- $i - json_add_object - json_add_string sourceAddr6 "$1" - json_add_string destAddr6 "$2" - json_add_string id "$3" - #json_add_string quality "$4" - json_add_double quality "$4" - json_close_object - IFS=';' -done -IFS="$IFSORIG" - json_close_array -json_add_string longitude "$longitude" + +# General node info +# Bug in add_double function. Mostly it adds unwanted digits +# but they disappear, if we send stuff to the server +json_add_double latitude $latitude +json_add_double longitude $longitude json_add_string hostname "$hostname" -json_add_string hardware "$system" -json_add_string latitude "$latitude" json_add_int updateInterval 3600 +json_add_string hardware "$system" json_add_object firmware -json_add_string name "$distribution $version" -json_add_string revision "$revision" + json_add_string name "$distribution $version" + json_add_string revision "$revision" json_close_object + json_close_object -json_dump +JSON_STRING=$(json_dump) +#insert json-string from OLSR and repair wrong syntax at string-borders (shell-quotes...) +JSON_STRING=$(echo "$JSON_STRING" | sed -e 's|$OLSRCONFIG|'"$OLSRCONFIG"'|; s|"{|{|; s|}"|}|' ) + +# just print data to stdout, if we have test-run. +if [ "$CMD_1" = "--dry-run" ]; then + printf "%s\n" "$JSON_STRING" + exit 0 +fi + + +################################ +# # +# Send data to openwifimap # +# # +################################ + +#echo $JSON_STRING +# get message lenght for request +LEN=$(echo $JSON_STRING | wc -m) + +MSG="\ +PUT /update_node/$hostname.olsr HTTP/1.1\r +User-Agent: nc/0.0.1\r +Host: api.openwifimap.net\r +Content-type: application/json\r +Content-length: $LEN\r +\r +$JSON_STRING\r\n" + +printf "$MSG" | nc api.openwifimap.net 80 +printf "\n\n"