#! /usr/bin/perl
use strict;
use LWP;
use Data::Dumper;
use Getopt::Std;
use HTML::TokeParser;
use HTML::TreeBuilder;
my $browser;
my %group;
my %host;
my %graph;
my $url;
my $content;
sub mergeHash{
my %des=();
for my $hash (@_){
for my $key (keys %$hash){
$des{$key} = $hash->{$key};
}
}
return %des;
}
sub fillHeader{
my ($h, $hash) = @_;
for my $key(keys %$hash){
$h->header($key => $hash->{$key});
}
}
sub parseToken(){
my ($idName, $hash, $content) = @_;
my $id;
my $name;
my $stream = HTML::TokeParser->new(\$content);
while (my $token = $stream->get_token){
if($token->[0] eq "S" &&
$token->[1] eq "select" &&
$token->[2]->{"id"} eq $idName){
while( my $option = $stream->get_token){
if($option->[0] eq "S" && $option->[1] eq "option"){
$id = $option->[2]->{"value"};
my $text = $stream->get_token;
if($text->[0] eq "T"){
$name = $text->[1];
}
#print $groupId, $groupName, "\n";
#$group{lc $groupName} = $groupId;
$hash->{lc $name} = $id;
}
if($option->[0] eq "E" && $option->[1] eq "option"){
next;
}
if($option->[0] eq "E" && $option->[1] eq "select"){
last;
}
}
}
}
}
my $loginBody = "
-----------------------------1234567890
Content-Disposition: form-data; name=\"form\"
1
-----------------------------1234567890
Content-Disposition: form-data; name=\"form_refresh\"
1
-----------------------------1234567890
Content-Disposition: form-data; name=\"name\"
<username>
-----------------------------1234567890
Content-Disposition: form-data; name=\"password\"
<password>
-----------------------------1234567890
Content-Disposition: form-data; name=\"enter\"
Enter
-----------------------------1234567890--
";
my %headers = (
"Host" => "xxx.xxx.xxx.com",
"User-Agent" => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3",
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language" => "en-us,en;q=0.5",
"Accept-Encoding"=> "gzip,deflate",
"Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive" => "115",
"Connection" => "Keep-Alive"
);
my %headerItems = (
"Cache-Control" => "no-cache",
"Content-Type" => "multipart/form-data; boundary=---------------------------1234567890",
"Referer" => "http://xxx.xxx.xxx.com/index.php?reconnect=1"
);
my %options=();
getopts("u:p:g:h:r:s:l:",\%options);
if (!defined $options{u} ||
!defined $options{p} ||
!defined $options{g} ||
!defined $options{h} ||
!defined $options{r} ||
!defined $options{s} ||
!defined $options{l}){
print "Usage: perl zabbix.pl -u username -p password -g groupname -h host -r graph -s starttime -l lasttime\n";
}
$loginBody =~ s/<username>/$options{u}/;
$loginBody =~ s/<password>/$options{p}/;
my %hash = &mergeHash(\%headers, \%headerItems);
my $ua = LWP::UserAgent->new;
my $header = HTTP::Headers->new;
&fillHeader($header, \%hash);
$url = 'http://xxx.xxx.xxx.com/index.php?login=1';
my $req = HTTP::Request->new("POST", $url, $header);
$req->content($loginBody);
my $response = $ua->request($req);
my $cookie = $response->header("Set-Cookie");
$cookie =~ /(?:zbx_sessionid=.*)(zbx_sessionid=[a-z0-9].*)/;
%headerItems = (
"Referer" => $url,
"Cookie" => $1
);
%hash = &mergeHash(\%headers, \%headerItems);
$header = HTTP::Headers->new;
&fillHeader($header, \%hash);
$url = 'http://xxx.xxx.xxx.com/charts.php';
$req = HTTP::Request->new("GET", $url, $header);
$content = $ua->request($req)->content;
my $root = HTML::TreeBuilder->new_from_content($content);
#my @headings = $root->find_by_tag_name('select');
my @groups = $root->look_down(_tag=>'select', id=>'groupid');
for my $group(@groups){
my @option_list = $group->content_list();
for my $option(@option_list){
#print $option->as_text(), " " , $option->attr('value'), "\n";
$group{lc $option->as_text()} = $option->attr('value');
}
}
#&parseToken('groupid', \%group, $content);
$root->eof( );
$root->delete();
my $groupid = $group{$options{g}};
$url = 'http://xxx.xxx.xxx.com/charts.php?fullscreen=0&groupid=<groupid>&hostid=0&graphid=0';
$url =~ s/<groupid>/$groupid/;
$req = HTTP::Request->new("GET", $url, $header);
$content = $ua->request($req)->content;
&parseToken('hostid', \%host, $content);
my $hostid = $host{$options{h}};
$url = 'http://xxx.xxx.xxx.com/charts.php?fullscreen=0&groupid=<groupid>&hostid=<hostid>&graphid=0';
$url =~ s/<groupid>/$groupid/;
$url =~ s/<hostid>/$hostid/;
$req = HTTP::Request->new("GET", $url, $header);
$content = $ua->request($req)->content;
&parseToken('graphid', \%graph, $content);
my $graphid = $graph{$options{r}};
$url = 'http://xxx.xxx.xxx.com/chart2.php?graphid=<graphid>&period=<lasttime>&width=900&stime=<starttime>';
$url =~ s/<graphid>/$graphid/;
$url =~ s/<lasttime>/$options{l}/;
$url =~ s/<starttime>/$options{s}/;
$req = HTTP::Request->new("GET", $url, $header);
$content = $ua->request($req)->content;
open PNGFILE , '>', "$options{r}.png";
print PNGFILE $content;
close PNGFILE;
print "$options{r}.png has been saved";