Ad Widget

Collapse

ZabbixAPI Ruby Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clahti
    Senior Member
    • Jan 2007
    • 126

    #1

    ZabbixAPI Ruby Problems

    Hello all:

    I am using the http://rubygems.org/gems/zabbixapi Ruby library to access zabbix. We have a long running process that does the following:
    • gets a list of locations from our vendors
    • loop through the list and create a host for each one if necessary
    • creates two applications for each host
    • creates five items for each host for zabbix sender later on


    This seems like is should be simple but what is happening is:
    • hosts are created as expected
    • applications are created as expected for each host
    • the five items only appear on the *first* host, others do not get the items


    Here is the code, this is called for each host to be created
    Code:
        #connect to zabbix api
        zbx = ZabbixApi.connect(:url => 'http://<server>/zabbix/api_jsonrpc.php',
                  :user => 'zabbixapi',
                  :password => '*******',
                  :debug => true
                  )
        #get hostgroupid for MeterPaymentAPI Hosts
        zhostgroupid = zbx.hostgroups.get_id(:name => "MeterPaymentAPI Hosts")
        
        #add or update host entry and return key
        debug("ZABBIX - create_or_update host #{zabbix_host_id}")
        zhostid = zbx.hosts.create_or_update(:host => zabbix_host_id,
                  :groups => [:groupid => zhostgroupid],
                  :interfaces => [{
                            :type => 1,
                            :main => 1,
                            :useip => 1,
                            :ip => "127.0.0.1",
                            :dns => "",
                            :port => "10050"
                                }],
                  :status => 0
                  )
    
        #get_or_create General key
        debug("ZABBIX - get_or_create application General")
        zappgeneral = zbx.applications.get_or_create(
                  :name => "General",
                  :hostid => zhostid
                  )
        #get_or_create Metrics key
        debug("ZABBIX - get_or_create application Metrics")
        zappmetrics = zbx.applications.get_or_create(
                  :name => "Metrics",
                  :hostid => zhostid
                  )
    
        #create_or_update item timezone.offset
        debug("ZABBIX - #{zabbix_host_id} create_or_update item Time Zone Offset")
        zres = zbx.items.create_or_update(
                  :name => "Time Zone Offset",
                  :key_ => "timezone.offset",
                  :hostid => zhostid,
                  :type => 2,
                  :trapper_hosts => '',
                  :value_type => 0,
                  :applications => [zbx.applications.get_id(:name => "General")]
                  )
        debug("ZABBIX - result:#{zres}")
                  
        #create_or_update item ALL: Count
        debug("ZABBIX - #{zabbix_host_id} create_or_update item ALL: Count")
        zbx.items.create_or_update(
                  :name => "Type ALL: Count",
                  :key_ => "payment_type.all.count",
                  :hostid => zhostid,
                  :type => 2,
                  :trapper_hosts => '',
                  :value_type => 0,
                  :applications => [zbx.applications.get_id(:name => "Metrics")]
                  )
        debug("ZABBIX - result:#{zres}")
    
        #create_or_update item ALL: 90th Percent Latency
        debug("ZABBIX - #{zabbix_host_id} create_or_update item ALL: 90th Percent Latency")
        zbx.items.create_or_update(
                  :name => "Type ALL: 90th Percent Latency",
                  :key_ => "payment_type.all.percentile_90",
                  :hostid => zhostid,
                  :type => 2,
                  :trapper_hosts => '',
                  :value_type => 0,
                  :applications => [zbx.applications.get_id(:name => "Metrics")]
                  )
        debug("ZABBIX - result:#{zres}")
    
        #create_or_update item ALL: Early Payments
        debug("ZABBIX - #{zabbix_host_id} create_or_update item ALL: Early Payments")
        zbx.items.create_or_update(
                  :name => "Type ALL: Early Payments",
                  :key_ => "payment_type.all.early",
                  :hostid => zhostid,
                  :type => 2,
                  :trapper_hosts => '',
                  :value_type => 0,
                  :applications => [zbx.applications.get_id(:name => "Metrics")]
                  )
        debug("ZABBIX - result:#{zres}")
        zbx = nil
    Here are the log entries, no errors are returned but the items only show up on the first host
    Code:
    2013-08-16 01:44:56 [DEBUG] ZABBIX - processing node:mp-zzztest1,zzztest1,-28800
    2013-08-16 01:44:56 [DEBUG] ZABBIX - create_or_update host mp-zzztest1
    2013-08-16 01:44:56 [DEBUG] ZABBIX - get_or_create application General
    2013-08-16 01:44:56 [DEBUG] ZABBIX - get_or_create application Metrics
    2013-08-16 01:44:56 [DEBUG] ZABBIX - mp-zzztest1 create_or_update item Time Zone Offset
    2013-08-16 01:44:56 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:56 [DEBUG] ZABBIX - mp-zzztest1 create_or_update item ALL: Count
    2013-08-16 01:44:56 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:56 [DEBUG] ZABBIX - mp-zzztest1 create_or_update item ALL: 90th Percent Latency
    2013-08-16 01:44:57 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:57 [DEBUG] ZABBIX - mp-zzztest1 create_or_update item ALL: Early Payments
    2013-08-16 01:44:57 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:57 [DEBUG] ZABBIX - mp-zzztest1 adding item timezone.offset to /srv/sl_meter_payment_api_monitor/releases/201308111029/log/zabbix-sender.log
    
    2013-08-16 01:44:57 [DEBUG] ZABBIX - processing node:mp-zzztest2,zzztest2,-28800
    2013-08-16 01:44:57 [DEBUG] ZABBIX - create_or_update host mp-zzztest2
    2013-08-16 01:44:57 [DEBUG] ZABBIX - get_or_create application General
    2013-08-16 01:44:57 [DEBUG] ZABBIX - get_or_create application Metrics
    2013-08-16 01:44:57 [DEBUG] ZABBIX - mp-zzztest2 create_or_update item Time Zone Offset
    2013-08-16 01:44:57 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:57 [DEBUG] ZABBIX - mp-zzztest2 create_or_update item ALL: Count
    2013-08-16 01:44:58 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:58 [DEBUG] ZABBIX - mp-zzztest2 create_or_update item ALL: 90th Percent Latency
    2013-08-16 01:44:58 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:58 [DEBUG] ZABBIX - mp-zzztest2 create_or_update item ALL: Early Payments
    2013-08-16 01:44:58 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:58 [DEBUG] ZABBIX - mp-zzztest2 adding item timezone.offset to /srv/sl_meter_payment_api_monitor/releases/201308111029/log/zabbix-sender.log
    
    2013-08-16 01:44:58 [DEBUG] ZABBIX - processing node:mp-zzztest3,zzztest3,-28800
    2013-08-16 01:44:58 [DEBUG] ZABBIX - create_or_update host mp-zzztest3
    2013-08-16 01:44:58 [DEBUG] ZABBIX - get_or_create application General
    2013-08-16 01:44:58 [DEBUG] ZABBIX - get_or_create application Metrics
    2013-08-16 01:44:58 [DEBUG] ZABBIX - mp-zzztest3 create_or_update item Time Zone Offset
    2013-08-16 01:44:59 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:59 [DEBUG] ZABBIX - mp-zzztest3 create_or_update item ALL: Count
    2013-08-16 01:44:59 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:59 [DEBUG] ZABBIX - mp-zzztest3 create_or_update item ALL: 90th Percent Latency
    2013-08-16 01:44:59 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:59 [DEBUG] ZABBIX - mp-zzztest3 create_or_update item ALL: Early Payments
    2013-08-16 01:44:59 [DEBUG] ZABBIX - result:25071
    2013-08-16 01:44:59 [DEBUG] ZABBIX - mp-zzztest3 adding item timezone.offset to /srv/sl_meter_payment_api_monitor/releases/201308111029/log/zabbix-sender.log
    I am guessing the return value of 25071 after adding each item is the problem, but I don't know where the problem is, the code looks right to me! Can anyone help? I looked for an author of the zabbixapi Ruby project but there is no contact information or I would ask them.
  • clahti
    Senior Member
    • Jan 2007
    • 126

    #2
    Problem Identified

    So looking at the JSON output I can see what is happening, I think the ruby bindings are broken:
    • When calling items.create_or_update in the first loop iteration, the hostid to create this for is also passed.
    • The api looks up the item by name, failing to also filter on the hostid. In the first round the item does not exist so it is created on the first host in the loop
    • in the second loop the api does the same thing, looks up the item by name, no hostid passed so the item is found. The api then updates the existing item and does not create the item for the next-loop host
    • rinse/repeat.


    This is definitely a bug in the ruby gem code, how do we get this fixed upstream? I can probably work around the problem by avoiding the items.create_or_update method and making two calls, items.exists and items.create. I will let you all know how it goes.

    Comment

    Working...