laradji / zabbix

Zabbix chef cookbook
Apache License 2.0
91 stars 125 forks source link

Host update does not work #149

Open odyssey4me opened 10 years ago

odyssey4me commented 10 years ago

When changing the attributes for a host to update the host group or templates, the update library does not update the host's configuration.

The error:

Error executing action `create_or_update` on resource 'zabbix_host[zabbix-server.example.org]'

NoMethodError
-------------
undefined method `values' for #<Array:0x00000003d264e8>

Cookbook Trace:
---------------
/var/chef/cache/cookbooks/zabbix/providers/host.rb:166:in `block (2 levels) in class_from_file'
/var/chef/cache/cookbooks/zabbix/libraries/zabbix_connection.rb:19:in `call'
/var/chef/cache/cookbooks/zabbix/libraries/zabbix_connection.rb:19:in `with_connection'
/var/chef/cache/cookbooks/zabbix/providers/host.rb:115:in `block in class_from_file'
/var/chef/cache/cookbooks/zabbix/providers/host.rb:18:in `block (2 levels) in class_from_file'
/var/chef/cache/cookbooks/zabbix/libraries/zabbix_connection.rb:19:in `call'
/var/chef/cache/cookbooks/zabbix/libraries/zabbix_connection.rb:19:in `with_connection'
/var/chef/cache/cookbooks/zabbix/providers/host.rb:2:in `block in class_from_file'

Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/zabbix/recipes/agent_registration.rb

 64: zabbix_host node['zabbix']['agent']['hostname'] do
 65:   create_missing_groups true
 66:   server_connection connection_info
 67:   parameters ({
 68:     :host => node['hostname'],
 69:     :groupNames => node['zabbix']['agent']['groups'],
 70:     :templates => node['zabbix']['agent']['templates'],
 71:     :interfaces => interface_data
 72:   })
 73:   action :nothing
 74: end
 75:

Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/zabbix/recipes/agent_registration.rb:64:in `from_file'

zabbix_host("zabbix-server.example.org") do
  action [:nothing]
  retries 0
  retry_delay 2
  cookbook_name "zabbix"
  recipe_name "agent_registration"
  create_missing_groups true
  server_connection {:url=>"http://zabbix-server.example.org/api_jsonrpc.php", :user=>"admin", :password=>"mysecretpassword"}
  parameters {:host=>"zabbix-server", :groupNames=>["chef-agent", "Zabbix Servers"], :templates=>[], :interfaces=>[{:type=>1, :main=>1, :useip=>1, :ip=>"10.12.12.8", :dns=>"zabbix-server.example.org", :port=>"10050"}]}
  hostname "zabbix-server.example.org"
end

If I delete the host from zabbix, then re-run chef-client it works as expected.

odyssey4me commented 10 years ago

It would appear that the offending line is this one: https://github.com/laradji/zabbix/blob/master/providers/host.rb#L161

I changed host["interfaces"].values.map to host["interfaces"].map and it appears to resolve the problem without any unwanted side-effects.

bduong commented 10 years ago

+1

mikesplain commented 10 years ago

+1 That works for me too. This issue occurs any time I try set node['zabbix']['agent']['templates'] and @odyssey4me's solution seems to work for me.

laradji commented 10 years ago

tks @odyssey4me @guilhem your the expert on this part of the cookbook :), what do you think ?

nightw commented 10 years ago

It also fixed the problem for me, I'm already using it. Please apply the proposed fix.

frostyslav commented 10 years ago

I don't know if it is just mine setup, but changing host["interfaces"].values.map to host["interfaces"].map just lead me to a new error. Server answer API error: { "code": -32602, "message": "Invalid params.", "data": "Host cannot have more than one default interface of the same type." } So, it tries to create the existing interface. After a little digging it appeared that it existing_interface has keys as Strings, while desired_interface has keys as Symbols. That's why when there is a check if the interface already exists on https://github.com/laradji/zabbix/blob/master/providers/host.rb#L224, check fails. I have fixed it by adding desired_interface = Hash[desired_interface.map{ |k, v| [k.to_s, v] }] and existing_interface = Hash[existing_interface.map{ |k, v| [k.to_s, v] }] after line 222 and line 223 respectively.