root / plugins / other / temperature_ @ e908d2d2
Historique | Voir | Annoter | Télécharger (2,22 ko)
| 1 | aedb2f4b | 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 | my $usehum = $ENV{humidity} || undef; # set to "yes" to enable humidity
|
||
| 19 | my $wcode = $ENV{wcode} || "ENGM"; # Find areacode here http://weather.noaa.gov/
|
||
| 20 | my $unit = $ENV{unit} || "C"; # "C" = Celsius, "F" = Fahrenheit
|
||
| 21 | my $proxy = $ENV{proxy} || undef; # Example: "http://proxy.foo.bar:8080/"
|
||
| 22 | |||
| 23 | my $ret = undef; |
||
| 24 | if (! eval "require LWP::UserAgent;") {
|
||
| 25 | $ret = "LWP::UserAgent not found"; |
||
| 26 | } |
||
| 27 | |||
| 28 | if (defined $ARGV[0] and $ARGV[0] eq "autoconf") {
|
||
| 29 | if (defined $ret) {
|
||
| 30 | print "no ($ret)\n"; |
||
| 31 | exit 1; |
||
| 32 | } else {
|
||
| 33 | print "yes\n"; |
||
| 34 | exit 0; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | # Extract weather-code from filename. Example: weather_CODE |
||
| 39 | if ($0 =~ /^(?:|.*\/)temperature_([^_]+)$/) {
|
||
| 40 | $wcode = $1; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | my $datasource = "http://weather.noaa.gov/pub/data/observations/metar/decoded/$wcode.TXT"; |
||
| 45 | |||
| 46 | my $ua = LWP::UserAgent->new(timeout => 30); |
||
| 47 | $ua->agent('Munin');
|
||
| 48 | |||
| 49 | # Use proxy, if defined. |
||
| 50 | if (defined $proxy) {
|
||
| 51 | $ua->proxy(['http'], $proxy); |
||
| 52 | } |
||
| 53 | |||
| 54 | my $response = $ua->request(HTTP::Request->new('GET',$datasource));
|
||
| 55 | |||
| 56 | if (defined $ARGV[0] and $ARGV[0] eq "config") {
|
||
| 57 | |||
| 58 | if ($response->content =~ /^((.*?),.*\)).*\n/) {
|
||
| 59 | print "graph_title Temperature at $2\n"; |
||
| 60 | } else {
|
||
| 61 | print "graph_title Temperature at locationcode $wcode\n"; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($unit =~ /F/) {
|
||
| 65 | print "graph_vlabel temp in F\n"; |
||
| 66 | } else {
|
||
| 67 | print "graph_vlabel temp in C\n"; |
||
| 68 | } |
||
| 69 | |||
| 70 | print "graph_args --base 1000 -l 0\n"; |
||
| 71 | print "graph_category sensors\n"; |
||
| 72 | print "graph_info Temperatures at $1 (fetched from weather.nooa.gov).\n"; |
||
| 73 | print "temperature.label temperature\n"; |
||
| 74 | if (defined $usehum) {
|
||
| 75 | print "humidity.label humidity\n"; |
||
| 76 | print "humidity.info Humidity in %.\n"; |
||
| 77 | } |
||
| 78 | exit 0; |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($response->content =~ /Temperature:\s*(.*)\s+F\s*\(\s*(.*)\s+C/) {
|
||
| 82 | if ($unit =~ /F/) {
|
||
| 83 | print "temperature.value $1\n"; |
||
| 84 | } else {
|
||
| 85 | print "temperature.value $2\n"; |
||
| 86 | } |
||
| 87 | } else {
|
||
| 88 | print "temperature.value U\n"; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (defined $usehum) {
|
||
| 92 | if ($response->content =~ /Relative Humidity:\s*(\d+)\%.*/) {
|
||
| 93 | print "humidity.value $1\n"; |
||
| 94 | } |
||
| 95 | } |
