It’s been a while since I posted a PowerCLI script, but since I started a new project one of my first tasks was to help out and setup SNMP to monitor ESXi hosts from Nagios (which is a great product BTW).  So a new script for the collection.
O yeah, a little notice as I’m not always working with scripts there are probably better scripts or better scripters out there that will be more efficient. I have chosen to use a script and not a killer one liner, just to make clear what is happening and for others to use and understand what is happening.
If you have tips and leave a comment to help me, no problem I would appreciate that and probably any reader out there as well.

SNMP Scripts

To change the SNMP settings we need some information about what need to be set. For this example I will set the readonly community, trap destination and trap community name.  I have set up the script to take input from the user for these values.

I’m using two scripts, one to change a single hosts and one to change hosts connected to vCenter in a specific cluster. The main difference is that the single host script takes a input for the ESXi hostname, while the cluster script takes vCenter and cluster name (which can be easily changed to another object location for example replacing Get-cluster with nothing or Get-datacenter) and loops through the hosts. There is a host connection needed to change the SNMP values for that host, this is what is done first in the loop. If not direct connected to the host, the SNMP settings will not be changed. This goes for the single host script as well.

For SNMP there are two cmdlet’s relevant Get-VMHostSNMP and Set-VMHostSNMP, respectively getting the SNMP values from the host and setting the values. First enable SNMP with Set-VMHostSNMP to set value –Enabled to true. The SNMP settings value are done via two more Set-VMHostSNMP cmdlets, first setting the Read only community with –ReadOnlyCommunity option and the other to set a SNMP trap target and communityname (-Target and -TrapCommunity).

The rest of the comments in the script should tell what action is done at that time. If not, you know where to find me….

I have chosen not  to hardcode or use files that store passwords, again this is user input with the Get-Credential cmdlet. For the ESXi hosts in the cluster script this is stored to a variable to be used in the host loop. The condition is that the hosts use the same credentials.

The Singlehost script:

#Settings

$ESXHost = Read-Host “ESXi hostname to change:”

$trapDestination = Read-Host “SNMP Trap Mgmt server hostname: “

$trapCommunity = Read-Host “Trap Community Name: “

$ReadOnlyCommunity = Read-Host “Read Only Community Name: “

# Running no need to change under this line

# Connect to the ESX server

$viServer = Connect-VIServer -Server $ESXHost -Credential (Get-Credential)

 

# Get snmp

$snmpConn = Get-VMHostSnmp -Server $defaultViServer

 

# Enable snmp

Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true

 

# Set read-only community

Set-VMHostSnmp -HostSnmp $snmpConn -ReadOnlyCommunity $ReadOnlyCommunity

 

# Set trap target and community name

Set-VMHostSnmp -HostSnmp $snmpConn -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination

 

#disconnect from ESX server

Disconnect-VIServer -Server $viServer -Confirm:$false

 

The cluster script with a host loop:

#Settings

$vCenterFQDN = Read-Host “Enter vCenter FQDN: “

$ClusterName = Read-Host “Enter Cluster name of hosts that will need to be changed: “

$trapDestination = Read-Host “SNMP Trap Mgmt server hostname: “

$trapCommunity = Read-Host “Trap Community Name: “

$ReadOnlyCommunity = Read-Host “Read Only Community Name: “

 

# Running no need to change under this line

# Connect to the vCenter server

Write-Host “vCenter credentials”

$viServer = Connect-VIServer -Server $vCenterFQDN -Credential (Get-Credential)

 

# Get all hosts in vCenter managed Cluster so we can cycle thru them

$Hosts = Get-Cluster -Name $ClusterName | Get-VMHost

 

# Get ESXi Host credentials

Write-Host “ESXi host credentials”

$EsxCred = Get-Credential

 

# Cycle through each host

ForEach ($VMHost in $Hosts)

{

                # Need to connect to ESXi itself

                $esxconnect = Connect-VIServer -Server $VMHost -Credential $EsxCred

 

                # Get snmp object

                $snmpConn = Get-VMHostSnmp

 

                # Enable snmp

                Set-VMHostSnmp -HostSnmp $snmpConn -Enabled:$true

 

                # Set read-only community

                Set-VMHostSnmp -HostSnmp $snmpConn -ReadOnlyCommunity $ReadOnlyCommunity

 

                # Set trap target host and trap community

                Set-VMHostSnmp -HostSnmp $snmpConn -AddTarget -TargetCommunity $trapCommunity -TargetHost $trapDestination

 

                # Disconnect-VIServer

                Disconnect-VIServer -Server $esxconnect -Confirm:$false

}

 

#disconnect from ESX server

Disconnect-VIServer -Server $viServer -Confirm:$false

Just copy and save the scripts. And when your execution policy allows it, execute them.

–          Enjoy!

Advertisements

One thought on “PowerCLI Collection – Adding SNMP settings to ESXi hosts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s