Created
June 9, 2014 02:07
-
-
Save fujiwara/76c6653de9809ee57d58 to your computer and use it in GitHub Desktop.
Quick open to Zabbix screen by percol.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.12.0; | |
use ZabbixAPI; | |
use YAML; | |
use IPC::Open2; | |
use IO::Handle; | |
use Encode; | |
use Config::Pit; | |
my $config = pit_get( | |
"zabbix", require => { | |
"url" => "zabbix url endpoint (e.g. https://example.com/zabbix/)", | |
"user" => "zabbix username", | |
"password" => "zabbix password", | |
} | |
); | |
my $url = delete $config->{url}; | |
my $api = ZabbixAPI->new( url => $url ); | |
$api->login(%$config); | |
my $screens = $api->call("screen.get", { output => "extend" }); | |
my $pid = open2(my $out, my $in, 'percol'); | |
for my $s (@$screens) { | |
$in->print($s->{screenid}, " ", encode_utf8($s->{name}), "\n"); | |
} | |
$in->close; | |
my $selected = <$out>; | |
my ($screenid) = split / /, $selected, 2; | |
exec "open", "$url/screens.php?elementid=$screenid"; | |
die; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ZabbixAPI; | |
use strict; | |
use warnings; | |
use Mouse; | |
use Try::Tiny; | |
use JSON; | |
use HTTP::Request::Common; | |
use LWP::UserAgent; | |
use Data::Dumper; | |
use Log::Minimal; | |
has ua => ( | |
is => "rw", | |
default => sub { | |
LWP::UserAgent->new( | |
ssl_opts => { | |
verify_hostname => 0, | |
SSL_verify_mode => "SSL_VERIFY_NONE", | |
}, | |
timeout => 180, | |
); | |
} | |
); | |
has auth => ( is => "rw", isa => "Str", ); | |
has id => ( is => "rw", isa => "Int", default => 1 ); | |
has url => ( is => "rw", isa => "Str", ); | |
has cookie => ( is => "rw", isa => "Str", ); | |
sub login { | |
my $self = shift; | |
my %params = @_; | |
my $r = { | |
method => "user.login", | |
params => \%params, | |
auth => undef, | |
}; | |
$self->auth( $self->_request($r) ); | |
debugf "auth: %s", $self->auth; | |
$self->auth; | |
} | |
sub call { | |
my ($self, $method, $params) = @_; | |
my $r = { | |
method => $method, | |
params => $params, | |
auth => $self->auth, | |
}; | |
$self->_request($r); | |
} | |
sub _request { | |
my $self = shift; | |
my $r = shift; | |
$r->{id} = $self->{id}++; | |
$r->{jsonrpc} = "2.0"; | |
my $json = encode_json $r; | |
my $req = POST $self->url . "api_jsonrpc.php", | |
"Content-Type" => "application/json-rpc", | |
"Content-Length" => length($json), | |
"Content" => $json, | |
; | |
my $res = $self->ua->request($req); | |
my $result; | |
try { | |
$result = decode_json($res->content)->{result}; | |
} | |
catch { | |
my $e = $_; | |
critf("decode json failed. %s\nreq: %s", $e, ddf $req); | |
}; | |
die "request failed" unless $result; | |
return $result; | |
} | |
sub get { | |
my $self = shift; | |
my $url = shift; | |
$url = $self->url . $url | |
unless $url =~ m{^https?://}; | |
my $req = GET $url; | |
debugf $req->as_string; | |
$self->ua->request($req); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment