|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectradtools.services.linux.NagiosStatsRequest
public class NagiosStatsRequest
This class is used to gather statistics from a running instance of Nagios. It reads the information from the Nagios status file, negating the need to reconfigure or even restart the Nagios installation. Though there is a lot of information available, the most useful data comes in the form of plugin output. If you're only interested in this data, pay attention to the methods getUsefulDataNames() and getUsefulDataValue(String).
When creating a new instance of a StatsRequest, you pass in the name of the Nagios status file (commonly found in either /usr/local/nagios/var/log/ or /var/log/nagios/, and commonly named either status.dat or status.log). You also pass in the refreshRate, or the number of milliseconds to wait between refreshing the stored status values. If you pass in a number <= 0, the statistics will only be refreshed by a call to refreshStats().
In order to add another useful status value to the list, you have to make the following additions:
- in getUsefulDataNames(), add the desired statistic name to the array - in getUsefulDataValue(String), add the created statistic name as a new condition, with the necessary code to extract the important piece of information as a double - in pluginRadToNagios(String), add a new condition to transform the created statistic name to the one that is used in the Nagios status file under service_description
This code assumes that Nagios v2.6 is being used. No guarantees are made about compatibility with other versions. All plugins are assumed to be running with host_name=localhost.
| Nested Class Summary | |
|---|---|
private static class |
NagiosStatsRequest.State
Used to keep track of which portion of the status file is being read during a call to refreshStats(). |
| Field Summary | |
|---|---|
private String |
END_BLOCK_1
Represents the end of a block in the status file. |
private String |
END_BLOCK_2
Represents an alternative version of the the end of a block in the status file. |
private String |
HOST
Represents the statistic that holds the value of the particular host's name in the status file. |
private String |
hostName
Stores the current host's name when inside of a Host or Service block in a call to refreshStats(). |
private long |
lastRefreshed
The last time that the statistic values were refreshed, as given by System.currentTimeMillis(). |
private char[] |
line_buffer
The buffer used to store characters when reading lines from the Nagios status file. |
private String |
NAME_SEPARATOR
The set of characters used to separate host, service, and statistic names when displaying the names of available statistics in the status file. |
private String |
SERV
Represents the statistic that holds the value of the particular service's name in the status file. |
private String |
servName
Stores the current service's name when inside of a Service block in a call to refreshStats(). |
private String |
START_HOST
Represents the beginning of a Host block in the status file. |
private String |
START_INFO
Represents the beginning of an Info block in the status file. |
private String |
START_PROG
Represents the beginning of a Program block in the status file. |
private String |
START_SERV
Represents the beginning of a Service block in the status file. |
private Hashtable<String,String> |
statsBase
Stores all statistics not related to a particular host or service; namely, statistics inside the Info or Program object blocks. |
private Hashtable<String,Hashtable<String,String>> |
statsHost
Stores statistics that are related to a particular host as given by host_name (so within a Host block). |
private Hashtable<String,Hashtable<String,Hashtable<String,String>>> |
statsServ
Stores statistics that are related to a particular service as given by service_description (so within a Service block) that is related to a particular host as given by host_name. |
| Constructor Summary | |
|---|---|
NagiosStatsRequest()
|
|
| Method Summary | |
|---|---|
String |
getFullPluginOutput(String service)
Get the plugin output from the specified service. |
Collection<String> |
getListHost()
Return a Collection containing the name of all hosts present in the Nagios status file. |
Collection<String> |
getListServ()
Return a Collection containing the name of all services present in the Nagios status file. |
String |
getSpecificStat(String host,
String service,
String stat)
Get the value of a specific statistic from the stored Nagios statistics. |
Collection<String> |
getStatListBase()
Return a Collection containing the name of every basic statistic available. |
Collection<String> |
getStatListFull()
Return a Collection containing the name of all available statistics in the Nagios status file. |
Collection<String> |
getStatListHost()
Return a Collection containing the name of every host-specific statistic available. |
Collection<String> |
getStatListServ()
Return a Collection containing the name of every service-specific statistic available. |
private void |
getStatVal(NagiosStatsRequest.State state,
int length)
After reading a line from the Nagios status file and storing its contents in line_buffer, this method is called to extract the value of the statistic on that line and store it in the appropriate place (in one of statsBase, statsHost, or statsServ). |
String[] |
getUsefulDataNames()
Get an array containing the names of available useful data values. |
double |
getUsefulDataValue(String name)
Returns the useful information from the specified piece of data, in the form of a double. |
private String |
pluginRadToNagios(String name)
Convert the given alias for a useful piece of data to the name used in the Nagios status file. |
private void |
putStatBase(String stat,
String value)
Stores the value for the given basic statistic in the proper location (statsBase). |
private void |
putStatHost(String host,
String stat,
String value)
Stores the value for the given host-specific statistic in the proper location (statsHost). |
private void |
putStatServ(String host,
String service,
String stat,
String value)
Stores the value for the given service-specific statistic in the proper location (statsServ). |
boolean |
refreshStats(InputStream input)
TODO: Javadoc Refreshes the values of the stored statistics by reading in the current values from the Nagios status file. |
long |
wasLastRefreshed()
Get the last time that the statistics were successfully refreshed, meaning a call was made to refreshStats() that returned true. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private Hashtable<String,String> statsBase
private Hashtable<String,Hashtable<String,String>> statsHost
private Hashtable<String,Hashtable<String,Hashtable<String,String>>> statsServ
private final String START_INFO
private final String START_PROG
private final String START_HOST
private final String START_SERV
private final String END_BLOCK_1
private final String END_BLOCK_2
private final String HOST
private final String SERV
private final String NAME_SEPARATOR
private char[] line_buffer
private long lastRefreshed
private String hostName
private String servName
| Constructor Detail |
|---|
public NagiosStatsRequest()
| Method Detail |
|---|
public String getFullPluginOutput(String service)
service - The name of the requested Service; assumes that this is one of the names returned by
getUsefulDataNames().
public Collection<String> getListHost()
public Collection<String> getListServ()
public String getSpecificStat(String host,
String service,
String stat)
host - The name of the particular host, or null if not applicable.service - The name of the particular service, or null if not applicable.stat - The name of the particular desired statistic.
public Collection<String> getStatListBase()
public Collection<String> getStatListFull()
public Collection<String> getStatListHost()
public Collection<String> getStatListServ()
private void getStatVal(NagiosStatsRequest.State state,
int length)
state - The State of the current line in the Nagios status file, indicating whether or not it is inside of
a particular block and, if so, which type of block it is (Info, Program, Host, or Service).length - The index at which the last character in the line is stored in line_buffer.public String[] getUsefulDataNames()
If you want to add a new statistic from the Nagios status file to the list of useful data points, you have to add an alias to the array contained in this method. It is assumed that the information you want comes out of the statistic plugin_output of a particular Service from the status file, and that the service is related to localhost (meaning that the value of host_name in that Service block is localhost).
public double getUsefulDataValue(String name)
If you want to add a new statistic from the Nagios status file to the list of useful data points, you have to add a condition to check for its alias in this method. It is assumed that the information you want comes out of the statistic plugin_output of a particular Service from the status file, and that the service is related to localhost (meaning that the value of host_name in that Service block is localhost). Also assumes that the statistic's alias has already been set in getUsefulDataNames(). Inside of the chain of if statements, under the condition that check for the relevant alias, you have to add logic to extract and return the needed piece of data in the form of a double.
name - The alias of the data value desired, as given by getUsefulDataNames().
private String pluginRadToNagios(String name)
If you want to add a new statistic from the Nagios status file to the list of useful data points, you have to add a condition to check for its alias in this method. The alias you use in the if condition must be the same as the one returned by getUsefulDataNames(). It is assumed that the information you desire is in plugin_output inside of the relevant Service block.
name - The alias used for the particular service.
private void putStatBase(String stat,
String value)
stat - The name of the specific statistic.value - The value of the given statistic, as read from the Nagios status file.
private void putStatHost(String host,
String stat,
String value)
host - The name of the specific host, as given in the status file by host_name.stat - The name of the specific statistic.value - The value of the given statistic, as read from the Nagios status file.
private void putStatServ(String host,
String service,
String stat,
String value)
host - The name of the specific host, as given in the status file by host_name.service - The name of the specific service, as given in the status file by service_description.stat - The name of the specific statistic.value - The value of the given statistic, as read from the Nagios status file.public boolean refreshStats(InputStream input)
public long wasLastRefreshed()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||