You are on page 1of 5

use 5.

010;
use strict;
use warnings;
use JSON::RPC::Client;
use Data::Dumper;
use Getopt::Long;
##XMLRPC Specific Includes
##use Fronter::Respondes;
my $client = new JSON::RPC::Client;
my $url = 'http://127.0.0.1/zabbix/api_jsonrpc.php';
our $user;
our $passwd;
#***************
#Liste des choix
#***************
my %opts;
GetOptions(\%opts,
'Auth' => \&Auth,
'GetHost' => \&GetHost,
'GetTemplate' => \&GetTemplate,
'CreGrHost' => \&CreGrHost,
'CreHost' => \&CreHost,
'user=u' => \$user,
'password=p' => \$passwd,
);
# # if ($opts{'Auth'}) {
# #
&Auth;
# # }
# #
# # if ($opts{'GetHost'}) {
# #
&GetHost;
# # }
# #
# # if ($opts{'GetTemplate'}) {
# #
&GetTemplate;
# # }
# #
# # if ($opts{'CreGrHost'}) {
# #
&CreGrHost;
# # }
# #
# # if ($opts{'CreHost'}) {
# # &CreHost;
# # }
##Initialize the XMLRPC listener
##my $response=Frontier::Responder->new (methods => $GetOptions);
##print $response->answer;
#***********************
#Fonction Authentication
#***********************
sub Auth {

# our $user = shift;


# our $passwd = shift;
my $json = {
"jsonrpc" => "2.0",
"method" => "user.login",
"params" => {
"user" => "$user",
"password" => "$passwd"
#
"user" => "abir",
#
"password" => "abirapi"
},
"id" => 1
};
my $response = $client->call($url, $json);
my $authHash;
if ($response->content->{'result'}) {
$authHash = $response->content->{'result'};
print "Auth successful - ID hash: " . $authHash . "\n";
}
else {
print "sorry, incorrect auth\n"
};
};
#*****************
#Fonction Get Host
#*****************
sub GetHost {
my $name = shift,
my $json = {
"jsonrpc" => "2.0",
"method" => "user.login",
"params" => {
#
"user" => "Admin",
#
"password" => "zabbix"
"user" => "abir",
"password" => "abirapi"
},
"id" => 1
};
my $response = $client->call($url, $json);
my $authHash;
$authHash = $response->content->{'result'};
my $json_obj = {
"jsonrpc" => '2.0',
"method"=>"host.get",
"params"=>{
"output"=>"extend",
"filter"=>{
"host"=>["$name"]
}
},
"auth" => "$authHash",
"id"=>2,
};

$response = $client->call($url, $json_obj);


print Dumper($response)
};
#*********************
#Fonction Get Template
#*********************
sub GetTemplate {
my $name=shift;
my $json = {
"jsonrpc" => "2.0",
"method" => "user.login",
"params" => {
"user" => "Admin",
"password" => "zabbix"
},
"id" => 1
};
my $response = $client->call($url, $json);
my $authHash;
if ($response->content->{'result'}) {
$authHash = $response->content->{'result'} };
my $json_obj = {
"jsonrpc"=>"2.0",
"method"=>"template.get",
"params"=>{
"output"=>"extend",
"filter"=>{
"host"=>"$name"
}
},
"auth"=>"$authHash",
"id"=> 1
};
return $client->call($url, $json_obj);
};
#***************************
#Fonction Create Host Groupe
#***************************
sub CreGrHost {
my $name=shift;
my $json = {
"jsonrpc" => "2.0",
"method" => "user.login",
"params" => {
"user" => "Admin",
"password" => "zabbix"
},
"id" => 1
};
my $response = $client->call($url, $json);
my $authHash;

if ($response->content->{'result'}) {
$authHash = $response->content->{'result'};
}
my $json_obj = {
"jsonrpc" => '2.0',
"method" => 'hostgroup.create',
"params" =>{
"name" => "$name",
},
"id" => 2,
"auth" => "$authHash"
};
return $client->call($url, $json_obj);
};
#********************
#Fonction Create Host
#********************
sub CreHost {
my $name=shift,
my $adresse=shift,
my $port=shift,
my $json = {
"jsonrpc" => "2.0",
"method" => "user.login",
"params" => {
"user" => "Admin",
"password" => "zabbix"
},
"id" => 1
};
my $response = $client->call($url, $json);
my $authHash;
if ($response->content->{'result'}) {
$authHash = $response->content->{'result'}
};
my $json_obj = {
"jsonrpc"=>"2.0",
"method"=>"host.create",
"params"=>{
"host"=>"$name",
"ip"=>"$adresse",
"port"=>$port,
"useip"=>1,
"groups"=>[
{
"groupid"=>7
}
],
"templates"=>[
{
"templateid"=>10001
}

]
},
"auth"=>"$authHash",
"id"=>2
};
return $client->call($url, $json_obj);
};

You might also like