SnmpObsUtils.cs :  » Network-Clients » SNMP-Observer » SnmpObserver » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SNMP Observer 
SNMP Observer » SnmpObserver » SnmpObsUtils.cs
////////////////////////////////////////////////////////////////////////////////
//
// SNMP Observer V 1.0.0.0  (2005-04-01 01:01:01)
//
// Copyright (c) 2005 Bashar A. Hamdan
// Author: Bashar A. Hamdan
//
// This software is based on SNMP++.NET v. 1.14 from Marek Malowidzki
// which is based on SNMP++ from Jochen Katz, Frank Fock,
// which is in turn based on SNMP++2.6 from Hewlett Packard:
//
// Copyright (c) 2003-2004 Military Communication Institute, Zegrze, Poland
//
// Copyright (c) 2001-2003 Jochen Katz, Frank Fock
//
// Copyright (c) 1996
// Hewlett-Packard Company
//
// ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
// Permission to use, copy, modify, distribute and/or sell this software 
// and/or its documentation is hereby granted without fee. User agrees 
// to display the above copyright notice and this license notice in all 
// copies of the software and any documentation of the software. User 
// agrees to assume all liability for the use of the software; 
// Hewlett-Packard, Jochen Katz and Military Communication Institute make
// no representations about the suitability of this software for any purpose.
// It is provided  "AS-IS" without warranty of any kind, either express
// or implied. User hereby grants a royalty-free license to any and all
// derivatives based upon this software code base. 
// 
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//
// SNMP++.NET v. 1.14 (2005-02-07 11:00:00)
//
// Copyright (c) 2003-2004 Military Communication Institute, Zegrze, Poland
// Author: Marek Malowidzki
//
// This software is based on SNMP++ from Jochen Katz, Frank Fock,
// which is in turn based on SNMP++2.6 from Hewlett Packard:
//
// Copyright (c) 2001-2003 Jochen Katz, Frank Fock
//
// Copyright (c) 1996
// Hewlett-Packard Company
//
// ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
// Permission to use, copy, modify, distribute and/or sell this software 
// and/or its documentation is hereby granted without fee. User agrees 
// to display the above copyright notice and this license notice in all 
// copies of the software and any documentation of the software. User 
// agrees to assume all liability for the use of the software; 
// Hewlett-Packard, Jochen Katz and Military Communication Institute make
// no representations about the suitability of this software for any purpose.
// It is provided  "AS-IS" without warranty of any kind, either express
// or implied. User hereby grants a royalty-free license to any and all
// derivatives based upon this software code base. 
// 
////////////////////////////////////////////////////////////////////////////////
/*_############################################################################
  _## 
  _##  SNMP++v3.2.9c
  _##  -----------------------------------------------
  _##  Copyright (c) 2001-2003 Jochen Katz, Frank Fock
  _##
  _##  This software is based on SNMP++2.6 from Hewlett Packard:
  _##  
  _##    Copyright (c) 1996
  _##    Hewlett-Packard Company
  _##  
  _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  _##  Permission to use, copy, modify, distribute and/or sell this software 
  _##  and/or its documentation is hereby granted without fee. User agrees 
  _##  to display the above copyright notice and this license notice in all 
  _##  copies of the software and any documentation of the software. User 
  _##  agrees to assume all liability for the use of the software; 
  _##  Hewlett-Packard and Jochen Katz make no representations about the 
  _##  suitability of this software for any purpose. It is provided 
  _##  "AS-IS" without warranty of any kind, either express or implied. User 
  _##  hereby grants a royalty-free license to any and all derivatives based
  _##  upon this software code base. 
  _##  
  _##########################################################################*/
/*===================================================================

  Copyright (c) 1999
  Hewlett-Packard Company

  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  Permission to use, copy, modify, distribute and/or sell this software
  and/or its documentation is hereby granted without fee. User agrees
  to display the above copyright notice and this license notice in all
  copies of the software and any documentation of the software. User
  agrees to assume all liability for the use of the software; Hewlett-Packard
  makes no representations about the suitability of this software for any
  purpose. It is provided "AS-IS without warranty of any kind,either express
  or implied. User hereby grants a royalty-free license to any and all
  derivatives based upon this software code base.
=====================================================================*/


using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Globalization;
using Org.Snmp.Snmp_pp;

namespace SnmpObserver{
  /// <summary>
  /// Summary description for SnmpObsUtils.
  /// </summary>
  public class SnmpObsUtils
  {
    private readonly Hashtable SYSOID2NAME_;

    public SnmpObsUtils()
    {
      #region from example
#if  REQUIRES_CRT_INIT
      Org.Snmp.Snmp_pp.CRT.Auto.Initialize();
#endif
      SYSOID2NAME_ = new Hashtable();
      SYSOID2NAME_.Add("1.3.6.1.2.1.1",     "system");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.1.0", "sysDescr");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.2.0", "sysObjectID");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.3.0", "sysUpTime");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.4.0", "sysContact");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.5.0", "sysName");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.6.0", "sysLocation");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.7.0", "sysServices");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.8.0", "sysORLastChange");
      SYSOID2NAME_.Add("1.3.6.1.2.1.1.9.0", "sysORTable");

      foreach (object key in ((Hashtable) SYSOID2NAME_.Clone()).Keys)
      {
        object name = SYSOID2NAME_[key];
        Oid oid = new Oid(key.ToString());
        SYSOID2NAME_.Add(oid, name);
        SYSOID2NAME_.Add(name, oid);
      }
      #endregion

    }
    public DataSet LoadData(string ipAddress) 
    {
      MyConfig objCfg = new MyConfig();

      DataRow dr;

      DataSet ds = new DataSet();
      DataTable dt = ds.Tables.Add("Requests");
      dt.Columns.Add("ObjectId");
      dt.Columns.Add("Name");
      dt.Columns.Add("Description");
      dt.Columns.Add("Contact");
      dt.Columns.Add("Location");
      dt.Columns.Add("Uptime");
      dt.Columns.Add("Ip");
      dr = dt.NewRow();
      dr["ip"] = ipAddress;

      SnmpTarget.DefaultTimeout = objCfg.SnmpTimeout * 100;
      SnmpTarget.DefaultRetries = objCfg.SnmpRetries;
      try 
      {
        using (Snmp snmp = new Snmp(false))
        {
          #region from example

          UdpAddress udp = new UdpAddress(ipAddress);
          SnmpVersion ver = SnmpVersion.SNMPv1;
          CTarget target = new CTarget(udp, ver, objCfg.Community, objCfg.Community);
          Oid systemOid = (Oid) SYSOID2NAME_["system"];
          Vb vb = new Vb(systemOid);
          Pdu pdu = new Pdu(PduType.GetNext, vb);
          #endregion
          while (true)
          {
            #region from example
            Pdu resp = snmp.Invoke(pdu, target);
            vb = resp[0];
            Oid oid = vb.Oid;
            if (!oid.StartsWith(systemOid))
            {
              break;
            }
            #endregion

            SnmpSyntax val = vb.Value;
            switch(SYSOID2NAME_[oid].ToString()) 
            {
              case "sysDescr":
                dr["Description"] = val;
                break;
              case "sysObjectID":
                dr["ObjectId"] = val;
                break;
              case "":
                break;
              case "sysName":
                dr["Name"] = val;
                break;
              case "sysContact":
                dr["Contact"] = val;
                break;
              case "sysLocation":
                dr["Location"] = val;
                break;
              case "sysUpTime":
                dr["Uptime"] = val;
                break;
              default:
                break;
            }

            pdu = pdu.Clone(vb);
          }
          dt.Rows.Add(dr);
        }
        return ds;
      } 
      catch(Exception ex) 
      {
        dr["Description"] = ex.Message;
        dt.Rows.Add(dr);
        return ds;
      }
      finally 
      {
        // close session
        // stop api thread
      }
    }

    public DataSet LoadData(string ipAddressFrom, string ipAddressTo) 
    {
      string strIp = ipAddressFrom;
      string strIpPref = "";
      int intIpF = 0;
      int intIpT = 0;
      double d = 0.0;

      Array arrX = ipAddressFrom.Split('.');

      if (arrX.Length == 4) 
      {
        if (double.TryParse(arrX.GetValue(3).ToString(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d)) 
        {
          intIpF = int.Parse(arrX.GetValue(3).ToString());
        }
        strIpPref = string.Format("{0}.{1}.{2}.", arrX.GetValue(0), arrX.GetValue(1), arrX.GetValue(2));
      }

      if (double.TryParse(ipAddressTo, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d)) 
      {
        intIpT = int.Parse(ipAddressTo);
      }

      DataSet ds = new DataSet("Requests");
      DataTable dt = ds.Tables.Add("Requests");
      dt.Columns.Add("ObjectId");
      dt.Columns.Add("Name");
      dt.Columns.Add("Description");
      dt.Columns.Add("Contact");
      dt.Columns.Add("Location");
      dt.Columns.Add("Uptime");
      dt.Columns.Add("Ip");
      
      for (int i=intIpF; i<=intIpT; i++)
      {
        // Load Ip information
        DataSet dsIp = LoadData(strIpPref + i.ToString());
        DataRow drIp = dsIp.Tables[0].Rows[0];
      
        // Add to the return dataset
        DataRow dr = dt.NewRow();

        dr["ObjectId"] = drIp["ObjectId"];
        dr["Name"] = drIp["Name"];
        dr["Description"] = drIp["Description"];
        dr["Contact"] = drIp["Contact"];
        dr["Location"] = drIp["Location"];
        dr["Uptime"] = drIp["Uptime"];
        dr["Ip"] = drIp["Ip"];

        dt.Rows.Add(dr);
      }
      return ds;
    }

    private bool isIpLessEqual(string Ip1, string Ip2) 
    {
      Array arrIp1 = Ip1.Split('.');
      Array arrIp2 = Ip2.Split('.');
      return true;

    }

    public DataRow GetIpData(string ipAddressFrom, string ipAddressTo) 
    {
      DataRow dr;

      DataSet ds = new DataSet();
      DataTable dt = ds.Tables.Add("Requests");
      dt.Columns.Add("ObjectId");
      dt.Columns.Add("Name");
      dt.Columns.Add("Description");
      dt.Columns.Add("Contact");
      dt.Columns.Add("Location");
      dt.Columns.Add("Uptime");
      dt.Columns.Add("Ip");
      dr = dt.NewRow();
      dr["ip"] = ipAddressFrom;

      SnmpTarget.DefaultTimeout = 5000;
      SnmpTarget.DefaultRetries = 2;
      try 
      {
        using (Snmp snmp = new Snmp(false))
        {
          UdpAddress udp = new UdpAddress(ipAddressFrom);
          SnmpVersion ver = SnmpVersion.SNMPv1;
          CTarget target = new CTarget(udp, ver, "public", "public");
          Oid systemOid = (Oid) SYSOID2NAME_["system"];
          Vb vb = new Vb(systemOid);
          Pdu pdu = new Pdu(PduType.GetNext, vb);
          while (true)
          {
            Pdu resp = snmp.Invoke(pdu, target);
            vb = resp[0];
            Oid oid = vb.Oid;
            if (!oid.StartsWith(systemOid))
            {
              break;
            }

            SnmpSyntax val = vb.Value;
            //            Console.WriteLine("{0}({1}): {2} ({3})",
            //              oid, SYSOID2NAME_[oid], val, val.SmiSyntax);
            switch(SYSOID2NAME_[oid].ToString()) 
            {
              case "sysDescr":
                dr["Description"] = val;
                break;
              case "sysObjectID":
                dr["ObjectId"] = val;
                break;
              case "":
                break;
              case "sysName":
                dr["Name"] = val;
                break;
              case "sysContact":
                dr["Contact"] = val;
                break;
              case "sysLocation":
                dr["Location"] = val;
                break;
              case "sysUpTime":
                dr["Uptime"] = val;
                break;
              default:
                break;
            }

            pdu = pdu.Clone(vb);
          }
        }
        return dr;
      } 
      catch(Exception ex) 
      {
        dr["Description"] = ex.Message;
        return dr;
      }
      finally 
      {
        // close session
        // stop api thread
      }
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.