| 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 |
| 33 |
|
// like things to monitor |
| 34 |
|
|
| 35 |
|
lastCheck = System.currentTimeMillis(); |
| 36 |
< |
sequence = 0; |
| 36 |
> |
sequence = 1; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
// why oh why wont ultra edit let me put Long in the next line? oh its trying to |
| 59 |
|
*/ |
| 60 |
|
public String getInfo(){ |
| 61 |
|
// called to retrieve the stored averages and output them as a XML string |
| 62 |
< |
XMLFormatter xml = new XMLFormatter("packet"); |
| 63 |
< |
|
| 63 |
< |
// the process to run |
| 62 |
> |
String host = new String(); |
| 63 |
> |
String ip = new String(); |
| 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 |
< |
|
| 65 |
> |
host = InetAddress.getLocalHost().getHostName(); |
| 66 |
> |
ip = InetAddress.getLocalHost().getHostAddress(); |
| 67 |
> |
} catch(UnknownHostException e) { |
| 68 |
> |
System.out.println(e); |
| 69 |
|
} |
| 70 |
< |
catch ( IOException e ){ |
| 71 |
< |
|
| 72 |
< |
} |
| 73 |
< |
|
| 74 |
< |
|
| 75 |
< |
|
| 76 |
< |
// 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(); |
| 70 |
> |
String date = Long.toString(System.currentTimeMillis()); |
| 71 |
> |
XMLFormatter xml = new XMLFormatter("packet", "machine_name=\""+host+"\" ip=\""+ip+"\" date=\""+date+"\" seq_no=\""+sequence+"\""); |
| 72 |
> |
|
| 73 |
> |
// get and decode the data |
| 74 |
> |
DecodeCPU_TXT details = new DecodeCPU_TXT(); |
| 75 |
> |
|
| 76 |
> |
// add the decoded info |
| 77 |
|
xml.addString(details.getItems()); |
| 78 |
|
|
| 79 |
|
|