PowerSNMP for .NET
GetTableColumns(MibNode) Method
Example 




The table MibNode parent of the columnar MibNode objects to return.
Returns the columnar MibNode objects that are under the specified table MibNode.
Syntax
Public Overloads Function GetTableColumns( _
   ByVal tableNode As MibNode _
) As MibNode()
Dim instance As MibNodes
Dim tableNode As MibNode
Dim value() As MibNode
 
value = instance.GetTableColumns(tableNode)
public MibNode[] GetTableColumns( 
   MibNode tableNode
)
public: MibNode*[]* GetTableColumns( 
   MibNode* tableNode
) 
public:
array<MibNode^>^ GetTableColumns( 
   MibNode^ tableNode
) 

Parameters

tableNode
The table MibNode parent of the columnar MibNode objects to return.

Return Value

An array of columnar MibNode objects.
Exceptions
ExceptionDescription
System.ArgumentException The collection does not contain a table entry or column MibNode under the specified table MibNode, or the collection is unsorted and MibNodes.Sort() should be called.
Remarks
This method assumes that the MibNodes collection is ordered by OID. If it has been modified by adding or inserting MibNode objects then Sort should be called prior to calling this method. This is not required after calling Import; it sorts this collection during the import process.
Example
The following example demonstrates adding a whole table to Agent.Variables from a 2d array. The following example demonstrates setting a whole table on a remote Agent from a Manager from values in a 2d array.
/// <summary>
/// Adds/sets table values from the specified 2d string array to/in the Agent's variables collection.
/// </summary>
/// <param name="myAgent">Agent instance to add/set the table data to/in.</param>
/// <param name="tableNameOrOid">Name or OID of the table.</param>
/// <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
/// <remarks>
/// This method takes a string tableNameOrOid argument, therefore it is compatible with dynamically loaded MIBs.
/// This method requires the MIB or Mib Code File defining the table to be loaded/imported in myManager.Mib.
/// To sparsely populate the table, or to not update a table cell, specify null/Nothing for the tableData element.
/// </remarks>
private void setLocalTable(Agent myAgent, string tableNameOrOid, string[,] tableData)
{
    //Can also take a MibNode argument
    MibNode[] tableNodes = myAgent.Mib.GetTableColumns(tableNameOrOid);
    int rows = tableData.GetLength(0);
    int columns = tableData.GetLength(1);

    if (columns != tableNodes.Length)
        throw new ArgumentException("The number of columns contained in the table data does not match the table's definition", "tableData");

    for (int rowIndex = 0; rowIndex <= rows - 1; rowIndex++)
    {
        for (int columnIndex = 0; columnIndex <= columns - 1; columnIndex++)
        {
            if (tableData[rowIndex, columnIndex] != null)
            {
                Variable tableCellVariable = new Variable(tableNodes[columnIndex], tableData[rowIndex, columnIndex], rowIndex.ToString());
                //The below allows updating Variables in and adding Variables to the collection. 
                //If initializing, Agent.Variables.Add() may be used instead.
                myAgent.Variables[tableCellVariable.Id] = tableCellVariable;
            }
        }
    }
}

/// <summary>
/// Overload that takes a NodeName parameter.
/// </summary>
/// <param name="myAgent">Agent instance to add/set the table data to/in.</param>
/// <param name="tableNodeName">NodeName of the table. From either Dart.Snmp.NodeName or Mib.NodeName.</param>
/// <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
/// <remarks>
/// This overload takes an Enum tableNodeName argument, therefore it can only be used with built-in NodeNames, or 
/// NodeNames from an imported Mib Code File.
/// </remarks>
private void setLocalTable(Agent myAgent, Enum tableNodeName, string[,] tableData)
{
    setLocalTable(myAgent, tableNodeName.ToString(), tableData);
}
''' <summary>
''' Adds/sets table values from the specified 2d string array to/in the Agent's variables collection.
''' </summary>
''' <param name="myAgent">Agent instance to add/set the table data to/in.</param>
''' <param name="tableNameOrOid">Name or OID of the table.</param>
''' <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
''' <remarks>
''' This method takes a string tableNameOrOid argument, therefore it is compatible with dynamically loaded MIBs.
''' This method requires the MIB or Mib Code File defining the table to be loaded/imported in myManager.Mib.
''' To sparsely populate the table, or to not update a table cell, specify null/Nothing for the tableData element.
''' </remarks>
Private Sub setLocalTable(ByVal myAgent As Agent, ByVal tableNameOrOid As String, ByVal tableData(,) As String)
    'Can also take a MibNode argument
    Dim tableNodes() As MibNode = myAgent.Mib.GetTableColumns(tableNameOrOid)
    Dim rows As Integer = tableData.GetLength(0)
    Dim columns As Integer = tableData.GetLength(1)

    If columns <> tableNodes.Length Then
        Throw New ArgumentException("The number of columns contained in the table data does not match the table's definition", "tableData")
    End If

    For rowIndex As Integer = 0 To rows - 1
        For columnIndex As Integer = 0 To columns - 1
            If tableData(rowIndex, columnIndex) IsNot Nothing Then
                Dim tableCellVariable As New Variable(tableNodes(columnIndex), tableData(rowIndex, columnIndex), rowIndex.ToString())
                'The below allows updating Variables in and adding Variables to the collection. 
                'If initializing, Agent.Variables.Add() may be used instead.
                myAgent.Variables(tableCellVariable.Id) = tableCellVariable
            End If
        Next columnIndex
    Next rowIndex
End Sub

''' <summary>
''' Overload that takes a NodeName parameter.
''' </summary>
''' <param name="myAgent">Agent instance to add/set the table data to/in.</param>
''' <param name="tableNodeName">NodeName of the table. From either Dart.Snmp.NodeName or Mib.NodeName.</param>
''' <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
''' <remarks>
''' This overload takes an Enum tableNodeName argument, therefore it can only be used with built-in NodeNames, or 
''' NodeNames from an imported Mib Code File.
''' </remarks>
Private Sub setLocalTable(ByVal myAgent As Agent, ByVal tableNodeName As [Enum], ByVal tableData(,) As String)
    setLocalTable(myAgent, tableNodeName.ToString(), tableData)
End Sub
/// <summary>
/// Sets table values on a remote agent from the values contained in a 2d string array.
/// </summary>
/// <param name="myManager">Local Manager instance</param>
/// <param name="agentEP">Agent address</param>
/// <param name="tableNameOrOid">Name or OID of the table.</param>
/// <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
/// <remarks>
/// This method takes a string tableNameOrOid argument, therefore it is compatible with dynamically loaded MIBs.
/// This method requires the MIB or Mib Code File defining the table to be loaded/imported in myManager.Mib.
/// To sparsely populate the table, or to not update a table cell, specify null/Nothing for the tableData element.
/// </remarks>
private void setRemoteTable(Manager myManager, IPEndPoint agentEP, string tableNameOrOid, string[,] tableData)
{
    //Can also take a MibNode argument
    MibNode[] tableNodes = myManager.Mib.GetTableColumns(tableNameOrOid);
    int rows = tableData.GetLength(0);
    int columns = tableData.GetLength(1);

    if (columns != tableNodes.Length)
        throw new ArgumentException("The number of columns contained in the table data does not match the table's definition", "tableData");

    SnmpSocket managerSocket = new SnmpSocket(myManager);

    try
    {
        //Set the variables on the agent row by row (instead of trying to set the entire table at once) so that it's unlikely that the set 
        //messages are too large (otherwise the response's ResponseMessage.ErrorStatus is ErrorStatus.TooBig).
        //-If this is the case (can be caused by many columns, or cells that contain a lot of data), break up the Variables across more SetMessages.
        for (int rowIndex = 0; rowIndex <= rows - 1; rowIndex++)
        {
            SetMessage rowSetMessage = new SetMessage();
            for (int columnIndex = 0; columnIndex <= columns - 1; columnIndex++)
                if(tableData[rowIndex, columnIndex] != null)
                    rowSetMessage.Variables.Add(new Variable(tableNodes[columnIndex], tableData[rowIndex, columnIndex], rowIndex.ToString()));
            ResponseMessage response = managerSocket.GetResponse(rowSetMessage, agentEP, 3);
            //Handle response as desired here...
        }
    }
    finally
    {
        managerSocket.Close();
    }
}

/// <summary>
/// Overload that takes a NodeName parameter.
/// </summary>
/// <param name="myManager">Local Manager instance</param>
/// <param name="agentEP">Agent address</param>
/// <param name="tableNodeName">NodeName of the table. From either Dart.Snmp.NodeName or Mib.NodeName.</param>
/// <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
/// <remarks>
/// This overload takes an Enum tableNodeName argument, therefore it can only be used with built-in NodeNames, or 
/// NodeNames from an imported Mib Code File.
/// </remarks>
private void setRemoteTable(Manager myManager, IPEndPoint agentEP, Enum tableNodeName, string[,] tableData)
{
    setRemoteTable(myManager, agentEP, tableNodeName.ToString(), tableData);
}
''' <summary>
''' Sets table values on a remote agent from the values contained in a 2d string array.
''' </summary>
''' <param name="myManager">Local Manager instance</param>
''' <param name="agentEP">Agent address</param>
''' <param name="tableNameOrOid">Name or OID of the table.</param>
''' <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
''' <remarks>
''' This method takes a string tableNameOrOid argument, therefore it is compatible with dynamically loaded MIBs.
''' This method requires the MIB or Mib Code File defining the table to be loaded/imported in myManager.Mib.
''' To sparsely populate the table, or to not update a table cell, specify null/Nothing for the tableData element.
''' </remarks>
Private Sub setRemoteTable(ByVal myManager As Manager, ByVal agentEP As IPEndPoint, ByVal tableNameOrOid As String, ByVal tableData(,) As String)
    'Can also take a MibNode argument
    Dim tableNodes() As MibNode = myManager.Mib.GetTableColumns(tableNameOrOid)
    Dim rows As Integer = tableData.GetLength(0)
    Dim columns As Integer = tableData.GetLength(1)

    If columns <> tableNodes.Length Then
        Throw New ArgumentException("The number of columns contained in the table data does not match the table's definition", "tableData")
    End If

    Dim managerSocket As New SnmpSocket(myManager)

    Try
        'Set the variables on the agent row by row (instead of trying to set the entire table at once) so that it's unlikely that the set 
        'messages are too large (otherwise the response's ResponseMessage.ErrorStatus is ErrorStatus.TooBig).
        '-If this is the case (can be caused by many columns, or cells that contain a lot of data), break up the Variables across more SetMessages.
        For rowIndex As Integer = 0 To rows - 1
            Dim rowSetMessage As New SetMessage()
            For columnIndex As Integer = 0 To columns - 1
                If tableData(rowIndex, columnIndex) IsNot Nothing Then
                    rowSetMessage.Variables.Add(New Variable(tableNodes(columnIndex), tableData(rowIndex, columnIndex), rowIndex.ToString()))
                End If
            Next columnIndex
            Dim response As ResponseMessage = managerSocket.GetResponse(rowSetMessage, agentEP, 3)
            'Handle response as desired here...
        Next rowIndex
    Finally
        managerSocket.Close()
    End Try
End Sub

''' <summary>
''' Overload that takes a NodeName parameter.
''' </summary>
''' <param name="myManager">Local Manager instance</param>
''' <param name="agentEP">Agent address</param>
''' <param name="tableNodeName">NodeName of the table. From either Dart.Snmp.NodeName or Mib.NodeName.</param>
''' <param name="tableData">A 2d array [rows, columns] containing the table data to set on the agent.</param>
''' <remarks>
''' This overload takes an Enum tableNodeName argument, therefore it can only be used with built-in NodeNames, or 
''' NodeNames from an imported Mib Code File.
''' </remarks>
Private Sub setRemoteTable(ByVal myManager As Manager, ByVal agentEP As IPEndPoint, ByVal tableNodeName As [Enum], ByVal tableData(,) As String)
    setRemoteTable(myManager, agentEP, tableNodeName.ToString(), tableData)
End Sub
See Also

Reference

MibNodes Class
MibNodes Members
Overload List


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