Ad Widget

Collapse

Zabbix template.get => {"jsonrpc":"2.0","result":[],"id":0}

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fabian1233
    Junior Member
    • Jun 2018
    • 3

    #1

    Zabbix template.get => {"jsonrpc":"2.0","result":[],"id":0}

    Hi,

    I'm currently trying to use the zabbix api to get a list of all template names and id's, but unfortunately I'm getting the following response: {"jsonrpc":"2.0","result":[],"id":0}

    An request looks ways like this:

    Code:
    class zabbix
    {
        private $authID = "<valid auth id>";
        private $url = "<url zur api>"
    
        private function sendRequest($data)
        {
            $query = json_encode($data);
    
            echo $query."<br><br>";
    
            $ch = curl_init($this->url);                                                                      
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);                                                                  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                'Content-Type: application/json'                                                                                                                                      
            ));                              
            return json_decode(curl_exec($ch), true);
        }
    
        public function getTemplates()
        {
            $request = array("jsonrpc" => "2.0",
                    "method" => "template.get",
                    "params" => array(
                        "output" => "simple",
                        "filter" => array(
                            "name" => array("Template OS Linux")
                        ),
                    ),
                    "id" => 0,
                    "auth" => $this->authID
                );
            print_r($this->sendRequest($request));
        }
    }
    Anyone has an idea what the problem is, or how I can debug this?

    Best regards,

    Fabian
    Last edited by fabian1233; 11-06-2018, 20:50.
  • kernbug
    Senior Member
    • Feb 2013
    • 330

    #2
    Originally posted by fabian1233
    Hi,



    Request JSON:

    {
    "jsonrpc":"2.0",
    "method":"template.get",
    "params":{
    "output":"extend",
    "sortfield":"name",
    "id":0,
    "auth":"a1b03e140e57468b8579155c4449d509"
    }

    Anyone has an idea what the problem is, or how I can debug this?

    Best regards,

    Fabian
    Hi, fabian1233

    You get an empty result. According to the official documentation you must specify requested "params": https://www.zabbix.com/documentation...e/template/get

    Comment

    • kloczek
      Senior Member
      • Jun 2006
      • 1771

      #3
      Feel free to check SH shell example about how to call template.get zabbix API
      zapish - Zabbix API SHell binding. Contribute to kloczek/zapish development by creating an account on GitHub.
      http://uk.linkedin.com/pub/tomasz-k%...zko/6/940/430/
      https://kloczek.wordpress.com/
      zapish - Zabbix API SHell binding https://github.com/kloczek/zapish
      My zabbix templates https://github.com/kloczek/zabbix-templates

      Comment

      • fabian1233
        Junior Member
        • Jun 2018
        • 3

        #4
        Originally posted by kernbug

        Hi, fabian1233

        You get an empty result. According to the official documentation you must specify requested "params": https://www.zabbix.com/documentation...e/template/get
        I've just updated the request json to:

        {
        "jsonrpc":"2.0",
        "method":"template.get",
        "params":{
        "output":"simple",
        "filter":{
        "host":[
        "Template OS Linux"
        ]
        }
        },
        "id":0,
        "auth":"d088f5eae1ea81e620cc1b05cf534353"
        }

        But stell got an empty result. Any idea, what the problem is?


        #EDIT:

        The full code looks similar to this:
        Code:
        class zabbix
        {
            private $authID = "<valid auth id>";
            private $url = "<url zur api>"
        
            private function sendRequest($data)
            {
                $query = json_encode($data);
        
                echo $query."<br><br>";
        
                $ch = curl_init($this->url);                                                                      
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
                curl_setopt($ch, CURLOPT_POSTFIELDS, $query);                                                                  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                    'Content-Type: application/json'                                                                                                                                      
                ));                              
                return json_decode(curl_exec($ch), true);
            }
        
            public function getTemplates()
            {
                $request = array("jsonrpc" => "2.0",
                        "method" => "template.get",
                        "params" => array(
                            "output" => "simple",
                            "filter" => array(
                                "name" => array("Template OS Linux")
                            ),
                        ),
                        "id" => 0,
                        "auth" => $this->authID
                    );
                print_r($this->sendRequest($request));
            }
        }
        Last edited by fabian1233; 11-06-2018, 20:50.

        Comment

        • kernbug
          Senior Member
          • Feb 2013
          • 330

          #5
          https://www.zabbix.com/documentation...e/template/get

          Tested on fresh install in AWS with OS X HS as client:

          Get Auth token:
          Code:
          curl  -H "Content-Type: application/json-rpc" -X POST  http://x.x.x.x/api_jsonrpc.php -d '{"jsonrpc":"2.0","method":"user.login","params":{ "user":"Admin","password":"zabbix"},"auth":null,"id":0}'
          Answer from Zabbix API:
          Code:
          {"jsonrpc":"2.0","result":"[COLOR=#FF0000]6fad8b8d9ddf367e55a21a56dca469b5[/COLOR]","id":0}
          Get Templates:
          Code:
          curl  -H "Content-Type: application/json-rpc" -X POST  http://x.x.x.x/api_jsonrpc.php -d '{"jsonrpc": "2.0",    "method": "template.get","params": {"output": "extend","filter": {"host": ["Template OS Linux","Template OS Windows"]}},"auth": "[COLOR=#FF0000]6fad8b8d9ddf367e55a21a56dca469b5[/COLOR]","id": 1}'
          Answer from Zabbix API:
          Code:
          {"jsonrpc":"2.0","result":[{"proxy_hostid":"0","host":"Template OS Linux","status":"3","disable_until":"0","error":"","available":"0","errors_from":"0","lastaccess":"0","ipmi_authtype":"-1","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":"","jmx_disable_until":"0","jmx_available":"0","jmx_errors_from":"0","jmx_error":"","name":"Template OS Linux","flags":"0","templateid":"10001","description":"","tls_connect":"1","tls_accept":"1","tls_issuer":"","tls_subject":"","tls_psk_identity":"","tls_psk":""},{"proxy_hostid":"0","host":"Template OS Windows","status":"3","disable_until":"0","error":"","available":"0","errors_from":"0","lastaccess":"0","ipmi_authtype":"-1","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":"","jmx_disable_until":"0","jmx_available":"0","jmx_errors_from":"0","jmx_error":"","name":"Template OS Windows","flags":"0","templateid":"10081","description":"","tls_connect":"1","tls_accept":"1","tls_issuer":"","tls_subject":"","tls_psk_identity":"","tls_psk":""}],"id":1}

          Comment

          Working...