Developers guide

127
F.1.9. LinuxSnmpTrapDaemonImpl.java
/**
* $Id: LinuxSnmpTrapDaemonImpl.java,v 1.2 2002/10/22 08:23:11 oliverm Exp $
* Author: Oliver Mather
*
*/
package apt.masterswitch.snmp;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
/**
* Description:
* <p>
* This is an implementation of the SnmpTrapDaemon using the
* linux command line tool /usr/sbin/snmptrapd
* <p>
* snmptrapd must be run with root permissions. This is
* achieved by using the "sudo" linux tool.
* <p>
* The stderr of snmptrapd is captured and used to
* fireSnmpTrapReceived() events when a new trap is received.
* <p>
* For more information see "man snmptrapd"
* <p>
*
* @author $Author: oliverm $ <a href="mailto:oliverm@student.unsw.edu.au">Oliver Mather</a>
* @version $Revision: 1.2 $
* @see apt.masterswitch.snmp.SnmpTrapListener
*/
public class LinuxSnmpTrapDaemonImpl extends AbstractSnmpTrapDaemon {
private static String snmptrapdCommand = "sudo /usr/sbin/snmptrapd -P";
private volatile boolean stopRequested;
private volatile boolean running;
private BufferedReader buffer;
private Process proc;
private StdErrReader readerThread;
private final Logger log = Logger.getLogger( getClass().getName() );
public static LinuxSnmpTrapDaemonImpl instance = new LinuxSnmpTrapDaemonImpl();
private LinuxSnmpTrapDaemonImpl() {}
/**
* Start the <code>/usr/sbin/snmptrapd</code> daemon and begin listening
* to its stderr so that we can find out trap information.
* <p>
* This will trigger the {@link apt.masterswitch.snmp.SnmpTrapListener#snmpTrapDaemonStarted() }
* method to be called on registered {@link apt.masterswitch.snmp.SnmpTrapListener SnmpTrapListeners}
*/
public void start() {
if( !running ) {
try {
stopRequested = false;
Runtime runtime = Runtime.getRuntime();
proc = runtime.exec(snmptrapdCommand);
InputStream stderr = proc.getErrorStream();
InputStreamReader reader = new InputStreamReader(stderr);
// Make the buffer size=1 so that readLine() returns as soon as a
// line is written to the buffer