Ad Widget

Collapse

Change IP of existing interface with JSON - 2.0 old way not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crye
    Junior Member
    • Aug 2011
    • 10

    #1

    Change IP of existing interface with JSON - 2.0 old way not working

    Folks,

    Seems the multi-interface changes to host definitions breaks the simple ability to change a hosts IP via a simple JSON such as:

    Code:
    {
       "jsonrpc":"2.0",
       "method":"host.update",
       "params":{
          "hostid": "10095",
            "ip":"2.2.2.2"
    
       },
       "auth":"5ba6879c5744e79306665bc5656044a3",
       "id":1
      }
    I see reference elsewhere to needing to send an array of "interfaces" data but
    can someone outline what the minimalist requirement in JSON for this would be. I sure hope I don't have to query the host for hostiD, then query the host ID for interfacess list etc etc etc. Is there some default in cases where you know it's always going to be the primary (and only) interface.

    Thanks
    ~
  • crye
    Junior Member
    • Aug 2011
    • 10

    #2
    Resolved -

    For others:

    After mucking about in the frontend code...

    This is for single interfaced systems only.

    * Query the host for it's interface ID.

    Code:
    {
    "jsonrpc":"2.0",
    "method":"host.get",
    "params":{
            "selectInterfaces":"extend",
        "filter":{
            "host":"aaoffice"
        }
    },
    "auth":"222e770630dfdd4cec6795e2a18b2d63",
    "id":2
    }
    Then update the Interface:

    Code:
    {
       "jsonrpc":"2.0",
       "method":"hostinterface.update",
       "params":{
          "interfaceid": "31162",
          "ip":"3.3.3.3"
       },
       "auth":"f1bfd7e2e804cccd034095212f713572",
       "id":1
      }

    Comment

    • kevind
      Member
      • Sep 2011
      • 40

      #3
      Here's a perl example which will get and change the SNMP IP address for a hostname in Zabbix 2.0, even if the host has multiple interfaces. It looks through the host's interface table and finds the SNMP interface.

      This code might also be useful as an example of how to parse some of the more complex results that can be returned by the Zabbix 2.0 JSON API.

      It requires the perl packages JSON and JSON::XS (available as RPMs perl-JSON and perl-JSON-XS).

      Code:
      #!/usr/bin/perl
      use strict;
      use JSON -support_by_pp;
      use JSON::XS qw(!to_json !from_json);
      use LWP::UserAgent;
      
      # Change the variables below:
      my $url="http://your_url/zabbix/api_jsonrpc.php";   # url for Zabbix 2.0 JSON API
      my $apiuser="your_username";          # API User's Username on Zabbix server
      my $apipassword="your_password";
      my $hostname="your_hostname";
      my $new_ip_address="your_new_ip_address";
      
      my $hostid;                           # Internal hostid number from Zabbix
      my $interfaceid;                      # Internal interfaceid number from Zabbix
      my $current_ip_address;               # current IP address configured in Zabbix
      
      my $auth = login($url, $apiuser, $apipassword);		# Authenticate against Zabbix API
      get_host_snmp_interface($hostname);			# lookup SBC's SNMP interfaceid and ipaddress in zabbix using JSON API
      set_interface_ip_address($interfaceid,$new_ip_address);
      exit;
      
      ##########################
      # Login to Zabbix
      ##########################
      sub login {
          my ($url, $user, $password) = @_;
          my $json_login = JSON::XS->new->pretty(0)->encode(	
          {
              jsonrpc => 2.0,
              method => "user.login",
              params => {
                  user => "$user",
                  password => "$password",
              },
              id => 1
          });
          my $req = HTTP::Request->new( 'POST', $url );
          $req->header( 'Content-Type' => 'application/json-rpc' );
          $req->content( $json_login );
          my $ua = LWP::UserAgent->new;
          my $response = $ua->request( $req );
          my $jhsh = from_json($response->decoded_content);
          my $auth_token = $jhsh->{result};
          if(!$auth_token){
              print "ERROR - Zabbix login failed.\n";
              exit(1);
          } 
          return $auth_token
      }
      
      ###########################################
      # Subroutine to query Zabbix to get hostid and SNMP interfaceid and IP address
      #
      sub get_host_snmp_interface{
          my $hostname = shift;
          my $json_host_get = JSON::XS->new->pretty(0)->encode(
          {   jsonrpc => 2.0,
              method => "host.get",
              params => {
                  selectInterfaces => "extend",
                  filter => {
                      host => "$hostname",
                  },
              },
              auth => "$auth",
              id => 2
          });
          my $req = HTTP::Request->new( 'POST', $url );
          $req->header( 'Content-Type' => 'application/json-rpc' );
          $req->content( $json_host_get );
          my $ua = LWP::UserAgent->new;
          my $response = $ua->request( $req );
          my $jhsh = from_json($response->decoded_content);
          my $result_array = $jhsh->{result};
          my $interface_hash = @$result_array[0]->{interfaces};
          for my $interface (keys %$interface_hash) {
      	if ( $interface_hash->{$interface}{type} eq "2") {	# look for SNMP interface
                  $hostid = $interface_hash->{$interface}{hostid};
                  $interfaceid = $interface_hash->{$interface}{interfaceid};
                  $current_ip_address = $interface_hash->{$interface}{ip};
              }
          }
          if(!$interfaceid){
              print "ERROR - SNMP interface for $hostname not found in Zabbix.\n";
              exit(1);
          }
      }
      
      #################################################
      # Subroutine to set new host ip address in Zabbix
      #
      sub set_interface_ip_address{
          my ($interfaceid,$new_interface_ip_address) = @_;
          my $json_hostinterface_update = JSON::XS->new->pretty(0)->encode(
          {   jsonrpc => 2.0,
              method => "hostinterface.update",
              params => {
                  interfaceid => "$interfaceid",
                  ip => "$new_interface_ip_address",
              },
              auth => "$auth",
              id => 2
          });
          my $req = HTTP::Request->new( 'POST', $url );
          $req->header( 'Content-Type' => 'application/json-rpc' );
          $req->content( $json_hostinterface_update );
          my $ua = LWP::UserAgent->new;
          my $response = $ua->request( $req );
          my $jhsh = from_json($response->decoded_content);
          my $result = $jhsh->{result};
          my $interface_array = $result->{interfaceids};
          if( @$interface_array[0] ne $interfaceid ) {
              print "ERROR - could not set interface $interfaceid to ip address $new_interface_ip_address in Zabbix.\n";
              exit(1);
          }
      }

      Comment

      Working...