| 1 |
+ |
//---PACKAGE DECLARATION--- |
| 2 |
+ |
|
| 3 |
+ |
//---IMPORTS--- |
| 4 |
+ |
|
| 5 |
+ |
import java.io.*; |
| 6 |
+ |
import java.net.*; |
| 7 |
+ |
import java.util.*; |
| 8 |
+ |
|
| 9 |
+ |
/** |
| 10 |
+ |
* Gathers system information then outputs it as XML |
| 11 |
+ |
* Collects data based on properties gained from the configurator |
| 12 |
+ |
* then packages these up using XMLFormatter and outputs them if |
| 13 |
+ |
* the timeout has passed. |
| 14 |
+ |
* |
| 15 |
+ |
* @author $Author$ |
| 16 |
+ |
* @version $Id$ |
| 17 |
+ |
*/ |
| 18 |
|
class SystemMonitor { |
| 19 |
|
|
| 20 |
< |
public SystemMonitor( Config config ){ |
| 4 |
< |
// create a new instace, get the info we need out of config |
| 5 |
< |
// like things to monitor |
| 6 |
< |
|
| 7 |
< |
lastCheck = System.currentTimeMillis(); |
| 8 |
< |
sequence = 0; |
| 20 |
> |
//---FINAL ATTRIBUTES--- |
| 21 |
|
|
| 22 |
+ |
//---STATIC METHODS--- |
| 23 |
|
|
| 24 |
< |
// why oh why wont ultra edit let me put Long in the next line? oh its trying to |
| 12 |
< |
// correct keywords =| |
| 13 |
< |
checkInterval = Long.parseLong(config.getProperty("UDPINTEVAL")); |
| 14 |
< |
|
| 15 |
< |
} |
| 24 |
> |
//---CONSTRUCTORS--- |
| 25 |
|
|
| 26 |
< |
public String getInfo(){ |
| 27 |
< |
// called to retrieve the stored averages and output them as a XML string |
| 28 |
< |
XMLFormatter xml = new XMLFormatter("packet"); |
| 29 |
< |
|
| 30 |
< |
// just send some dummy info for now. |
| 31 |
< |
xml.addNest("packet_info"); |
| 32 |
< |
String currentTime = Long.toString(System.currentTimeMillis()); |
| 33 |
< |
xml.addElement("date_time", currentTime ); |
| 34 |
< |
xml.addElement("sequence", Integer.toString(sequence)); |
| 35 |
< |
xml.closeNest(); |
| 36 |
< |
xml.addNest("core"); |
| 28 |
< |
xml.addElement("cpu","100"); |
| 29 |
< |
xml.addElement("memory","200"); |
| 30 |
< |
xml.closeNest(); |
| 31 |
< |
xml.addNest("additional"); |
| 32 |
< |
xml.addElement("users","20"); |
| 33 |
< |
xml.closeNest(); |
| 34 |
< |
|
| 35 |
< |
// MUST FIX THIS..!!!! |
| 36 |
< |
while ( System.currentTimeMillis() < ( lastCheck + checkInterval ) ){ |
| 37 |
< |
// errm do nothing.. block or something. |
| 38 |
< |
} |
| 39 |
< |
|
| 40 |
< |
// increment sequence. |
| 41 |
< |
sequence++; |
| 42 |
< |
|
| 43 |
< |
// finally return a string |
| 44 |
< |
return xml.returnXML(); |
| 45 |
< |
} // getinfo() |
| 26 |
> |
/** |
| 27 |
> |
* Public constructor for the class. Takes in a Config object to gain its |
| 28 |
> |
* properties from. |
| 29 |
> |
* |
| 30 |
> |
*/ |
| 31 |
> |
public SystemMonitor( Config config ){ |
| 32 |
> |
// create a new instace, get the info we need out of config |
| 33 |
> |
// like things to monitor |
| 34 |
> |
|
| 35 |
> |
lastCheck = System.currentTimeMillis(); |
| 36 |
> |
sequence = 1; |
| 37 |
|
|
| 38 |
< |
// last time the system data was returned. |
| 38 |
> |
|
| 39 |
> |
// why oh why wont ultra edit let me put Long in the next line? oh its trying to |
| 40 |
> |
// correct keywords =| |
| 41 |
> |
try { |
| 42 |
> |
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
| 43 |
> |
} |
| 44 |
> |
catch ( NumberFormatException e ){ |
| 45 |
> |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
| 46 |
> |
// 5 mins |
| 47 |
> |
udpcheckInterval = 5000 * 60; |
| 48 |
> |
} |
| 49 |
> |
// make the check interval into seconds |
| 50 |
> |
|
| 51 |
> |
} |
| 52 |
> |
|
| 53 |
> |
//---PUBLIC METHODS--- |
| 54 |
> |
|
| 55 |
> |
/** |
| 56 |
> |
* Gathers system information and will black until the timeout has passed. |
| 57 |
> |
* |
| 58 |
> |
* @return the system information in an XML packet |
| 59 |
> |
*/ |
| 60 |
> |
public String getInfo(){ |
| 61 |
> |
// called to retrieve the stored averages and output them as a XML string |
| 62 |
> |
String host = new String(); |
| 63 |
> |
String ip = new String(); |
| 64 |
> |
try { |
| 65 |
> |
host = InetAddress.getLocalHost().getHostName(); |
| 66 |
> |
ip = InetAddress.getLocalHost().getHostAddress(); |
| 67 |
> |
} catch(UnknownHostException e) { |
| 68 |
> |
System.out.println(e); |
| 69 |
> |
} |
| 70 |
> |
String date = Long.toString(System.currentTimeMillis()); |
| 71 |
> |
XMLFormatter xml = new XMLFormatter("packet", "machine_name=\""+host+"\" ip=\""+ip+"\" date=\""+date+"\" seq_no=\""+sequence+"\""); |
| 72 |
> |
|
| 73 |
> |
// get and decode the data |
| 74 |
> |
DecodeCPU_TXT details = new DecodeCPU_TXT(); |
| 75 |
> |
|
| 76 |
> |
// add the decoded info |
| 77 |
> |
xml.addString(details.getItems()); |
| 78 |
> |
|
| 79 |
> |
|
| 80 |
> |
// MUST FIX THIS..!!!! |
| 81 |
> |
try { |
| 82 |
> |
long updateIn = ( lastCheck + udpcheckInterval )-System.currentTimeMillis(); |
| 83 |
> |
if ( updateIn > 0 ){ |
| 84 |
> |
Thread.sleep(updateIn); |
| 85 |
> |
} |
| 86 |
> |
else |
| 87 |
> |
{ |
| 88 |
> |
Thread.sleep(defaultUpdateTime); |
| 89 |
> |
} |
| 90 |
> |
} |
| 91 |
> |
catch( InterruptedException e ){ |
| 92 |
> |
System.out.println("Sleep interrupted"); |
| 93 |
> |
} |
| 94 |
> |
|
| 95 |
> |
// increment sequence. |
| 96 |
> |
sequence++; |
| 97 |
> |
lastCheck = System.currentTimeMillis(); |
| 98 |
> |
|
| 99 |
> |
// finally return a string |
| 100 |
> |
return xml.returnXML(); |
| 101 |
> |
} // getinfo() |
| 102 |
> |
|
| 103 |
> |
//---PRIVATE METHODS--- |
| 104 |
> |
|
| 105 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
| 106 |
> |
|
| 107 |
> |
//---ATTRIBUTES--- |
| 108 |
> |
|
| 109 |
|
private long lastCheck; |
| 110 |
|
private int sequence; |
| 111 |
< |
private long checkInterval; |
| 111 |
> |
private long udpcheckInterval; |
| 112 |
> |
private final long defaultUpdateTime = 60000; |
| 113 |
> |
|
| 114 |
> |
//---STATIC ATTRIBUTES--- |
| 115 |
|
|
| 116 |
|
} // class |