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