'************************************************************************* ' Script Name - "SNMP" ' ' Purpose: To performe SNMP query to SNMP enabled device (Networking, OS ETC) ' ' Assumptions: ' ' SNMPUtil.exe install on the server and exist in c:\windows\system32 ' ' Parameters: ' ' TargetIP = SNMP Device IP Address or DNS name, for ex "192.168.0.1" / "server.domain.com" ' TargetName = SNMP Device name, for ex "PIX" ' Community = SNMP Community string ' Oid = Object ID, for exm: ".1.3.6.1.4.1.9.9.48" ' OidDescription = Oid description, for ex "Current connection" ' MaxPar = Generate Alert while the SNMP above this value, for ex 1000 ' ' Create by: Idan Lerer - idan_le@hotmail.com ' ' $Date: 2005/09/06 19:13:35 $ $Revision: 9 $ '************************************************************************* 'Option Explicit Dim strSNMPUtilPath Dim oParams, oEvent, WshShell Dim strComputerName, dtmEventTime Dim strTargetIP, strTargetName, strOidDescription, nResult, MsgEvent Dim strCommunity, strOid, MaxPar, stObjectName Const EVENT_SOURCE = "SNMP Get" 'Event Type Constants Const EVENT_TYPE_SUCCESS = 0 Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENT_TYPE_INFORMATION = 4 Const SUCCESS = 0 Const ERROR = 1 Const WARNING = 2 Const INFORMATION = 4 Const AUDIT_SUCCESS = 8 Const AUDIT_FAILURE = 16 'Events IDs for start, end and error during this script Const EVENTID = 9876 'Get Parameters Set oParams = ScriptContext.Parameters strTargetIP = oParams.get("TargetIP") strTargetName = oParams.get("TargetName") strCommunity = oParams.get("Community") strOid = oParams.get("Oid") strOidDescription = oParams.get("OidDescription") MaxPar = CLng(oParams.get("MaxPar")) 'Get triggering event properties Set oEvent = ScriptContext.Event strComputerName = oEvent.SourceComputer dtmEventTime = DateAdd("d",-1,oEvent.UTCTime) stObjectName = "SNMP" strSNMPUtilPath = "c:\windows\system32\snmputil.exe" nResult = (GetValueFromCmd(strSNMPUtilPath & " get " & strTargetIP & " " & strCommunity & " " & strOid)) Set WshShell = CreateObject("WScript.Shell") nResult = Cdbl(nResult) if nResult > MaxPar then MsgEvent = "The " & strTargetName & " " & strOidDescription & " > " & MaxPar & ", Current value = " & nResult CreateEvent EVENT_SOURCE, EVENTID ,EVENT_TYPE_WARNING, MsgEvent ,strComputerName end if CreatePerformanceData stObjectName, _ strTargetName , _ strOidDescription, _ nResult, _ strComputerName '================================================================================= ' Publish resulting performance data / Create events '================================================================================= Sub CreatePerformanceData (ByVal strObjectName, _ ByVal strCounterName, _ ByVal strInstanceName, _ ByVal dblSampleValue, _ strSrcComputer) Dim oPerfData Set oPerfData = ScriptContext.CreatePerfData If strSrcComputer <> "" Then oPerfData.SourceComputer = strSrcComputer End If oPerfData.ObjectName = strObjectName oPerfData.CounterName = strCounterName oPerfData.InstanceName = strInstanceName oPerfData.Value = dblSampleValue ScriptContext.Submit(oPerfData) Set oPerfData = Nothing End Sub '========================================================================= ' Insert event to MOM all other events '========================================================================= Sub CreateEvent(strSource, lngEventID, lngEventType, strMsg, strSrcComputer) Const MAX_EVENT_DESCRIPTION_LENGTH = 3450 Dim oNewEvent ' Create a new event Set oNewEvent = ScriptContext.CreateEvent ' Set event properties oNewEvent.Message = Left(strMsg, MAX_EVENT_DESCRIPTION_LENGTH) oNewEvent.EventNumber = lngEventID oNewEvent.EventType = lngEventType oNewEvent.EventSource = strSource If strSrcComputer <> "" Then oNewEvent.SourceComputer = strSrcComputer oNewEvent.LoggingComputer = strSrcComputer End If ' Submit the event ScriptContext.Submit oNewEvent Set oNewEvent = Nothing End Sub '========================================================================= ' Runs a snmputil command and returns the last word in the Value line '========================================================================= function GetValueFromCmd(sCmd) Dim WshShell, oUtil, oExec, sLine, arrLine, sResult Set WshShell = CreateObject("WScript.Shell") Set oUtil = CreateObject("OpScrUtil.Utility") Set oExec = WshShell.Exec(sCmd) Do While oExec.Status = 0 oUtil.Sleepms(100) Loop Do While Not oExec.StdOut.AtEndOfStream sLine = oExec.StdOut.ReadLine if instr(sLine, "Value") > 0 then arrLine = split(sLine, " ") sResult = trim(arrLine(ubound(arrline))) exit do end if Loop GetValueFromCmd = sResult end function