VMware Cloud Community
chrisstewart
Contributor
Contributor

Add multiple SNMP traps destination locations

I'm trying to set multiple SNMP trap destinations, but can't seem to get the correct format.

Any ideas?

#Set Variables

$communities = "R3@d0nly"

$trapDestinationA = "1.1.1.1"

$trapDestinationB = "1.1.1.2"

Connect-VIServer server1

$vmhosts = @(Get-VMHost) | sort

foreach ($vmhost in $vmhosts)

{

Write-Host "Conecting to $vmhost"

$esxConnect =  Connect-VIServer -Server $vmhost -User root -Password "password"

#Get SNMP Object

$snmpObj = Get-VMHostSnmp

if(!((Get-VMHostSnmp).Enabled))

{

      Write-Host "Enabling SNMP"

      Set-VMHostSnmp -HostSnmp $snmpObj -Enabled:$true -ReadOnlyCommunity $communities

}

Else

{

      Write-Host "SNMP Already Enabled"

}

Write-Host "Setting SNMP Configuration"

Set-VMHostSnmp -HostSnmp $snmpObj -ReadOnlyCommunity $communities

Write-Host "Setting the new SNMP targets"

Set-VMHostSnmp -HostSnmp $snmpObj -AddTarget -TargetCommunity $communities -TargetHost $trapDestinationA

Set-VMHostSnmp -HostSnmp $snmpObj -AddTarget -TargetCommunity $communities -TargetHost $trapDestinationB

Write-Host "Disconnecting from $vmhost"

Disconnect-VIServer $esxConnect -Confirm:$false

}

ERROR

Set-VMHostSnmp : 7/14/2015 5:20:44 PM Set-VMHostSnmp

At C:\Users\admin\Documents\Set-snmp-traps.ps1:35 char:15

+ Set-VMHostSnmp <<<<  -HostSnmp $snmpObj -AddTarget -TargetCommunity $communities -TargetHost $trapDestinationB

+ CategoryInfo          : NotSpecified: (:) [Set-VMHostSnmp], SystemError

+ FullyQualifiedErrorId : Client20_SystemManagementServiceImpl_InitTrapTargetList_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVmHostSnmp

14 Replies
LucD
Leadership
Leadership

Seems to be working for me.

Which vSphere and PowerCLI versions are you using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership

As an alternative, you can use the Get-EsxCli cmdlet for managing SNMP.

Setting multiple traptargets could be done like this

$esxcli = Get-EsxCli -VMHost $vmhost

$snmp = $esxcli.system.snmp.get()

$auth = $snmp.authentication

if($snmp.authentication -eq ''){

    $auth = 'none'

}

$privacy = $snmp.privacy

if($snmp.priacy -eq ''){

    $privacy = 'none'

}

$esxcli.system.snmp.set($auth,

    $snmp.communities,

    $snmp.enable,

    $snmp.engineid,

    $snmp.hwsrc,

    $snmp.largestorage,

    $snmp.loglevel,

    $snmp.notraps,

    $snmp.port,

    $privacy,

    $snmp.remoteusers,

    $true,

    $snmp.syscontact,

    $snmp.syslocation,

    "1.1.1.1@162/R3@dOnly,1.1.1.2@162/R3@dOnly",

    $snmp.users,

    $snmp.v3targets)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
chrisstewart
Contributor
Contributor

vSphere 5.5 and Powercli 5.5

When trying to run I get:

Connecting to server1

Enabling SNMP

Set-VMHostSnmp : Cannot validate argument on parameter 'HostSnmp'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Users\admin\Documents\Set-snmp-traps.ps1:23 char:26

+ Set-VMHostSnmp -HostSnmp <<<<  $snmpObj -Enabled:$true -ReadOnlyCommunity $communities

+ CategoryInfo          : InvalidData: (:) [Set-VMHostSnmp], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVmHostSnmp

Set-VMHostSnmp : Cannot validate argument on parameter 'HostSnmp'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Users\admin\Documents\Set-snmp-traps.ps1:24 char:26

+ Set-VMHostSnmp -HostSnmp <<<<  $snmpObj

+ CategoryInfo          : InvalidData: (:) [Set-VMHostSnmp], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVmHostSnmp

Setting SNMP Configuration

Set-VMHostSnmp : Cannot validate argument on parameter 'HostSnmp'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Users\admin\Documents\Set-snmp-traps.ps1:34 char:25

+ Set-VMHostSnmp -HostSnmp <<<<  $snmpObj -ReadOnlyCommunity $communities

+ CategoryInfo          : InvalidData: (:) [Set-VMHostSnmp], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVmHostSnmp

Setting the new SNMP targets

Set-VMHostSnmp : Cannot validate argument on parameter 'HostSnmp'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Users\admin\Documents\Set-snmp-traps.ps1:37 char:25

+ Set-VMHostSnmp -HostSnmp <<<<  $snmpObj -AddTarget -TargetCommunity $communities -TargetHost $trapDestinationA

+ CategoryInfo          : InvalidData: (:) [Set-VMHostSnmp], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVmHostSnmp

Disconnecting from server1

Connecting to server2

Reply
0 Kudos
LucD
Leadership
Leadership

Your PowerCLI version is a bit outdated.

Can you try with a more recent build ?

Did the $esxcli method work ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
chrisstewart
Contributor
Contributor

Got an error with this, just running

Connect-VIServer server1

$esxcli = Get-EsxCli -VMHost server1

$snmp = $esxcli.system.snmp.get()

gives this error:

Community string was not specified in trap target: 1.1.1.1

At C:\Users\admin\AppData\Local\Temp\2\bda9564f-3d6b-41c6-afee-fd330749cf2c.ps1:5 char:32

+ $snmp = $esxcli.system.snmp.get <<<< ()

+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : MethodInvocationException

Reply
0 Kudos
LucD
Leadership
Leadership

That seems to indicate you are stuck with an invalid configuration.

Try to clean up with

$esxcli.system.snmp.set('none',

    $null,

    $false,

    $null,

    $null,

    $null,

    $null,

    $null,

    $null,

    'none',

    $null,

    $true,

    $null,

    $null,

    $null,

    $null,

    $null)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
chrisstewart
Contributor
Contributor

I opened ssh and tried to reset with the --reset and got error:

esxcli system snmp set --reset and get "Community string was not specified in the trap target: 1.1.1.1

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid you will have to dive into the config files to get this straight again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
chrisstewart
Contributor
Contributor

I have tried numerous methods to reset the snmp.xml file this back to default, but can't find any solution.

Looks like in my previous attempts I only had the ip address and no community string was associated with the trap destination and because of that it errors out.

The only way I can figure out how to fix it is to manually turn on ssh, and then remove the ip address in the <trap>1.1.1.1</trap> setting.  Very painful since I have about 64 servers to do this on.

Reply
0 Kudos
LucD
Leadership
Leadership

You could automate that with plink.exe from the PuTTY Suite.

There are numerous examples on the use of plink.exe available in this community, do a search.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
chrisstewart
Contributor
Contributor

Since the esxcli system snmp set --reset does not work, or the other ways, it sound like a little bug.

Thanks anyway for your help will try and use plink to see if I can get those working again.

Reply
0 Kudos
chrisstewart
Contributor
Contributor

Here is what I have come up with:

pastedImage_1.png

and in the commands.txt file:

sed -i 's/1.1.1.1/1.1.1.1 R3@d0nly/g' /etc/vmware/snmp.xml

esxcli system snmp set --reset

exit

Note you will still see the server rsa2 key fingerprint information, but the script is still working.

Now that I got all the systems back to default I can rerun the script with the correct syntax and get is properly configured.

Thanks again.

Reply
0 Kudos
LucD
Leadership
Leadership

Great, thanks for sharing that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
piercej2
Contributor
Contributor

Hi,

I was having a similar issue after I removed the Community string from the snmp config, before I removed the targets.

Easiest fix for me was to edit /etc/vmware/snmp.xml using vi

I deleted the IP Address of the Targets so that the fille looked like the following ....... <targets></targets> .........

After that, I was able to reset the snmp config using esxcli system snmp set -r

Quick question, what are the maximum number of scnp v2 targets per Community, is it really 3 ?

is there any way to add up to 30 targets per community ?