org.opennms.bb.dp.poller.plugins
Class ServiceMonitorFTP

java.lang.Object
  |
  +--org.opennms.bb.dp.poller.plugins.ServiceMonitorFTP
All Implemented Interfaces:
ServiceMonitor

public class ServiceMonitorFTP
extends Object
implements ServiceMonitor

This class is designed to be used by the service poller framework to test the availability of the FTP service on remote interfaces. The class implements the ServiceMonitor interface that allows it to be used along with other plug-ins by the service poller framework.

Version:
CVS $Revision: 1.5 $
Author:
OpenNMS

Field Summary
private static int DEFAULT_PORT
          Default FTP port.
private static int DEFAULT_RETRY
          Default retries.
private static int DEFAULT_TIMEOUT
          Default timeout.
private static String SERVICE_NAME
          Name of monitored service.
 
Fields inherited from interface org.opennms.bb.dp.poller.plugins.ServiceMonitor
SERVICE_AVAILABLE, SERVICE_STATUS_MASK, SERVICE_UNAVAILABLE, SURPRESS_EVENT_MASK
 
Constructor Summary
ServiceMonitorFTP()
           
 
Method Summary
private static long convertTimeToLong(String valueToConvert)
          Converts the passed time string to a time value that is measured in milliseconds.
private  String getResponse(InputStream istream)
          Reads a one or more response lines from the input stream.
private  void getUntilEOF(InputStream istream)
          Reads from the input stream until EOF.
 void initialize(ConfigurationProxy proxy, Properties parameters)
          Initialize the service monitor.
 void initialize(NetworkInterface iface)
          Called by the poller framework when an interface is being added to the scheduler.
 int poll(NetworkInterface iface, EventProxy eproxy, Properties parameters)
          Poll the specified address for FTP service availability
 void release()
          Called by the poller framework when the plug-in is being unloaded.
 void release(NetworkInterface iface)
          Called by the poller framework when an interface is being removed from the scheduler.
 String serviceName()
          Returns the name of the service that the plug-in monitors ("FTP").
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

SERVICE_NAME

private static final String SERVICE_NAME
Name of monitored service.

DEFAULT_PORT

private static final int DEFAULT_PORT
Default FTP port.

DEFAULT_RETRY

private static final int DEFAULT_RETRY
Default retries.

DEFAULT_TIMEOUT

private static final int DEFAULT_TIMEOUT
Default timeout. Specifies how long (in milliseconds) to block waiting for data from the monitored interface.
Constructor Detail

ServiceMonitorFTP

public ServiceMonitorFTP()
Method Detail

serviceName

public String serviceName()

Returns the name of the service that the plug-in monitors ("FTP").

Specified by:
serviceName in interface ServiceMonitor
Returns:
The service that the plug-in monitors.

initialize

public void initialize(ConfigurationProxy proxy,
                       Properties parameters)
                throws ServiceMonitorException

Initialize the service monitor.

Specified by:
initialize in interface ServiceMonitor
Parameters:
proxy - The object that can be used to load/save config information.
parameter - The parameters from the appropiate package(s).
Throws:
ServiceMonitorException - Thrown if an unrecoverable error occurs that prevents the plug-in from functioning.

release

public void release()
             throws ServiceMonitorException

Called by the poller framework when the plug-in is being unloaded. Any resources still being used by the monitor are released.

Specified by:
release in interface ServiceMonitor
Throws:
ServiceMonitorException - Thrown if an error occurs during deallocation.

initialize

public void initialize(NetworkInterface iface)
                throws ServiceMonitorException

Called by the poller framework when an interface is being added to the scheduler. Here we perform any necessary initialization to prepare the NetworkInterface object for polling.

Specified by:
initialize in interface ServiceMonitor
Parameters:
iface - The network interface to be added to the scheduler.
Throws:
ServiceMonitorException - Thrown if an unrecoverable error occurs that prevents the interface from being monitored.

release

public void release(NetworkInterface iface)
             throws ServiceMonitorException

Called by the poller framework when an interface is being removed from the scheduler. Here we free any resources or save any persistent information associated with the interface.

If an exception is thrown during the release the exception will be logged, but the interface will still be discarded for garbage collection.

Specified by:
release in interface ServiceMonitor
Parameters:
iface - The network interface that was being monitored.
Throws:
ServiceMonitorException - Thrown if an unrecoverable error occurs for the interface.

poll

public int poll(NetworkInterface iface,
                EventProxy eproxy,
                Properties parameters)
         throws ServiceMonitorException

Poll the specified address for FTP service availability

During the poll an attempt is made to connect on the specified port (by default TCP port 21). If the connection request is successful, the banner line generated by the interface is parsed and if the extracted return code indicates that we are talking to an FTP server we continue. Next, an FTP 'QUIT' command is sent. Provided that the interface's response is valid we set the service status to SERVICE_AVAILABLE and return.

Specified by:
poll in interface ServiceMonitor
Parameters:
iface - The network interface to test the service on.
eproxy - The event proxy object for sending custom events.
parameters - The package parameters (timeout, retry, etc...) to be used for this poll.
Returns:
The availibility of the interface and if a transition event should be supressed.

getUntilEOF

private void getUntilEOF(InputStream istream)
                  throws IOException
Reads from the input stream until EOF.

getResponse

private String getResponse(InputStream istream)
                    throws IOException

Reads a one or more response lines from the input stream. Each line is terminated by the sequence '\r\n'. According to RFC 959 (FTP) an FTP reply may be a single-line reply or a multi-line reply. Single line replies look like the following: <3-digit code> Text\r\n Multi-line responses, however, have the following format: <3-digit code>-Text\r\n More Text\r\n <3-digit code> Text\r\n In the case of a single-line response the entire response is returned. In the case of a multi-line response, data is read from the input stream until the final reply line is read. Only the last line is returned. The first and any intermediate reply lines are discarded. For more information on the format of the line see the FTP RFC.

If the end of file condition is reached before the cariage-return line-feed sequence then the received buffer is returned, or a string of zero length.

NOTE: The \r\n sequence is stripped from the returned line.

Parameters:
istream - The input stream from which characters are to be read.
Returns:
A line of characters read off the input stream.
Throws:
java.lang.IOException - Thrown if an error occurs reading the data from the stream.

convertTimeToLong

private static long convertTimeToLong(String valueToConvert)
                               throws NumberFormatException

Converts the passed time string to a time value that is measured in milliseconds. The following extension are considered when converting the string:

ExtensionConversion Value
usMicroseconds
msMilliseconds
sSeconds
mMinutes
hHours
dDays

A number entered with out any units is considered to be in milliseconds.

Parameters:
valueToConvert - The string to convert to milliseconds.
Returns:
Returns the string converted to a millisecond value.
Throws:
NumberFormatException - Thrown if the string is malformed and a number cannot be extracted from the value.