Projet

Général

Profil

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

root / plugins / sensors / humidity-sensor @ 97cf6d32

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

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