Ad Widget

Collapse

Zabbix API 1.0 -> hostgroup.getid problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jhgrc
    Member
    • Jun 2009
    • 52

    #1

    Zabbix API 1.0 -> hostgroup.getid problem

    I try to search hostgroup ID by the name using PHP library provided else where on this forum.

    I believe my request is correct but Zabbix API returns an error. Can someone help?

    Zabbix hostgroups contains (from output of my PHP):
    Code:
        [11] => Array
            (
                [groupid] => 13
                [name] => IPX CPE routers
                [internal] => 0
            )
    Source code causing the problem is:
    Code:
    // Get hostgroupid by name "IPX CPE routers"
    $ipxcpeid = ZabbixAPI::fetch_string('hostgroup','getid',array('name'=>"IPX CPE routers"))
        or die('Unable to get hostgroup ID: '.print_r(ZabbixAPI::getLastError(),true));
    echo "IPX CPE routers ID: $ipxcpeid\n";
    Result with debugging on is:
    Code:
    Server running Zabbix API Version: 1.0
    CURL URL: http://localhost/zabbix/api_jsonrpc.php
    <br>CURL Options: Array
    (
        [19913] => 1
        [13] => 30
        [78] => 5
        [81] =>
        [64] =>
        [52] => 1
        [74] => 1
        [10023] => Array
            (
                [0] => Content-Type: application/json-rpc
                [1] => User-Agent: ZabbixAPI v1.0 - http://andrewfarley.com/zabbix_php_api
            )
    
        [10036] => POST
        [10015] => {"auth":"d48fcf3995dd33c3bb003d4463562087","method":"hostgroup.getid","id":1,"params":{"name":"IPX CPE routers"},"jsonrpc":"2.0"}
        [10102] => gzip
    )
    Got response from API: ({"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Action does not exists"},"id":1})
    Response decoded: (Array
    (
        [jsonrpc] => 2.0
        [error] => Array
            (
                \[code\] => -32602
                [message] => Invalid params.
                [data] => Action does not exists
            )
    
        [id] => 1
    )
    
    Unable to get hostgroup ID: Array
    (
        \[code\] => -32602
        [message] => Invalid params.
        [data] => Action does not exists
    )
  • Aly
    ZABBIX developer
    • May 2007
    • 1126

    #2
    Use hostgroup.getObjects method.
    PHP Code:
    // Get hostgroupid by name "IPX CPE routers"
    $ipxcpeid ZabbixAPI::fetch_string('hostgroup','getObjects',array('name'=>"IPX CPE routers"))
        or die(
    'Unable to get hostgroup ID: '.print_r(ZabbixAPI::getLastError(),true));
    echo 
    "IPX CPE routers ID: $ipxcpeid\n"
    Zabbix | ex GUI developer

    Comment

    • jhgrc
      Member
      • Jun 2009
      • 52

      #3
      Thanks.

      I just wonder if HTML-based documentation is lacking such much, how community can do the scripts to enhance Zabbix?

      http://www.zabbix.com/documentation/...ects/hostgroup

      Does not mention getObjects, and lists GetID as valid query. Is there some other better place for API documentation?

      For example how do I delete items? I suspect I can use itemid?

      Originally posted by http://www.zabbix.com/documentation/1.8/api/objects/item#add
      6. delete

      Available since version: 1.8
      Parameters:
      Parameter Type Optional Description Details

      Returns: array|boolean
      Last edited by jhgrc; 21-01-2010, 10:11.

      Comment

      • Aly
        ZABBIX developer
        • May 2007
        • 1126

        #4
        I think documentation will be updated after 1.8.1 release, due to changes we make in API during bug fixes and performance improvements it's not up to date, sorry.

        We replaced almost every occurrence of parameter like array of ids, by array of objects.
        For example host.delete():

        was: [10012, 10013, ...]
        now: [{'hostid': 10012}, {'hostid': 10013}, ...)

        Exception is *.get(), where one of possible parameters could be (for example) hostids: [10012, 10013]

        Hope that helps.
        Zabbix | ex GUI developer

        Comment

        • jhgrc
          Member
          • Jun 2009
          • 52

          #5
          Thanks, now I get a bit more about how API functions. I am not so familiar with the Zabbix internals but so far I've achieved fairly nice software which adds SNMP queries of interfaces to Zabbix.

          But next problem is that I would like to tie searched hosts to templates. So I'd like to probe all routers which are linked with template 'A' for example. I can get templateid of 'A', but how can I list hosts linked with 'A'.

          I see API calls linkhosts and unlinkhosts, but where is getlinkedhosts or similar? And host object does not have templateid probably because on SQL the system is via table 'hosts_templates'

          Similar - I cannot get into my head how to use application (via API) to divide added items to for example "interface traffic" and "interface info".

          ***
          So I've achieved so far:
          - get hostgroup ID and retrieve hosts belonging to that hostgroup
          - fetch items per host
          - seek items for valid SNMP query (type=4, status=0, error='')
          - use this found item's community to access router
          - so you need to have one working SNMP query to router
          - fetch interface list of host via SNMP with admin mode enabled
          - compare items & snmp list
          - add missing interfaces to zabbix
          - bps & pps & max speed & description etc.
          - remove disabled from zabbix

          TODO:
          - update changed entries to Zabbix
          - interface description change for example
          - create graphs & graphitems per interface
          - create triggers
          - 70% of link capacity
          - port up/down change
          - create actions for triggers
          - create screen for host
          - make PHP-software runnable via (now running from shell)
          - hosts.php for single hostid
          - templates.php for template
          - hostgroups.php for hostgroups


          This is fairly complex thing to do. Doing graphs etc is not easy task just by looking examples.

          Also "addupdate" method would be fine, like if 'key_' exists then update item, of not found then add.

          Or perhaps I should do direct PHP-stuff and update SQL. And stop using Zabbix API.

          Comment

          • Aly
            ZABBIX developer
            • May 2007
            • 1126

            #6
            Originally posted by jhgrc
            But next problem is that I would like to tie searched hosts to templates. So I'd like to probe all routers which are linked with template 'A' for example. I can get templateid of 'A', but how can I list hosts linked with 'A'.

            I see API calls linkhosts and unlinkhosts, but where is getlinkedhosts or similar? And host object does not have templateid probably because on SQL the system is via table 'hosts_templates'
            Paste to host.get parameter "templateids": [1213,123123,3231,...]
            Zabbix | ex GUI developer

            Comment

            • Jason
              Senior Member
              • Nov 2007
              • 430

              #7
              I'm trying same thing, but can't see any item returned called templateids... I'm using release 1.8 version... Is the templateids part of the 1.8.1 release?

              Comment

              • Aly
                ZABBIX developer
                • May 2007
                • 1126

                #8
                add parameter like 'extendoutput' : 1 and 'select_templates': 1.
                Zabbix | ex GUI developer

                Comment

                • Jason
                  Senior Member
                  • Nov 2007
                  • 430

                  #9
                  Ah... I'm getting the info in a slightly different way, but I can see...

                  <br>Retrieved maximum of five hosts: Array
                  (
                  [0] => Array
                  (
                  [templates] => Array
                  (
                  [0] => Array
                  (
                  [hosts] => Array
                  (
                  [0] => Array
                  (
                  [hostid] => 100100000010028
                  )
                  [hostid] => 100100000010001
                  [proxy_hostid] => 0
                  [host] => Template_Linux


                  The [hostids] under the [hosts] array shows all the hosts that the template is applied to, which is what I'm looking for...

                  Comment

                  • ugh
                    Senior Member
                    • Jun 2009
                    • 296

                    #10
                    Originally posted by Aly

                    For example host.delete():

                    was: [10012, 10013, ...]
                    now: [{'hostid': 10012}, {'hostid': 10013}, ...)
                    )))
                    and now vice versa

                    Comment

                    Working...