Zabbix-templates / 2.0 / VMware / VMware vCenter API and objects templates

本文介绍了一种利用Ruby脚本实现与Zabbix和vSphere集成的方法,用于获取和同步ESXi主机、数据存储和虚拟机的状态信息至Zabbix监控系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 
#!/usr/bin/ruby

require '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 < ZabbixAPI

   def initialize ()
     @user = "Admin"
     @pass = "zabbix"
     begin
       @zbxapi = ZabbixAPI . new ( $zbxUrl ) . login ( @user , @pass )
     rescue => exc
       p exc
       $log . error ( exc )
       exit
     end
   end

   def send_zbx ( zbxCmd , zbxHash )
     begin
       @zbxapi . raw_api ( zbxCmd , zbxHash )
     rescue => exc
       p exc
       $log . error ( exc )
       exit
     end
   end

   def search_zbxGroup ( groupName )
     zbxHash = {
       :name => groupName
     }
     send_zbx ( "hostgroup.exists" , zbxHash )
   end

   def create_zbxGroup ( groupName )
     zbxHash = {
       :name => groupName
     }
     send_zbx ( "hostgroup.create" , zbxHash )
   end

   def get_zbxGroupId ( groupName )
     if search_zbxGroup ( groupName )
       zbxHash = {
       :name => groupName
       }
       send_zbx ( "hostgroup.getobjects" , zbxHash ) [ 0 ][ 'groupid' ]
     else
       create_zbxGroup ( groupName ) [ 'groupids' ][ 0 ]
     end
   end

   def search_zbxTemplate ( templateName )
     zbxHash = {
       :name => templateName
     }
     send_zbx ( "template.exists" , zbxHash )
   end

   def 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' ]
   end

   def search_zbxHost ( hostName )
     zbxHash = {
       :host => hostName
     }
     send_zbx ( "host.exists" , zbxHash )
   end

   def 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 )
     }
   end

   def get_zbxHostId ( hostName )
     zbxHash = {
       :host => hostName
     }
     send_zbx ( "host.getobjects" , zbxHash ) [ 0 ][ 'hostid' ]
   end

   def delete_zbxHost ( hostName )
     zbxHash = {
       :hostid => get_zbxHostId ( hostName )
     } if search_zbxHost ( hostName )
     send_zbx ( "host.delete" , zbxHash ) if zbxHash
   end
end

class VSphere < RbVmomi :: VIM
   def initialize ( host , user , pass )
     @host = host
     @user = user
     @password = pass

     begin
       @vim = RbVmomi :: VIM . connect :host => @host , :user => @user , :password => @password , :insecure => true
     rescue => exc
       p exc
       $log . error ( exc )
       exit
     end

     begin
       @dc = @vim . serviceInstance . find_datacenter
     rescue => exc
       p exc
       $log . error ( exc )
       exit
     end

     begin
       @sc = @vim . serviceContent . viewManager
     rescue => exc
       p exc
       $log . error ( exc )
       exit
     end

   end


   def get_host_status ( type )

     new_list = Array . new

     case type

     when "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 )
       end
       if new_list . length > 0
         unless defined? ( @zbxapi )
           @zbxapi = Zbx . new
         end
         @zbxapi . create_zbxHost ( new_list , ESX_GROUP , ESX_TEMPLATE )
       end

     when "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 )
       end
       if new_list . length > 0
         unless defined? ( @zbxapi )
           @zbxapi = Zbx . new
         end
         @zbxapi . create_zbxHost ( new_list , DS_GROUP , DS_TEMPLATE )
       end

     when "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 )
       end
       if new_list . length > 0
         unless defined? ( @zbxapi )
           @zbxapi = Zbx . new
         end
         @zbxapi . create_zbxHost ( new_list , VM_GROUP , VM_TEMPLATE )
       end
     end

    
   end

end

def print_usage
   puts "usage: rbvmomi_zabbix.rb (vCenter Host) (vCenter Username) (vCenter Password) (Zabbix URL)"
   exit
end

def writefile ( fileName , data )
   begin
     statsFile = open ( $filePath + fileName , "w" )
   rescue => exc
     p exc
     $log . error ( exc )
     exit
   end
   data . each_pair { | key , value | statsFile . puts " #{ key } : #{ value } " }
   statsFile . close
end

def 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 . new
       end
       @zbxapi . delete_zbxHost ( $1 )
       File . delete ( f )
     end
   end
end

def stats_file_check ( zbx_host , fileName )
   create_zbxHost ( hosts , groupName , templateName ) unless File . exist? ( fileName )
end


print_usage if ARGV . size != 4
vcHost = 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" )
[root@zabbix conf]# file -i /usr/local/zabbix-7.2.4/ui/*.php /usr/local/zabbix-7.2.4/ui/api_jsonrpc.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/api_scim.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/browserwarning.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart2.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart3.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart4.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart6.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart7.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/chart.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/graphs.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/history.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/host_discovery.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/hostinventoriesoverview.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/hostinventories.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/host_prototypes.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/httpconf.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/httpdetails.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/image.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/imgstore.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/index_http.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/index_mfa.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/index.php: text/x-php; charset=us-ascii /usr/local/zabbix-7.2.4/ui/index_sso.php: text/x-php; c
最新发布
03-17
<think>好的,用户想检查Zabbix 7.2.4 UI目录下的PHP文件的MIME类型和字符编码。我需要先理清步骤,确保回答正确且符合用户需求。 首先,用户提到的Zabbix 7.2.4可能是指某个特定版本,但根据引用[3],提到的是CentOS Stream 8编译安装PHP 7.2.4,这可能是指PHP版本,而Zabbix的版本可能不同。需要确认用户是否混淆了版本号。但用户的问题明确提到Zabbix 7.2.4,可能是一个笔误,但按照用户的问题来处理。 接下来,用户需要检查PHP文件的MIME类型和字符编码。通常,PHP文件在服务器端的MIME类型是text/html或application/x-php,但实际传输给客户端时可能由服务器配置决定,比如Apache的默认处理方式。字符编码则通常在文件头部或meta标签中指定。 根据引用[2],用户需要找到UI目录的位置。Zabbix的Web前端通常安装在类似/usr/share/zabbix/的目录下。用户可以使用find命令查找,如find / -name "*.php"来定位PHP文件的位置。 检查MIME类型可以通过服务器配置查看,比如Apache的mime.types文件,或者使用curl命令检查响应头。例如,curl -I http://localhost/zabbix/index.php可以查看Content-Type头部信息,其中包含MIME类型和可能的字符编码。 字符编码也可以在PHP文件内部查看,通常在<?php标签之后或HTML的meta标签中指定,如header('Content-Type: text/html; charset=utf-8');或者<meta charset="UTF-8">。 另外,引用[3]提到了php.ini的配置,可能涉及到默认字符设置,如default_charset = "UTF-8",这也可能影响PHP输出的编码。 需要提醒用户,直接检查文件内容和服务器的响应头是最直接的方法。同时,注意Zabbix版本是否正确,因为7.2.4可能不存在,需要用户确认是否为PHP版本,或者是否有其他误解。 最后,生成相关问题时要围绕Zabbix配置、PHP设置、MIME类型检查和字符编码问题展开,帮助用户进一步解决问题。</think>### 检查Zabbix UI目录下PHP文件的MIME类型和字符编码 #### 1. 定位Zabbix UI目录 根据引用[2]的路径查找方法,Zabbix Web前端文件通常位于以下路径: ```bash find / -name "zabbix.conf.php" 2>/dev/null # 典型路径为/usr/share/zabbix/ ``` 若使用默认安装(引用[1]),可能路径为: `/usr/share/zabbix/` 或 `/var/www/html/zabbix/` #### 2. 检查PHP文件的MIME类型 PHP文件的MIME类型通常由Web服务器配置决定: - **Apache**:默认处理`.php`为`text/html`,但实际类型取决于`AddType`指令(检查`/etc/httpd/conf.d/zabbix.conf`) - **Nginx**:在引用[1]的配置中,检查`/etc/nginx/conf.d/zabbix.conf`是否包含: ```nginx location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php-fpm; } ``` 默认会通过FastCGI处理PHP文件,响应头中的MIME类型为`text/html; charset=UTF-8` #### 3. 验证实际响应类型 使用`curl`检查HTTP响应头: ```bash curl -I http://localhost/zabbix/index.php ``` 输出应包含: ``` Content-Type: text/html; charset=UTF-8 ``` #### 4. 检查PHP文件字符编码 通过以下方式验证编码: - **文件元数据**:使用`file`命令检测编码: ```bash file -i /usr/share/zabbix/index.php # 输出示例:charset=us-ascii ``` - **代码声明**:查看PHP文件头部是否包含编码定义: ```php <?php header('Content-Type: text/html; charset=utf-8'); ?> ``` 或在HTML部分检查: ```html <meta charset="UTF-8"> ``` - **PHP配置**:根据引用[3],检查`/usr/local/php/lib/php.ini`中的配置: ```ini default_charset = "UTF-8" ``` #### 5. 关键配置文件参考 - **Zabbix Web配置**:`/etc/zabbix/web/zabbix.conf.php`(影响全局设置) - **PHP-FPM配置**:`/etc/php-fpm.d/zabbix.conf`(可能包含`php_value[default_charset]`) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值