I'm trying to use Zabbix JSON API to automate some monitoring stuff in our IT shop. I'd like to use the graph.create method described here : https://www.zabbix.com/documentation/2.2/manual/api/reference/graph/create
I'm struggling with the gitems array. It must contain hash tables (one per item in the graph), each with "itemid" and "color" rows.
This is part of my code :
#i get a list of itemids in $items
$colours=@("C04000", "800000", "191970", "3EB489", [...])
$params=@{}
$gitems=@() #an array of hash tables...
$c=0
foreach ($itemid in $items.result) {
$graphmember=@{}
$graphmember.add("itemid", $itemid)
$graphmember.add("color", $colours[$c])
$gitems += $graphmember
$c += 1
}
$params.add("gitems", $gitems)
#construct the JSON object
$objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graph.create' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
return $objgraph
Which, when called returns this :
{
"jsonrpc": "2.0",
"method": "graph.create",
"params": {
"gitems": [
"System.Collections.Hashtable",
"System.Collections.Hashtable",
"System.Collections.Hashtable",
"System.Collections.Hashtable",
"System.Collections.Hashtable"
]
},
"auth": "dc50acf4c337e5430c00936f998f74da",
"id": "2"
}
So i get 5 rows, which is the right number based on the arguments i supplied, but it seems like convertto-json doesnt like my object... can't figure out why.
I was not sure about the hash table in a array thing so i did a test and it seems to work :
$gitems=@()
$i1=@{}
$1.add("itemid","123")
$i1.add("color","blue")
$gitems += $i1
$i2=@{}
$i2.add("itemid","567")
$i2.add("color","yellow")
$gitems += $i2
$gitems
Name Value
---- -----
color bleu
itemid 123
color yellow
itemid 567
Thanks for ideas people!