How To – Configure SNMP on an ESXi Host

Recently I needed to configure all of our 50 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 50 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 🙂

  1. Start the SSH service on the ESXi host (Configuration >> Software >> Security Profile >> Services)
  2. SSH into host (using putty or something similar)
  3. Run the following to configure SNMP settings, enable SNMP in the firewall and start the SNMP agent:

esxcli system snmp set --communities 
esxcli system snmp set --targets 
esxcli system snmp set --enable true

esxcli network firewall ruleset set --ruleset-id snmp --allowed-all true
esxcli network firewall ruleset set --ruleset-id snmp --enabled true

/etc/init.d/snmpd restart

Note 1: Replace <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 of target_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.


#Script Variables
$sESXiHost = ''
$sCommunity = ''
$sTarget = ''
$sPort = ''

#Connect to ESXi host
Connect-VIServer -Server $sESXiHost

#Clear SNMP Settings
Get-VMHostSnmp | Set-VMHostSnmp -ReadonlyCommunity @()

#Add SNMP Settings
Get-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 the community_string will be added automatically by the Set-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 I have written that will connect to a vCenter Server, get a list of all ESXi Hosts and then configure SNMP on each ESXi host:

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:

  1. From the VI Client, navigate to Management >> Host Profiles
  2. Select the profile you want to add the SNMP settings and click Edit Profile
  3. Expand the SNMP Agent Configuration policy and select SNMP Agent Configuration
  4. 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
  5. Click OK to save changes
  6. 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. Any questions or suggestions, then let me know in the comments below.

Comments

    1. I used this page as a basis and updated to work for me with 6.5. Used the Get-EsxCli cmdlet though.

      require -module VMware.VimAutomation.Core

      $vcsa = "vcentre.yourdomain.local" # Your vSphere server
      $snmpdROCommunities = "PuBl1c" # v2c RO comminuties (can comma seperated list of up to 10)
      $syscontact = "administrator@vsphere.local" # Set the snmp attributes
      $syslocation = "My DataCentre"
      $port = "161" #Change the port if you want
      $me = Get-Credential -Message "vSphere Admin"

      Connect-VIServer -Server $vcsa -Credential $me | Out-Null

      $vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}
      foreach ($vmhost in $vmhosts) {
      #Set SNMP settings using new V2 esxcli interface
      $esxcli = Get-EsxCli -VMHost $vmhost -V2
      $arguments = $esxcli.system.snmp.set.CreateArgs()
      $arguments.communities = $snmpdROCommunities
      $arguments.enable = $true
      $arguments.port = $port
      $arguments.syscontact = $syscontact
      $arguments.syslocation = $syslocation

      $esxcli.system.snmp.set.Invoke($arguments)

      #Quickly dump out the new config
      write-host $vmhost.Name
      $esxcli.system.snmp.get.Invoke()

      $snmpd = Get-VMHostService -VMHost $vmhost | where {$_.Key -eq "snmpd"}
      #Start SNMP service - if not configured properly it will error here
      $snmpd | start-VMhostservice
      #configure Auto start and stop with host
      $snmpd | set-VMhostservice -Policy "On"
      }

      Disconnect-VIServer -Server $vcsa

      http://pio.nz/2017/09/18/setting-snmp-on-all-esxi-6-5-hosts-in-a-cluster/

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.