From 44aa29d933782beccdce472a56df36c9e8df975b Mon Sep 17 00:00:00 2001 From: floh1111 Date: Wed, 27 Jul 2011 19:31:21 +0000 Subject: [PATCH] Add configurator --- .../default/etc/config/configurator | 29 ++++ root_file_system/default/etc/configurator.sh | 152 ++++++++++++++++++ root_file_system/default/etc/nodewatcher.sh | 18 +-- 3 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 root_file_system/default/etc/config/configurator create mode 100644 root_file_system/default/etc/configurator.sh diff --git a/root_file_system/default/etc/config/configurator b/root_file_system/default/etc/config/configurator new file mode 100644 index 0000000..1c3c7bf --- /dev/null +++ b/root_file_system/default/etc/config/configurator @@ -0,0 +1,29 @@ + +config 'script' + option 'error_level' '2' + option 'logfile' '/var/log/configurator.log' + option 'version' '1' + option 'sync_hostname' '1' + +config 'api' + option 'ipv4_address' '1' + option 'ipv6_interface' 'br-mesh' + option 'ipv6_address' 'fe80::201:2ff:fe03:405' + option 'timeout' '3' + option 'retry' '3' + +config 'update' + option 'autoupdate' '1' + +config 'crawl' + option 'method' 'hash' + option 'nickname' '1' + option 'password' '1' + option 'login_string' '1' + option 'router_id' '1' + option 'update_hash' '1' + +config 'network' + option 'mesh_interface' 'br-mesh' + option 'client_interfaces' 'ath0' + diff --git a/root_file_system/default/etc/configurator.sh b/root_file_system/default/etc/configurator.sh new file mode 100644 index 0000000..3d040f7 --- /dev/null +++ b/root_file_system/default/etc/configurator.sh @@ -0,0 +1,152 @@ +#!/bin/sh +# Netmon Configurator (C) 2010-2011 Freifunk Oldenburg +# Lizenz: GPL + +SCRIPT_DIR=`dirname $0` + +if [ -f /etc/config/configurator ];then + API_IPV4_ADRESS=`uci get configurator.@api[0].ipv4_address` + API_IPV6_ADRESS=`uci get configurator.@api[0].ipv6_address` + API_IPV6_INTERFACE=`uci get configurator.@api[0].ipv6_interface` + API_TIMEOUT=`uci get configurator.@api[0].timeout` + API_RETRY=`uci get configurator.@api[0].retry` + SCRIPT_VERSION=`uci get configurator.@script[0].version` + SCRIPT_ERROR_LEVEL=`uci get configurator.@script[0].error_level` + SCRIPT_LOGFILE=`uci get configurator.@script[0].logfile` + SCRIPT_SYNC_HOSTNAME=`uci get configurator.@script[0].sync_hostname` + CRAWL_METHOD=`uci get configurator.@crawl[0].method` + CRAWL_ROUTER_ID=`uci get configurator.@crawl[0].router_id` + CRAWL_UPDATE_HASH=`uci get configurator.@crawl[0].update_hash` + CRAWL_NICKNAME=`uci get configurator.@crawl[0].nickname` + CRAWL_PASSWORD=`uci get configurator.@crawl[0].password` + UPDATE_AUTOUPDATE=`uci get configurator.@update[0].autoupdate` + MESH_INTERFACE=`uci get configurator.@network[0].mesh_interface` + CLIENT_INTERFACES=`uci get configurator.@network[0].client_interfaces` +else + . $SCRIPT_DIR/configurator_config +fi + +API_RETRY=$(($API_RETRY - 1)) + +get_url() { + if [[ $API_IPV4_ADRESS != "1" ]]; then + url=$API_IPV4_ADRESS + else + url="[$API_IPV6_ADRESS"%"$API_IPV6_INTERFACE]" + fi + echo $url +} + +sync_hostname() { + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Syncing hostname" >> $SCRIPT_LOGFILE + fi + netmon_api=`get_url` + command="wget -q -O - http://$netmon_api/api_csv_configurator.php?section=get_hostname&authentificationmethod=$CRAWL_METHOD&nickname=$CRAWL_NICKNAME&password=$CRAWL_PASSWORD&router_auto_update_hash=$CRAWL_UPDATE_HASH&router_id=$CRAWL_ROUTER_ID" + api_return=`$command&sleep $API_TIMEOUT; kill $!` + netmon_hostname=`echo $api_return| cut '-d,' -f2` + if [ "$netmon_hostname" != "" ]; then + if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Setze neuen Hostname: $netmon_hostname" >> $SCRIPT_LOGFILE + fi + uci set system.@system[0].hostname=$netmon_hostname + uci commit + echo $netmon_hostname > /proc/sys/kernel/hostname + else + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Hostname ist aktuell" >> $SCRIPT_LOGFILE + fi + fi + fi +} + +assign_router() { + netmon_api=`get_url` + hostname=`cat /proc/sys/kernel/hostname` + + #Choose right login String + login_strings="$(ifconfig br-mesh | grep HWaddr | awk '{ print $5 }'|sed -e 's/://g');$(ifconfig eth0 | grep HWaddr | awk '{ print $5 }'|sed -e 's/://g');$(ifconfig ath0 | grep HWaddr | awk '{ print $5 }'|sed -e 's/://g')" + command="wget -q -O - http://$netmon_api/api_csv_configurator.php?section=test_login_strings&login_strings=$login_strings" + ergebnis=`$command&sleep $API_TIMEOUT; kill $!` + if [ `echo $ergebnis| cut '-d;' -f1` = "success" ]; then + router_auto_assign_login_string=`echo $ergebnis| cut '-d;' -f2` + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Es existiert ein Router mit dem Login String $router_auto_assign_login_string" >> $SCRIPT_LOGFILE + fi + elif [ `echo $ergebnis| cut '-d;' -f1` = "error" ]; then + router_auto_assign_login_string=`echo $login_strings| cut '-d;' -f1` + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Es existiert kein Router mit einem der Login Strings: $login_strings" >> $SCRIPT_LOGFILE + echo "`date`: Nutze $router_auto_assign_login_string als login string" >> $SCRIPT_LOGFILE + fi + fi + + #Try to assign Router with choosen login string + command="wget -q -O - http://$netmon_api/api_csv_configurator.php?section=router_auto_assign&router_auto_assign_login_string=$router_auto_assign_login_string&hostname=$hostname" + ergebnis=`$command&sleep $API_TIMEOUT; kill $!` + if [ `echo $ergebnis| cut '-d;' -f1` != "success" ]; then + if [ `echo $ergebnis| cut '-d;' -f2` = "already_assigned" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "0" ]; then + echo "`date`: Der Login String `echo $ergebnis| cut '-d;' -f3` ist bereits mit einem Router verknüpft" >> $SCRIPT_LOGFILE + fi + elif [ `echo $ergebnis| cut '-d;' -f2` = "autoassign_not_allowed" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "0" ]; then + echo "`date`: Der dem Login String `echo $ergebnis| cut '-d;' -f3` zugewiesene Router erlaubt autoassign nicht" >> $SCRIPT_LOGFILE + fi + elif [ `echo $ergebnis| cut '-d;' -f2` = "new_not_assigned" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "0" ]; then + echo "`date`: Router wurde der Liste der nicht zugewiesenen Router hinzugefügt" >> $SCRIPT_LOGFILE + fi + elif [ `echo $ergebnis| cut '-d;' -f2` = "updated_not_assigned" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "0" ]; then + echo "`date`: Router auf der Liste der nicht zugewiesenen Router wurde geupdated" >> $SCRIPT_LOGFILE + fi + fi + if [ $SCRIPT_ERROR_LEVEL -gt "0" ]; then + echo "`date`: Der Router wurde nicht mit Netmon verknüpft" >> $SCRIPT_LOGFILE + fi + elif [ `echo $ergebnis| cut '-d;' -f1` = "success" ]; then + #write new config + uci set configurator.@crawl[0].router_id=`echo $ergebnis| cut '-d;' -f2` + uci set configurator.@crawl[0].update_hash=`echo $ergebnis| cut '-d;' -f3` + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Der Router wurde mit Netmon verknüpft" >> $SCRIPT_LOGFILE + fi + uci commit + + CRAWL_METHOD=`uci get configurator.@crawl[0].method` + CRAWL_ROUTER_ID=`uci get configurator.@crawl[0].router_id` + CRAWL_UPDATE_HASH=`uci get configurator.@crawl[0].update_hash` + CRAWL_NICKNAME=`uci get configurator.@crawl[0].nickname` + CRAWL_PASSWORD=`uci get configurator.@crawl[0].password` + fi +} + +if [ $CRAWL_METHOD == "login" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Authentifizierungsmethode ist: Username und Passwort" >> $SCRIPT_LOGFILE + fi +elif [ $CRAWL_METHOD == "hash" ]; then + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Authentifizierungsmethode ist: Autoassign und Hash" >> $SCRIPT_LOGFILE + echo "`date`: Prüfe ob Roter schon mit Netmon verknüpft ist" >> $SCRIPT_LOGFILE + fi + if [ $CRAWL_UPDATE_HASH == "1" ]; then + can_crawl=0 + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Der Router ist noch NICHT mit Netmon verknüpft" >> $SCRIPT_LOGFILE + echo "`date`: Versuche verknüpfung herzustellen" >> $SCRIPT_LOGFILE + fi + assign_router + else + if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then + echo "`date`: Der Router ist bereits mit Netmon verknüpft" >> $SCRIPT_LOGFILE + fi + fi +fi + +#Sync Hostname +if [[ $SCRIPT_SYNC_HOSTNAME = "1" ]]; then + sync_hostname +fi \ No newline at end of file diff --git a/root_file_system/default/etc/nodewatcher.sh b/root_file_system/default/etc/nodewatcher.sh index c2f5b92..a4fb35b 100755 --- a/root_file_system/default/etc/nodewatcher.sh +++ b/root_file_system/default/etc/nodewatcher.sh @@ -418,19 +418,19 @@ crawl() { echo $DATA > /tmp/node.data if [[ $SCRIPT_SYNC_HOSTNAME = "1" ]]; then netmon_hostname="`echo $api_return | cut '-d;' -f2`" - if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then - if [ $error_level -gt "1" ]; then - echo "`date`: Setze neuen Hostname (Hostname synchronisation)" >> $logfile - fi - uci set system.@system[0].hostname=$netmon_hostname - uci commit - echo $netmon_hostname > /proc/sys/kernel/hostname + if [ "$netmon_hostname" != "" ]; then + if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then + if [ $error_level -gt "1" ]; then + echo "`date`: Setze neuen Hostname (Hostname synchronisation)" >> $logfile + fi + uci set system.@system[0].hostname=$netmon_hostname + uci commit + echo $netmon_hostname > /proc/sys/kernel/hostname + fi fi fi } - - LANG=C SCRIPT_DIR=`dirname $0`