Skip to content

Instantly share code, notes, and snippets.

@crashdump
Last active December 17, 2015 16:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crashdump/5642104 to your computer and use it in GitHub Desktop.
Save crashdump/5642104 to your computer and use it in GitHub Desktop.
weather.yahooapis.com -> zabbix
#!/usr/bin/perl
use strict;
use XML::Simple;
use WWW::Curl::Easy;
my $WOEID = 12215; # Yahoo! "Where On Earth Identifier" for Bedford, UK
my $ZABBIX_SENDER = "/usr/bin/zabbix_sender";
my $ZABBIX_SERVER = "172.24.1.36";
my $ZABBIX_HOSTNAME = "summertime.aeromark1.lan";
##
# Fetch the data
##
my $curl = WWW::Curl::Easy->new;
my $xml;
$curl->setopt(CURLOPT_HEADER,0);
$curl->setopt(CURLOPT_URL, "http://weather.yahooapis.com/forecastrss?w=$WOEID&u=c");
$curl->setopt(CURLOPT_WRITEDATA,\$xml);
$curl->setopt(CURLOPT_PROXY, 'http://myproxy:myport/');
my $retcode = $curl->perform;
if ($retcode != 0 or length($xml) < 64 ) {
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
exit($retcode);
}
##
# Process em
##
my $weather = new XML::Simple->XMLin($xml)->{channel};
sub send_value {
print "$_[0] => $_[1]\r\n";
print SENDER qq("$ZABBIX_HOSTNAME" $_[0] "$_[1]"\n);
select(undef, undef, undef, 0.125); # sleep for 125ms
}
open(SENDER, "| $ZABBIX_SENDER --zabbix-server $ZABBIX_SERVER --input-file -") || die "Cannot execute zabbix_sender: $!\n";
my $location = $weather->{'yweather:location'};
send_value("weather.location[city]", $location->{city});
send_value("weather.location[country]", $location->{country});
my $wind = $weather->{'yweather:wind'};
send_value("weather.wind[chill]", $wind->{chill});
send_value("weather.wind[direction]", $wind->{direction});
send_value("weather.wind[speed]", $wind->{speed});
my $atmosphere = $weather->{'yweather:atmosphere'};
send_value("weather.atmosphere[humidity]", $atmosphere->{humidity});
send_value("weather.atmosphere[pressure]", $atmosphere->{pressure});
send_value("weather.atmosphere[visibility]", $atmosphere->{visibility});
my $astronomy = $weather->{'yweather:astronomy'};
send_value("weather.astronomy[sunrise]", $astronomy->{sunrise});
send_value("weather.astronomy[sunset]", $astronomy->{sunset});
my $condition = $weather->{item}->{'yweather:condition'};
send_value("weather.condition[text]", $condition->{text});
send_value("weather.condition[code]", $condition->{code});
send_value("weather.condition[temperature]", $condition->{temp});
my $count = 0;
my @forecasts = @{$weather->{item}->{'yweather:forecast'}};
foreach my $forecast (@forecasts) {
last if (++$count > 2);
my $day = ($count == 1 ? "today" : "tomorrow");
send_value("weather.forecast[$day,text]", $forecast->{text});
send_value("weather.forecast[$day,code]", $forecast->{code});
send_value("weather.forecast[$day,temperature,low]", $forecast->{low});
send_value("weather.forecast[$day,temperature,high]", $forecast->{high});
}
close(SENDER);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment