rivers/river_levels_

84 lines
2.3 KiB
Plaintext
Raw Normal View History

2014-01-04 20:39:39 +00:00
#!/bin/sh
#
2014-01-05 10:54:46 +00:00
# (C) Matthew Slowe, 2014
2014-01-04 20:39:39 +00:00
#
2014-01-05 10:54:46 +00:00
# This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
# http://creativecommons.org/licenses/by-sa/4.0/
2014-01-04 20:39:39 +00:00
#
2014-01-05 10:54:46 +00:00
# Wildcard-script to monitor a river system's catchment area. To monitor a
# particular catchment aread, link river_levels_<id> to this file. E.g.
#
# ln /usr/share/munin/node/plugins-contrib/river_levels_ /etc/munin/node.d/river_levels_123
#
# ...will monitor catchment area 123.
2014-01-04 20:39:39 +00:00
#
# Parameters:
#
2014-01-05 10:54:46 +00:00
# config
2014-01-04 20:39:39 +00:00
#
#%# family=manual
2014-01-05 10:54:46 +00:00
# Expects a config file to specify which stations to include in the data check and name the catchment
#
# [river_levels_]
# env.urlbase http://path.to.service/path
#
# [river_levels_123]
# env.stations 23 24 25 26
# env.catchmentname Great River
#
# The station list can be generated by using getstations.pl:
2014-01-04 20:39:39 +00:00
2014-01-05 10:54:46 +00:00
# host$ perl getstations.pl 123 </tmp/river_level_.dat
# 23 24 25 26
2014-01-04 20:39:39 +00:00
2014-01-05 10:54:46 +00:00
CACHE=/tmp/river_level_.dat
checkdata () {
2014-02-01 13:20:18 +00:00
if [ ! -f $CACHE ] || test `find "$CACHE" -mtime +1`
2014-01-05 10:54:46 +00:00
then
curl -o $CACHE -Ss $urlbase || exit 1
2014-01-05 10:54:46 +00:00
fi
2014-01-04 20:39:39 +00:00
}
2014-01-05 10:54:46 +00:00
CATCHMENT=$(basename $0 | sed 's/^river_levels_//g' | cut -d_ -f 3)
2014-01-04 20:39:39 +00:00
if [ "$1" = "autoconf" ]; then
2014-01-05 10:54:46 +00:00
echo "no"
2014-01-04 20:39:39 +00:00
exit 0
fi
if [ "$1" = "suggest" ]; then
exit 0
fi
if [ "$1" = "config" ]; then
2014-01-05 10:54:46 +00:00
echo "graph_title River Levels for $catchmentname [$CATCHMENT]"
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'
2014-01-04 20:39:39 +00:00
2014-01-05 10:54:46 +00:00
checkdata
perl -e '
use JSON::PP;
$CatchmentId = shift @ARGV;
$in = decode_json(<>) or die;
foreach $station (@{$in->{data}}) {
if ($station->{url} =~ /CatchmentId=$CatchmentId$/) {
$id = $station->{id};
$river = $station->{watercourse};
2014-01-05 10:54:46 +00:00
$name = $station->{station};
print "river_level_$id.label $name on $river ($id)\n";
print "river_level_$id.info Current river level (metres)\n";
}
}
' $CATCHMENT <$CACHE
exit 0
fi
2014-01-04 20:39:39 +00:00
2014-01-05 10:54:46 +00:00
for STATION in $stations ; do
curl -m 5 -sS $urlbase/$STATION |
perl -MJSON::PP -e '$id=shift(@ARGV); $d=decode_json(<>) or die "error decoding station $id"; print "river_level_$id.value ", $d->{data}->{current}->{level} || "U", "\n";' $STATION
2014-01-05 10:54:46 +00:00
done