Nodewatcher optimisation

The nodewatcher.sh got a diet. :)

Stats from DIR300

Old nodewatcher:
real    1m 43.86s
user    0m 5.59s
sys 0m 32.78s
Memory consumption.. over ~1.5MB

New nodewatcher:
real    0m 17.17s
user    0m 0.99s
sys 0m 4.33s
Memory consumption.. below ~0.5MB

Signed-off-by: Tim Niemeyer <tim.niemeyer@mastersword.de>
This commit is contained in:
Tim Niemeyer 2013-01-31 22:43:38 +01:00
parent 16fd33e7c7
commit 239234c0fa
1 changed files with 133 additions and 177 deletions

View File

@ -2,10 +2,11 @@
# Netmon Nodewatcher (C) 2010-2012 Freifunk Oldenburg
# License; GPL v3
SCRIPT_VERSION="30"
#Get the configuration from the uci configuration file
#If it does not exists, then get it from a normal bash file with variables.
if [ -f /etc/config/nodewatcher ];then
SCRIPT_VERSION=`uci get nodewatcher.@script[0].version`
SCRIPT_ERROR_LEVEL=`uci get nodewatcher.@script[0].error_level`
SCRIPT_LOGFILE=`uci get nodewatcher.@script[0].logfile`
SCRIPT_DATA_FILE=`uci get nodewatcher.@script[0].data_file`
@ -15,14 +16,22 @@ else
. `dirname $0`/nodewatcher_config
fi
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
err() {
echo $1 >> $SCRIPT_LOGFILE
}
else
err() {
:
}
fi
#this method checks id the logfile has bekome too big and deletes the first X lines
delete_log() {
if [ -f $SCRIPT_LOGFILE ]; then
if [ `ls -la $SCRIPT_LOGFILE | awk '{ print $5 }'` -gt "6000" ]; then
sed -i '1,60d' $SCRIPT_LOGFILE
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Logfile has been made smaller" >> $SCRIPT_LOGFILE
fi
err "`date`: Logfile has been made smaller"
fi
fi
}
@ -30,195 +39,146 @@ delete_log() {
#this method generates the crawl data xml file that is beeing fetched by netmon
#and provided by a small local httpd
crawl() {
#Get system data from UCI
if which uci >/dev/null; then
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: UCI is installed, trying to collect extra data from UCI" >> $SCRIPT_LOGFILE
fi
location="`uci get freifunk.contact.location`"
latitude="`uci get system.@system[0].latitude`"
longitude="`uci get system.@system[0].longitude`"
community_essid="`uci get freifunk.community.ssid`"
community_nickname="`uci get freifunk.contact.nickname`"
community_email="`uci get freifunk.contact.mail`"
community_prefix="`uci get freifunk.community.prefix`"
description="`uci get freifunk.contact.note`"
fi
#Get system data from LUA
if which lua >/dev/null; then
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: LUA is installed, trying to collect extra data from LUA" >> $SCRIPT_LOGFILE
fi
luciname=`lua -l luci.version -e 'print(luci.version.luciname)'`
lucversion=`lua -l luci.version -e 'print(luci.version.luciversion)'`
fi
#Get system data from other locations
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Collecting basic system status data" >> $SCRIPT_LOGFILE
fi
hostname="`cat /proc/sys/kernel/hostname`"
uptime=`cat /proc/uptime | awk '{ print $1 }'`
idletime=`cat /proc/uptime | awk '{ print $2 }'`
err "`date`: Collecting basic system status data"
hostname="$(cat /proc/sys/kernel/hostname)"
uptime=$(awk '{ printf "<uptime>"$1"</uptime><idletime>"$2"</idletime>" }' /proc/uptime)
memory_total=`cat /proc/meminfo | grep 'MemTotal' | awk '{ print $2 }'`
memory_caching=`cat /proc/meminfo | grep -m 1 'Cached:' | awk '{ print $2 }'`
memory_buffering=`cat /proc/meminfo | grep 'Buffers' | awk '{ print $2 }'`
memory_free=`cat /proc/meminfo | grep 'MemFree' | awk '{ print $2 }'`
cpu=`grep -m 1 "cpu model" /proc/cpuinfo | cut -d ":" -f 2`
if [ -n $cpu ]; then
cpu=`grep -m 1 "model name" /proc/cpuinfo | cut -d ":" -f 2`
fi
chipset=`grep -m 1 "system type" /proc/cpuinfo | cut -d ":" -f 2`
memory=$(awk '
/^MemTotal/ { printf "<memory_total>"$2"</memory_total>" }
/^Cached:/ { printf "<memory_caching>"$2"</memory_caching>" }
/^Buffers/ { printf "<memory_buffering>"$2"</memory_buffering>" }
/^MemFree/ { printf "<memory_free>"$2"</memory_free>" }
' /proc/meminfo)
cpu=$(awk -F': ' '
/model/ { printf "<cpu>"$2"</cpu>" }
/system type/ { printf "<chipset>"$2"</chipset>" }
' /proc/cpuinfo)
local_time="`date +%s`"
processes=`cat /proc/loadavg | awk '{ print $4 }'`
loadavg=`cat /proc/loadavg | awk '{ print $1 }'`
load=$(awk '{ printf "<loadavg>"$3"</loadavg><processes>"$4"</processes>" }' /proc/loadavg)
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Collecting version information" >> $SCRIPT_LOGFILE
fi
if which batctl >/dev/null; then
batctl_adv_version=`batctl -v | awk '{ print $2 }'`
batman_adv_version=`batctl o -n|head -n1|awk '{ print $3 }'|sed 's/,//'`
fi
kernel_version=`uname -r`
err "`date`: Collecting version information"
batman_adv_version=$(cat /sys/module/batman_adv/version)
kernel_version=$(uname -r)
nodewatcher_version=$SCRIPT_VERSION
openwrt_version_file="/etc/openwrt_release"
if [ -f $openwrt_version_file ]; then
. $openwrt_version_file
# example for /etc/openwrt_release:
#DISTRIB_ID="OpenWrt"
#DISTRIB_RELEASE="Attitude Adjustment"
#DISTRIB_REVISION="r35298"
#DISTRIB_CODENAME="attitude_adjustment"
#DISTRIB_TARGET="atheros/generic"
#DISTRIB_DESCRIPTION="OpenWrt Attitude Adjustment 12.09-rc1"
. /etc/openwrt_release
distname=$DISTRIB_ID
distversion=$DISTRIB_RELEASE
distname=$DISTRIB_ID
distversion=$DISTRIB_RELEASE
fi
# example for /etc/firmware_release:
#FIRMWARE_VERSION="95f36685e7b6cbf423f02cf5c7f1e785fd4ccdae-dirty"
#RELEASE_DATE="build date: Di 29. Jan 19:33:34 CET 2013"
#FIRMWARE_REVISION="build date: Di 29. Jan 19:33:34 CET 2013"
#OPENWRT_CORE_REVISION="35298"
#OPENWRT_FEEDS_PACKAGES_REVISION="35298"
. /etc/firmware_release
SYSTEM_DATA="<status>online</status><hostname>$hostname</hostname><distname>$distname</distname><distversion>$distversion</distversion>$cpu$memory$load$uptime<local_time>$local_time</local_time><batman_advanced_version>$batman_adv_version</batman_advanced_version><kernel_version>$kernel_version</kernel_version><nodewatcher_version>$nodewatcher_version</nodewatcher_version><firmware_version>$FIRMWARE_VERSION</firmware_version><firmware_revision>$FIRMWARE_REVISION</firmware_revision><openwrt_core_revision>$OPENWRT_CORE_REVISION</openwrt_core_revision><openwrt_feeds_packages_revision>$OPENWRT_FEEDS_PACKAGES_REVISION</openwrt_feeds_packages_revision>"
firmware_version_file="/etc/firmware_release"
if [ -f $firmware_version_file ]; then
. $firmware_version_file
fi
err "`date`: Collecting information from network interfaces"
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Collecting information from network interfaces" >> $SCRIPT_LOGFILE
fi
#Get interfaces
IFACES=`cat /proc/net/dev | awk -F: '!/\|/ { gsub(/[[:space:]]*/, "", $1); split($2, a, " "); printf("%s=%s=%s ", $1, a[1], a[9]) }'`
#IFACES=`cat /proc/net/dev | awk -F: '!/\|/ { gsub(/[[:space:]]*/, "", $1); split($2, a, " "); printf("%s=%s=%s ", $1, a[1], a[9]) }'`
interface_data=""
#Loop interfaces
for entry in $IFACES; do
iface=`echo $entry | cut -d '=' -f 1`
rcv=`echo $entry | cut -d '=' -f 2`
xmt=`echo $entry | cut -d '=' -f 3`
#for entry in $IFACES; do
for filename in `grep up /sys/class/net/*/operstate`; do
ifpath=${filename%/operstate*}
iface=${ifpath#/sys/class/net/}
if [ "$iface" = "lo" ]; then
continue
fi
wlan_mode=""
wlan_bssid=""
wlan_essid=""
wlan_frequency=""
wlan_tx_power=""
if [ "$iface" != "lo" ]; then
if [ "`ifconfig ${iface} | grep UP`" != "" ]; then
#Get interface data
name="${iface}"
mac_addr="`ifconfig ${iface} | grep 'HWaddr' | awk '{ print $5}'`"
ipv4_addr="`ifconfig ${iface} | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`"
ipv6_addr="`ifconfig ${iface} | grep 'inet6 addr:' | grep 'Scope:Global' | awk '{ print $3}'`"
ipv6_link_local_addr="`ifconfig ${iface} | grep 'inet6 addr:' | grep 'Scope:Link' | awk '{ print $3}'`"
mtu="`ifconfig ${iface} | grep 'MTU' | cut -d: -f2 | awk '{ print $1}'`"
traffic_rx="$rcv"
traffic_tx="$xmt"
interface_data=$interface_data"<$name><name>$name</name><mac_addr>$mac_addr</mac_addr><ipv4_addr>$ipv4_addr</ipv4_addr><ipv6_addr>$ipv6_addr</ipv6_addr><ipv6_link_local_addr>$ipv6_link_local_addr</ipv6_link_local_addr><traffic_rx>$traffic_rx</traffic_rx><traffic_tx>$traffic_tx</traffic_tx><mtu>$mtu</mtu>"
if [ "`iwconfig ${iface} 2>/dev/null | grep Frequency | awk '{ print $2 }' | cut -d ':' -f 2`" != "" ]; then
wlan_mode="`iwconfig ${iface} 2>/dev/null | grep 'Mode' | awk '{ print $1 }' | cut -d ':' -f 2`"
if [ $wlan_mode = "Master" ]; then
wlan_bssid="`iwconfig ${iface} 2>/dev/null | grep 'Access Point' | awk '{ print $6 }'`"
elif [ $wlan_mode = "Ad-Hoc" ]; then
wlan_bssid="`iwconfig ${iface} 2>/dev/null | grep Cell | awk '{ print $5 }'`"
fi
wlan_essid="`iwconfig ${iface} 2>/dev/null | grep ESSID | awk '{ split($4, a, \"\\"\"); printf(\"%s\", a[2]); }'`"
wlan_frequency="`iwconfig ${iface} 2>/dev/null | grep Frequency | awk '{ print $2 }' | cut -d ':' -f 2`"
wlan_tx_power="`iwconfig ${iface} 2>/dev/null | grep 'Tx-Power' | awk '{ print $4 }' | cut -d ':' -f 2`"
interface_data=$interface_data"<wlan_mode>$wlan_mode</wlan_mode><wlan_frequency>$wlan_frequency</wlan_frequency><wlan_essid>$wlan_essid</wlan_essid><wlan_bssid>$wlan_bssid</wlan_bssid><wlan_tx_power>$wlan_tx_power</wlan_tx_power>"
fi
interface_data=$interface_data"</$name>"
fi
fi
#Get interface data
addrs="$(ip addr show dev ${iface} | awk '
/ether/ { printf "<mac_addr>"$2"</mac_addr>" }
/inet / { split($2, a, "/"); printf "<ipv4_addr>"a[1]"</ipv4_addr>" }
/inet6/ && /scope global/ { printf "<ipv6_addr>"$2"</ipv6_addr>" }
/inet6/ && /scope link/ { printf "<ipv6_link_local_addr>"$2"</ipv6_link_local_addr>"}
/mtu/ { printf "<mtu>"$5"</mtu>" }
')"
#mac_addr="`cat $ifpath/address`"
#ipv4_addr="`ip addr show dev ${iface} | awk '/inet / { split($2, a, "/"); print a[1] }'`"
#ipv6_addr="`ip addr show dev ${iface} scope global | awk '/inet6/ { print $2 }'`"
#ipv6_link_local_addr="`ip addr show dev ${iface} scope link | awk '/inet6/ { print $2 }'`"
#mtu=`cat $ifpath/mtu`
traffic_rx=`cat $ifpath/statistics/rx_bytes`
traffic_tx=`cat $ifpath/statistics/tx_bytes`
#interface_data=$interface_data"<$iface><name>$iface</name><mac_addr>$mac_addr</mac_addr><ipv4_addr>$ipv4_addr</ipv4_addr><ipv6_addr>$ipv6_addr</ipv6_addr><ipv6_link_local_addr>$ipv6_link_local_addr</ipv6_link_local_addr><traffic_rx>$traffic_rx</traffic_rx><traffic_tx>$traffic_tx</traffic_tx><mtu>$mtu</mtu>"
interface_data=$interface_data"<$iface><name>$iface</name>$addrs<traffic_rx>$traffic_rx</traffic_rx><traffic_tx>$traffic_tx</traffic_tx>"
interface_data=$interface_data$(iwconfig ${iface} 2>/dev/null | awk -F':' '
/Mode/{ split($2, m, " "); printf "<wlan_mode>"m[1]"</wlan_mode>" }
/Cell/{ split($0, c, " "); printf "<wlan_bssid>"c[5]"</wlan_bssid>" }
/ESSID/ { split($0, e, "\""); printf "<wlan_essid>"e[2]"</wlan_essid>" }
/Freq/{ split($3, f, " "); printf "<wlan_frequency>"f[1]f[2]"</wlan_frequency>" }
/Tx-Power/{ split($0, p, "="); sub(/[[:space:]]*$/, "", p[2]); printf "<wlan_tx_power>"p[2]"</wlan_tx_power>" }
')"</$iface>"
#if [ "`iwconfig ${iface} 2>/dev/null | grep IEEE`" != "" ]; then
# wlan_mode="`iwconfig $iface | awk -F':' '/Mode/{ split($2, m, " "); print m[1] }'`"
#
# if [ $wlan_mode = "Master" ]; then
# wlan_bssid="`iw $iface info | awk '/addr/{ print $2 }'`"
# elif [ $wlan_mode = "Ad-Hoc" ]; then
# wlan_bssid="`iwconfig $iface | awk '/Cell/{ print $5 }'`"
# fi
#
# wlan_essid="`iwconfig ${iface} | awk -F'"' '/ESSID/ { print $2 }'`"
# wlan_frequency="`iwconfig $iface | awk -F':' '/Freq/{ split($3, f, " "); print f[1] }'`"
# wlan_tx_power="`iwconfig $iface | awk -F'=' '/Tx-Power/{ print $2 }'`"
#
# interface_data=$interface_data"<wlan_mode>$wlan_mode</wlan_mode><wlan_frequency>$wlan_frequency</wlan_frequency><wlan_essid>$wlan_essid</wlan_essid><wlan_bssid>$wlan_bssid</wlan_bssid><wlan_tx_power>$wlan_tx_power</wlan_tx_power>"
#fi
#interface_data=$interface_data"</$iface>"
done
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Collecting information from batman advanced and it´s interfaces" >> $SCRIPT_LOGFILE
fi
err "`date`: Collecting information from batman advanced and it´s interfaces"
#B.A.T.M.A.N. advanced
if which batctl >/dev/null; then
batman_check_running=`batctl if | grep 'Error'`
if [ "$batman_check_running" == "" ]; then
has_active_interface="0"
BAT_ADV_IFACES=`batctl if | awk '{ print $1 }' | cut -d ':' -f 1`
for device_name in $BAT_ADV_IFACES; do
if [ "`batctl if | grep $device_name | grep active`" != "" ]; then
status='active'
has_active_interface="1"
else
status='inactive'
fi
if [ -f /sys/module/batman_adv/version ]; then
for iface in $(grep active /sys/class/net/*/batman_adv/iface_status); do
status=${iface#*:}
iface=${iface%/batman_adv/iface_status:active}
iface=${iface#/sys/class/net/}
BATMAN_ADV_INTERFACES=$BATMAN_ADV_INTERFACES"<$iface><name>$iface</name><status>$status</status></$iface>"
done
BATMAN_ADV_INTERFACES=$BATMAN_ADV_INTERFACES"<$device_name><name>$device_name</name><status>$status</status></$device_name>"
done
if [ $has_active_interface = "1" ]; then
BAT_ADV_ORIGINATORS=`batctl o -n | grep 'No batman nodes in range'`
if [ "$BAT_ADV_ORIGINATORS" == "" ]; then
OLDIFS=$IFS
IFS="
"
BAT_ADV_ORIGINATORS=`batctl o -n | awk '/O/ {next} /B/ {next} {print}'`
count=0;
for row in $BAT_ADV_ORIGINATORS; do
row="${row//[\(\)]/}"
originator=`echo $row | awk '{print $1}'`
last_seen=`echo $row | awk '{print $2}'`
last_seen="${last_seen//s/}"
link_quality=`echo $row | awk '{print $3}'`
outgoing_interface=`echo $row | cut -d] -f1 | cut -d[ -f2`
outgoing_interface="${outgoing_interface// /}"
nexthop=`echo $row | awk '{print $4}'`
batman_adv_originators=$(awk \
'BEGIN { FS=" "; i=0 }
/O/ { next }
/B/ { next }
{ sub("\\(", "", $0)
sub("\\)", "", $0)
sub("\\[", "", $0)
sub("\\]:", "", $0)
sub(" ", " ", $0)
printf "<originator_"i"><originator>"$1"</originator><link_quality>"$3"</link_quality><nexthop>"$4"</nexthop><last_seen>"$2"</last_seen><outgoing_interface>"$5"</outgoing_interface></originator_"i">"
i++
}' /sys/kernel/debug/batman_adv/bat0/originators)
fi
batman_adv_originators=$batman_adv_originators"<originator_$count><originator>$originator</originator><link_quality>$link_quality</link_quality><nexthop>$nexthop</nexthop><last_seen>$last_seen</last_seen><outgoing_interface>$outgoing_interface</outgoing_interface></originator_$count>"
count=`expr $count + 1`
done
IFS=$OLDIFS
fi
fi
fi
fi
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Collecting information about conected clients" >> $SCRIPT_LOGFILE
fi
err "`date`: Collecting information about conected clients"
#CLIENTS
SEDDEV=`brctl showstp $MESH_INTERFACE | egrep '\([0-9]\)' | sed -e "s/(//;s/)//" | awk '{ print "s/^ "$2"/"$1"/;" }'`
SEDDEV=$(brctl showstp $MESH_INTERFACE | awk '/\([0-9]\)/ {
sub("\\(", "", $0)
sub("\\)", "", $0)
print "s/^ "$2"/"$1"/;"
}')
for entry in $CLIENT_INTERFACES; do
CLIENT_MACS=$CLIENT_MACS`brctl showmacs $MESH_INTERFACE | sed -e "$SEDDEV" | awk '{if ($3 != "yes" && $1 == "'"$entry"'") print $2}'`" "
done
client_count=$(brctl showmacs $MESH_INTERFACE | sed -e "$SEDDEV" | egrep -c "(${CLIENT_INTERFACES// /|}).*no")
i=0
for client in $CLIENT_MACS; do
i=`expr $i + 1` #Zähler um eins erhöhen
done
client_count=$i
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Putting all information into a XML-File and save it at "$SCRIPT_DATA_FILE >> $SCRIPT_LOGFILE
fi
SYSTEM_DATA="<status>online</status><hostname>$hostname</hostname><description>$description</description><location>$location</location><latitude>$latitude</latitude><longitude>$longitude</longitude><luciname>$luciname</luciname><luciversion>$luciversion</luciversion><distname>$distname</distname><distversion>$distversion</distversion><chipset>$chipset</chipset><cpu>$cpu</cpu><memory_total>$memory_total</memory_total><memory_caching>$memory_caching</memory_caching><memory_buffering>$memory_buffering</memory_buffering><memory_free>$memory_free</memory_free><loadavg>$loadavg</loadavg><processes>$processes</processes><uptime>$uptime</uptime><idletime>$idletime</idletime><local_time>$local_time</local_time><community_essid>$community_essid</community_essid><community_nickname>$community_nickname</community_nickname><community_email>$community_email</community_email><community_prefix>$community_prefix</community_prefix><batman_advanced_version>$batman_adv_version</batman_advanced_version><kernel_version>$kernel_version</kernel_version><nodewatcher_version>$nodewatcher_version</nodewatcher_version><firmware_version>$FIRMWARE_VERSION</firmware_version><firmware_revision>$FIRMWARE_REVISION</firmware_revision><openwrt_core_revision>$OPENWRT_CORE_REVISION</openwrt_core_revision><openwrt_feeds_packages_revision>$OPENWRT_FEEDS_PACKAGES_REVISION</openwrt_feeds_packages_revision>"
err "`date`: Putting all information into a XML-File and save it at "$SCRIPT_DATA_FILE
DATA="<?xml version='1.0' standalone='yes'?><data><system_data>$SYSTEM_DATA</system_data><interface_data>$interface_data</interface_data><batman_adv_interfaces>$BATMAN_ADV_INTERFACES</batman_adv_interfaces><batman_adv_originators>$batman_adv_originators</batman_adv_originators><client_count>$client_count</client_count></data>"
@ -229,15 +189,11 @@ crawl() {
LANG=C
#Prüft ob das logfile zu groß geworden ist
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Check logfile" >> $SCRIPT_LOGFILE
fi
err "`date`: Check logfile"
delete_log
#Erzeugt die statusdaten
if [ $SCRIPT_ERROR_LEVEL -gt "1" ]; then
echo "`date`: Generate actual status data" >> $SCRIPT_LOGFILE
fi
err "`date`: Generate actual status data"
crawl
exit 0