2 |
|
|
3 |
|
//---IMPORTS--- |
4 |
|
|
5 |
– |
import java.util.Random; |
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. |
11 |
> |
* Collects data based on properties gained from the |
12 |
> |
* configurator then packages these up using XMLFormatter |
13 |
> |
* and outputs them if the timeout has passed. |
14 |
|
* |
15 |
|
* @author $Author$ |
16 |
|
* @version $Id$ |
24 |
|
//---CONSTRUCTORS--- |
25 |
|
|
26 |
|
/** |
27 |
< |
* Public constructor for the class. Takes in a Config object to gain its |
28 |
< |
* properties from. |
27 |
> |
* Public constructor for the class. Takes in a Config |
28 |
> |
* object to gain its properties from. |
29 |
|
* |
30 |
|
*/ |
31 |
|
public SystemMonitor( Config config ){ |
33 |
|
// like things to monitor |
34 |
|
|
35 |
|
lastCheck = System.currentTimeMillis(); |
36 |
< |
sequence = 0; |
36 |
< |
|
37 |
< |
|
38 |
< |
// why oh why wont ultra edit let me put Long in the next line? oh its trying to |
39 |
< |
// correct keywords =| |
40 |
< |
try { |
41 |
< |
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
42 |
< |
} |
43 |
< |
catch ( NumberFormatException e ){ |
44 |
< |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
45 |
< |
// 5 mins |
46 |
< |
udpcheckInterval = 5000 * 60; |
47 |
< |
} |
48 |
< |
// make the check interval into seconds |
36 |
> |
sequence = 1; |
37 |
|
|
38 |
|
} |
39 |
|
|
46 |
|
*/ |
47 |
|
public String getInfo(){ |
48 |
|
// called to retrieve the stored averages and output them as a XML string |
49 |
< |
XMLFormatter xml = new XMLFormatter("packet"); |
50 |
< |
|
63 |
< |
// the process to run |
49 |
> |
String host = new String(); |
50 |
> |
String ip = new String(); |
51 |
|
try { |
52 |
< |
Process proc = Runtime.getRuntime().exec("/usr/local/sbin/top -s1 -d2 0 > top.txt"); |
53 |
< |
// this process will take atleast 2 seconds to complete |
54 |
< |
try { |
55 |
< |
proc.waitFor(); |
69 |
< |
} |
70 |
< |
catch ( InterruptedException e ){ |
71 |
< |
|
72 |
< |
} |
73 |
< |
|
52 |
> |
host = InetAddress.getLocalHost().getHostName(); |
53 |
> |
ip = InetAddress.getLocalHost().getHostAddress(); |
54 |
> |
} catch(UnknownHostException e) { |
55 |
> |
System.out.println(e); |
56 |
|
} |
57 |
< |
catch ( IOException e ){ |
58 |
< |
|
59 |
< |
} |
60 |
< |
|
61 |
< |
|
62 |
< |
|
63 |
< |
// now do some text processing. |
82 |
< |
DecodeCPU_TXT details = new DecodeCPU_TXT("top.txt"); |
83 |
< |
|
84 |
< |
|
85 |
< |
Random rand = new Random(); |
86 |
< |
|
87 |
< |
// just send some dummy info for now. |
88 |
< |
xml.addNest("packet_info"); |
89 |
< |
String currentTime = Long.toString(System.currentTimeMillis()); |
90 |
< |
xml.addElement("date_time", currentTime ); |
91 |
< |
xml.addElement("sequence", Integer.toString(sequence)); |
92 |
< |
xml.closeNest(); |
57 |
> |
String date = Long.toString(System.currentTimeMillis()); |
58 |
> |
XMLFormatter xml = new XMLFormatter("packet", "machine_name=\""+host+"\" ip=\""+ip+"\" date=\""+date+"\" seq_no=\""+sequence+"\" type=\"data\""); |
59 |
> |
|
60 |
> |
// get and decode the data |
61 |
> |
DecodeCPU_TXT details = new DecodeCPU_TXT(); |
62 |
> |
|
63 |
> |
// add the decoded info |
64 |
|
xml.addString(details.getItems()); |
65 |
|
|
95 |
– |
|
96 |
– |
// MUST FIX THIS..!!!! |
97 |
– |
try { |
98 |
– |
long updateIn = ( lastCheck + udpcheckInterval )-System.currentTimeMillis(); |
99 |
– |
if ( updateIn > 0 ){ |
100 |
– |
Thread.sleep(updateIn); |
101 |
– |
} |
102 |
– |
else |
103 |
– |
{ |
104 |
– |
Thread.sleep(defaultUpdateTime); |
105 |
– |
} |
106 |
– |
} |
107 |
– |
catch( InterruptedException e ){ |
108 |
– |
System.out.println("Sleep interrupted"); |
109 |
– |
} |
110 |
– |
|
66 |
|
// increment sequence. |
67 |
|
sequence++; |
68 |
< |
lastCheck = System.currentTimeMillis(); |
68 |
> |
// lastCheck = System.currentTimeMillis(); |
69 |
|
|
70 |
|
// finally return a string |
71 |
|
return xml.returnXML(); |
79 |
|
|
80 |
|
private long lastCheck; |
81 |
|
private int sequence; |
127 |
– |
private long udpcheckInterval; |
128 |
– |
private final long defaultUpdateTime = 60000; |
82 |
|
|
83 |
|
//---STATIC ATTRIBUTES--- |
84 |
|
|
85 |
< |
} // class |
85 |
> |
} // class |