Zabbix-templates / 2.0 / VMware / VMware vCenter API and objects templates
来源:互联网 发布:她理财是否可靠 知乎 编辑:程序博客网 时间:2024/05/16 10:15
#!/usr/bin/rubyrequire 'rubygems'require 'zbxapi'require 'logger'require 'rbvmomi'require 'fileutils'ESX_GROUP = "VM ESXi"DS_GROUP = "VM Datastore"VM_GROUP = "VM VirtualMachine"ESX_TEMPLATE = "Template-vSphere-ESXi"DS_TEMPLATE = "Template-vSphere-Datastore"VM_TEMPLATE = "Template-vSphere-VM"FILEPATH = "/tmp/vsphere"class Zbx < ZabbixAPIdef initialize()@user = "Admin"@pass = "zabbix"begin@zbxapi = ZabbixAPI.new($zbxUrl).login(@user, @pass)rescue => excp exc$log.error(exc)exitendenddef send_zbx(zbxCmd, zbxHash)begin@zbxapi.raw_api(zbxCmd, zbxHash)rescue => excp exc$log.error(exc)exitendenddef search_zbxGroup(groupName)zbxHash = {:name => groupName}send_zbx("hostgroup.exists", zbxHash)enddef create_zbxGroup(groupName)zbxHash = {:name => groupName}send_zbx("hostgroup.create", zbxHash)enddef get_zbxGroupId(groupName)if search_zbxGroup(groupName)zbxHash = {:name => groupName}send_zbx("hostgroup.getobjects", zbxHash)[0]['groupid']elsecreate_zbxGroup(groupName)['groupids'][0]endenddef search_zbxTemplate(templateName)zbxHash = {:name => templateName}send_zbx("template.exists", zbxHash)enddef get_zbxTemplateId(templateName)unless search_zbxTemplate(templateName)errmsg = "#{templateName} is not exist."$log.error(errmsg)abort(errmsg)end@zbxapi.raw_api("template.getobjects", {:name => templateName})[0]['templateid']enddef search_zbxHost(hostName)zbxHash = {:host => hostName}send_zbx("host.exists", zbxHash)enddef create_zbxHost(hosts, groupName, templateName)@groupId = get_zbxGroupId(groupName)@templateId = get_zbxTemplateId(templateName)hosts.each {|host|zbxHash = {:host => host,:interfaces => [{:type => 1,:main => 1,:useip => 1,:ip => "127.0.0.1",:dns => "",:port => "10050"}],:groups => [{:groupid => @groupId}],:templates => [{:templateid => @templateId}]} unless search_zbxHost(host)send_zbx("host.create", zbxHash)}enddef get_zbxHostId(hostName)zbxHash = {:host => hostName}send_zbx("host.getobjects", zbxHash)[0]['hostid']enddef delete_zbxHost(hostName)zbxHash = {:hostid => get_zbxHostId(hostName)} if search_zbxHost(hostName)send_zbx("host.delete", zbxHash) if zbxHashendendclass VSphere < RbVmomi::VIMdef initialize(host, user, pass)@host = host@user = user@password = passbegin@vim = RbVmomi::VIM.connect :host => @host, :user => @user, :password => @password, :insecure => truerescue => excp exc$log.error(exc)exitendbegin@dc = @vim.serviceInstance.find_datacenterrescue => excp exc$log.error(exc)exitendbegin@sc = @vim.serviceContent.viewManagerrescue => excp exc$log.error(exc)exitendenddef get_host_status(type)new_list = Array.newcase typewhen "host"@dc.hostFolder.childEntity[0].host.grep(RbVmomi::VIM::HostSystem).each do |stat|newname = stat.name.gsub(/:/,"-")stat_fileName = "h_#{newname}"new_list << newname unless File.exist?($filePath + stat_fileName)statData = {"host-Hostname" => stat.name,"host-Product" => stat.summary.config.product.fullName,"host-HardwareMode" => stat.summary.hardware.model,"host-CPUModel" => stat.summary.hardware.cpuModel,"host-CPUMHz" => stat.summary.hardware.cpuMhz,"host-CPUCore" => stat.summary.hardware.numCpuCores,"host-CPUUsage" => stat.summary.quickStats.overallCpuUsage,"host-TotalMemorySize" => stat.summary.hardware.memorySize/1024/1024,"host-MemoryUsage" => stat.summary.quickStats.overallMemoryUsage,"host-PowerState" => stat.summary.runtime.powerState,"host-MaintenanceMode" => stat.summary.runtime.inMaintenanceMode,"host-Uptime" => stat.summary.quickStats.uptime}writefile(stat_fileName, statData)endif new_list.length > 0unless defined?(@zbxapi)@zbxapi = Zbx.newend@zbxapi.create_zbxHost(new_list, ESX_GROUP, ESX_TEMPLATE)endwhen "ds"@dc.datastore.grep(RbVmomi::VIM::Datastore).each do |stat|newname = stat.name.gsub(/:/,"-")stat_fileName = "d_#{newname}"new_list << newname unless File.exist?($filePath + stat_fileName)vm_list =[]stat.vm.grep(RbVmomi::VIM::VirtualMachine).each {|v| vm_list << v.name }statData = {"ds-Name" => stat.name,"ds-Capacity" => stat.summary.capacity,"ds-FreeSpace" => stat.summary.freeSpace,"ds-VM" => vm_list.join(', ')}writefile(stat_fileName, statData)endif new_list.length > 0unless defined?(@zbxapi)@zbxapi = Zbx.newend@zbxapi.create_zbxHost(new_list, DS_GROUP, DS_TEMPLATE)endwhen "vm"@sc.CreateContainerView({:container => @vim.rootFolder ,:type => ['VirtualMachine'], :recursive => true}).view.each do |stat|newname = stat.name.gsub(/:/,"-")stat_fileName = "v_#{newname}"new_list << newname unless File.exist?($filePath + stat_fileName)statData = {"vm-Name" => stat.name,"vm-ESXi" => stat.runtime.host.name,"vm-powerState" => stat.summary.runtime.powerState,"vm-guestFullName" => stat.summary.guest.guestFullName,"vm-HostName" => stat.summary.guest.hostName,"vm-IPAddress" => stat.summary.guest.ipAddress,"vm-VMwareTools" => stat.summary.guest.toolsStatus,"vm-maxCpuUsage" => stat.summary.runtime.maxCpuUsage,"vm-numCpu" => stat.summary.config.numCpu,"vm-overallCpuUsage" => stat.summary.quickStats.overallCpuUsage,"vm-memorySizeMB" => stat.summary.config.memorySizeMB,"vm-hostMemoryUsage" => stat.summary.quickStats.hostMemoryUsage,"vm-guestMemoryUsage" => stat.summary.quickStats.guestMemoryUsage,"vm-UncommittedStorage" => stat.summary.storage.uncommitted,"vm-UsedStorage" => stat.summary.storage.committed,"vm-UnsharedStorage" => stat.summary.storage.unshared,"vm-Storagelocation" => stat.summary.config.vmPathName,"vm-Uptime" => stat.summary.quickStats.uptimeSeconds}writefile(stat_fileName, statData)endif new_list.length > 0unless defined?(@zbxapi)@zbxapi = Zbx.newend@zbxapi.create_zbxHost(new_list, VM_GROUP, VM_TEMPLATE)endendendenddef print_usageputs "usage: rbvmomi_zabbix.rb (vCenter Host) (vCenter Username) (vCenter Password) (Zabbix URL)"exitenddef writefile(fileName, data)beginstatsFile = open($filePath + fileName, "w")rescue => excp exc$log.error(exc)exitenddata.each_pair {|key, value| statsFile.puts "#{key}:#{value}"}statsFile.closeenddef stats_file_age_check(time)# 1日以上更新がないホストはZabbixから削除Dir::glob($filePath + "*").each do |f|if Time.now - File.stat(f).mtime >= time/\A[vhd]_(.*)\z/ =~ File.basename(f)unless defined?(zbxapi)@zbxapi = Zbx.newend@zbxapi.delete_zbxHost($1)File.delete(f)endendenddef stats_file_check(zbx_host, fileName)create_zbxHost(hosts, groupName, templateName) unless File.exist?(fileName)endprint_usage if ARGV.size != 4vcHost = ARGV[0]vcUser = ARGV[1]vcPass = ARGV[2]$zbxUrl = ARGV[3]$filePath = FILEPATH + "/stats/"FileUtils.mkdir_p($filePath) unless File.exists?($filePath)logPath = FILEPATH + "/logs/"FileUtils.mkdir_p(logPath) unless File.exists?(logPath)$log = Logger.new(logPath + 'rbvmoni.log', 'weekly')stats_file_age_check(3600 * 24)ds = VSphere.new(vcHost, vcUser, vcPass)ds.get_host_status("host")ds.get_host_status("ds")ds.get_host_status("vm")
0 0
- Zabbix-templates / 2.0 / VMware / VMware vCenter API and objects templates
- VMware vSphere and vCenter Introduction
- Templates
- templates
- Templates
- Templates
- Templates
- templates
- templates
- templates
- Index Templates API
- Templates and Smart Pointers
- Templates and Collection Classes
- Coding templates and formatters
- Templates and Generic Programming
- python_Django_3:Views and templates
- wpf style and templates
- vmware vcenter 介绍
- AS3中对象的深度复制
- cocos2d学习时期好文章链接地址整理
- 真值和机器数
- 静态代理与动态代理
- XP,且退且追忆;用户,且用且担心
- Zabbix-templates / 2.0 / VMware / VMware vCenter API and objects templates
- 关于argc与argv的理解
- MongoDB结合Spring存储文件(图片、音频等等)
- U-Boot编译过程完全分析
- 译-PHP rabbitMQ Tutorial-3
- javascript 对象行为模式 迭代器模式
- 比特币ATM登入澳大利亚 政府未供认其法定位置
- MongoDB学习之旅一:走进MongoDB世界
- char * 与char []的区别