View Single Post
 
Old 05-21-2014, 10:46 PM
FrogFumes FrogFumes is offline
Newcomer
 
Join Date: May 2014
Posts: 4
Default Creating a function to condense SMNP get

Hello all,

Excited to be a part of a new forum.

I am not a programmer by trade but play my hand at it when the need arises. I have worked with VB6 mostly and I am finally trying to wrap my head around OOP.

I work with some gear which has atrocious UI and decided to write a program which can pull the relevant information I need vis SNMP without pouring through tons of menus.

That being said I discovered a nice dll written by the folks over a sharpsnmp.


There VB.NET example is here
http://www.snmpsharpnet.com/?page_id=105

WHat I would like to do is insert the Get command into a function so that each time I need to use it I can just pass the function and some arguments without retyping the whole code each time.

For example.

Code:
Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        Dim host As String = "localhost"
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If
    End Sub
 
End Module
Is the example given and I would like the function to operate with arguments for the string for host and the string for OID.

The problem I am running into is im not sure what return value I need to place for the function.

so If I want it to be

Code:
Public Function GetSNMP(byval HostAddress as string, Byval OID as string) as ReturnValue???
Essentially what I am wanting to do is connect to a host pull various integers by passing the get request to different OID's and parse the returned kvp.key.tostring() and kvp.value.tostring() into listviews.

My attempt so far has led me to.

Code:
 Imports SnmpSharpNet

Module GetSNMP


    Public Function GetSNMP(ByVal HostAddress As String, ByVal OID As String) As String '<~~~ Passing as a string is not correct
        Dim host As String = HostAddress
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {OID}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Function
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If

    End Function

End Module
Again, I'd like to be able to pass this by using a function but am unsure as how to go about it and be able to get the information.

My current use is placed under a button as a proof of concept but is a bit lengthy and I would like to reduce the redundant code.

Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        lvDetails2.Items.Clear()

        Dim host As String = ComboBox1.Text
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        Dim rootOid As Oid = New Oid(".1.3.6.1.4.1.15921.1.3.3.1.1.11")
        Dim nextOid As Oid = rootOid
        Dim keepGoing As Boolean = True
        requestOid = New String() {rootOid.ToString()}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        While keepGoing
            result = snmp.GetNext(SnmpVersion.Ver1, New String() {nextOid.ToString()})
            If result IsNot Nothing Then
                Dim kvp As KeyValuePair(Of Oid, AsnType)
                For Each kvp In result
                    If rootOid.IsRootOf(kvp.Key) Then
                        Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                              SnmpConstants.GetTypeName(kvp.Value.Type), _
                                              kvp.Value.ToString())
                        Dim btsID As String = kvp.Key.ToString()
                        Dim dlPIN As String = kvp.Value.ToString()
                        Dim objItem As New ListViewItem

                        If btsID.Contains("1.1.11.1.1.1") Then
                            ListBox1.Items.Add("RIU 1 LTE DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "1"
                                .SubItems.Add("700")
                                .SubItems.Add("A")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.2") Then
                            ListBox1.Items.Add("RIU 1 Cell A DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "2"
                                .SubItems.Add("Cell A")
                                .SubItems.Add("n/a")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.3") Then
                            ListBox1.Items.Add("RIU 1 PCS DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "3"
                                .SubItems.Add("PCS")
                                .SubItems.Add("n/a")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.4") Then
                            ListBox1.Items.Add("RIU 1 AWS DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "4"
                                .SubItems.Add("AWS")
                                .SubItems.Add("A")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.2.1") Then
                            ListBox1.Items.Add("RIU 2 LTE DL Input = " & dlPIN - 100)
                        End If
                        If btsID.Contains("1.1.11.1.2.2") Then
                            ListBox1.Items.Add("RIU 2 Cell B DL Input = " & dlPIN - 100)
                        End If
                        If btsID.Contains("1.1.11.1.2.3") Then
                            ListBox1.Items.Add("RIU 2 AWS DL Input = " & dlPIN - 100)
                        End If
                        nextOid = kvp.Key
                    Else
                        keepGoing = False
                    End If
                Next
            Else
                Console.WriteLine("No results received.")
                keepGoing = False
            End If
        End While
End Sub
Thanks in advance for any pointers that can be offered.
Reply With Quote