Thursday, January 27, 2011

collectd-mod-exec Part 5

Part 1
Part 2
Part 3
Part 4

In this final part I'm publishing a real world example of the collectd-exec usage - weather info collecting script.
I'm using yahoo weather api to get weather information because it sends back very concise xml formatted text file which is only 2k in size. The weather info is updated once per hour so I've added special logic to retrieve it once per hour and feed same values in between.

root@Alix:~# cat /mnt/sd/bin/wetterMunich.sh
#!/bin/sh

logger "*** wetter script has started ***"
HOST=$COLLECTD_HOSTNAME
INTERVAL=$COLLECTD_INTERVAL
CITYID=676757       # city WOEID
TUNITS=c            # c for cilsius, f for fahrenheit

URL="http://weather.yahooapis.com/forecastrss?w=$CITYID&u=$TUNITS"
DATAFILE="/tmp/wetterMunich"
 
# for debuging
[ -z "$INTERVAL" ] && INTERVAL=5
INTERVAL=$(awk -v i=$INTERVAL 'BEGIN{print int(i)}')

RESCAN=1
while sleep $INTERVAL; do
  [ -s "$DATAFILE" ] && [ "`date +%F%H`" != "`date -r "$DATAFILE" +%F%H`" ] && rm -f "$DATAFILE"
  [ ! -s "$DATAFILE" ] && wget -q -O "$DATAFILE" "$URL" && RESCAN=1

  if [ -s "$DATAFILE" ] && [ $RESCAN == 1 ]; then
    TEMPER=`sed -n -r 's/.*yweather:condition.*temp="([^"]*?).*/\1/p' "$DATAFILE"`
    WCODE=`sed -n -r 's/.*yweather:condition.*code="([^"]*?).*/\1/p' "$DATAFILE"`
    HUMID=`sed -r -n 's/.*yweather:atmosphere.*humidity="([^"]*?).*/\1/p' $DATAFILE`
    PRESS=`sed -r -n 's/.*yweather:atmosphere.*pressure="([^"]*?).*/\1/p' $DATAFILE`
    WINDDIR=`sed -r -n 's/.*yweather:wind.*direction="([^"]*?).*/\1/p' $DATAFILE`
    WINDSPD=`sed -r -n 's/.*yweather:wind.*speed="([^"]*?).*/\1/p' $DATAFILE`
    RESCAN=0
  fi

  echo "PUTVAL \"$HOST/exec-wetter_munich/temperature\" interval=$INTERVAL N:$TEMPER"
  echo "PUTVAL \"$HOST/exec-wetter_munich/weathercode\" interval=$INTERVAL N:$WCODE"
  echo "PUTVAL \"$HOST/exec-wetter_munich/humidity\" interval=$INTERVAL N:$HUMID"
  echo "PUTVAL \"$HOST/exec-wetter_munich/pressure\" interval=$INTERVAL N:$PRESS"
  echo "PUTVAL \"$HOST/exec-wetter_munich/winddirection\" interval=$INTERVAL N:$WINDDIR"
  echo "PUTVAL \"$HOST/exec-wetter_munich/windspeed\" interval=$INTERVAL N:$WINDSPD"
done


The only parameter to change is the WOEID. Google the WOEID for the place you are interested in or you can can try this site http://sigizmund.info/woeidinfo/?woeid=texas

Script produces the following if started from the command line
root@Alix:~# /mnt/sd/bin/wetterMunich.sh
PUTVAL "/exec-wetter_munich/temperature" interval=5 N:-3
PUTVAL "/exec-wetter_munich/weathercode" interval=5 N:27
PUTVAL "/exec-wetter_munich/humidity" interval=5 N:71
PUTVAL "/exec-wetter_munich/pressure" interval=5 N:1027.3
PUTVAL "/exec-wetter_munich/winddirection" interval=5 N:290
PUTVAL "/exec-wetter_munich/windspeed" interval=5 N:6.44
^C

The temperature and humidity data types are already registered in the types.db but you still need to register new types for pressure, wind direction and wind speed. How to do this is explained in the Part 3

As you can see with only very simple scripts it is possible to collect interesting data and also present it the way you want.

Hope to hear from you if this HowTo was useful to read and also share examples of how you use 'collected' knowledge.

No comments: