Automate creation of SNMP Trap Destinations with SMS/ConfigMgr!

We had a situation where 1400+ servers needed to be able to communicate with a second SNMP server that was added as a backup to receive SNMP trap alerts.

Changing this information manually would be far too intensive.

I developed the following VBScript that was deployed by SMS to make the revision. Note that you would modify the 'SERVERNAME1' and 'SERVERNAME2' values below. The value for <identifier> relates to unique name that a company would choose.

The SNMP service gets recycled after the update so the trap destination change takes effect immediately.

Enjoy, as always test in a lab before deploying to 1000's of servers ;)

' watch word wrap!

OPTION EXPLICIT
const HKEY_LOCAL_MACHINE = &H80000002
dim strKeyPathRoot
dim strKeyPath
dim strComputer
dim strESXHost
dim strValueName
dim oReg
dim strValue
dim strUpdateMode
dim dwValue
Dim objWMIService
Dim colServiceList
DIM objService

' test mode reads and reports
'strUpdateMode = "Test"
' Update mode updates reg keys and does not report
strUpdateMode = "Update"

strComputer = "."

' adjust SNMP parms
SetSNMP

' recycle SNMP service
RecycleSNMP

' Set StdOut = WScript.StdOut
Sub SetSNMP

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")


strKeyPathRoot = "SYSTEM\CurrentControlSet\Services\SNMP\Parameters"

' read TrapConfig key, if not found create new!
strKeyPath = strKeyPathRoot & "\TrapConfiguration\<identifier>\"
strValueName = "1"
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
if strUpdateMode = "Test" Then
    wscript.echo "strValue = " & strValue
    wscript.echo "strValueName = " & strValueName
end if
' strValue contains a NULL if not found...

     strKeyPath = strKeyPathRoot & "\TrapConfiguration\<identifier."
'     wscript.echo "strKeyPath = " & strKeyPath
     oReg.CreateKey HKEY_LOCAL_MACHINE, strKeyPath

    strValue = "SERVERNAME1"
    strValueName = "1"
    oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue

    strValue = "SERVERNAME2"
    strValueName = "2"
    oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue

End Sub

Sub RecycleSNMP
' stop & restart service...
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service Where Name = 'SNMP'")
'    ("Select * from Win32_Service Where Name = 'ccmexec'")

For each objService in colServiceList

if UCASE(objService.Name) = "SNMP" Then

       ' wscript.echo "objService.Name= '" & objService.Name & "'"
       ' stop service
       objService.StopService()
  '     strErr = err
  '     strLogData = "Stop Service step -- '" & objService.Name & "'. Return Code('"  & strErr & "')"
  '     WriteEventLog EVENT_SUCCESS, strLogData
       ' wait
       Wscript.Sleep 20000
       ' change out service account
'      strErr = objService.Change _
'        ( , , , , , , strAccount, "nt$local")
'      strErr = err
'      strLogData = "Change Service Account and password step -- " & "Return Code('"  & strErr & "')"
'      WriteEventLog EVENT_SUCCESS, strLogData

       ' restart service
       objService.StartService()
'       strErr = err
'       strLogData = "Re-start Service step -- '" & objService.Name & "Return Code('"  & strErr & "')"
'       WriteEventLog EVENT_SUCCESS, strLogData
   end if
Next
End Sub

Technorati Tags: ,,
Published Friday, August 08, 2008 6:51 PM by sthompson

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems