' Unbrowse SNMP Scripting Interface Demo ' ' t2sql - start, stop, and control the unbrowse trap ' passive receiver. stores all incoming traps ' to a SQL database via an ODBC connection ' ' Usage: cscript t2sql.vbs ' trapdatabase = The VOTRP file where traps will be stored ' maxtraps = Stops after these many traps are captured ' ' Licensing ' You can use this in any way you please. No warranties. ' --------------------------------------------------------------- ' -------------------------------------------------- ' Check usage & arguments ' -------------------------------------------------- Set Sout = WScript.StdOut If WScript.Arguments.Count <> 1 Then Sout.WriteLine "Usage: t2sql " WScript.Quit End If DSNName = WScript.Arguments.Item(0) MaxTraps = 100000 Stopping = False Set RepMgr = CreateObject("UnbrowseSNMP.RepositoryManager") Set RepDB = RepMgr.LoadRepositoryReadOnly ' ------------------------------------------------------------- ' Create the ODBC connection to a remote or local SQL database ' You can use any database MySQL, Oracle, SQL Server. ' You must create a DSN (Data source name) by supplying the ' correct authentication credentials. This is typically done ' via Control Panel -> Admin Tools -> Data Sources ' ------------------------------------------------------------- Dim SqlConn Set SqlConn = CreateObject("ADODB.Connection") SqlConn.open "DSN=" & DSNName ' -------------------------------------------------- ' Create the trap server ' -------------------------------------------------- Set TrapMgr = WScript.CreateObject("UnbrowseSNMP.TrapReceiver","OnTrap_") Sout.WriteLine "Loaded the Unbrowse SNMP Trap Server" ' -------------------------------------------------- ' Set some parameters ' -------------------------------------------------- 'TrapMgr.AdapterIndex = 2 ' Capture from this adapter (only if Winpcap installed) ' To get the name ({E308..}) ' 1. Go to Tools->Customize->Trap Console ' 2. Select the adapter you want to use and click Details ' 3. Copy the Full Name (GUID) field (select and press Ctrl-C) ' 4. Paste it into the code below for TrapMgr.AdapterName TrapMgr.AdapterName = "{FDA463D9-ADEC-4E27-A37F-BC221A50A9C0}" ' Hint Set to False for Wireless 802.11 TrapMgr.UsePromiscuousMode = False ' If you have both Winpcap (recommended) but still want to use Raw Sockets ' Leave it at False for most cases TrapMgr.PreferRawOverPcap = False ' -------------------------------------------------- ' Open a new database - you can save this later ' -------------------------------------------------- TrapMgr.NewDatabase ' -------------------------------------------------- ' Start the passive trap receiver ' -------------------------------------------------- Sout.WriteLine "Starting .." Stopping=False TrapMgr.Start Sout.WriteLine "Listening for traps .." ' ------------------------------------------------------ ' Enter into an loop, processing traps ' Here were stopping after a few traps for demo purposes ' ------------------------------------------------------ Sout.WriteLine "Dumping traps as they are received .. (Ctrl+C) to quit" TrapCount = 0 TrapsLeft = MaxTraps Do While TrapsLeft > 0 WScript.Sleep 100 TrapsLeft = MaxTraps - TrapCount Loop Stopping=True ' -------------------------------------------------- ' Stop the trap receiver ' -------------------------------------------------- Sout.WriteLine "Stopping the trap receiver" ' Wait for script to catch up with server WScript.Sleep 3000 TrapMgr.Stop ' -------------------------------------------------- ' Close the ODBC connection. This could take a few ' seconds, this is normal behavior. ' -------------------------------------------------- Sout.WriteLine "Closing the SQL ODBC connection" SqlConn.close Set SqlConn = Nothing ' -------------------------------------------------- ' Save contents into the database given by user ' -------------------------------------------------- Sout.WriteLine "Closing trap receiver" TrapMgr.Close TrapMgr = Nil Sout.WriteLine "Done" WScript.Quit 0 ' --------------------------------------------------- ' OnNewTrap : Event handler ' When you get a new trap, do your thing ' --------------------------------------------------- Sub OnTrap_TNF_NewTrap (id , OneTrap) If Not Stopping Then PrintTrap2(OneTrap) StoreTrapToSQL OneTrap,SqlConn '-------------------------- ' After a few traps quit '-------------------------- TrapCount = TrapCount+1 End If End Sub ' --------------------------------------------------- ' PrintTrap : Subroutine ' Dumps almost all trap information onto screen ' --------------------------------------------------- Sub PrintTrap (OneTrap) ' -------------------------- ' Print all trap information ' -------------------------- Sout.WriteLine " ------------------------------------------" Sout.WriteLine " Trap ID : " & OneTrap.ID Sout.WriteLine " OID : " & OneTrap.EffectiveTrapOID Sout.WriteLine " From Agent : " & OneTrap.AgentAddress Sout.WriteLine " To Manager : " & OneTrap.DestinationAddress Sout.WriteLine " Timestamp : " & OneTrap.TimestampLocal Sout.WriteLine " OID : " & OneTrap.EffectiveTrapOID Sout.WriteLine " User/Comm : " & OneTrap.UserCommunity Sout.WriteLine " Varbinds : " & OneTrap.VarbindCount ' -------------------------- ' Print all varbinds in trap ' -------------------------- Sout.WriteLine " --------- Varbind list ----------" For I = 0 To OneTrap.VarbindCount - 1 Set OneVar = OneTrap.GetVarbindByIdx(I) Sout.WriteLine vbTab & " " & I+1 & " ) " & OneVar.OID & " = " & OneVar.Value Next Sout.WriteLine vbCrLf End Sub ' --------------------------------------------------- ' PrintTrap2 : Subroutine ' Dummy simply translates OID ' --------------------------------------------------- Sub PrintTrap2 (OneTrap) Sout.WriteLine " OID : " & OneTrap.EffectiveTrapOID Sout.WriteLine " OID : " & RepDB.OIDToNamePrefix(OneTrap.EffectiveTrapOID) Sout.WriteLine "----------------------------------------------------" Sout.WriteLine vbCrLf End Sub ' --------------------------------------------------- ' StoreTrapToSQL : Subroutine ' Store the incoming trap to a SQL Connection ' Parameters ' TheTrap - the trap that needs to be stored ' SqlConn - the ODBC connection to a database ' --------------------------------------------------- Sub StoreTrapToSQL (TheTrap,Conn) ' ----------------------------- ' Create list of valid columns ' (see schema above) ' ----------------------------- ColNames = "Timestamp,AgentAddress,EffectiveTrapOID," &_ "Version,Context,UserCommunity,VarbindCount" For I = 0 To TheTrap.VarbindCount - 1 ColNames = ColNames & ", Varbind_OID_" & I & ", Varbind_Value_" & I Next ' --------------------------------------------------- ' Assign values from the incoming trap to the columns ' --------------------------------------------------- ColValues = QuoteVal(TheTrap.TimestampLocal) & "," &_ QuoteVal(TheTrap.AgentAddress) & "," &_ QuoteVal(TheTrap.EffectiveTrapOID) & "," &_ TheTrap.SNMPVersion & "," &_ QuoteVal(TheTrap.ContextName) & "," &_ QuoteVal(TheTrap.UserCommunity) & "," &_ TheTrap.VarbindCount For I = 0 To TheTrap.VarbindCount - 1 Set OneVar = TheTrap.GetVarbindByIdx(I) ColValues = ColValues & "," &_ QuoteVal(OneVar.OID) &_ "," &_ QuoteVal(OneVar.Value) Next ' --------------------------------------------------- ' The complete INSERT statement ' --------------------------------------------------- InsertStmt = "INSERT INTO alltraps " &_ "( " & ColNames & ") " &_ " VALUES " &_ "( " & ColValues & ") ;" Sout.WriteLine " SQL Statement = " & InsertStmt Sout.WriteLine vbCrLf ' --------------------------------------------------- ' Execute the INSERT statement ' --------------------------------------------------- Conn.execute InsertStmt End Sub ' --------------------------------------------------- ' QuoteVal - given a string append quotes to it ' required for the SQL INSERT statements ' also remove any NULLs from string ' --------------------------------------------------- Function QuoteVal(str) If Len(str) > 0 Then str = Left(str,Len(str)-1) End If QuoteVal = "'" & str & "'" End Function