Use Net SNMP under IPython to manage the tutorial for the UNIX system class

  • 2020-05-10 18:22:13
  • OfStack

The introduction

For the simple network management protocol (SNMP), most system administrators have some experience with it, or at least have heard of it. If you are working in a data center, you probably interact with SNMP in some way every day. There are many impressive network management systems (NMS) or network monitoring systems of the same size that use SNMP monitoring, but this article does not attempt to cover these systems. This article deals primarily with the use of Python? Language to study SNMP and write the relevant code yourself.

A friend recently told me that sometimes it's like walking down the street to grandma's house without taking the Saturn V rocket 1. There are many tasks, and if you use or configure a large NMS, like an Saturn V rocket, you will get better service if you try 1 Python before filling the liquid oxygen tank. Knowing how to write flexible Python code to interact with SNMP is probably one of the most interesting and efficient skills a system administrator can acquire. Although the setup and use of SNMP is very complex, what is discussed in this article will make it very interesting.
Install and configure Net-SNMP

To learn from this article, you need to install the latest version of Python (Python 2.3 or later) on your *nix computer. At the time of this writing, Python 2.5.1 is the latest version of Python. You also need IPython to interactively use the Net-SNMP library with Python bindings. The Net-SNMP team has conducted detailed tests of support in a variety of operating systems, including AIX & # 63; , HP UX & # 63; , GNU/Linux & # 63; Distributions (e.g. Red Hat), Windows? And even OS X. .

Installing IPython is a very simple task. A good option is to use Easy Install to manage the Python package. You can easily install any Python package by running the ez_setup.py script. For example, you only need to type the following command:


easy_install ipython

Other options include using your favorite package management system, or simply downloading IPython and typing the following command:


python setup.py install

Note that mainline (trunk) refers to the root path in the version control system that holds the most recent copy of the code. In addition, mainlines often represent a subversion and version control system. For more details, see the links to child versions in the resources section.

To learn from this article, you need to make sure that your client computer, or the computer running all of the code, has NET-SNMP Version 5.4.x or later installed, because this source code version starts with the Python binding. In most cases, the binding installation requires compiling the source files; However, you can also use Red Hat Package Managers (RPM). If you're interested and have the time, check out the latest version of it from the Net - SNMP Web site.

There are many compilation options available, but the main task is to compile NET-SNMP correctly and then run the standalone Python installer in the Python directory. Another thing to note is that when you compile and run./configure, it will run the configuration script for the local machine (the machine on which the agent is being compiled). You should not use this configuration script; for the purposes of this article, you only need to create a simple configuration script.

On/etc snmp/snmpd conf configuration file that is stored in the backup, and build this very basic configuration:


syslocation "My Local Machine"
syscontact me@localhost.com
rocommunity public

Save it and restart the snmpd daemon. In the most * nix system, you can use/etc/init d/snmpd restart to finish the work.

It is best not to compile an active development version unless you have to, because doing so will require you to fix the faulty code yourself. In CentOS 5 or Red Hat Enterprise Linux 5 (RHEL 5), you can download the latest, stable source file RPM (version 5.4.1 at the time of this writing). Note that you also need to use the Python source file to build the binding. So, if you are using an Red Hat based computer, make sure you have the python-dev (or its equivalent) and Python Header files installed for the specific *nix operating system. If you need any help building RPM from source files, please refer to the official Red Hat documentation. Building packages from source files can be a very complex topic and is beyond the scope of this article. If you have trouble using Python bindings, you should ask for help on the Net-SNMP mailing list.
Analyze the code

What are you waiting for? Assume that you have installed the Python binding and IPython. Now you are ready to use IPython and start working on it. At some point, though, you may need to browse through the IPython documentation. Jeff Rush is the current Python Advocacy Coordinator, which provides some very good on-screen video content for IPython. Ok, so let's start coding.

Let's do a simple query to identify a computer by using its object identifier (OID) value, sysDescr. Start IPython by typing ipython, and then execute the interactive session:
Listing 1. IPython example

             


 In [1]: import netsnmp

    In [2]: oid = netsnmp.Varbind('sysDescr')

    In [3]: result = netsnmp.snmpwalk(oid,
    ...:             Version = 2,
    ...:             DestHost="localhost",
    ...:             Community="public")

    In [4]: result = netsnmp.snmpwalk(oid,
                Version = 2,
                DestHost="localhost",
                Community="public")

    In [16]: result
    Out[16]: ('Linux localhost 2.6.18-8.1.14.el5 #1 SMP Thu Sep 27 
    18:58:54 EDT 2007 i686',)

Note that the result value you get is different from the result value shown here. If you have followed the configuration shown in listing 1 above, everything else should be available. If you're familiar with SNMP, you might be able to see what this actually does right away.

One of the benefits of using IPython to test the SNMP code snippet is that it ACTS like a normal Shell, and many of the basic, interactive Shell concepts "all work," but it should be noted that they work in the python way. Writing SNMP code can be a tedious activity, but using the Net-SNMP library through IPython makes it a lot of fun.

As you can see, the task of converting the results to the Python data type is very simple. This is why IPython and Net-SNMP work well in 1. Now, for writing custom scripts, you only need to interactively analyze the combination of OID to query. Ideally, you would need to run a large-scale, easy-to-configure NMS setup script to automatically integrate the new computer into the network.

Of course, there is no such thing as an ideal situation, and you need to know how to combine some good SNMP code into one, which is very useful for a system administrator. Here is an example. Suppose you just converted a high-speed DDR to a 2 TB RAID 0 server that is running Ubuntu Linux. You have to do this because you have to solve the problem in an hour.

Now you're in big trouble, and you only have a few minutes to monitor specific questions to see if you need to start sending resumes, or if you should start preparing speeches and asking for promotions. Let's use the editing functionality in IPython to write the script, save it to a file, and then run it in one session without leaving IPython: ed snmpinput.py
Listing 2. Creation of the IPython module


import netsnmp

class snmpSessionBaseClass(object):
  """A Base Class For a SNMP Session"""
  def __init__(self,
        oid="sysDescr",
        Version=2,
        DestHost="localhost",
        Community="public"):

    self.oid = oid
    self.Version = Version
    self.DestHost = DestHost
    self.Community = Community


  def query(self):
    """Creates SNMP query session"""
    try:
      result = netsnmp.snmpwalk(self.oid,
                  Version = self.Version,
                  DestHost = self.DestHost,
                  Community = self.Community)
    except:
      import sys
      print sys.exc_info()
      result = None

    return result

Customize IPython to use the correct editor

You can customize Ipython by editing $HOME/.ipython /ipythonrc. One of the first things you need to customize is the %edit command, which you can call by typing ed. By default, it is set to use vi, but you can change it to use other editors, including Emacs. This article provides guidance on how to change your environment. You can also use the following command to launch Ipython to hard-code the use of a particular editor: ipython-editor =vim.

Go ahead and cut and paste the following code into the file you just created. When you save this file, IPython will automatically run it and place the class in the corresponding module in your environment. If you type who, you will see something similar to this:


In [2]: who
netsnmp snmpSessionBaseClass

This is powerful because you can get all the benefits of using your favorite text editor (perhaps Vim or Emacs) and then immediately use the code in an interactive IPython Shell session. Note that if you have already written a module, you can also simply type and run it to get the same result. Execute and run the module in IPython, which is equivalent to running the code in it and putting it into the IPython environment.
Code in an iterative fashion

Now, by using IPython, you can combine the best features of Python Shell, UNIX Shell, and your favorite text editor into one. You need all the help you can get when interacting with very complex objects like the SNMP library, and in this example, you really show the power of IPython.

You can write modules dynamically, and you can test and use them later. IPython works well with any programming style, including test-driven development (TDD) or test-enhanced development (TED). To demonstrate this convenience, let's go to the module you just wrote.

Now that you have an object-oriented SNMP interface, you can start asking your local computer:
Listing 3. IPython iterative encoding


In [1]: run snmpinput

In [2]: who
netsnmp snmpSessionBaseClass  

In [3]: s = snmpSessionBaseClass()

In [4]: s.query()
Out[4]: ('Linux localhost 2.6.18-8.1.14.el5 #1 SMP Thu Sep 27 18:58:54 EDT 2007 i686',)

In [5]: result = s.query()

In [6]: len(result)
Out[6]: 1

It's very easy to get the relevant results using this module, but you're basically just running a hard-coded script, so you need to change the value of the OID object to traverse the system subtree:
Listing 4. Changing the value of the OID object


In [7]: s.oid
Out[7]: 'sysDescr'

In [8]: s.oid = ".1.3.6.1.2.1.1"

In [9]: result = s.query()

In [10]: print result
('Linux localhost 2.6.18-8.1.14.el5 #1 SMP Thu Sep 27 18:58:54 EDT 2007 i686', 
      '.1.3.6.1.4.1.8072.3.2.10', '121219', 'me@localhost.com', 
      'localhost', '"My Local 
Machine"', '0', '.1.3.6.1.6.3.10.3.1.1', '.1.3.6.1.6.3.11.3.1.1', 
'.1.3.6.1.6.3.15.2.1.1', 
'.1.3.6.1.6.3.1', '.1.3.6.1.2.1.49', '.1.3.6.1.2.1.4', '.1.3.6.1.2.1.50',
 '.1.3.6.1.6.3.16.2.2.1', 
'The SNMP Management Architecture MIB.', 'The MIB for Message Processing and
 Dispatching.', 'The management information definitions for the SNMP 
User-based Security Model.', 'The MIB module for SNMPv2 entities', 'The 
MIB module for managing TCP implementations', 'The MIB module for
managing IP and ICMP implementations', 'The MIB module for managing 
UDP implementations', 'View-based Access 
Control Model for SNMP.', '0', '0', '0', '0', '0', '0', '0', '0')

As you can see, the task of using this module and starting to analyze the entire network (one computer at a time) is very easy. Analyze carefully and determine what needs to be queried on the network. This is another interesting feature of IPython that deserves further study. IPython has an incredible feature that allows you to run Python snippets as background processes. Fortunately, this is very easy to do. Let's run the same query again, but this time as a background process (see listing 5).
Listing 5. IPython example of iterative coding -- background process


In [11]: bg s.query()
Starting job # 0 in a separate thread.

In [12]: jobs[0].status
Out[12]: 'Completed'

In [16]: jobs[0].result
Out[16]: 
('Linux localhost 2.6.18-8.1.14.el5 #1 SMP Thu Sep 27 18:58:54 EDT 2007 i686',
 '.1.3.6.1.4.1.8072.3.2.10', '121219', 'me@localhost.com', 'localhost', '"My Local
 Machine"', '0', '.1.3.6.1.6.3.10.3.1.1', '.1.3.6.1.6.3.11.3.1.1', 
'.1.3.6.1.6.3.15.2.1.1', '.1.3.6.1.6.3.1', '.1.3.6.1.2.1.49', 
'.1.3.6.1.2.1.4', '.1.3.6.1.2.1.50', '.1.3.6.1.6.3.16.2.2.1',
 'The SNMP Management Architecture MIB.', 'The MIB for Message Processing and
 Dispatching.', 'The management information definitions for the SNMP User-based
 Security Model.', 'The MIB module for SNMPv2 entities', 'The MIB module for
 managing TCP implementations', 'The MIB module for managing IP and ICMP
 implementations', 'The MIB module for managing UDP implementations',
 'View-based Access Control Model for SNMP.', '0', '0', '0', '0', '0', '0', '0', '0')

The exciting thing is that background threads in IPython are a very valuable feature, but it only works with libraries that support asynchronous threads. Unfortunately, the Net-SNMP is synchronous. If you are interested in this 1 point, you can test it by changing the s.oid value to.iso. Before the query completes, you should notice that the IPython interpreter is "blocked" or "suspended." Although it's only a warning, the SNMP traversal of the entire.iso tree can take a long time, so you might accept my point.

Of course, there is another solution. You can use one of the many processing libraries provided by Python to spawn a new process for this blocked process. Python Cheese Shop provides some third party libraries. If you are using easy_install, it is fairly simple to install a package (such as Parallel Python) and test the library using Net-SNMP, so it is up to you.


easy_install http://www.parallelpython.com/downloads/pp/pp-1.5.zip

The last feature to show here is running unit tests in IPython Shell. It is also very easy to run unit tests frequently when making changes to a module. You need to add a tag to run run-e, so that by 1, you can avoid backtracking to the unit test module in IPython Shell. You can download this unit test in the source file that accompanies this article.

Note that IPython 0.8.2 also has a new document testing feature that allows you to generate document tests in IPython. Document testing is a great feature of Python because, along with other features 1, it provides a way to create testable documents for API. Here is an example of how to run doctest for our module in IPython:
Listing 6. Running IPython in doctest mode


python setup.py install

0

Because doctest mode will execute Python statements without analysis, you must be careful not to use values that may change in doctest, such as those given above. If you have pasted several lines of code into the docstring of the module, you can test your API document by using the following methods:


python setup.py install

1

conclusion

In this article, you've learned that the collaborative use of Net-SNMP and IPython can be a powerful combination. This article introduces the following key concepts:

      Python binding: Net-SNMP now provides Python binding, which allows us to take full advantage of Python's capabilities to handle the SNMP protocol.       processing library: currently, the Python binding is synchronous, but the processing library can be used to derive a new process for each request to solve this problem.       flexible technology: for system administrators and software engineers, IPython is a very mature and powerful tool. Although this article has only briefly covered a few flexible techniques, such as document testing and unit testing, you can apply these techniques to any test-centric development or to writing and analyzing code interactively.       SNMP and IPython: this article provides only a brief overview of what SNMP and IPython can achieve when used alone or together.

SNMP is so complex that it's almost impossible to imagine writing any meaningful code, but we hope that the techniques presented in this article will inspire some new ideas. If you're curious to see how far Python's implementation of SNMP really goes, you can explore Zenoss 1, download a virtual machine, and test it. There is also an API that you can use to write scripts, so you can combine what you've learned here with the full Python NMS. Of course, it also applies to any other NMS.


Related articles: