VMware ESXI6批量配置snmp和snmptrap

需要对一大批esxi主机进行snmp监控,数量级在300+以上,纯手工一台台来配置snmp开启不现实,故用到vmware powercli进行批量开通。

团体名:mysnmpcomu123
snmptrap服务器地址:10.10.20.123
vcenter用户名密码:admin123@my.vmware/ zzzxxx123
vcenter地址:10.10.10.4
目的:设置snmp后,执行snmpwalk -v 2c -c mysnmpcomu123 10.10.10.119 system会有反馈。设置snmptrap后,snmptrap服务器可以收到esxi反馈的数据。

—————————————————————————————————-其中,VMware PowerCLI安装步骤如下(具体版本请根据实际情况操作):
1、安装netframework 4.5 NDP452-KB2901907-x86-x64-AllOS-ENU
2、安装powershell 3.0 Windows6.1-KB2506143-x64.msu (Window2008 r2 sp1才支持)
在 Windows PowerShell 控制台中的命令提示符下键入以下命令,然后按 ENTER:
Get-Host | Select-Object Version
如果 Windows PowerShell 3.0 已安装,您将看到如下所示输出:
Version
——-
3.0
3、安装VMware-PowerCLI-6.3.0-3737840.exe
打开:开始菜单-附件-vmware-VMware vSphere PowerCLI
有报错的话执行:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
—————————————————————————————————-

1、单台esxi开启snmp
方法一:使用自身的esxcli,ssh登录esxi,执行如下:
esxcli network ip connection list | grep -i snmp
esxcli system snmp set –communities mysnmpcomu123
esxcli system snmp set –enable true
esxcli system snmp get
esxcli system snmp test
方法二:使用powercli,设置esxi主机10.10.10.124的snmp,执行如下:
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-VMHost “10.10.10.124”
$ReadOnlyCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -ReadOnlyCommunity $ReadOnlyCommunity
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}

2、针对一个cluster开启snmp
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-Cluster -Name mycluster01 | Get-VMHost
$ReadOnlyCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -ReadOnlyCommunity $ReadOnlyCommunity
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}

3、整个数据中心开启snmp
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-VMHost
$ReadOnlyCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -ReadOnlyCommunity $ReadOnlyCommunity
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}

5、单台开启snmptrap
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-VMHost “10.10.10.119”
$trapDestination = “10.10.20.123”
$trapCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}

6、一个cluster开启snmptrap
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-Cluster -Name mycluster01 | Get-VMHost
$trapDestination = “10.10.20.123”
$trapCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}

7、整个数据中心开启snmptrap
Connect-VIServer -Server 10.10.10.4 -User admin123@my.vmware -Password zzzxxx123
$Hosts = Get-VMHost
$trapDestination = “10.10.20.123”
$trapCommunity = “mysnmpcomu123”
$EsxCred = Get-Credential
ForEach ($VMHost in $Hosts)
{
$esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred
$snmpConn = Get-VMHostSnmp
Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true
Set-VMHostSnmp -HostSnmp $snmpConn -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination
Disconnect-VIServer -Server $esxconnect -Confirm:$false
}