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