From 3365ae9c0821c85088c05d6d4420fc12824fc0cc Mon Sep 17 00:00:00 2001 From: foo Date: Sat, 4 Jan 2014 20:39:39 +0000 Subject: [PATCH] working! --- getstations.pl | 12 +++++++++ river_levels_ | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 getstations.pl create mode 100755 river_levels_ diff --git a/getstations.pl b/getstations.pl new file mode 100755 index 0000000..3efaf26 --- /dev/null +++ b/getstations.pl @@ -0,0 +1,12 @@ +use JSON::PP; +$DEBUG = $ENV{DEBUG}; +$CatchmentId = shift @ARGV; + +print "Decoding...\n" if $DEBUG; +$in = decode_json(<>) or die; +foreach $station (@{$in->{data}}) { + if ($station->{url} =~ /CatchmentId=$CatchmentId$/) { + print "$station->{id}", $DEBUG ? ": $station->{station} on $station->{river}\n" : " "; + } +} +print "\n" unless $DEBUG; diff --git a/river_levels_ b/river_levels_ new file mode 100755 index 0000000..8008250 --- /dev/null +++ b/river_levels_ @@ -0,0 +1,68 @@ +#!/bin/sh +# +# Wildcard-script to monitor network interfaces. To monitor an +# interface, link vlan_ to this file. E.g. +# +# ln /usr/share/munin/node/plugins-contrib/vlan_ /etc/munin/node.d/vlan_eth0_1 +# +# ...will monitor eth0.1 +# +# Parameters: +# +# config +# autoconf +# suggest +# +#%# family=manual +#%# capabilities=autoconf suggest + +# URLBASE= + +CACHE=/tmp/river_level_.dat +checkdata () { + if test `find "$CACHE" -mtime +1` + then + curl -o $CACHE -Ss $URLBASE || exit 1 + fi +} + + +getstationname () { + checkdata + NAME=$( perl -MJSON::PP -e ' + open $fh, "/tmp/river_level_.dat" or die $!; + $data_arr = decode_json(<$fh>) or die $!; + my %data; + foreach (@{$data_arr->{data}}) { + $data{$_->{id}} = $_; + } + foreach (@ARGV) { print $data{$_}->{river}, " at ", $data{$_}->{station}, "\n"; } + ' $* ) +} + +STATION=$(basename $0 | sed 's/^river_level_//g' | cut -d_ -f 3) + +if [ "$1" = "autoconf" ]; then + echo "no" + exit 0 +fi + +if [ "$1" = "suggest" ]; then + exit 0 +fi + +if [ "$1" = "config" ]; then + getstationname $STATION + echo "graph_title River Level for $NAME [$STATION]" + echo 'graph_args --base 1000 -l 0 ' + echo 'graph_vlabel station level (metres)' + echo 'graph_category rivers' + echo 'graph_info This graph shows river levels' + echo "level.label $NAME" + echo 'level.info Current river level (metres)' + exit 0 +fi + +curl -sS $URLBASE/$STATION | + perl -MJSON::PP -e '$d=decode_json(<>) or die; print "level.value $d->{data}->{current}->{level}\n";' +