Created
September 12, 2018 10:11
-
-
Save stasikos/acac9e4294f7dd851dafdb1e405c4d21 to your computer and use it in GitHub Desktop.
zabbix module
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/perl -w | |
use strict; | |
use JSON::RPC::Client; | |
use Data::Dumper; | |
my $url = "https://myzabbixhost.god.org/api_jsonrpc.php"; | |
my $user = 'apiuser'; | |
my $password = 'apipassword'; | |
sub authenticate { | |
my ($client, $user, $password) = @_; | |
my $call = { | |
method => "user.authenticate", | |
params => { | |
user => $user, | |
password => $password | |
}, | |
id => 0, | |
jsonrpc => "2.0" | |
}; | |
my $res = $client->call($url, $call); | |
return $res->result; | |
} | |
sub host_id { | |
my ($client, $auth, $host_name) = @_; | |
my $call = { | |
method => "host.get", | |
params => { | |
filter => { host => [$host_name] }, | |
output => "extend" | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 2 | |
}; | |
my $res = $client->call($url, $call); | |
my @result = $res->result; | |
return $result[0][0]->{"hostid"}; | |
} | |
# Most params are hardcoded for fast development | |
sub host_create { | |
my ($client, $auth, $host_name) = @_; | |
my $call = { | |
method => "host.create", | |
params => { | |
host => $host_name, | |
ip => "127.0.0.1", | |
port => 10050, | |
groups => [{"groupid" => 300300000000020}], | |
useip => 1 | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 3 | |
}; | |
my $res = $client->call($url, $call); | |
my $result = $res->result; | |
return $result->{"hostids"}[0]; | |
} | |
sub item_create { | |
my ($client, $auth, $hostid, $item_key, $desc) = @_; | |
my $call = { | |
method => "item.create", | |
params => { | |
description => $desc, | |
key_ => $item_key, | |
hostid => $hostid, | |
trapper_hosts => '', | |
type => 2, # Zabbix trapper | |
data_type => 0, | |
value_type => 3 # Unsigned numeric | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 4 | |
}; | |
my $res = $client->call($url, $call); | |
my $result = $res->result; | |
; | |
return $result->{"itemids"}[0]; | |
} | |
sub item_exist { | |
my ($client, $auth, $hostid, $item_key) = @_; | |
my $call = { | |
method => "item.exists", | |
params => { | |
key_ => $item_key, | |
hostid => $hostid | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 5 | |
}; | |
my $res = $client->call($url, $call); | |
my $result = $res->result; | |
return $result; | |
} | |
sub trigger_exist { | |
my ($client, $auth, $hostid, $expr, $desc) = @_; | |
my $call = { | |
method => "trigger.exists", | |
params => { | |
expression => $expr, | |
description => $desc, | |
hostid => $hostid | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 5 | |
}; | |
my $res = $client->call($url, $call); | |
my $result = $res->result; | |
return $result; | |
} | |
sub trigger_create { | |
my ($client, $auth, $hostid, $expr, $desc) = @_; | |
my $call = { | |
method => "trigger.create", | |
params => { | |
description => $desc, | |
expression => $expr, | |
priority => 4, | |
status => 0, | |
hostid => $hostid | |
}, | |
auth => $auth, | |
jsonrpc => "2.0", | |
id => 6 | |
}; | |
my $res = $client->call($url, $call); | |
my $result = $res->result; | |
return $result; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment