Ad Widget

Collapse

Zabbix 2.0 - Updating Host IP with API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tom.maddox
    Junior Member
    • Jan 2012
    • 3

    #1

    Zabbix 2.0 - Updating Host IP with API

    Hi guys,

    I've just upgraded from zabbix 1.8 to 2.0 and expectedly, i've got to make some tweaks to my php API scaling scripts.

    in particular, I use this code to compare an AWS instance's IP and then update the zabbix host if necessary:

    Code:
    if ( !strcmp($current_instances[$ai]->nameTag,$current_hosts[$zi]['host']) ) {
        //AWS host already exists in Zabbix.
        if (strcmp($current_instances[$ai]->privateIpAddress,$current_hosts[$zi]['interfaces']["$interfaceID"]['ip'])) {
            //IP needs to be updated
            $updated_interface = array('interfaceid'=>$current_hosts[$zi]['interfaces']["$interfaceID"]['interfaceid'],'ip'=>$current_instances[$ai]->privateIpAddress->to_string());
            print_r($updated_interface);
            print_r($current_hosts[$zi]['interfaces']["$interfaceID"]);
            echo "    -/+ ".$current_hosts[$zi]['host']."   IP Update:";
            if ($this->update_host($current_hosts[$zi]['hostid'],'interfaces',$updated_interface)) {
                echo " SUCCESS\n";
                $changes_made=true;
            } else {
                $errormsg = ZABBIXAPI::getLastError();
                echo " ERROR: ".$errormsg['data']."\n";
            }
       }
       $found=true;
       $safe_zabbix_hosts[]=$zi;
       break;
    }

    The result coming back is:

    Code:
    Array
    (
        [interfaceid] => 36160
        [ip] => 10.32.7.186
    )
    Array
    (
        [interfaceid] => 36160
        [hostid] => 12061
        [main] => 1
        [type] => 1
        [useip] => 1
        [ip] => 10.32.7.187
        [dns] => **************
        [port] => 10050
    )
        -/+ HOSTNAME   IP Update: ERROR: Host cannot have more than one default interface of the same type.

    For some reason, even thought the interfaceid's are the same, it's trying to create a new interface rather than updating the existing one.

    Unfortunately there's no example of updating an interface in the API reference, so, after trying a few different things (including sending all of the fields, not just id and ip) i'm not sure how to proceed.

    Any help would be really appreciated.

    Thanks,
    T
    Last edited by tom.maddox; 23-05-2012, 19:00. Reason: tidied code up a bit
  • tom.maddox
    Junior Member
    • Jan 2012
    • 3

    #2
    Worked it out.
    The update function call should have been:

    Code:
    $this->update_host($current_hosts[$zi]['hostid'],'interfaces',[B]array($updated_interface)[/B])
    rather than:

    Code:
    $this->update_host($current_hosts[$zi]['hostid'],'interfaces',$updated_interface)
    As the API expects an array of interfaces.

    This is my function update_host just for reference:

    Code:
    function update_host($hostid,$parameter,$value) {
       return ZabbixAPI::fetch('host','update',array('hostid'=>$hostid,$parameter=>$value));
    }

    Comment

    • ghoz
      Senior Member
      • May 2011
      • 204

      #3
      Thank you for reporting the answer ...
      could you add a feature request in the tracker so the API document get an update ?


      Ghoz

      Comment

      Working...