Chapter 14. Java and SNMP
第十四章 Java和SNMP
So far, we have shown how to use Perl scripts to perform SNMP tasks. In this chapter, we will show how to use Java to create SNMP applications. Java is not widely used in system and network administration circles, but there are those who have made the leap from scripting language to object-oriented language. While Java is an object-oriented language, you don't have to be an object guru to use Java.
到目前为止,我们已经演示了如何使用Perl脚本执行SNMP任务。本章中,我们将演示如何使用Java来创建SNMP应用程序。Java在系统和网络管理领域应用的并不广泛,但有一些人完成了从脚本语言到面向对象语言的飞跃。尽管Java是一个面向对象语言,但你不必成为一个对象大牛后才能使用Java。
Java has similar advantages to Perl. It's platform independent with built-in support for network sockets and threading. One advantage that Java has over Perl is that it can outperform Perl for certain types of tasks. Perl is commonly regarded as being well suited for processing text since its regular expression handling is very good. Java also has regular expression support and can generally outperform Perl in this regard. Java also has the advantage of the HotSpot compiler. HotSpot allows for a long-running Java program to be self-optimized over time. This is something that traditional compiled languages like C and C++ do not have and is also not found in Perl. Another advantage to Java is that creating multithreaded applications is very easy.
Java有着与Perl类似的优点。它具有平台无关性,并内置对网络套结字和线程的支持。Java比Perl的一个优点是在某些类型的任务中,Java要比Perl性能高。人们广泛认为Perl适于文本处理是由于它的正则表达式处理很好。Java也有正则表达式,而且在这方面往往比Perl性能高。Java还有HotSpot编译器的优势。HotSpot允许长时间运行的Java程序随着时间进行自我优化。这是传统的编译语言如C和C++等所没有的,并且在Perl中也没有发现。Java的另一个优点是创建多线程应用程序非常容易。
14.1. SNMP4J
14.1 SNMP4J
The SNMP API presented in this chapter is SNMP4J. The current version is 1.5 and it works with Jav a 1.4.1 or later. It is released for free under the Apache software license. You can obtain a copy of SNMP4J from http://www.snmp4j.org. The library's design is patterned after the successful SNMP++ C++ library whose early versions were developed by HP.[*] Because of its roots, SNMP4J has a clean and easy-to-use API. SNMP4J's features include:
本章中展示的SNMP API是SNMP4J。当前版本是1.5,可以运行在Java 1.4及以上版本。发布遵循Apache软件许可。你可以从http://www.snmp4j.org 得到。这个库的设计源自HP所开发的成功的SNMP++ C++库。由于它的根源,SNMP4J拥有一个整洁应用的API。SNMP4J的特征包括:
[*] HP no longer develops or supports SNMP++. Frank Fock and Jochen Katz have taken SNMP++ and added SNMPv3 support. See http://www.agentpp.com for details.
SNMPv3 with MD5 and SHA authentication and DES and AES 128, AES 192, and AES 256 privacy.
Pluggable Message Processing Models with implementations for MPv1, MPv 2c , and MPv3.
All PDU types.
Pluggable transport mappings. UDP and TCP are supported out of the box.
Pluggable timeout model.
Synchronous and asynchronous requests.
Command generator as well as command responder support.
Jav a 1.4.1 or later.
Logging based on Log4J but supports other logging APIs like Java 1.4 Logging.
Row-based efficient asynchronous table retrieval with GETBULK.
Multithreading support.
JUnit tests (coming in release 2.0).
支持MD5和SHA认证,以及DES,AES 128,AES 192和AES 256的SNMPv3。
可插拔的实现了MPv1,MPv 2c 和MPv3的消息处理模型。
所有的PDU类型。
可插拔的传输映射。支持UDP和TCP。
可插拔的timeout模型。
同步和异步请求。
支持命令生成器和命令响应器。
Java 1.4及以上版本。
基于Log4J的日志并支持其他的日志APIs如Java 1.4 Logging。
通过GETBULK实现基于行的高效的异步表检索。
多线程支持。
JUnit测试(始自发布2.0)
SNMP4J has a built-in thread pool model. This means that you can specify the number of threads that respond to and process incoming requests, making your SNMP applications highly efficient.
SNMP有一个内置的线程池模型。这意味着你能够指明响应和处理请求的线程数目,这将使得你的SNMP应用程序十分高效。
At this writing, the maintainers of SNMP4J have released an SNMP agent API based on SNMP4J. It is currently in the early alpha stage, so we will not discuss it in this chapter. But for those of you who are interested, keep an eye on the SNMP4J web site to track its development.
写此书时,SNMP4J的维护者已经发布了基于SNMP4J的SNMP代理API。目前处于早期alpha测试阶段,因此,我们在本章中不会讨论。但是对于感兴趣的读者,请关注SNMP4J的网站来跟踪他的开发。
A detailed discussion of SNMP4J's API is beyond the scope of this chapter. Instead, we present examples of how to use the library to perform various SNMP operations. You will see that creating SNMP applications is quite easy and requires minimal programming. As a result, you don't have to think as much about using the API and can focus more on creating useful applications that solve whatever problems you face.
详细的SNMP4J API的讨论超出了本章的范围。然而,我们将展示如何使用这个库执行各种SNMP操作的例子。你将看到创建SNMP应用程序十分简单,需要很少编程。因此,你不必过多地考虑如何使用API,将精力更多地放在创建解决你所面对的问题的有用的应用上。
Example source code is provided on this book's web site, at http://www.oreilly.com/catalog/esnmp2/ . The example sources were built using a command-line tool that is available as a separate download from the SNMP4J web site. Each section in this chapter discusses pertinent aspects of the SNMP operation it is implementing as it pertains to SNMP4J.
本书网站提供例子源代码,在http://www.oreilly.com/catalog/esnmp2/ 。例子源代码使用命令行工具构建,该工具可以在SNMP4J站点的独立下载找到。本章每一节讨论所实现的SNMP操作的某一方面。