1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
|
|
22 |
|
//---IMPORTS--- |
23 |
|
|
24 |
< |
import java.util.Random; |
24 |
> |
import java.io.*; |
25 |
> |
import java.net.*; |
26 |
> |
import java.util.*; |
27 |
|
|
28 |
|
/** |
29 |
|
* Gathers system information then outputs it as XML |
30 |
< |
* Collects data based on properties gained from the configurator |
31 |
< |
* then packages these up using XMLFormatter and outputs them if |
32 |
< |
* the timeout has passed. |
30 |
> |
* Collects data based on properties gained from the |
31 |
> |
* configurator then packages these up using XMLFormatter |
32 |
> |
* and outputs them if the timeout has passed. |
33 |
|
* |
34 |
|
* @author $Author$ |
35 |
|
* @version $Id$ |
43 |
|
//---CONSTRUCTORS--- |
44 |
|
|
45 |
|
/** |
46 |
< |
* Public constructor for the class. Takes in a Config object to gain its |
47 |
< |
* properties from. |
46 |
> |
* Public constructor for the class. Takes in a Config |
47 |
> |
* object to gain its properties from. |
48 |
|
* |
49 |
|
*/ |
50 |
|
public SystemMonitor( Config config ){ |
52 |
|
// like things to monitor |
53 |
|
|
54 |
|
lastCheck = System.currentTimeMillis(); |
55 |
< |
sequence = 0; |
35 |
< |
|
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 |
55 |
> |
sequence = 1; |
56 |
|
|
57 |
|
} |
58 |
|
|
65 |
|
*/ |
66 |
|
public String getInfo(){ |
67 |
|
// called to retrieve the stored averages and output them as a XML string |
68 |
< |
XMLFormatter xml = new XMLFormatter("packet"); |
69 |
< |
|
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..!!!! |
68 |
> |
String host = new String(); |
69 |
> |
String ip = new String(); |
70 |
|
try { |
71 |
< |
long updateIn = ( lastCheck + udpcheckInterval )-System.currentTimeMillis(); |
72 |
< |
if ( updateIn > 0 ){ |
73 |
< |
Thread.sleep(updateIn); |
74 |
< |
} |
85 |
< |
else |
86 |
< |
{ |
87 |
< |
Thread.sleep(defaultUpdateTime); |
88 |
< |
} |
71 |
> |
host = InetAddress.getLocalHost().getHostName(); |
72 |
> |
ip = InetAddress.getLocalHost().getHostAddress(); |
73 |
> |
} catch(UnknownHostException e) { |
74 |
> |
System.out.println(e); |
75 |
|
} |
76 |
< |
catch( InterruptedException e ){ |
77 |
< |
System.out.println("Sleep interrupted"); |
78 |
< |
} |
76 |
> |
String date = Long.toString(System.currentTimeMillis()); |
77 |
> |
XMLFormatter xml = new XMLFormatter("packet", "machine_name=\""+host+"\" ip=\""+ip+"\" date=\""+date+"\" seq_no=\""+sequence+"\" type=\"data\""); |
78 |
> |
|
79 |
> |
// get and decode the data |
80 |
> |
DecodeCPU_TXT details = new DecodeCPU_TXT(); |
81 |
> |
|
82 |
> |
// add the decoded info |
83 |
> |
xml.addString(details.getItems()); |
84 |
|
|
85 |
|
// increment sequence. |
86 |
|
sequence++; |
87 |
< |
lastCheck = System.currentTimeMillis(); |
87 |
> |
// lastCheck = System.currentTimeMillis(); |
88 |
|
|
89 |
|
// finally return a string |
90 |
|
return xml.returnXML(); |
98 |
|
|
99 |
|
private long lastCheck; |
100 |
|
private int sequence; |
110 |
– |
private long udpcheckInterval; |
111 |
– |
private final long defaultUpdateTime = 60000; |
101 |
|
|
102 |
|
//---STATIC ATTRIBUTES--- |
103 |
|
|
104 |
< |
} // class |
104 |
> |
} // class |