Developing lightweight computation at the DSG edge

Commit 21d605bf authored by p4u's avatar p4u
Browse files

Add altermundi map agent to draw a centralized network map based on...

Add altermundi map agent to draw a centralized network map based on OpenStreetMaps in http://map.qmp.cat
parent 33ea01af
#
# Copyright (C) 2012 Gui Iribarren
#
# This is free software, licensed under the GNU General Public License v3.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=altermap-agent
PKG_VERSION:=2013061100
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define Package/altermap-agent
SECTION:=admin
CATEGORY:=Administration
TITLE:=Send system information to an AlterMap server
MAINTAINER:=Gui Iribarren <gui@altermundi.net>
DEPENDS:= +curl +jshn
endef
define Package/altermap-agent/description
Small script that collects info (network settings, wifi stations, etc) from a router, and sends it to a map server.
endef
define Build/Compile
endef
define Package/altermap-agent/install
$(INSTALL_DIR) $(1)/
$(CP) ./files/* $(1)/
endef
$(eval $(call BuildPackage,altermap-agent))
#!/bin/sh /etc/rc.common
#
# Copyright © 2012 Routek S.L.
# This is free software, licensed under the GNU General Public License v3.
#
# Original Authors:
# Agusti Moll i Garcia
# Simo Albert i Beltran
# Adapted by:
# Gui Iribarren
# Readapted by:
# p4u
#
START=97
STOP=10
generate_configfile() {
uci import altermap </dev/null
uci batch <<EOF
set altermap.agent=altermap
set altermap.agent.enabled=1
set altermap.agent.server_url=http://map.qmp.cat/altermap
set altermap.agent.network=qMp.cat
commit altermap
EOF
}
start() {
if [ ! -f /etc/config/altermap ]; then
echo "Generate Config File [/etc/config/altermap]."
generate_configfile
fi
[ "$(uci -q get altermap.agent.enabled)" == "1" ] || return
if ! ( grep -q /usr/sbin/altermap-agent /etc/crontabs/root 2>/dev/null ) ; then
echo "0 * * * * /usr/sbin/altermap-agent > /tmp/log/altermap-agent.log" >> /etc/crontabs/root
/etc/init.d/cron enable
/etc/init.d/cron start
fi
DEBUG=1 /usr/sbin/altermap-agent
}
stop() {
sed -r -i -e '\|^([^\t ]+[\t ]+){5}/usr/sbin/altermap-agent > /tmp/log/altermap-agent.log$|d' /etc/crontabs/root
/etc/init.d/cron restart
}
reload() {
start
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /lib/altermap/functions.sh
read_hardware () {
cat /tmp/sysinfo/model
}
read_firmware () {
. /etc/openwrt_release
echo "$DISTRIB_DESCRIPTION"
}
device_json () {
json_init
json_add_string collection "devices"
json_add_string hostname "$(read_hostname)"
json_add_string hardware "$(read_hardware)"
json_add_string firmware "$(read_firmware)"
json_dump
}
update_device () {
node_id="$(get_id node)"
[ -n "$node_id" ] || return
json_load "$(device_json)"
json_add_string node_id "$node_id"
device_id="$(read_device_id)"
if [ -n "$device_id" ] ; then
action=PUT
rev="$(get_rev "$device_id")"
[ -n "$rev" ] \
&& json_add_string _rev "$rev"
else
action=POST
fi
reply="$(query_server $action "/$device_id" "$(json_dump)")"
json_init; json_load "$reply"
[ -n "$DEBUG" ] && json_dump >&2
json_get_vars id ok
[ "$ok" == 1 ] && save_device_id "$id"
}
run_hook () {
update_device
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /lib/altermap/functions.sh
. /lib/json/ieee80211.sh
get_interfaces_json () {
query_server POST /_design/altermap/_view/interfacesByDeviceId \
'{"keys":["'$(read_device_id)'"]}'
}
update_interfaces () {
del_interfaces
add_interfaces
}
del_interfaces () {
device_id="$(read_device_id)"
[ -n "$device_id" ] || return
for id in $(get_ids interfaces) ; do
query_server DELETE "/${id}?rev=$(get_rev $id)"
done
}
add_interfaces () {
device_id="$(read_device_id)"
[ -n "$device_id" ] || return
local IFS=$NEWLINE
for line in $(ieee80211_interfaces_json) ; do
json_init
json_load "$line"
json_add_string device_id "$device_id"
json_add_string collection "interfaces"
json_add_string medium "wireless"
query_server POST / "$(json_dump)"
done
unset IFS
}
run_hook () {
update_interfaces
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /lib/altermap/functions.sh
. /lib/json/ieee80211.sh
get_wifilinks_json () {
query_server POST /_design/altermap/_view/wifilinksByDeviceId \
'{"keys":["'$(read_device_id)'"]}'
}
update_wifilinks () {
del_wifilinks
add_wifilinks
}
del_wifilinks () {
device_id="$(read_device_id)"
[ -n "$device_id" ] || return
for id in $(get_ids wifilinks) ; do
query_server DELETE "/${id}?rev=$(get_rev $id)"
done
}
add_wifilinks () {
device_id="$(read_device_id)"
[ -n "$device_id" ] || return
local IFS=$NEWLINE
for line in $(ieee80211_stations_json) ; do
json_init
json_load "$line"
json_add_string device_id "$device_id"
json_add_string collection "wifilinks"
query_server POST / "$(json_dump)"
done
unset IFS
}
run_hook () {
update_wifilinks
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /usr/share/libubox/jshn.sh
NEWLINE="
"
read_server_url () {
uci -q get altermap.agent.server_url
}
read_network_name () {
uci -q get altermap.agent.network
}
read_hostname () {
uci -q get system.@system[0].hostname
}
read_node_name () {
echo $(read_hostname | sed "s/--.*$//")
}
read_device_id () {
uci -q get altermap.agent.device_id
}
save_device_id () {
uci set altermap.agent.device_id="$1"
uci commit altermap
}
get_network_json () {
query_server POST /_design/altermap/_view/networkByName \
'{"keys":["'$(read_network_name)'"]}'
}
get_node_json () {
query_server POST /_design/altermap/_view/nodeByNetIdAndName \
'{"keys":[["'$(get_id network)'", "'$(read_node_name)'"]]}'
}
get_id () {
get_ids "$@" | head -n 1
}
get_ids () {
local i=0
reply="$(get_${1}_json)" || return
json_init; json_load "$reply"
json_select rows
while i=$(($i+1)) ; do
json_get_type type $i
[ "$type" != "object" ] && break
json_select $i
json_get_vars id
echo "$id"
json_select ..
done
}
get_rev () {
reply="$(query_server GET "/$1")" || return
json_init; json_load "$reply"
json_get_vars _rev
echo "$_rev"
}
query_server () { action="$1" ; url="${2#/}" ; shift 2; query="$@"
[ -n "$DEBUG" ] && echo curl "$action" "/$url" "$query" >&2
curl -s -X $action "$(read_server_url)/$url" \
-H "Content-Type: application/json" \
-d "$query"
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /usr/share/libubox/jshn.sh
ieee80211_interfaces_json () {
ieee80211_dir="/sys/kernel/debug/ieee80211"
for path in $ieee80211_dir/*/netdev\:*/ ; do
eval $(echo $path | sed -r "s|$ieee80211_dir/(.*)/netdev:(.*)/|phy=\1 interface=\2|")
macaddr="$(cat /sys/class/net/$interface/address)"
mode="$(iw $interface info |sed -r "s/.*type (.*)/\1/p;d")"
json_init
json_add_string name $interface
json_add_string phydev $phy
json_add_string macaddr $macaddr
json_add_string mode $mode
json_dump
json_dump >&2
done
}
ieee80211_stations_json () {
ieee80211_dir="/sys/kernel/debug/ieee80211"
for path in $ieee80211_dir/*/netdev\:*/stations/*/last_signal ; do
eval $(echo $path | sed -r "s|$ieee80211_dir/(.*)/netdev:(.*)/stations/(.*)/last_signal|phy=\1 interface=\2 station=\3|")
macaddr="$(cat /sys/class/net/$interface/address)"
signal="$(cat $path)"
channel="$(iw $interface info | sed -r "s/.*channel (\w*) .*/\1/p;d")"
json_init
json_add_string macaddr $macaddr
json_add_string station $station
json_add_object attributes
json_add_string signal $signal
json_add_string channel $channel
json_close_object
json_dump
json_dump >&2
done
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /usr/share/libubox/jshn.sh
interfaces_json () {
net_dir="/sys/class/net"
for interface in $(cd $net_dir; ls -d * 2>/dev/null) ; do
json_init
json_add_object "$phy"
json_load "$(ubus call network.device status '{ "name": "'$phy'" }')"
json_close_object
json_dump
done
}
#!/bin/sh
# Copyleft 2012 Gui Iribarren <gui@altermundi.net>
# This is free software, licensed under the GNU General Public License v3.
. /lib/altermap/functions.sh
agent_scripts="/lib/altermap/agent/"
for hook in $agent_scripts/* ; do
[ -x "$hook" ] && ( . $hook ; run_hook )
done
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment