Projet

Général

Profil

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

root / plugins / sensors / temperatures @ e5ce7492

Historique | Voir | Annoter | Télécharger (2,18 ko)

1 d3ae4577 Lars Strand
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2006 Lars Strand
4
#
5
# Plugin to fetch temperature 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
18
# Find areacodes here http://weather.noaa.gov/
19
my @wcode = undef;
20
21
if (defined($ENV{wcode})) {
22
    @wcode = split(' ', $ENV{wcode});
23
} else {
24
    @wcode = ("ENGM", "ENBR", "ENVA", "ENTC");
25
}
26
27
my $unit  = $ENV{unit}    || "C";     # "C" = Celsius, "F" = Fahrenheit
28
my $proxy = $ENV{proxy}   || undef;   # Example: "http://proxy.foo.bar:8080/"
29
30
my $ret = undef;
31
if (! eval "require LWP::UserAgent;")
32
{
33
    $ret = "LWP::UserAgent not found";
34
}
35
36
if (defined $ARGV[0] and $ARGV[0] eq "autoconf") {
37
    if (defined $ret) {
38
	print "no ($ret)\n";
39
	exit 1;
40
    } else {
41
	print "yes\n";
42
	exit 0;
43
    }
44
}
45
46
my $datasource = "http://weather.noaa.gov/pub/data/observations/metar/decoded/";
47
48
my $ua = LWP::UserAgent->new(timeout => 30);
49
$ua->agent('Munin');
50
51
# Use proxy, if defined.
52
if (defined($proxy)) {
53
    $ua->proxy(['http'], $proxy);
54
}
55
56
if (defined $ARGV[0] and $ARGV[0] eq "config") {
57
58
    print "graph_title Outside temperature\n";
59
    print "graph_args --base 1000 -l 0\n";
60
    print "graph_category sensors\n";
61
    print "graph_info This graph shows temperatures fetched from weather.nooa.gov.\n";
62
63
    if ($unit =~ /F/) {
64
	print "graph_vlabel temp in F\n";
65
    } else {
66
	print "graph_vlabel temp in C\n";
67
    }
68
    
69
    for my $station (@wcode) {
70
	my $url = "$datasource$station.TXT";
71
	my $response = $ua->request(HTTP::Request->new('GET',$url));
72
	# New York City, Central Park, NY, United States (KNYC) 40-47-00N 073-58-00W 48M
73
        if ($response->content =~ /^((.*?),.*\)).*\n/) {
74
	    print "$station.label $2\n";
75
	    print "$station.info $1\n";
76
	} else {
77
	    print "$station.label $station\n";
78
	}
79
    }
80
    exit 0;
81
}
82
83
for my $station (@wcode) {
84
    my $url = "$datasource$station.TXT";
85
    my $response = $ua->request(HTTP::Request->new('GET',$url));
86
87
    if ($response->content =~ /Temperature:\s*(.*)\s+F\s*\(\s*(.*)\s+C/) {
88
	if ($unit =~ /F/) {
89
	    print "$station.value $1\n";
90
	} else {
91
	    print "$station.value $2\n";
92
	}
93
    } else {
94
	print "$station.value U\n";
95
    }
96
}