-
Notifications
You must be signed in to change notification settings - Fork 126
/
agent_registration.rb
90 lines (82 loc) · 2.56 KB
/
agent_registration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Author:: Guilhem Lettron (<guilhem.lettron@youscribe.com>)
# Cookbook Name:: zabbix
# Recipe:: agent_registration
#
# Apache 2.0
#
if !Chef::Config[:solo]
zabbix_server = search(:node, 'recipe:zabbix\\:\\:server').first
else
if node['zabbix']['web']['fqdn']
zabbix_server = node
else
Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
Chef::Log.warn("If you did not set node['zabbix']['web']['fqdn'], the recipe will fail")
return
end
end
connection_info = {
:url => "http://#{zabbix_server['zabbix']['web']['fqdn']}/api_jsonrpc.php",
:user => zabbix_server['zabbix']['web']['login'],
:password => zabbix_server['zabbix']['web']['password']
}
ip_address = node['ipaddress']
if node['zabbix']['agent']['network_interface']
target_interface = node['zabbix']['agent']['network_interface']
if node['network']['interfaces'][target_interface]
ip_address = node['network']['interfaces'][target_interface]['addresses'].keys[1]
Chef::Log.debug "zabbix::agent_registration : Using ip address of #{ip_address} for host"
else
Chef::Log.warn "zabbix::agent_registration : Could not find interface address for #{target_interface}, falling back to default"
end
end
interface_definitions = {
:zabbix_agent => {
:type => 1,
:main => 1,
:useip => 1,
:ip => ip_address,
:dns => node['fqdn'],
:port => node['zabbix']['agent']['zabbix_agent_port']
},
:jmx => {
:type => 4,
:main => 1,
:useip => 1,
:ip => ip_address,
:dns => node['fqdn'],
:port => node['zabbix']['agent']['jmx_port']
},
:snmp => {
:type => 2,
:main => 1,
:useip => 1,
:ip => ip_address,
:dns => node['fqdn'],
:port => node['zabbix']['agent']['snmp_port']
}
}
interface_list = node['zabbix']['agent']['interfaces']
interface_data = []
interface_list.each do |interface|
if interface_definitions.key?(interface.to_sym)
interface_data.push(interface_definitions[interface.to_sym])
else
Chef::Log.warn "WARNING: Interface #{interface} is not defined in agent_registration.rb"
end
end
zabbix_host node['zabbix']['agent']['hostname'] do
create_missing_groups true
server_connection connection_info
parameters(
:host => node['hostname'],
:groupNames => node['zabbix']['agent']['groups'],
:templates => node['zabbix']['agent']['templates'],
:interfaces => interface_data
)
action :nothing
end
log 'Delay agent registration to wait for server to be started' do
level :debug
notifies :create_or_update, "zabbix_host[#{node['zabbix']['agent']['hostname']}]", :delayed
end