Orange County, Tustin, CA 92780

Enable and Configure ESXi for SNMP Monitoring with PowerCLI

GitHub Repository: https://github.com/virtualization247/vmware-scripts.git
Script Name: snmp_config.ps1

VMware documentation is not always great. While you will find documentation on how to configure SNMP for your ESXi hosts, the way it's presented is not very clear and will leave you scratching your head in confusion for some of you, especially if you've never configured SNMP for your devices or servers. 

Simple Network Management Protocol (SNMP) is used to manage and monitor network devices and their functions by setting up a community string using version 1 or 2 or using version 3 which adds both encryption and authentication. 

Warning! If you have monitoring software, be aware that if you have SNMP already running and are updating the community string and restart the service, you'll get a false alert that the ESXi host has rebooted. If you're part of a large team, seeing alerts that an ESXi host has rebooted can send some people into a panic, unless of course you already know that maintenance is going on. Make sure to let your teammates know you are changing SNMP settings.

If you're ready to setup SNMP for you ESXi hosts, let's get started.

Assumption(s)Assumption(s) made:
You have a working vCenter Server, you have PowerCLI installed and are connected to vCenter Server via PowerCLI. Additionally, you have credentials for both vCenter Server and the root password for ESXi. You are configuring SNMP for version 1 or 2 using the script below.

The script below will allow you to multi-select the ESXi hosts you want to configure, then the script will set the SNMP community string to "abracadabra!123" and start up the SNMP service. Make sure that you change the community string to something you use in your environment.


####### Automate configuring SNMP on ESXi hosts #######
#################### version 1.0  #####################
#######################################################
<# This script will connect to both your vCenter Server,
ESXi hosts you select, update the SNMP community string
for either SNMP v1 or v2, then start or restart the
SNMP service for the change to take effect. #>
#######################################################
###################  Variables ########################
#######################################################


$vcenter = Read-Host -Prompt "Enter your vCenter Server name (FQDN)"
$vcenterCred = Get-Credential -Message "Enter your vCenter Server Credentials"
$esxiCred = Get-Credential -Message "Enter your ESXi root credentials"


#######################################################
##############  Connect to vCenter ####################
#######################################################


Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false `
    -DefaultVIServerMode Multiple | Out-Null
Connect-VIServer -Server $vcenter -Credential $vcenterCred


#######################################################
###############  Get SNMP Settings ####################
#######################################################


$myHosts = Get-VMHost | Out-GridView -OutputMode Multiple
Connect-ViServer $myHosts -Credential $esxiCred
$hostSNMP = Get-VMHostSnmp -Server $myHosts.Name
Write-Host "`nThe current settings for your ESXi hosts are as follows:" `
    -ForegroundColor Blue
$hostSNMP | Select-Object VMHost,Enabled,Port,ReadOnlyCommunities | `
    Format-Table -AutoSize



#######################################################
###############  Set SNMP Settings ####################
#######################################################


$communityString = Read-Host "Enter SNMP string."
Write-Host "SNMP community string entered is: $communityString `n" `
    -ForegroundColor Blue
Write-Host "Updated settings for your ESXi hosts are as follows: `n" `
    -ForegroundColor Green
$hostSNMP = Set-VMHostSNMP $hostSNMP -Enabled:$true `
    -ReadOnlyCommunity $communityString
$hostSNMP | Select-Object VMHost,Enabled,Port,ReadOnlyCommunities | `
     Format-Table -AutoSize
$snmpStatus = $myHosts| Get-VMHostService | `
    Where-Object{$_.Key -eq "snmpd"} 


ForEach ($i in $snmpStatus) {
    if ($snmpStatus.running -eq $true) {
        $i | Restart-VMHostService -Confirm:$false | Out-Null
    }
    else {
        $i | Start-VMHostService -Confirm:$false | Out-Null
    }
}


Write-Host "SNMP service has been started on the ESXi host(s)." `
    -ForegroundColor Blue
$myHosts | Get-VMHostService | Where-Object{$_.Key -eq "snmpd"} | `
    Select-Object VMHost,Key,Running | Format-Table -AutoSize


#######################################################
#######  Disconnect from vCenter and ESXi hosts #######
#######################################################
Disconnect-VIServer -Server * -Confirm:$false