Recently I needed to configure all of our 40 or so ESXi hosts to forward SNMP traps to our corporate monitoring solution. This meant enabling and configuring SNMP on each of the hosts. Naturally, I wrote a script for this as 40 hosts is way too many to do manually.
This article shows you how configure SNMP on an ESXi host manually, via PowerCLI and via host profiles.
Option 1: Manually via Command Line
This is the most boring approach and should really only be used if you only have a few ESXi hosts to do, or if you really like doing things manually 🙂
- Start the SSH service on the ESXi host (Configuration >> Software >> Security Profile >> Services)
- SSH into host (using putty or something similar)
- Run the following to configure SNMP settings, enable SNMP in the firewall and start the SNMP agent:
12345678esxcli system snmp set --communitiesesxcli system snmp set --targetsesxcli system snmp set --enable trueesxcli network firewall ruleset set --ruleset-id snmp --allowed-all trueesxcli network firewall ruleset set --ruleset-id snmp --enabled true/etc/init.d/snmpd restart<COMMUNITY_STRING>
with the community string for your monitoring solution.Note 2: Replace
<TARGET_STRING>
with the target string that maps to your environment, in the format oftarget_address@port/community_string
.Option 2: Manually via PowerCLI
Option number 2 is to use PowerCLI to configure SNMP on an ESXi host. The following script is how to do this on a single host. To configure SNMP on an whole bunch of ESXi hosts, see option 3 below.
123456789101112131415161718#Script Variables$sESXiHost = ''$sCommunity = ''$sTarget = ''$sPort = ''#Connect to ESXi hostConnect-VIServer -Server $sESXiHost#Clear SNMP SettingsGet-VMHostSnmp | Set-VMHostSnmp -ReadonlyCommunity @()#Add SNMP SettingsGet-VMHostSnmp | Set-VMHostSnmp -Enabled:$true -AddTarget -TargetCommunity $sCommunity -TargetHost $sTarget -TargetPort $sPort -ReadOnlyCommunity $sCommunity#Get SNMP Settings$Cmd= Get-EsxCli -VMHost $sESXiHost$Cmd.System.Snmp.Get()Note: Prior to being able to use the script above, ensure you configure the following variable values:
<ESXI_HOST>
– The FQDN or the IP address of the ESXi host you want to enable SNMP on.<COMMUNITY>
– This is the community string you require for your environment (same as in option 1 above).<TARGET>
– This is the FQDN or IP address of the target you want to send the SNMP traps to. Note: THIS IS NOT A TARGET STRING as in option 1. In this instance you ONLY need the FQDN or IP address. The@port
and thecommunity_string
will be added automatically by theSet-VMHostSnmp
cmdlet.<PORT>
– The port you require SNMP traps to be sent on.
Option 3: Automatically via PowerCLI
If you have to configure SNMP for more than just a handful of ESXi hosts, then it is worth automating the entire process through a PowerCLI script. The logic around enabling SNMP on the ESXi host is the same as in option 2 above, with some additional logic around this to enumerate and complete the process on all ESXi Hosts.
Here is a script that will connect to a vCenter Server, get a list of all ESXi Hosts and then configure SNMP on each ESXi host:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#requires -version 4 <# .SYNOPSIS Configure SNMP Settings on ESXi Hosts .DESCRIPTION Connect to vCenter Server and configure all ESXi hosts with SNMP settings .PARAMETER None .INPUTS Server Mandatory. The vCenter Server or ESXi Host the script will connect to, in the format of IP address or FQDN. .INPUTS Credentials Mandatory. The user account credendials used to connect to the vCenter Server of ESXi Host. .OUTPUTS Log File The script log file stored in C:\Windows\Temp\Set-HostSNMP.log. .EXAMPLE .\Set-HostSNMP.ps1 #> #---------------------------------------------------------[Initialisations]-------------------------------------------------------- #Set Error Action to Silently Continue $ErrorActionPreference = 'SilentlyContinue' #Dot Source required Function Libraries . 'C:\Scripts\Logging_Functions.ps1' #Add VMware PowerCLI Snap-Ins Add-PSSnapin VMware.VimAutomation.Core #----------------------------------------------------------[Declarations]---------------------------------------------------------- #Script Version $sScriptVersion = '1.0' #Log File Info $sLogPath = 'C:\Windows\Temp' $sLogName = 'Set-HostSNMP.log' $sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName #SNMP Settings $global:sCommunity = '<COMMUNITY>' $global:sTarget = '<TARGET>' $global:sPort = '<PORT>' #-----------------------------------------------------------[Functions]------------------------------------------------------------ Function Connect-VMwareServer{ Param([Parameter(Mandatory=$true)][string]$VMServer) Begin{ Log-Write -LogPath $sLogFile -LineValue "Connecting to VMware environment [$VMServer]..." } Process{ Try{ $oCred = Get-Credential -Message 'Enter credentials to connect to vSphere Server or Host' Connect-VIServer -Server $VMServer -Credential $oCred } Catch{ Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True Break } } End{ If($?){ Log-Write -LogPath $sLogFile -LineValue 'Completed Successfully.' Log-Write -LogPath $sLogFile -LineValue ' ' } } } Function Start-ScriptExecution{ Param() Begin{ Log-Write -LogPath $sLogFile -LineValue 'Enumerating ESXi Hosts and setting SNMP configuration...' } Process{ Try{ #Get list of all ESXi hosts in connected environment $ESXHosts = Get-VMHost ForEach($ESXHost in $ESXHosts){ Set-SNMPSettings -ESXHost $ESXHost } } Catch{ Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True Break } } End{ If($?){ Log-Write -LogPath $sLogFile -LineValue ' ' Log-Write -LogPath $sLogFile -LineValue 'Completed Successfully.' Log-Write -LogPath $sLogFile -LineValue ' ' } } } Function Set-SNMPSettings { Param([Parameter(Mandatory=$true)][string]$ESXHost) Begin{ Log-Write -LogPath $sLogFile -LineValue ' ' Log-Write -LogPath $sLogFile -LineValue " $ESXHost - Configuring SNMP Settings" } Process{ Try{ #Clear existing SNMP Configuration Get-VMHostSnmp -Server $ESXHost | Set-VMHostSnmp -ReadonlyCommunity @() #Add new SNMP Configuration Get-VMHostSnmp -Server $ESXHost | Set-VMHostSnmp -Enabled:$true -AddTarget -TargetCommunity $global:sCommunity -TargetHost $global:sTarget -TargetPort $global:sPort -ReadOnlyCommunity $global:sCommunity } Catch{ Log-Error -LogPath $sLogFile -ErrorDesc " $ESXHost - An error has occurred" -ExitGracefully $False } } End{ If($?){ Log-Write -LogPath $sLogFile -LineValue " $ESXHost - Completed Successfully" } } } #-----------------------------------------------------------[Execution]------------------------------------------------------------ Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion $Server = Read-Host 'Specify the vCenter Server or ESXi Host to connect to (IP or FQDN)?' Connect-VMwareServer -VMServer $Server Start-ScriptExecution Log-Finish -LogPath $sLogFile |
Note: Similar to option 2 above, you will need to configure the following variables first:
<COMMUNITY>
– This is the community string you require for your environment (same as in option 1 above).<TARGET>
– This is the FQDN or IP address of the target you want to send the SNMP traps to.Note: THIS IS NOT A TARGET STRING as in option 1. In this instance you ONLY need the FQDN or IP address. The @port and the community_string will be added automatically by the Set-VMHostSnmp cmdlet.<PORT>
– The port you require SNMP traps to be sent on.
Option 4: Automatically via Host Profiles
Finally, if you are lucky enough to be running Enterprise Plus licensing, then you will have the ability to use Host Profiles. This allows you to configure SNMP within the host profile and then just apply that profile to all of your ESXi hosts.
Follow these steps to add the SNMP configuration into an existing Host Profile:
- From the VI Client, navigate to Management >> Host Profiles
- Select the profile you want to add the SNMP settings and click Edit Profile
- Expand the SNMP Agent Configuration policy and select SNMP Agent Configuration
- In the Configuration Details pane, complete the followng:
- Enable or Disable agent: Ticked
- IP/UDP Port: The port you require SNMP traps to be sent on
- SNMP Community String: The community string for your environment
- Notification Receiver: The target string that maps to your environment, in the format of
target_address@port/community_string
- Click OK to save changes
- For each ESXi host attach and apply the profile (Note: An ESXi host needs to be in maintenance mode to be able to apply the host profile)
And that concludes how to configure SNMP on a ESXi host.