Created
November 15, 2012 19:26
-
-
Save waffle2k/4080643 to your computer and use it in GitHub Desktop.
Get all active checks for your host
This file contains hidden or 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/perl | |
use strict; | |
package ZBX::SERVER; | |
use JSON; | |
use IO::Socket; | |
use Net::Domain 'hostfqdn'; | |
use constant ZBX_SERVER_PORT => 10051; | |
use constant ZBX_AGENT_PROTO_TEMPL => 'a4 b c4 c4 a*'; | |
sub new { | |
my $class = shift; | |
my $self = { | |
port => ZBX_SERVER_PORT, | |
hostname => hostfqdn(), | |
@_, | |
}; | |
die "You must specify a server\n" unless $self->{server}; | |
return bless $self => $class; | |
} | |
sub encode { | |
my ($self,$request) = @_; | |
my $json = encode_json( $request ); | |
# turn on byte semantics to get the real length of the string | |
use bytes; | |
my $length = length($json); | |
no bytes; | |
my $output = pack( | |
ZBX_AGENT_PROTO_TEMPL, | |
"ZBXD", 0x01, | |
( $length & 0xFF ), | |
( $length & 0x00FF ) >> 8, | |
( $length & 0x0000FF ) >> 16, | |
( $length & 0x000000FF ) >> 24, | |
0x00, 0x00, 0x00, 0x00, $json | |
); | |
return $output; | |
} | |
sub checks { | |
my $self = shift; | |
my $request = { | |
request => "active checks", | |
host => $self->{hostname}, | |
}; | |
$self->send_request( $request ); | |
} | |
sub send_request { | |
my ($self, $request) = @_; | |
my $encoded = $self->encode( $request ); | |
my $sock = new IO::Socket::INET ( | |
PeerAddr => $self->{server}, | |
PeerPort => ZBX_SERVER_PORT, | |
Proto => 'tcp', | |
) || die "Could not connect to zabbix peer: $!\n"; | |
print $sock $encoded . "\r\n"; | |
local $/; | |
my $result = <$sock>; | |
my @r = unpack( ZBX_AGENT_PROTO_TEMPL, $result ); | |
return decode_json( $r[-1] ); | |
} | |
package MAIN; | |
use Data::Dumper; | |
my $server = $ARGV[0] || die 'Please specify the zabbix servername'; | |
my $o = ZBX::SERVER->new( server => $server ); | |
print "Active Checks: ",Dumper($o->checks),"\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment