PowerSNMP for .NET
Send a Trap Defined in a MIB



The following example demonstrates sending a trap defined in a loaded MIB.

C#
Copy Code
/*  This snippet uses a sample MIB file containing the following content, and a Mib code file 
    generated from the following MIB file content (our included MIB Treeview sample may be 
    used to easily create a Mib code file):

DART-TEST-TRAP-MIB DEFINITIONS ::= BEGIN

IMPORTS
    private, MODULE-IDENTITY, OBJECT-TYPE, NOTIFICATION-TYPE, Integer32
        FROM SNMPv2-SMI;

dart MODULE-IDENTITY
    LAST-UPDATED    "201406120000Z"
    ORGANIZATION    "Dart Communications"
    CONTACT-INFO    "support@dart.com"
    DESCRIPTION        "Upper-level Dart node"
    REVISION        "201406120000Z"
    DESCRIPTION        "First release."
    ::= { private 42 }

dartTrapObjects        OBJECT IDENTIFIER ::= {dart 2}
dartTraps            OBJECT IDENTIFIER ::= {dart 3}

testInt OBJECT-TYPE
    SYNTAX        Integer32
    MAX-ACCESS    read-write
    STATUS        current
    DESCRIPTION
        "Description for an integer"
    ::= {dartTrapObjects 1}

dartNotification NOTIFICATION-TYPE
    OBJECTS
    {
        testInt
    }
    STATUS    current
    DESCRIPTION
        "A custom SNMPv2 trap"
    ::= {dartTraps 1}

END

*/

/// <summary>
/// Send the trap defined in the MIB above, imported from a Mib code file generated from the MIB.
/// </summary>
/// <remarks>
/// For this button, the developer must include the Mib code file in their project, so that they 
/// don't need to load the MIB file directly at runtime. button2_Click is therefore redundant.
/// </remarks>
private void button1_Click(object sender, EventArgs e)
{
    //Import the Mib code file (must included in the project) generated from the MIB defined above
    Mib.Import(agent1.Mib);

    MibNode trapMibNode = agent1.Mib.GetByNodeName(Mib.NodeName.dartNotification);
    //Create variable to add to the trap. Examine MibNode.Objects for the objects associated with the MibNode.
    Variable vari = agent1.Mib.CreateVariable(Mib.NodeName.testInt, "5");

    sendCustomTrap(trapMibNode, vari);
}

/// <summary>
/// Send the trap defined in the MIB above, loaded from a MIB file.
/// </summary>
/// <remarks>
/// In this button, the MIB file is loaded at runtime. button1_Click is therefore redundant.
/// </remarks>
private void button2_Click(object sender, EventArgs e)
{
    //Load the MIB file defined above
    using (FileStream fs = new FileStream("DART-TEST-TRAP-MIB.mib", FileMode.Open, FileAccess.Read))
        agent1.Mib.Parse(fs);
    agent1.Mib.GenerateNodes();

    MibNode trapMibNode = agent1.Mib["dartNotification"];
    //Create variable to add to the trap. Examine MibNode.Objects for the objects associated with the MibNode.
    Variable vari = new Variable(agent1.Mib["testInt"], "6");

    sendCustomTrap(trapMibNode, vari);
}

/// <summary>
/// Sends an SNMPv2 trap defined by the provided MibNode, containing the provided Variable.
/// </summary>
/// <param name="trapMibNode">A MibNode object representing the trap</param>
/// <param name="vari">The variable required by the trap</param>
private void sendCustomTrap(MibNode trapMibNode, Variable vari)
{
    //Construct Trap2Message object
    Trap2Message trap = new Trap2Message(trapMibNode.Oid, trapMibNode.Description, agent1.SysUpTime);
    //Add the passed-in variable to the trap
    trap.Variables.Add(vari);
    //Send trap to manager
    agent1.Send(trap, myManagerAddress);
}
Visual Basic
Copy Code
'  This snippet uses a sample MIB file containing the following content, and a Mib code file 
'    generated from the following MIB file content (our included MIB Treeview sample may be 
'    used to easily create a Mib code file):
'
'DART-TEST-TRAP-MIB DEFINITIONS ::= BEGIN
'
'IMPORTS
'    private, MODULE-IDENTITY, OBJECT-TYPE, NOTIFICATION-TYPE, Integer32
'        FROM SNMPv2-SMI;
'
'dart MODULE-IDENTITY
'    LAST-UPDATED    "201406120000Z"
'    ORGANIZATION    "Dart Communications"
'    CONTACT-INFO    "support@dart.com"
'    DESCRIPTION        "Upper-level Dart node"
'    REVISION        "201406120000Z"
'    DESCRIPTION        "First release."
'    ::= { private 42 }
'
'dartTrapObjects        OBJECT IDENTIFIER ::= {dart 2}
'dartTraps            OBJECT IDENTIFIER ::= {dart 3}
'
'testInt OBJECT-TYPE
'    SYNTAX        Integer32
'    MAX-ACCESS    read-write
'    STATUS        current
'    DESCRIPTION
'        "Description for an integer"
'    ::= {dartTrapObjects 1}
'
'dartNotification NOTIFICATION-TYPE
'    OBJECTS
'    {
'        testInt
'    }
'    STATUS    current
'    DESCRIPTION
'        "A custom SNMPv2 trap"
'    ::= {dartTraps 1}
'
'END
'
'

''' <summary>
''' Send the trap defined in the MIB above, imported from a Mib code file generated from the MIB.
''' </summary>
''' <remarks>
''' For this button, the developer must include the Mib code file in their project, so that they 
''' don't need to load the MIB file directly at runtime. button2_Click is therefore redundant.
''' </remarks>
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Import the Mib code file (must included in the project) generated from the MIB defined above
    Mib.Import(agent1.Mib)

    Dim trapMibNode As MibNode = agent1.Mib.GetByNodeName(Mib.NodeName.dartNotification)
    'Create variable to add to the trap. Examine MibNode.Objects for the objects associated with the MibNode.
    Dim vari As Variable = agent1.Mib.CreateVariable(Mib.NodeName.testInt, "5")

    sendCustomTrap(trapMibNode, vari)
End Sub

''' <summary>
''' Send the trap defined in the MIB above, loaded from a MIB file.
''' </summary>
''' <remarks>
''' In this button, the MIB file is loaded at runtime. button1_Click is therefore redundant.
''' </remarks>
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Load the MIB file defined above
    Using fs As New FileStream("DART-TEST-TRAP-MIB.mib", FileMode.Open, FileAccess.Read)
        agent1.Mib.Parse(fs)
    End Using
    agent1.Mib.GenerateNodes()

    Dim trapMibNode As MibNode = agent1.Mib("dartNotification")
    'Create variable to add to the trap. Examine MibNode.Objects for the objects associated with the MibNode.
    Dim vari As New Variable(agent1.Mib("testInt"), "6")

    sendCustomTrap(trapMibNode, vari)
End Sub

''' <summary>
''' Sends an SNMPv2 trap defined by the provided MibNode, containing the provided Variable.
''' </summary>
''' <param name="trapMibNode">A MibNode object representing the trap</param>
''' <param name="vari">The variable required by the trap</param>
Private Sub sendCustomTrap(ByVal trapMibNode As MibNode, ByVal vari As Variable)
    'Construct Trap2Message object
    Dim trap As New Trap2Message(trapMibNode.Oid, trapMibNode.Description, agent1.SysUpTime)
    'Add the passed-in variable to the trap
    trap.Variables.Add(vari)
    'Send trap to manager
    agent1.Send(trap, myManagerAddress)
End Sub

PowerSNMP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic