Skip to content

Instantly share code, notes, and snippets.

@yasu47b
Created August 11, 2015 12:46
Show Gist options
  • Save yasu47b/3bf4ddeb1721826dfd81 to your computer and use it in GitHub Desktop.
Save yasu47b/3bf4ddeb1721826dfd81 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Mojo::UserAgent;
use Mojo::JSON('decode_json');
use utf8;
use Encode qw(encode);
use Email::Sender::Simple 'sendmail';
use Email::MIME;
use Email::MIME::Creator;
#use Path::Class;
my $conf = {
host=>'127.0.0.1',
user=>'admin',
pass=>'zabbix',
itemid => $ARGV[0],
sendto => $ARGV[1],
};
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};
# cookie
=commnet
$tx = $ua->post('http://'.$conf->{host}.'/zabbix/api_jsonrpc.php' =>
json => {
"method"=>"graph.get",
"id"=>100,
"params"=>{
"output"=>"extend",
"sortfield"=>"name",
"hostid"=>"10084"
},
"auth" => $token,
"jsonrpc"=>"2.0"}
);
say $tx->res->body;
=cut
my $stime = `date -d "2 hour ago" +%Y%m%d%H%M%S`;
#$tx = $ua->build_tx(POST =>'http://'.$conf->{host}.'/zabbix/chart2.php?graphid=593&period=3600&stime=20150803175524&width=1024');
$tx = $ua->build_tx(POST =>'http://'.$conf->{host}.'/zabbix/chart.php?itemid='.$conf->{itemid}.'&period=3600&stime='.$stime.'&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');
my $email = Email::MIME->create(
header => [
from => encode('MIME-Header-ISO_2022_JP' => '"グラフメール" <zabbix@example.com>'),
To => encode('MIME-Header-ISO_2022_JP' => '"B.A." <'.$conf->{sendto}),
Subject => encode('MIME-Header-ISO_2022_JP' => 'タイトル'),
],
parts => [
Email::MIME->create(
attributes => {
content_type => 'text/plain',
charset => 'ISO-2022-JP',
encoding => '7bit',
},
body => encode(jis => '本文'),
),
Email::MIME->create(
attributes => {
content_type => 'image/png',
name => 'image.png',
filename => 'image.png',
encoding => 'base64',
disposition => 'attachment',
},
#body => scalar file('image.png')->slurp,
body=> scalar $image,
),
],
);
sendmail($email);
1;
@yasu47b
Copy link
Author

yasu47b commented Aug 11, 2015

Using system perl on CentOS6 64bit, these are needed.

  • perl-ExtUtils-MakeMaker.x86_64
  • gcc
  • Mojo::UserAgent
  • IO::Socket::IP
  • Digest::SHA
  • Time::HiRes
  • Compress::Raw::Zlib
  • IO::Compress::Gzip
  • Email::Sender::Simple
  • Email::MIME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment