1 |
ab11 |
1.2 |
//---PACKAGE DECLARATION--- |
2 |
|
|
|
3 |
|
|
//---IMPORTS--- |
4 |
|
|
|
5 |
ab11 |
1.3 |
import java.util.Random; |
6 |
ab11 |
1.5 |
import java.io.*; |
7 |
ab11 |
1.3 |
|
8 |
ab11 |
1.2 |
/** |
9 |
|
|
* Gathers system information then outputs it as XML |
10 |
|
|
* Collects data based on properties gained from the configurator |
11 |
|
|
* then packages these up using XMLFormatter and outputs them if |
12 |
|
|
* the timeout has passed. |
13 |
|
|
* |
14 |
|
|
* @author $Author: ab11 $ |
15 |
ab11 |
1.5 |
* @version $Id: SystemMonitor.java,v 1.4 2000/11/30 17:33:36 ab11 Exp $ |
16 |
ab11 |
1.2 |
*/ |
17 |
ab11 |
1.1 |
class SystemMonitor { |
18 |
|
|
|
19 |
ab11 |
1.2 |
//---FINAL ATTRIBUTES--- |
20 |
|
|
|
21 |
|
|
//---STATIC METHODS--- |
22 |
|
|
|
23 |
|
|
//---CONSTRUCTORS--- |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* Public constructor for the class. Takes in a Config object to gain its |
27 |
|
|
* properties from. |
28 |
|
|
* |
29 |
|
|
*/ |
30 |
|
|
public SystemMonitor( Config config ){ |
31 |
|
|
// create a new instace, get the info we need out of config |
32 |
|
|
// like things to monitor |
33 |
|
|
|
34 |
|
|
lastCheck = System.currentTimeMillis(); |
35 |
|
|
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 |
ab11 |
1.4 |
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 |
ab11 |
1.3 |
// make the check interval into seconds |
49 |
ab11 |
1.2 |
|
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
//---PUBLIC METHODS--- |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Gathers system information and will black until the timeout has passed. |
56 |
|
|
* |
57 |
|
|
* @return the system information in an XML packet |
58 |
|
|
*/ |
59 |
|
|
public String getInfo(){ |
60 |
|
|
// called to retrieve the stored averages and output them as a XML string |
61 |
|
|
XMLFormatter xml = new XMLFormatter("packet"); |
62 |
|
|
|
63 |
ab11 |
1.5 |
// the process to run |
64 |
|
|
try { |
65 |
|
|
Process proc = Runtime.getRuntime().exec("/usr/local/sbin/top -s1 -d2 0 > top.txt"); |
66 |
|
|
// this process will take atleast 2 seconds to complete |
67 |
|
|
try { |
68 |
|
|
proc.waitFor(); |
69 |
|
|
} |
70 |
|
|
catch ( InterruptedException e ){ |
71 |
|
|
|
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
} |
75 |
|
|
catch ( IOException e ){ |
76 |
|
|
|
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
// now do some text processing. |
82 |
|
|
DecodeCPU_TXT details = new DecodeCPU_TXT("top.txt"); |
83 |
|
|
|
84 |
|
|
|
85 |
ab11 |
1.3 |
Random rand = new Random(); |
86 |
|
|
|
87 |
ab11 |
1.2 |
// 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(); |
93 |
ab11 |
1.5 |
xml.addString(details.getItems()); |
94 |
|
|
|
95 |
|
|
|
96 |
ab11 |
1.2 |
// MUST FIX THIS..!!!! |
97 |
ab11 |
1.3 |
try { |
98 |
ab11 |
1.4 |
long updateIn = ( lastCheck + udpcheckInterval )-System.currentTimeMillis(); |
99 |
|
|
if ( updateIn > 0 ){ |
100 |
|
|
Thread.sleep(updateIn); |
101 |
|
|
} |
102 |
|
|
else |
103 |
|
|
{ |
104 |
|
|
Thread.sleep(defaultUpdateTime); |
105 |
|
|
} |
106 |
ab11 |
1.3 |
} |
107 |
|
|
catch( InterruptedException e ){ |
108 |
|
|
System.out.println("Sleep interrupted"); |
109 |
ab11 |
1.2 |
} |
110 |
|
|
|
111 |
|
|
// increment sequence. |
112 |
|
|
sequence++; |
113 |
ab11 |
1.3 |
lastCheck = System.currentTimeMillis(); |
114 |
ab11 |
1.2 |
|
115 |
|
|
// finally return a string |
116 |
|
|
return xml.returnXML(); |
117 |
|
|
} // getinfo() |
118 |
|
|
|
119 |
|
|
//---PRIVATE METHODS--- |
120 |
|
|
|
121 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
122 |
|
|
|
123 |
|
|
//---ATTRIBUTES--- |
124 |
ab11 |
1.1 |
|
125 |
|
|
private long lastCheck; |
126 |
|
|
private int sequence; |
127 |
ab11 |
1.4 |
private long udpcheckInterval; |
128 |
|
|
private final long defaultUpdateTime = 60000; |
129 |
ab11 |
1.2 |
|
130 |
|
|
//---STATIC ATTRIBUTES--- |
131 |
ab11 |
1.1 |
|
132 |
|
|
} // class |