I try to grab all my host from check_mk with http-api "action=get_all_hosts"
the response is json
format and look like this:
"{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}"
Now I try to format the response without success. How can I format the result to table
with all the properties?
That JSON you pasted is incorrect. It shouldn't have quotes at the beginning nor at the end. And you're missing one }
at the end. You can validate it using any online tool like this.
Once you have the correct JSON which should be:
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
You can access the attributes once you convert them from JSON:
# Convert and save to variable
$convertedJSON = @"
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
"@ | ConvertFrom-Json
# Access attributes
$convertedJSON.result.'some host name'.attributes
# If you don't know the hostname you can find it like this
($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name
# List all attributes from your JSON
$convertedJSON.result.$(($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name).attributes
# Output will be like this
tag_Chassis : Vm
tag_ServerFamily : WindowsServer
tag_criticality : prod
tag_Application : AllApp
alias : some alias
ipaddress : 172.21.x.x
tag_networking : lan