#!/usr/local/bin/ruby -w
require "zabbixapi"
#get the host from input line
host_receive_count="#{ARGV.size}"
if "#{host_receive_count}" !="1"
puts "only one at one time Use:ruyb zabbix_delte_host.rb <FQDN>"
else
puts "#{ARGV[0]}"
host_name="#{ARGV[0]}"
zbx = ZabbixApi.connect(
:url => 'https://localhost/zabbix/api_jsonrpc.php',
:user => 'testuser',
:password => 'testpassword',
#:debug => true
)
#get the host we want to delte
hosts = zbx.query(
:method => "host.get",
:params => {
:filter => {
:host => "#{host_name}"
},
:output => ['hostid','host']
}
)
host=Hash.new
length="#{hosts.length}"
puts "#{hosts.length}"
#delte one at one time
if "#{hosts.length}"!="1"
puts "Something may wrong,we get more than one node ,we get #{length}"
else
host=hosts[0]
puts "#{host}"
#get the id of host
host_id=host["hostid"]
#
puts "going to delete the host:#{host_name} with hostid:#{host_id}"
hosts_delteid=zbx.query(
:method => "host.delete",
:params =>[
"#{host_id}"
]
)
end
end