Setup SNMP: VMware ESXi via vMA

Do you have an SNMP monitor you would like to tie together with your VMware infrastructure? Do you want to set this up quickly and efficiently?

If you answered ‘yes’ to both of those questions then continue reading this post!

There are 2 ways to do this – one is to loop through requesting the password from the user for each host (if each host has a different root password), or using the vifp and vifptarget commands (which come in handy for other scriptable items). using vifp/viptarget is another subject, possibly for a different blog post.

First – let me push out a shameless plug for LogicMonitor – the best tool I have found for doing monitoring and measurement of my hosts since 2011. I don’t work there and I get nothing for mentioning their name; I just feel that they are the best so far and deserve the kudos.

Second, create a text file with either host names or IP addresses, like so:

192.168.31.14
vmhost01.geekandi.net
172.28.91.194

and we will use this as input to the for loop.

#!/bin/sh

# set username for authentication, normally this is 'root'
VI_USERNAME=root ; export VI_USERNAME

# set SNMP community string - don't use public
COMMUNITY=public ; export COMMUNITY

# SNMP trap receiver IP address
TRAPRECV=192.168.182.17 ; export TRAPRECV

# within the for loop it will prompt you for each host's
# root password. This section could be commented out if
# you have already set up vifp parameters

for host in `cat that-text-file.txt` ; do
  echo -n "Enter ${host} root password: "
  read VI_PASSWORD ; export VI_PASSWORD

  # now the good parts

  # set the port and community
  esxcfg-snmp -server ${host} -p 161 -c $COMMUNITY

  # enable SNMP
  esxcfg-snmp -server ${host} -E

  # send traps to your SNMP trap receiver
  esxcfg-snmp -server ${host} -t ${TRAPRECV}@162/${COMMUNITY}

  # SNMP can be a bit noisy about things, so map out some OIDs
  # that you probably don't wanna see anyway as I haven't seen
  # these being useful at all
  esxcfg-snmp --server ${host} -n \
    1.3.6.1.4.1.6876.4.90,1.3.6.1.4.1.6876.4.30.3.0,\
1.3.6.1.4.1.6876.50.101.0,1.3.6.1.4.1.6876.50.102.0,\
1.3.6.1.4.1.6876.4.1

done

There you have it, simple, quick, efficient.

Updated: Setting trap receiver uses ‘-t’ (lowercase) instead of ‘-T’ (uppercase) from vMA 5.1 to ESXi v5.1. Hat tip to @vglen_mpls (Glen) for finding this so I could update this post!