Skip to content

Instantly share code, notes, and snippets.

@yasu47b
Last active August 29, 2015 14:26
Show Gist options
  • Save yasu47b/adcaf653b51e94d0b00a to your computer and use it in GitHub Desktop.
Save yasu47b/adcaf653b51e94d0b00a to your computer and use it in GitHub Desktop.
mojo useragentでzabbixからpng画像を取得するサンプル
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Mojo::UserAgent;
use Mojo::JSON('decode_json');
my $conf = {
host=>'localhost',
user=>'admin',
pass=>'zabbix'
};
my $ua = Mojo::UserAgent->new;
my $tx = $ua->build_tx(POST => 'http://'.$conf->{host}.'/zabbix/api_jsonrpc.php',
json => {
"method"=>"user.login",
"id"=>100,
"params"=>{
"password"=> $conf->{pass},
"user"=> $conf->{user}},
"jsonrpc"=>"2.0"}
);
$tx->req->headers->header('Content-Type'=>'application/json-rpc');
$tx = $ua->start($tx);
my $json = decode_json $tx->res->body;
my $token = $json->{result};
$tx = $ua->build_tx(POST =>'http://'.$conf->{host}.'/zabbix/chart2.php?graphid=593&period=3600&stime=20150803175524&width=1024');
$tx->req->cookies({name => 'zbx_sessionid', value => $token});
$tx = $ua->start($tx);
#my $image = $tx->res->body;
my $image = $tx->res->content->asset->move_to('./image.png');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment