|
require 'zabbixapi' |
|
require 'optparse' |
|
|
|
zabbix_api = nil |
|
zabbix_user = "admin" |
|
zabbix_password = "zabbix" |
|
host_info = {} |
|
group_names = [] |
|
template_names = [] |
|
host_info[:main] = 1 |
|
host_info[:port] = "10050" |
|
host_info[:type] = 1 |
|
host_info[:status] = 0 |
|
TYPE = {"agent" => 1, "SNMP" => 2, "IPMI" => 3, "JMX" => 4} |
|
|
|
opt = OptionParser.new |
|
opt.on('-s', '--zabbix-server=VAL') { |v| zabbix_api = "#{v}/zabbix/api_jsonrpc.php" } |
|
opt.on('-u', '--zabbix-user=VAL', "default:#{zabbix_user}") { |v| zabbix_user = v } |
|
opt.on('-p', '--zabbix-password=VAL', "default:#{zabbix_password}") { |v| zabbix_password = v } |
|
opt.on('--host=VAL') { |v| host_info[:host] = v } |
|
opt.on('--ip=VAL') { |v| host_info[:ip] = v } |
|
opt.on('--dns=VAL') { |v| host_info[:dns] = v } |
|
opt.on('-g', '--group-name=VAL') { |v| group_names << v } |
|
opt.on('-t', '--template-name=VAL') { |v| template_names << v } |
|
opt.on('--type=VAL', "default:#{TYPE.invert[host_info[:main]]},select from #{TYPE.keys.join(",")}") { |v| |
|
host_info[:type] = TYPE[v.to_i] } |
|
opt.on('--port=VAL', "default:#{host_info[:port]}") { |v| |
|
host_info[:port] = v } |
|
opt.on('--main-default', 'default') { |v| host_info[:main] = 1 } |
|
opt.on('--main-not-default') { |v| host_info[:main] = 0 } |
|
opt.on('--status-monitored', 'default') { |v| host_info[:status] = 0 } |
|
opt.on('--status-unmonitored') { |v| host_info[:status] = 1 } |
|
opt.parse!(ARGV) |
|
|
|
raise ArgumentError.new("Specify zabbix server") unless zabbix_api |
|
raise ArgumentError.new("Specify host") unless host_info[:host] |
|
raise ArgumentError.new("Specify group-name") if group_names.empty? |
|
|
|
if host_info[:ip].nil? && host_info[:dns].nil? |
|
raise ArgumentError.new("Specify dns or ip") |
|
elsif host_info[:ip] |
|
host_info[:useip] = 1 |
|
host_info[:dns] = "" unless host_info[:dns] |
|
else |
|
host_info[:useip] = 0 |
|
host_info[:ip] = "0.0.0.0" unless host_info[:ip] |
|
end |
|
|
|
zbx = ZabbixApi.connect(:url => zabbix_api, :user => zabbix_user, :password => zabbix_password) |
|
|
|
host_info[:interfaces] = {} |
|
group_names.each do |group_name| |
|
host_info[:groups] ||= [] |
|
host_info[:groups] << {"groupid" => zbx.hostgroups.get_id(:name => group_name)} |
|
raise ArgumentError.new("not exist group name[#{group_name}]") unless host_info[:groups].last |
|
end |
|
template_names.each do |template_name| |
|
host_info[:templates] ||= [] |
|
host_info[:templates] << {"templateid" => zbx.templates.get_id(:host => template_name)} |
|
raise ArgumentError.new("not exist template name[#{group_name}]") unless host_info[:templates].last |
|
end |
|
|
|
p zbx.hosts.create_or_update(host_info) |