SNMP Polling with C#.NET


Traditionally Simple Network Management Protocol (SNMP) polling has been achieved using the command line. There are some freeware GUI's available such as Cacti but they require a lot of complex configuration to get working and are overly complicated for the majority of simple applications. In my PhD research I just needed to poll a few values from equipment in a legacy data centre via SNMP, store them on a SQL Server database and visualize them using .NET Chart controls on a website. The code below shows how the GET method in SNMP is called using C# code.

private void buttonGet_Click(object sender, EventArgs e)
{
    //empty listbox
    listBoxAPC3.Items.Clear();

    string output;
    string strVersion = "1";
    string strCommunityString = "public";
    //read host from textbox on screen
    string strHost = textBoxHost.Text;
    //read OID from textbox on screen
    string strOID = textBoxOID.Text;
    //default timeout
    int iTimeout = 10;

    output = LaunchExecutable.Run("snmpget.exe", String.Format(" -v{0} -c {1} {2} {3}", strVersion, strCommunityString, strHost, strOID), iTimeout);

    //display results   
    listBoxAPC3.Items.Add(output);
}


The pivotal idea here is that you need to run snmpget.exe from within the .NET environment as opposed to from the command line. The LaunchExecutable class (included in the zip file) has a run method which takes:
 

  1. The name of the executable

  2. The command line arguments (SNMP version, SNMP community string, host to poll, OID to poll)

  3. The timeout value

As parameters and runs the executable as a process. This class will work for any executable which you normally run from the command line but now would prefer to include in one of your C#.NET projects.


Similar Articles