Update to 2024-10-16 14:00

This commit is contained in:
Daniel Berteaud 2024-10-16 14:00:08 +02:00
parent a01e7abc5f
commit db4f5ab690

View File

@ -148,7 +148,12 @@ sub sendjson {
$jsonstr = $_[0]; $jsonstr = $_[0];
# send json to zabbix and get result # send json to zabbix and get result
$res = `curl -s -i -X POST -H $header -d '$data' $url`; my $cmd = "curl -s -i -X POST -H $header";
if ($auth){
$cmd .= " -H 'Authorization: Bearer $auth'";
}
$cmd .= " -d '$jsonstr' $url";
$res = `$cmd`;
# find start of json # find start of json
$i = index($res, "{"); $i = index($res, "{");
# get json only # get json only
@ -161,7 +166,7 @@ sub sendjson {
# authenticate with zabbix, returns the auth token # authenticate with zabbix, returns the auth token
sub authenticate { sub authenticate {
# load auth json # load auth json
$data = '{"jsonrpc":"2.0","method":"user.login","params":{"user":"'.$user.'","password":"'.$pass.'"},"id": 1,"auth":null}'; $data = '{"jsonrpc":"2.0","method":"user.login","params":{"username":"'.$user.'","password":"'.$pass.'"}, "id": 1}';
# send json # send json
$res = sendjson($data); $res = sendjson($data);
@ -173,19 +178,10 @@ sub authenticate {
return $auth_out; return $auth_out;
} }
# get hostgroups from zabbix (auth) # get hostgroups from zabbix ()
sub gethostgroups { sub gethostgroups {
$auth_in = $_[0];
# load hostgroups json # load hostgroups json
$data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1, "auth": "" }'; $data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1 }';
# decode json
$dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# encode back to data
$data = encode_json($dec);
# send json # send json
$res = sendjson($data); $res = sendjson($data);
# decode json # decode json
@ -194,17 +190,14 @@ sub gethostgroups {
return $dec_out return $dec_out
} }
# get hosts from zabbix (auth, groupid) # get hosts from zabbix (groupid)
sub gethosts { sub gethosts {
$auth_in = $_[0]; $groupid_in = $_[0];
$groupid_in = $_[1];
# load items json # load items json
$data = '{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "sortfield": "name", "selectParentTemplates": "extend", "groupids": [ "" ] }, "id": 2, "auth": "" }'; $data = '{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "sortfield": "name", "selectParentTemplates": "extend", "groupids": [ "" ] }, "id": 2 }';
# decode json # decode json
$dec = decode_json($data); $dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# set groupid filter (outside filter) # set groupid filter (outside filter)
$dec->{"params"}->{"groupids"}[0] = $groupid_in; $dec->{"params"}->{"groupids"}[0] = $groupid_in;
# encode back to data # encode back to data
@ -218,18 +211,15 @@ sub gethosts {
return $dec_out; return $dec_out;
} }
# get items from zabbix (auth, hostid) # get items from zabbix (hostid)
sub getitems { sub getitems {
$auth_in = $_[0]; $hostid_in = $_[0];
$hostid_in = $_[1];
# load items json # load items json
$data = '{ "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "sortfield": "name", "filter": { "hostid": "" } }, "id": 1, "auth": "" }'; $data = '{ "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "sortfield": "name", "filter": { "hostid": "" } }, "id": 1 }';
# decode json # decode json
$dec = decode_json($data); $dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# set hostid filter # set hostid filter
$dec->{"params"}->{"filter"}->{"hostid"} = $hostid_in; $dec->{"params"}->{"filter"}->{"hostid"} = $hostid_in;
# encode back to data # encode back to data
@ -243,18 +233,15 @@ sub getitems {
return $dec_out return $dec_out
} }
# get graphs from zabbix (auth, hostid, graphname) # get graphs from zabbix (hostid, graphname)
sub getgraphs { sub getgraphs {
$auth_in = $_[0]; $hostid_in = $_[0];
$hostid_in = $_[1]; $graph_in = $_[1];
$graph_in = $_[2];
# load graphs json # load graphs json
$data = '{ "jsonrpc": "2.0", "method": "graph.get", "params": { "output": "extend", "sortfield": "name", "hostids": [ "" ], "filter": { "name": "" } }, "id": 3, "auth": "" }'; $data = '{ "jsonrpc": "2.0", "method": "graph.get", "params": { "output": "extend", "sortfield": "name", "hostids": [ "" ], "filter": { "name": "" } }, "id": 3 }';
# decode json # decode json
$dec = decode_json($data); $dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# set name filter # set name filter
$dec->{"params"}->{"filter"}->{"name"} = $graph_in; $dec->{"params"}->{"filter"}->{"name"} = $graph_in;
# set hostid filter (outside filter) # set hostid filter (outside filter)
@ -270,17 +257,14 @@ sub getgraphs {
return $dec_out; return $dec_out;
} }
# delete graph from zabbix (auth, graphid) # delete graph from zabbix (graphid)
sub deletegraph { sub deletegraph {
$auth_in = $_[0]; $graphid_in = $_[0];
$graphid_in = $_[1];
# load graphs json # load graphs json
$data = '{ "jsonrpc": "2.0", "method": "graph.delete", "params": [ "" ], "id": 4, "auth": "" }'; $data = '{ "jsonrpc": "2.0", "method": "graph.delete", "params": [ "" ], "id": 4 }';
# decode json # decode json
$dec = decode_json($data); $dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# set graphid # set graphid
$hash[0] = $graphid_in; $hash[0] = $graphid_in;
$dec->{"params"} = \@hash; $dec->{"params"} = \@hash;
@ -297,14 +281,13 @@ sub deletegraph {
return $dec_out; return $dec_out;
} }
# search and delete existing graphs from zabbix (auth, hostid, graphname) # search and delete existing graphs from zabbix (hostid, graphname)
sub deletegraphs { sub deletegraphs {
$auth_in = $_[0]; $hostid_in = $_[0];
$hostid_in = $_[1]; $graph_in = $_[1];
$graph_in = $_[2];
# get graph with name # get graph with name
$graphs = getgraphs($auth_in, $hostid_in, $graph_in); $graphs = getgraphs($hostid_in, $graph_in);
# each graph in list # each graph in list
# filter graphs that do not belong to our hostid # filter graphs that do not belong to our hostid
@ -317,42 +300,38 @@ sub deletegraphs {
print " "." "." "."Graph found: ".$graph_name." (".$graphid.")"."\n"; print " "." "." "."Graph found: ".$graph_name." (".$graphid.")"."\n";
# delete the graph # delete the graph
deletegraph($auth_in, $graphid); deletegraph($graphid);
} }
} }
# create graph in zabbix (auth, graphname, graphtype, mintype, maxtype, minvalue, maxvalue, showtriggers, graphitems, hostid) # create graph in zabbix (graphname, graphtype, mintype, maxtype, minvalue, maxvalue, showtriggers, graphitems, hostid)
sub creategraph { sub creategraph {
#ymin_type = 0 -> calculated #ymin_type = 0 -> calculated
#ymin_type = 1 -> fixed #ymin_type = 1 -> fixed
#graphtype = 0 -> normal #graphtype = 0 -> normal
#graphtype = 1 -> stack #graphtype = 1 -> stack
#auth
$auth_in = $_[0];
#graph name #graph name
$graph_in = $_[1]; $graph_in = $_[0];
#graphtype, mintype, maxtype, minvalue, maxvalue #graphtype, mintype, maxtype, minvalue, maxvalue
$graphtype_in = $_[2]; $graphtype_in = $_[1];
$mintype_in = $_[3]; $mintype_in = $_[2];
$maxtype_in = $_[4]; $maxtype_in = $_[3];
$minvalue_in = $_[5]; $minvalue_in = $_[4];
$maxvalue_in = $_[6]; $maxvalue_in = $_[5];
$showtriggers_in = $_[7]; $showtriggers_in = $_[6];
#graphitems #graphitems
$graphitems_in = $_[8]; $graphitems_in = $_[7];
#hostid #hostid
$hostid_in = $_[9]; $hostid_in = $_[8];
# load graphs json # load graphs json
$data = '{ "jsonrpc": "2.0", "method": "graph.create", "params": { "gitems": [ "" ], "name": "", "width": "900", "height": "300", "yaxismin": "0", "yaxismax": "100", $data = '{ "jsonrpc": "2.0", "method": "graph.create", "params": { "gitems": [ "" ], "name": "", "width": "900", "height": "300", "yaxismin": "0", "yaxismax": "100",
"show_work_period": "1", "show_triggers": "1", "graphtype": "0", "show_legend": "1", "show_3d": "0", "percent_left": "0", "percent_right": "0", "ymin_type": "0", "show_work_period": "1", "show_triggers": "1", "graphtype": "0", "show_legend": "1", "show_3d": "0", "percent_left": "0", "percent_right": "0", "ymin_type": "0",
"ymax_type": "0", "ymin_itemid": "0", "ymax_itemid": "0" }, "id": 4, "auth": "" }'; "ymax_type": "0", "ymin_itemid": "0", "ymax_itemid": "0" }, "id": 4 }';
# decode json # decode json
$dec = decode_json($data); $dec = decode_json($data);
# set auth
$dec->{"auth"} = $auth_in;
# set graph name # set graph name
$dec->{"params"}->{"name"} = $graph_in; $dec->{"params"}->{"name"} = $graph_in;
# set graphtype, mintype, maxtype, minvalue, maxvalue # set graphtype, mintype, maxtype, minvalue, maxvalue
@ -432,7 +411,7 @@ print "\n";
$auth = authenticate(); $auth = authenticate();
# get hostgroup list # get hostgroup list
$hostgroups = gethostgroups($auth); $hostgroups = gethostgroups();
# each hostgroup in list # each hostgroup in list
foreach $hostgroup(@{$hostgroups->{result}}) { foreach $hostgroup(@{$hostgroups->{result}}) {
@ -444,7 +423,7 @@ foreach $hostgroup(@{$hostgroups->{result}}) {
next if ((lc($name) eq "templates") || (lc($name) eq "discovered hosts") || (lc($name) eq "modèles")); next if ((lc($name) eq "templates") || (lc($name) eq "discovered hosts") || (lc($name) eq "modèles"));
# get hosts list # get hosts list
$hosts = gethosts($auth, $groupid); $hosts = gethosts($groupid);
print "HOSTGROUP: ".$name." (".$groupid.")"."\n"; print "HOSTGROUP: ".$name." (".$groupid.")"."\n";
@ -493,10 +472,10 @@ foreach $hostgroup(@{$hostgroups->{result}}) {
######### #########
# search for existing graphs and delete if found # search for existing graphs and delete if found
######### #########
deletegraphs($auth, $hostid, $graph); deletegraphs($hostid, $graph);
# get item list # get item list
$items = getitems($auth, $hostid); $items = getitems($hostid);
$count = 0; $count = 0;
# reset colorbase; # reset colorbase;
$colorbase = 0; $colorbase = 0;
@ -552,7 +531,7 @@ foreach $hostgroup(@{$hostgroups->{result}}) {
######### #########
# create a new graph # create a new graph
######### #########
creategraph($auth, $graph, $graphtype, $mintype, $maxtype, $minvalue, $maxvalue, $showtriggers, \@graph_item, $hostid); creategraph($graph, $graphtype, $mintype, $maxtype, $minvalue, $maxvalue, $showtriggers, \@graph_item, $hostid);
} }
} }