ZenPacks, JSON API, and Integrations

Expand all | Collapse all

Zenoss 4.2.5 JSON API getDevices() method

  • 1.  Zenoss 4.2.5 JSON API getDevices() method

    Posted 11-24-2020 02:41 AM

    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:

    [2020-11-24 08:16:30] local.INFO: [<Device at /zport/dmd/Devices/Network/Router/Cisco/devices/c3750.xx-xxxxxx-ull-01>, <Device at /zport/dmd/Devices/Server/Linux/devices/10.xxx.10.xxx>, <Device at /zport/dmd/Devices/Server/Linux/devices/ZenLab>]

    There seems to be a mistake. But I don't understand which one.
    Question:
    Where am I doing wrong? 



    ------------------------------
    Davide Mancosu
    system administartor
    Tiscali Italia S.p.A.
    Cagliari
    ------------------------------


  • 2.  RE: Zenoss 4.2.5 JSON API getDevices() method

    Posted 11-24-2020 03:45 AM
    Edited by Davide 11-24-2020 03:47 AM
    I tried the curl command from another linux server:
    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_router

    and it works!
    I changed my code:
    in my class from:
    return $this->zenQuery($json_main'/zport/dmd/Devices/getSubDevices');
    to:
    return $this->zenQuery($json_main'/zport/dmd/Devices/device_router');

    and in my class from:
    public function getDeviceInfo()
    to:
    public function getDeviceInfo($deviceURI="/zport/dmd/Devices/")

    It return a zenoss error page to report the error details .

    Where am I doing wrong again?

    thanks

    ------------------------------
    Davide Mancosu
    system administartor
    Tiscali Italia S.p.A.
    Cagliari
    ------------------------------