-
Notifications
You must be signed in to change notification settings - Fork 4
/
pegelonline_
executable file
·118 lines (99 loc) · 3.08 KB
/
pegelonline_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl -w
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2015 Alexander Dahl <[email protected]>
use strict;
use warnings;
use utf8; # this script is written in utf-8
use feature qw(say);
use lib $ENV{'MUNIN_LIBDIR'};
use Munin::Plugin;
use Carp;
use Data::Dumper;
use DateTime;
use DateTime::Format::ISO8601;
use Encode::Locale;
use Encode;
use English qw( -no_match_vars );
use HTTP::Request;
use JSON;
use LWP::UserAgent;
use Readonly;
use UUID::Tiny ':std';
Readonly my $AGE_THRESHOLD => 120;
# get uuid from symbolic link filename
my $uuid;
if ( $PROGRAM_NAME =~ /pegelonline_(.+)*$/xms ) {
$uuid = $1;
} else {
croak '$uuid undefined';
}
if ( !is_uuid_string($uuid) ) {
croak "'$uuid' is no valid UUID!";
}
# build url and get it
my $url
= 'http://www.pegelonline.wsv.de/webservices/rest-api/v2/'
. 'stations.json?'
. "ids=${uuid}"
. '&includeTimeseries=true'
. '&includeCurrentMeasurement=true'
. '&prettyprint=false';
my $ua = LWP::UserAgent->new();
my $request = HTTP::Request->new( GET => $url );
$request->accept_decodable;
my $response = $ua->request($request);
my $decoded_response;
if ( $response->is_success() ) {
$decoded_response = $response->decoded_content;
} else {
say {*STDERR} "get '$url' failed: ", $response->status_line;
croak 'could not download data';
}
# decode json
my $stations = decode_json($decoded_response);
# values
my $timeseries = $stations->[0]{timeseries};
my $unit = $timeseries->[0]{unit};
my $current_meas = $timeseries->[0]{currentMeasurement};
my $value = $current_meas->{value};
my $dt = DateTime::Format::ISO8601->parse_datetime(
$current_meas->{timestamp} );
my $now = DateTime->now;
my $dur = $now->subtract_datetime($dt);
if ( $dur->in_units('minutes') > $AGE_THRESHOLD ) {
croak "Value older than $AGE_THRESHOLD minutes ("
. $current_meas->{timestamp}
. '), ignoring it.';
}
my $key = clean_fieldname( 'u' . $uuid );
# config
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
say 'graph_title PEGELONLINE';
say 'graph_args --base 1000 --lower-limit 0';
say 'graph_scale no';
say 'graph_category pegelonline';
say "graph_vlabel ${unit}";
say "${key}.label "
. encode( 'iso-8859-1', $stations->[0]{longname} );
say "${key}.type GAUGE";
say "${key}.min 0";
say "${key}.colour COLOUR1";
say "${key}.warning 600";
say "${key}.critical 700";
say "${key}.draw AREA";
exit 0;
}
say "${key}.value ${value}";
# vim: set ft=perl et sts=0 ts=4 sw=4 sr: