| 1 |
ab11 |
1.2 |
//---PACKAGE DECLARATION--- |
| 2 |
|
|
|
| 3 |
|
|
//---IMPORTS--- |
| 4 |
|
|
|
| 5 |
ab11 |
1.5 |
import java.io.*; |
| 6 |
tdb |
1.6 |
import java.net.*; |
| 7 |
|
|
import java.util.*; |
| 8 |
ab11 |
1.3 |
|
| 9 |
ab11 |
1.2 |
/** |
| 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 |
ab11 |
1.7 |
* @author $Author: tdb1 $ |
| 16 |
|
|
* @version $Id: SystemMonitor.java,v 1.6 2000/12/07 23:21:13 tdb1 Exp $ |
| 17 |
ab11 |
1.2 |
*/ |
| 18 |
ab11 |
1.1 |
class SystemMonitor { |
| 19 |
|
|
|
| 20 |
ab11 |
1.2 |
//---FINAL ATTRIBUTES--- |
| 21 |
|
|
|
| 22 |
|
|
//---STATIC METHODS--- |
| 23 |
|
|
|
| 24 |
|
|
//---CONSTRUCTORS--- |
| 25 |
|
|
|
| 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 |
tdb |
1.6 |
sequence = 1; |
| 37 |
ab11 |
1.2 |
|
| 38 |
|
|
|
| 39 |
|
|
// why oh why wont ultra edit let me put Long in the next line? oh its trying to |
| 40 |
|
|
// correct keywords =| |
| 41 |
ab11 |
1.7 |
/* |
| 42 |
|
|
|
| 43 |
ab11 |
1.4 |
try { |
| 44 |
|
|
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
| 45 |
|
|
} |
| 46 |
|
|
catch ( NumberFormatException e ){ |
| 47 |
|
|
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
| 48 |
|
|
// 5 mins |
| 49 |
|
|
udpcheckInterval = 5000 * 60; |
| 50 |
|
|
} |
| 51 |
ab11 |
1.7 |
*/ |
| 52 |
|
|
|
| 53 |
ab11 |
1.3 |
// make the check interval into seconds |
| 54 |
ab11 |
1.2 |
|
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
//---PUBLIC METHODS--- |
| 58 |
|
|
|
| 59 |
|
|
/** |
| 60 |
|
|
* Gathers system information and will black until the timeout has passed. |
| 61 |
|
|
* |
| 62 |
|
|
* @return the system information in an XML packet |
| 63 |
|
|
*/ |
| 64 |
|
|
public String getInfo(){ |
| 65 |
|
|
// called to retrieve the stored averages and output them as a XML string |
| 66 |
tdb |
1.6 |
String host = new String(); |
| 67 |
|
|
String ip = new String(); |
| 68 |
ab11 |
1.5 |
try { |
| 69 |
tdb |
1.6 |
host = InetAddress.getLocalHost().getHostName(); |
| 70 |
|
|
ip = InetAddress.getLocalHost().getHostAddress(); |
| 71 |
|
|
} catch(UnknownHostException e) { |
| 72 |
|
|
System.out.println(e); |
| 73 |
ab11 |
1.5 |
} |
| 74 |
tdb |
1.6 |
String date = Long.toString(System.currentTimeMillis()); |
| 75 |
|
|
XMLFormatter xml = new XMLFormatter("packet", "machine_name=\""+host+"\" ip=\""+ip+"\" date=\""+date+"\" seq_no=\""+sequence+"\""); |
| 76 |
|
|
|
| 77 |
|
|
// get and decode the data |
| 78 |
|
|
DecodeCPU_TXT details = new DecodeCPU_TXT(); |
| 79 |
|
|
|
| 80 |
|
|
// add the decoded info |
| 81 |
ab11 |
1.5 |
xml.addString(details.getItems()); |
| 82 |
|
|
|
| 83 |
|
|
|
| 84 |
ab11 |
1.7 |
/* |
| 85 |
ab11 |
1.2 |
// MUST FIX THIS..!!!! |
| 86 |
ab11 |
1.3 |
try { |
| 87 |
ab11 |
1.4 |
long updateIn = ( lastCheck + udpcheckInterval )-System.currentTimeMillis(); |
| 88 |
|
|
if ( updateIn > 0 ){ |
| 89 |
|
|
Thread.sleep(updateIn); |
| 90 |
|
|
} |
| 91 |
|
|
else |
| 92 |
|
|
{ |
| 93 |
|
|
Thread.sleep(defaultUpdateTime); |
| 94 |
|
|
} |
| 95 |
ab11 |
1.3 |
} |
| 96 |
|
|
catch( InterruptedException e ){ |
| 97 |
|
|
System.out.println("Sleep interrupted"); |
| 98 |
ab11 |
1.2 |
} |
| 99 |
ab11 |
1.7 |
*/ |
| 100 |
ab11 |
1.2 |
|
| 101 |
|
|
// increment sequence. |
| 102 |
|
|
sequence++; |
| 103 |
ab11 |
1.7 |
// lastCheck = System.currentTimeMillis(); |
| 104 |
ab11 |
1.2 |
|
| 105 |
|
|
// finally return a string |
| 106 |
|
|
return xml.returnXML(); |
| 107 |
|
|
} // getinfo() |
| 108 |
|
|
|
| 109 |
|
|
//---PRIVATE METHODS--- |
| 110 |
|
|
|
| 111 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
| 112 |
|
|
|
| 113 |
|
|
//---ATTRIBUTES--- |
| 114 |
ab11 |
1.1 |
|
| 115 |
|
|
private long lastCheck; |
| 116 |
|
|
private int sequence; |
| 117 |
ab11 |
1.2 |
|
| 118 |
|
|
//---STATIC ATTRIBUTES--- |
| 119 |
ab11 |
1.1 |
|
| 120 |
|
|
} // class |