![]() |
![]() |
| Subject: | Zenoss 4.2.5 JSON API getDevices() method |
| Author: | Davide Mancosu |
| Posted: | 2020-11-24 03:41 |
Hi,
Premise:
I have a class in my Laravel 5.5 application to interface itself to Zenoss 4.2.5 server.
In the application controller I call the getDeviceInfo() method after creating the zenoss object.
This is the code for class Zenoss:
class Zenoss{ private $tmp; private $protocol; private $address; private $port; private $username; private $password; private $cookie; function __construct($address,$username='admin',$password='password',$port='8080',$tmp='/tmp/',$protocol='http') { if(!is_writable($tmp)) throw new Exception('Specified cookie file directory does not exist or is not writable.'); $this->address = $address; $this->username = $username; $this->password = $password; $this->port = $port; $this->tmp = $tmp; $this->protocol = $protocol; $this->cookie = $tmp."zenoss_cookie.txt"; } private function zenQuery(array $data, $uri) { $data['tid'] = 1; $data['type'] = "rpc"; $ch = curl_init("{$this->protocol}://{$this->address}:{$this->port}/zport/acl_users/cookieAuthHelper/login"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8')); curl_exec($ch); curl_setopt($ch, CURLOPT_URL, "{$this->protocol}://{$this->address}:{$this->port}{$uri}"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $result = curl_exec($ch); if($result===false){ Log::info('Zenoss::zenQuery - curl_exec error: '.curl_error($ch)); }// throw new Exception('Curl error: ' . curl_error($ch));
curl_close($ch); Log::info($result); print_r($result); //return back(); } public function getDevices($start=0, $limit=100, $sort="name", $dir="ASC") { Log::info('Zenoss::getDevices'); $json_params = array(); $json_data = array(); $json_main = array(); $json_keys = array("name", "ipAddress", "productionState", "groups", "location"); $json_data['keys'] = $json_keys; $json_data['dir'] = $dir; $json_data['limit'] = $limit; $json_data['sort'] = $sort; $json_data['start'] = $start; $json_data['params'] = $json_params; $json_main['action'] = "DeviceRouter"; $json_main['method'] = "getDevices"; $json_main['data'] = $json_data; return $this->zenQuery($json_main, '/zport/dmd/Devices/getSubDevices'); }
}This is the method code of laravel controller:
public function list_all_devices(){ Log::info('ZenosController::list_all_devices'); $zenoss = new Zenoss('10.10.10.10'); $devices = $zenoss->getDevices(); }
The problem
I expect it to return the information passed in the keys argument in the getDevices() method of the JSON API invoked by the
getDevices($start=0, $limit=100, $sort="name", $dir="ASC") method of the class Zenoss.That is:
$json_keys = array("name", "ipAddress", "productionState", "groups", "location");But it returns only the uid of the three devices loaded in my Zenoss LAB:
There seems to be a mistake. But I don't understand which one.
Question:
Where am I doing wrong?
| Subject: | RE: Zenoss 4.2.5 JSON API getDevices() method |
| Author: | Davide Mancosu |
| Posted: | 2020-11-24 04:45 |
curl -u "admin:password" -X POST -H "Content-Type: application/json" -d '{"action": "DeviceRouter", "method": "getDevices", "data": [{"uid": "/zport/dmd/Devices/"}], "tid": 1}' http://10.xxx.xxx.xxx:8080/zport/dmd/device_routerreturn $this->zenQuery($json_main, '/zport/dmd/Devices/getSubDevices');return $this->zenQuery($json_main, '/zport/dmd/Devices/device_router');public function getDeviceInfo()public function getDeviceInfo($deviceURI="/zport/dmd/Devices/")Hi,
Premise:
I have a class in my Laravel 5.5 application to interface itself to Zenoss 4.2.5 server.
In the application controller I call the getDeviceInfo() method after creating the zenoss object.
This is the code for class Zenoss:
class Zenoss{ private $tmp; private $protocol; private $address; private $port; private $username; private $password; private $cookie; function __construct($address,$username='admin',$password='password',$port='8080',$tmp='/tmp/',$protocol='http') { if(!is_writable($tmp)) throw new Exception('Specified cookie file directory does not exist or is not writable.'); $this->address = $address; $this->username = $username; $this->password = $password; $this->port = $port; $this->tmp = $tmp; $this->protocol = $protocol; $this->cookie = $tmp."zenoss_cookie.txt"; } private function zenQuery(array $data, $uri) { $data['tid'] = 1; $data['type'] = "rpc"; $ch = curl_init("{$this->protocol}://{$this->address}:{$this->port}/zport/acl_users/cookieAuthHelper/login"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8')); curl_exec($ch); curl_setopt($ch, CURLOPT_URL, "{$this->protocol}://{$this->address}:{$this->port}{$uri}"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $result = curl_exec($ch); if($result===false){ Log::info('Zenoss::zenQuery - curl_exec error: '.curl_error($ch)); }// throw new Exception('Curl error: ' . curl_error($ch));
curl_close($ch); Log::info($result); print_r($result); //return back(); } public function getDevices($start=0, $limit=100, $sort="name", $dir="ASC") { Log::info('Zenoss::getDevices'); $json_params = array(); $json_data = array(); $json_main = array(); $json_keys = array("name", "ipAddress", "productionState", "groups", "location"); $json_data['keys'] = $json_keys; $json_data['dir'] = $dir; $json_data['limit'] = $limit; $json_data['sort'] = $sort; $json_data['start'] = $start; $json_data['params'] = $json_params; $json_main['action'] = "DeviceRouter"; $json_main['method'] = "getDevices"; $json_main['data'] = $json_data; return $this->zenQuery($json_main, '/zport/dmd/Devices/getSubDevices'); }
}This is the method code of laravel controller:
public function list_all_devices(){ Log::info('ZenosController::list_all_devices'); $zenoss = new Zenoss('10.10.10.10'); $devices = $zenoss->getDevices(); }
The problem
I expect it to return the information passed in the keys argument in the getDevices() method of the JSON API invoked by the
getDevices($start=0, $limit=100, $sort="name", $dir="ASC") method of the class Zenoss.That is:
$json_keys = array("name", "ipAddress", "productionState", "groups", "location");But it returns only the uid of the three devices loaded in my Zenoss LAB:
There seems to be a mistake. But I don't understand which one.
Question:
Where am I doing wrong?
| < |
Previous Access previous metric values from Data Source |
Next How to set "command timeout" value for a particular datasource |
> |