84 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/sh
 | |
| #
 | |
| # (C) Matthew Slowe, 2014
 | |
| #
 | |
| # This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
 | |
| # http://creativecommons.org/licenses/by-sa/4.0/
 | |
| #
 | |
| # 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.
 | |
| #
 | |
| # Parameters:
 | |
| #
 | |
| #   config
 | |
| #
 | |
| #%# family=manual
 | |
| 
 | |
| # 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:
 | |
| 
 | |
| # host$ perl getstations.pl 123 </tmp/river_level_.dat
 | |
| # 23 24 25 26
 | |
| 
 | |
| 
 | |
| CACHE=/tmp/river_level_.dat
 | |
| checkdata () {
 | |
|     if test `find "$CACHE" -mtime +1`
 | |
|     then
 | |
|         curl -o $CACHE -Ss $urlbase || exit 1
 | |
|     fi
 | |
| }
 | |
| 
 | |
| CATCHMENT=$(basename $0 | sed 's/^river_levels_//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
 | |
|     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'
 | |
| 
 | |
|     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};
 | |
|                         $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
 | |
| 
 | |
| 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
 | |
| done
 |