diff --git a/roles/zabbix_lld_all_graph/files/zabbix_lld_all_graph b/roles/zabbix_lld_all_graph/files/zabbix_lld_all_graph index 046bc45..f9d0eb4 100755 --- a/roles/zabbix_lld_all_graph/files/zabbix_lld_all_graph +++ b/roles/zabbix_lld_all_graph/files/zabbix_lld_all_graph @@ -148,7 +148,12 @@ sub sendjson { $jsonstr = $_[0]; # 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 $i = index($res, "{"); # get json only @@ -161,7 +166,7 @@ sub sendjson { # authenticate with zabbix, returns the auth token sub authenticate { # 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 $res = sendjson($data); @@ -173,19 +178,10 @@ sub authenticate { return $auth_out; } -# get hostgroups from zabbix (auth) +# get hostgroups from zabbix () sub gethostgroups { - $auth_in = $_[0]; - # load hostgroups json - $data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1, "auth": "" }'; - # decode json - $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; - # encode back to data - $data = encode_json($dec); - + $data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1 }'; # send json $res = sendjson($data); # decode json @@ -194,17 +190,14 @@ sub gethostgroups { return $dec_out } -# get hosts from zabbix (auth, groupid) +# get hosts from zabbix (groupid) sub gethosts { - $auth_in = $_[0]; - $groupid_in = $_[1]; + $groupid_in = $_[0]; # 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 $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; # set groupid filter (outside filter) $dec->{"params"}->{"groupids"}[0] = $groupid_in; # encode back to data @@ -218,18 +211,15 @@ sub gethosts { return $dec_out; } -# get items from zabbix (auth, hostid) +# get items from zabbix (hostid) sub getitems { - $auth_in = $_[0]; - $hostid_in = $_[1]; + $hostid_in = $_[0]; # 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 $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; # set hostid filter $dec->{"params"}->{"filter"}->{"hostid"} = $hostid_in; # encode back to data @@ -243,18 +233,15 @@ sub getitems { return $dec_out } -# get graphs from zabbix (auth, hostid, graphname) +# get graphs from zabbix (hostid, graphname) sub getgraphs { - $auth_in = $_[0]; - $hostid_in = $_[1]; - $graph_in = $_[2]; + $hostid_in = $_[0]; + $graph_in = $_[1]; # 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 $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; # set name filter $dec->{"params"}->{"filter"}->{"name"} = $graph_in; # set hostid filter (outside filter) @@ -270,17 +257,14 @@ sub getgraphs { return $dec_out; } -# delete graph from zabbix (auth, graphid) +# delete graph from zabbix (graphid) sub deletegraph { - $auth_in = $_[0]; - $graphid_in = $_[1]; + $graphid_in = $_[0]; # 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 $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; # set graphid $hash[0] = $graphid_in; $dec->{"params"} = \@hash; @@ -297,14 +281,13 @@ sub deletegraph { return $dec_out; } -# search and delete existing graphs from zabbix (auth, hostid, graphname) +# search and delete existing graphs from zabbix (hostid, graphname) sub deletegraphs { - $auth_in = $_[0]; - $hostid_in = $_[1]; - $graph_in = $_[2]; + $hostid_in = $_[0]; + $graph_in = $_[1]; # get graph with name - $graphs = getgraphs($auth_in, $hostid_in, $graph_in); + $graphs = getgraphs($hostid_in, $graph_in); # each graph in list # filter graphs that do not belong to our hostid @@ -317,42 +300,38 @@ sub deletegraphs { print " "." "." "."Graph found: ".$graph_name." (".$graphid.")"."\n"; # 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 { #ymin_type = 0 -> calculated #ymin_type = 1 -> fixed #graphtype = 0 -> normal #graphtype = 1 -> stack - #auth - $auth_in = $_[0]; #graph name - $graph_in = $_[1]; + $graph_in = $_[0]; #graphtype, mintype, maxtype, minvalue, maxvalue - $graphtype_in = $_[2]; - $mintype_in = $_[3]; - $maxtype_in = $_[4]; - $minvalue_in = $_[5]; - $maxvalue_in = $_[6]; - $showtriggers_in = $_[7]; + $graphtype_in = $_[1]; + $mintype_in = $_[2]; + $maxtype_in = $_[3]; + $minvalue_in = $_[4]; + $maxvalue_in = $_[5]; + $showtriggers_in = $_[6]; #graphitems - $graphitems_in = $_[8]; + $graphitems_in = $_[7]; #hostid - $hostid_in = $_[9]; + $hostid_in = $_[8]; # load graphs json $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", - "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 $dec = decode_json($data); - # set auth - $dec->{"auth"} = $auth_in; # set graph name $dec->{"params"}->{"name"} = $graph_in; # set graphtype, mintype, maxtype, minvalue, maxvalue @@ -432,7 +411,7 @@ print "\n"; $auth = authenticate(); # get hostgroup list -$hostgroups = gethostgroups($auth); +$hostgroups = gethostgroups(); # each hostgroup in list 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")); # get hosts list - $hosts = gethosts($auth, $groupid); + $hosts = gethosts($groupid); print "HOSTGROUP: ".$name." (".$groupid.")"."\n"; @@ -493,10 +472,10 @@ foreach $hostgroup(@{$hostgroups->{result}}) { ######### # search for existing graphs and delete if found ######### - deletegraphs($auth, $hostid, $graph); + deletegraphs($hostid, $graph); # get item list - $items = getitems($auth, $hostid); + $items = getitems($hostid); $count = 0; # reset colorbase; $colorbase = 0; @@ -552,7 +531,7 @@ foreach $hostgroup(@{$hostgroups->{result}}) { ######### # 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); } }