有时候需要删除zabbix上的主机,web页面懒的打开,写了一个ruby的脚本
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 |
#!/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 |