使用zabbix监控VMware
概述
1、VMware本身報警可以在vcenter中獲取到,但無法通過微信或者短信通知,如果不是專門人負責也不好24小時一直查看,所以使用監控軟件進行實時監控,很有必要
2、zabbix是一款開源監控軟件,功能強大,內置很多的監控模板,還可以根據自己的需求定制模板,使用起來非常方便
3、要做的事就是使用zabbix監控Vmware
詳細步驟
一、私有云平臺zabbix_proxy搭建
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
yum install zabbix-proxy-mysql
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;
zcat /usr/share/doc/zabbix-proxy-mysql*/schema.sql.gz | mysql -uroot -p zabbix_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
systemctl?start?zabbix-proxy
systemctl?enable zabbix-proxy
?
注:名稱要與第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文件
?
?2.在proxy上安裝snmp
???yum? -y install net-snmp-*??
?
vim /etc/snmp/snmptrapd.conf
authCommunity ??log,execute,net [關鍵字]?#與第1步中vcentr社區字符一致
perl do "/usr/bin/zabbix_trap_receiver.pl" ????????#確定使用的perl腳本
可以官網下載源碼
#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";
???StartSNMPTrapper=1 將這里設為1
????SNMPTrapperFile=/tmp/zabbix_traps.tmp
???touch /tmp/zabbix_traps.tmp
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;
注意:這里遇到一個大坑,/tmp/zabbix_traps.tmp能收到數據,只有VC故障--,后面的沒能解析出來,最后發現:
?少了-On這個參數。在/etc/sysconfig/snmptrapd 添加上即可。
總結
以上是生活随笔為你收集整理的使用zabbix监控VMware的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P2562 [AHOI2002]Kitt
- 下一篇: 量化基金投资简介