使用zabbix监控VMware

概述

1、VMware本身报警可以在vcenter中获取到,但无法通过微信或者短信通知,如果不是专门人负责也不好24小时一直查看,所以使用监控软件进行实时监控,很有必要

2、zabbix是一款开源监控软件,功能强大,内置很多的监控模板,还可以根据自己的需求定制模板,使用起来非常方便

3、要做的事就是使用zabbix监控Vmware

详细步骤

一、私有云平台zabbix_proxy搭建

  1. 在私有云平台搭建虚拟机,配置好基础环境,开通上网权限。
  2. 添加Zabbix安装源

1).rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm

2).yum clean all

3).yum install munin –nogpgcheck

  1. 安装Proxy和Agent

yum install zabbix-proxy-mysql

  1. 创建数据库和账号

1)create database zabbix_proxy character set utf8 collate utf8_bin;

2)grant all privileges on zabbix_proxy.* to zabbix_proxy@'localhost' identified by 'password';

3)flush privileges;

  1. 初始化数据库

zcat /usr/share/doc/zabbix-proxy-mysql*/schema.sql.gz | mysql -uroot -p zabbix_proxy

  1. 配置Proxy

vi /etc/zabbix/zabbix_proxy.conf

Server=XX.XX.XX.XX    #server地址

ServerPort=10051       #server端口

Hostname=XX.XX.XX.XX  #proxy名称,web配置代理程序时名称要与这个一致

LogFile=/var/log/zabbix/zabbix_proxy.log   #日志文件地址

LogFileSize=0                

PidFile=/var/run/zabbix/zabbix_proxy.pid

SocketDir=/var/run/zabbix

DBName=zabbix_proxy          #数据库名称,与第4步配置数据库名称一致

DBUser=zabbix_proxy           #数据库用户名,与第4步配置用户名一致

DBPassword=password          #数据库密码,与第4步配置密码一致

StartVMwareCollectors=6        #预先配置的vmware监控实例数量

VMwareFrequency=60   # 获取更新vmware集群结构的最小间隔时间(分钟)

VMwarePerfFrequency=60 #  从单个VMware服务检索性能计数器统计数据的间隔时间(秒)

VMwareCacheSize=80M    #内存中维护的vmware集群结构的大小,建议80M

VMwareTimeout=300  # vmware采集器等待 VMware 服务(vCenter or ESX 管理程序)响应的最大时长    

SNMPTrapperFile=/tmp/zabbix_traps.tmp  #trap使用的文件

StartSNMPTrapper=1                  #需要trap消息的此处改为1

CacheSize=4096M

Timeout=4            #超时时间,默认是3秒,根据网络情况而定

ExternalScripts=/usr/lib/zabbix/externalscripts

LogSlowQueries=3000

  1. 启动proxy服务和设置开机启动

systemctl start zabbix-proxy

systemctl enable zabbix-proxy

  1. 在server页面添加代理

 

注:名称要与第6步中Hostname项中配置的名称一致

至此,proxy配置完成,可以通过第6步中LogFile中日志文件的位置查看日志,来确定proxy已经正常运行。

二、在proxy所在虚拟机上安装agent监控proxy代理

1、安装agent:

   yum –y install zabbix-agent

2、配置agent

   vim /etc/zabbix/zabbix_agentd.conf

PidFile=/var/run/zabbix/zabbix_agentd.pid

LogFile=/var/log/zabbix/zabbix_agentd.log

LogFileSize=0

Server=XX.XX.XX.XX              #直接对接server,如果是使用代理则写代理地址

ServerActive=XX.XX.XX.XX         #直接对接server,如果是使用代理则写代理地址

Hostname=XX.XX.XX.XX          #hostname要跟网页上配置时名称一致

Timeout=10

Include=/etc/zabbix/zabbix_agentd.d/*.conf

三、配置vcenter监控

Esxi可以使用vcenter作为采集器监控,vcenter本身的监控只支持trap方式,此处先配置vcenter本身的监控。基本流程是vcenter把trap消息发送到代理,然后snmptrapd接受消息,调用zabbix_trap_receiver.pl脚本,这个脚本调用里面写入的vcenter脚本,将处理后的消息写入到/tmp/zabbix_traps.tmp,最后页面再调用这个tmp文件

  1. 首先在vcenter上配置trap

 

 2.在proxy上安装snmp

   yum  -y install net-snmp-*  

 

  1. 配置snmptrap文件

vim /etc/snmp/snmptrapd.conf

authCommunity   log,execute,net [关键字] #与第1步中vcentr社区字符一致

perl do "/usr/bin/zabbix_trap_receiver.pl"         #确定使用的perl脚本

  1. 编辑/usr/bin/zabbix_trap_receiver.pl脚本

可以官网下载源码

#vim /usr/bin/zabbix_trap_receiver.pl

#!/usr/bin/env perl

use NetSNMP::TrapReceiver;

use lib '/etc/zabbix/snmptrap';             #脚本存放位置

use vcenter;                            #根据需求写的脚本,祥见下文第七步

#

# Zabbix

# Copyright (C) 2001-2019 Zabbix SIA

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#

#########################################

#### ABOUT ZABBIX SNMP TRAP RECEIVER ####

#########################################

# This is an embedded perl SNMP trapper receiver designed for sending data to the server.

# The receiver will pass the received SNMP traps to Zabbix server or proxy running on the

# same machine. Please configure the server/proxy accordingly.

#

# Read more about using embedded perl with Net-SNMP:

#   http://net-snmp.sourceforge.net/wiki/index.php/Tut:Extending_snmpd_using_perl

#################################################

#### ZABBIX SNMP TRAP RECEIVER CONFIGURATION ####

#################################################

#   as in the server (or proxy) configuration file.

#

# Mandatory: yes

# Default:

$SNMPTrapperFile = '/tmp/zabbix_traps.tmp';

### Option: DateTimeFormat

#

# Mandatory: yes

# Default:

$DateTimeFormat = '%H:%M:%S %Y/%m/%d';

###################################

#### ZABBIX SNMP TRAP RECEIVER ####

###################################

#use URI::Escape;

#use Encode;

sub zabbix_receiver

{

    my (%pdu_info) = %{$_[0]};

    my (@varbinds) = @{$_[1]};

    # open the output file

    unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))

    {

        print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";

        return NETSNMPTRAPD_HANDLER_FAIL;

    }

    # get the host name

    my $hostname = $pdu_info{'receivedfrom'} || 'unknown';

    if ($hostname ne 'unknown')

    {

        $hostname = $1 || 'unknown';

    }

    if ($hostname eq 'vc地址')

    {

       close (OUTPUT_FILE);

       return NETSNMPTRAPD_HANDLER_OK;

#!/usr/bin/env perl

use NetSNMP::TrapReceiver;

use lib '/etc/zabbix/snmptrap';

use vcenter;

#

# Zabbix

# Copyright (C) 2001-2019 Zabbix SIA

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#

#########################################

#### ABOUT ZABBIX SNMP TRAP RECEIVER ####

#########################################

# This is an embedded perl SNMP trapper receiver designed for sending data to the server.

# The receiver will pass the received SNMP traps to Zabbix server or proxy running on the

# same machine. Please configure the server/proxy accordingly.

#

# Read more about using embedded perl with Net-SNMP:

#   http://net-snmp.sourceforge.net/wiki/index.php/Tut:Extending_snmpd_using_perl

#################################################

#### ZABBIX SNMP TRAP RECEIVER CONFIGURATION ####

#################################################

#   as in the server (or proxy) configuration file.

#

# Mandatory: yes

# Default:

$SNMPTrapperFile = '/tmp/zabbix_traps.tmp';

### Option: DateTimeFormat

#

# Mandatory: yes

# Default:

$DateTimeFormat = '%H:%M:%S %Y/%m/%d';

###################################

#### ZABBIX SNMP TRAP RECEIVER ####

###################################

#use URI::Escape;

#use Encode;

sub zabbix_receiver

{

    my (%pdu_info) = %{$_[0]};

    my (@varbinds) = @{$_[1]};

    # open the output file

    unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))

    {

        print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";

        return NETSNMPTRAPD_HANDLER_FAIL;

    }

    # get the host name

    my $hostname = $pdu_info{'receivedfrom'} || 'unknown';

    if ($hostname ne 'unknown')

    {

        $hostname = $1 || 'unknown';

    }

    if ($hostname eq 'vc地址')

    {

       close (OUTPUT_FILE);

       return NETSNMPTRAPD_HANDLER_OK;

    }

    # print trap header

    #       timestamp must be placed at the beggining of the first line (can be omitted)

    #       the first line must include the header "ZBXTRAP [IP/DNS address] "

    #              * IP/DNS address is the used to find the corresponding SNMP trap items

    #              * this header will be cut during processing (will not appear in the item value)

    printf OUTPUT_FILE "%s ZBXTRAP %s\n", strftime($DateTimeFormat, localtime), $hostname;

    # print the PDU info

    #print OUTPUT_FILE "PDU INFO:\n";

    #foreach my $key(keys(%pdu_info))

    #{

    #   if ($pdu_info{$key} !~ /^[[:print:]]*$/)

    #   {

    #       my $OctetAsHex = unpack('H*', $pdu_info{$key}); # convert octet string to hex

    #       $pdu_info{$key} = "0x$OctetAsHex";      # apply 0x prefix for consistency

    #   }

#

#       printf OUTPUT_FILE "  %-30s %s\n", $key, $pdu_info{$key};

#   }

    # print the variable bindings:

    print OUTPUT_FILE "VARBINDS:\n";

    foreach my $x (@varbinds)

    {

        $oid = $x->[0]->{'oidptr'}->to_string;

        $val = $x->[1];

        printf OUTPUT_FILE "  %-30s = %s\n", $x->[0], $val;

    }

    close (OUTPUT_FILE);

    return NETSNMPTRAPD_HANDLER_OK;

}

NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or

    die "failed to register Zabbix SNMP trap receiver\n";

print STDOUT "Loaded Zabbix SNMP trap receiver\n";

  1. 将/usr/bin/zabbix_trap_receiver.pl 加执行权限直接运行,出现“Loaded Zabbix SNMP trap receiver”即为正常运行,有问题解决问题,注意net-snmp版本问题,zabbix4.4.10对应net-snmp 5.7.2-49(这个问题曾卡住好久)
  2. 更改/etc/zabbix/zabbix_proxy.conf配置文件,开启snmptrap

   StartSNMPTrapper=1 将这里设为1

    SNMPTrapperFile=/tmp/zabbix_traps.tmp

  1. 建立SNMPTrapperFile使用的文件

   touch /tmp/zabbix_traps.tmp

  1. 编写第4步用到的vcenter,根据第4步lib,放到/etc/zabbix/snmptrap下

vim /etc/zabbix/snmptrap/vcenter

#!/usr/bin/env perl

use URI::Escape;

use Encode;

package vcenter;

sub to_value {

    my (@varbinds) = @{$_[0]};

    my %values = ();

    foreach my $x (@varbinds)

    {

        my $isHex = 0;

        my $oid = $x->[0];

        my $val = $x->[1];

        if($val =~ /Hex-STRING:/){

            $isHex = 1;

        }

        $val =~ s/^[^\s]+\s//g;

        $val =~ s/(^"|"$)//g;

        if($isHex == 1){

            $val =~ s/[\r\n]//g;

            $val = URI::Escape::uri_unescape("%" . join("%",split(/\s+/, $val)));

        }

        $values{$oid} = exists($oid_value{$oid}) && exists($oid_value{$oid}{$val}) ? $oid_value{$oid}{$val} : $val;

    }

    return "VC故障--$values{'.1.3.6.1.4.1.6876.4.3.306.0'}";

}

1;

  1. 重启zabbix-proxy和snmptrap服务
  2. /tmp/zabbix_traps.tmp是否正常收到数据。

注意:这里遇到一个大坑,/tmp/zabbix_traps.tmp能收到数据,只有VC故障--,后面的没能解析出来,最后发现:

 少了-On这个参数。在/etc/sysconfig/snmptrapd 添加上即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值