![]() |
![]() |
Subject: | List Devices Using cUrl |
Author: | [Not Specified] |
Posted: | 2014-11-26 10:07 |
Hi Guys,
I am trying to monitor a dynamic IP system using Zenoss. I have my own DNS service and have written a PHP script to add users and update their IP every ten minutes using cUrl. This is working perfectly and I'm really happy with the system. The final thing I need to do is delete devices that are no longer used. So effectively I need to get a list of all devices on Zenoss, compare them against the true devices and remove any that do not match.
I am struggling to get the list of devices.
If I navigate to the following url I can see all the devices in an array. However, when using cUrl rather than seeing the devices, I get an empty array with commas, so it seems to get the right count of devices, but does not display any details like when navigating via a web browser.
http://myurl:8080/zport/dmd/Devices/devices
Here is the code I am using:
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$zenossusername:$zenossPassword");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, 'cookie.txt' );
curl_setopt( $ch, CURLOPT_COOKIEFILE, 'cookie.txt' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$url = "http://myurl:8080/zport/dmd/Devices/devices";
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
Subject: | You're going to need to use |
Author: | [Not Specified] |
Posted: | 2014-11-26 10:58 |
You're going to need to use curl + the JSON API to return device information
Subject: | working with the JSON API |
Author: | [Not Specified] |
Posted: | 2014-11-26 11:00 |
Not a whole lot of great examples about using the JSON api via curl out there. I've been doing a lot of work with it myself lately. I found this little script to be real useful to start: http://wiki.zenoss.org/Working_with_the_JSON_API
Once you've got that thing, the method to list the hosts out is as simple as
zenoss_api device_router DeviceRouter getDevices "{\"limit\":2000}"
What you get back is a huge string of JSON. I'm a perl guy so what I've done is convert that zenoss_api shell function from json_api.sh into a Perl subroutine. Then, there's a perl module JSON::Parse that converts JSON to a data structure of arrays and hashes. In perl you could do this:
#assuming the output you get from zenoss_api is in $output
my $parsed=parse_json($output)
my $total_devices = %$parsed->{'result'}->{'totalCount'};
for (my $i=0;$i<$total_devices;$i++) {
print "Host $i : " . %$parsed->{'result'}->{'devices'}[$i]->{'name'} . "\n";
}
What you need is an equivalent JSON to data structure method for PHP.
Dave
< |
Previous Zenoss Windows Integrated Login |
Next Maintenance windows not working |
> |