Tuesday, May 10, 2016

PowerCLI Collection – Adding SNMP settings to ESXi hosts Single or vCenter

LINK

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












# PowerCLI Script for adding syslogserver to hosts
# @mrlesmithjr
# EverythingShouldBeVirtual.com
# Change the following to match your environment
# vi_server is your vCenter
$vi_server = “p111vic01.st.com”
$vcuser = "st\admin.rb"
$vcpass = "--password--"
$communities = "public"
$syslocation = "Esx"

Connect-VIServer -Server $vi_server -User $vcuser -Password $vcpass

# Setup variable to use in script for all hosts in vCenter
$vmhosts = @(Get-VMHost)

# Configure syslog on each host in vCenter
foreach ($vmhost in $vmhosts) {
Write-Host ‘$vmhost = ‘ $vmhost
$esxcli = Get-EsxCli -VMHost $vmhost
$esxcli.system.snmp.set($null,$communities,"true",$null,$null,$null,$null,$null,$null,$null,$null,$null,$syslocation)
$esxcli.system.snmp.set($null,$communities,”$enable”)
$esxcli.system.snmp.get()
}

Disconnect-VIServer * -Confirm:$false










You may want to pass the credentials on the command line. I just forgot to do so 🙂
Now, the system can be queried from your LAN:
# snmpwalk -v 2c -c C0MMUN1TY esxi system SNMPv2-MIB::sysDescr.0 = STRING: VMware ESX 5.0.0 build-469512 VMware, Inc. x86_64 SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.6876.4.1 SNMPv2-MIB::sysUpTime.0 = Timeticks: (6874825) 19:05:48.25 SNMPv2-MIB::sysContact.0 = STRING: not set SNMPv2-MIB::sysName.0 = STRING: esxi.tumfatig.net SNMPv2-MIB::sysLocation.0 = STRING: not set SNMPv2-MIB::sysServices.0 = INTEGER: 72 SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00 (...)
But there is more! Try the following command:
# snmpwalk -v 2c -c C0MMUN1TY -On esxi .1.3.6.1.4.1.6876.2.1.1.8 .1.3.6.1.4.1.6876.2.1.1.8.1 = STRING: "running" .1.3.6.1.4.1.6876.2.1.1.8.2 = STRING: "notRunning" .1.3.6.1.4.1.6876.2.1.1.8.3 = STRING: "notRunning" .1.3.6.1.4.1.6876.2.1.1.8.4 = STRING: "notRunning"
Now, grab the VMware vSphere SNMP MIB Modules (VMware-esx-mibs-1.0.1-368563.zip) and install it. Then run the following command:
# snmpwalk -v 2c -c C0MMUN1TY -M +. -m ALL esxi .1.3.6.1.4.1.6876.2.1.1.8 VMWARE-VMINFO-MIB::vmwVmGuestState.1 = STRING: running VMWARE-VMINFO-MIB::vmwVmGuestState.2 = STRING: notRunning VMWARE-VMINFO-MIB::vmwVmGuestState.3 = STRING: notRunning VMWARE-VMINFO-MIB::vmwVmGuestState.4 = STRING: notRunning
Tada! This is quite simple. But there is more to get using the Perl SDK ; and maybe the vCenter… Got to figure this out… later on 😉
BTW, it is also possible to enable SNMP from the ESXi shell. It just needs a bit of editing and services restart:
# vi /etc/vmware/snmp.xml <config><snmpSettings><enable>true</enable><communities>C0MMUN1TY</communities><targets></targets></snmpSettings></config> # services.sh restart

No comments:

Post a Comment

Vmware NSX SSL creation 

Using OpenSSL for NSX Manager SSL import: Creates CSR and 4096 bit KEY Creating NSX 6.4.2 SSL    openssl req -out nsxcert.csr -newkey rsa:40...