#!/usr/bin/perl -w $|=1 if $ENV{DEBUG}; use strict; use diagnostics; use DBI; use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent("Munin[river_levels_]"); my @stations = qw( 1129 1135 1133 1143 1132 1137 1134 1145 1139 1131 1130 1144 1086 1085 1073 1091 1071 1076 1069 1066 1067 1072 1074 1083 1064 1090 1089 1088 1087 1078 1080 1063 1030 1120 1121 1122 1125 1126 1127 6074 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6246 6247 6249 6250 6251 6253 6254 6255 6257 6259 6260 6262 6265 6267 6270 6271 6272 6273 6275 6278 6279 6280 6281 6245 6252 6256 6261 6009 6248 6268 6277 6269 6075 6076 6077 6078 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6263 6264 6266 6276 6258 6164 6274 ); my $base = "http://apps.environment-agency.gov.uk/river-and-sea-levels/136486.aspx?stationId="; my $db = DBI->connect("dbi:SQLite:dbname=rivers.db") or die $DBI::errstr; my $getlastrecord = $db->prepare("SELECT timestamp FROM lastrecord WHERE id=?") or die $db->errstr; sub newer($$) { my ($id, $time) = @_; $getlastrecord->execute($id); my ($lastrecord) = $getlastrecord->fetchrow(); return $lastrecord ? $time gt $lastrecord : 1; } my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr; my $recordlevel = $db->prepare("INSERT INTO levels (stationid, timestamp, level) VALUES (?,?,?)") or die $db->errstr; sub recordlevel($$$) { my ($id, $time, $level) = @_; $recordlevel->execute($id, $time, $level); $setlastrecord->execute($id, $time); } my $rememberstation = $db->prepare("INSERT INTO stations (id, name, watercourse, datum) VALUES (?,?,?,?)") or die $db->errstr; # First thing on the line should be the current time print time, "\t" unless $ENV{R_GPLOT}; my $c = 2; foreach my $station (@stations) { my $req = HTTP::Request->new(GET => $base . $station); my $res = $ua->request($req); my $level; my $stationname; my $lastupdate; my $watercourse; my $datum; if ($res->is_success and $res->content =~ m~Current level:~) { $res->content =~ m~