Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / weather / humidity-sensor @ 35c403ed

Historique | Voir | Annoter | Télécharger (1,92 ko)

1
#!/usr/bin/perl -w
2
#
3
# Copyright (C) Viktoras Pecia 2010(Based on 2006 Lars Strand "temperatures" code)
4
#
5
# Plugin to fetch humidity from weather.noaa.gov
6
#
7
# Parameters supported:
8
#
9
#       config
10
#       autoconf
11
#
12
# Magic markers:
13
#%# family=auto
14
#%# capabilities=autoconf
15

    
16
use strict;
17
my @wcode = undef;
18
if (defined($ENV{wcode})) {
19
    @wcode = split(' ', $ENV{wcode});
20
} else {
21
    @wcode = ("EYSA","EYKA","EYPA","EYVI");
22
}
23
my $proxy = $ENV{proxy}   || undef;   # Example: "http://proxy.foo.bar:8080/"
24
my $ret = undef;
25
if (! eval "require LWP::UserAgent;")
26
{
27
    $ret = "LWP::UserAgent not found";
28
}
29
if (defined $ARGV[0] and $ARGV[0] eq "autoconf") {
30
    if (defined $ret) {
31
	print "no ($ret)\n";
32
    } else {
33
	print "yes\n";
34
    }
35
    exit 0;
36
}
37
my $datasource = "http://weather.noaa.gov/pub/data/observations/metar/decoded/";
38

    
39
my $ua = LWP::UserAgent->new(timeout => 30);
40
$ua->agent('Munin');
41
# Use proxy, if defined.
42
if (defined($proxy)) {
43
    $ua->proxy(['http'], $proxy);
44
}
45
if (defined $ARGV[0] and $ARGV[0] eq "config") {
46
    print "graph_title Humidity\n";
47
    print "graph_args --base 1000 -l 0\n";
48
    print "graph_category sensors\n";
49
    print "graph_info This graph shows humidity fetched from weather.nooa.gov.\n";
50
    print "graph_vlabel humidity in %\n";
51
    for my $station (@wcode) {
52
	my $url = "$datasource$station.TXT";
53
	my $response = $ua->request(HTTP::Request->new('GET',$url));
54
	# New York City, Central Park, NY, United States (KNYC) 40-47-00N 073-58-00W 48M
55
        if ($response->content =~ /^((.*?),.*\)).*\n/) {
56
	    print "$station.label $2\n";
57
	    print "$station.info $1\n";
58
	} else {
59
	    print "$station.label $station\n";
60
	}
61
    }
62
}
63
for my $station (@wcode) {
64
    my $url = "$datasource$station.TXT";
65
    my $response = $ua->request(HTTP::Request->new('GET',$url));
66
    if ($response->content =~ /Relative Humidity:\s*(\d+)\%.*/) {
67
	    print "$station.value $1\n";
68
        } else {
69
	    print "$station.value U\n";
70
        }
71
    }