麻烦,这几天做了一个web的服务器资源监控,这是cpu使用率效果图。
更新数据:
/**
*
*/
package com.infobank.smsweb.stakeout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Random;
import java.util.StringTokenizer;
import com.infobank.smsweb.util.DateUtil;
import com.mahh.comm.stakeout.Stakeout;
import com.mahh.comm.stakeout.StakeoutData;
import com.mahh.comm.stakeout.StakeoutStartException;
import com.mahh.comm.stakeout.StakeoutStopException;
/**
* @author Ma Huihuang
*
*/
public class CPUStakeout extends Thread implements Stakeout {
private boolean isRun=false;
private StakeoutData data=new StakeoutData();
private long frequency=3000;
private int maxNum=40;
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#getData()
*/
public StakeoutData getData() {
return this.data;
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#isRight()
*/
public boolean isRight() {
return false;
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#setAutomatism(boolean)
*/
public void setAutomatism(boolean b) {
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#setFrequency(long)
*/
public void setFrequency(long frequency) {
this.frequency=frequency;
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#setMaxNum(int)
*/
public void setMaxNum(int i) {
this.maxNum=i;
}
public int getMaxNum(){
return this.maxNum;
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#start()
*/
public boolean startStakeout() throws StakeoutStartException {
this.isRun=true;
this.data.setStakeoutname("服务器CPU监控");
this.data.setAnalysetime(DateUtil.getCurDateTime());
this.start();
return true;
}
/* (non-Javadoc)
* @see com.mahh.comm.stakeout.Stakeout#stop()
*/
public boolean stopStakeout() throws StakeoutStopException {
this.isRun=false;
return true;
}
public void run(){
while(this.isRun){
try{
while(this.data.getData().size()>(this.maxNum-1))
this.data.getData().remove(0);
this.data.getData().add(getCpuData());
sleep(frequency);
}catch(Exception e){
e.printStackTrace();
}
}
}
private CPU getCpuData(){
CPU cpu=new CPU();
//cpu.setTime(getCurTime());
Random r=new Random();
//int i= r.nextInt(100);
//cpu.setUse(i);
try{
float i=getCpuUsage();
cpu.setUse(i);
}catch(Exception e){
e.printStackTrace();
}
return cpu;
}
private float getCpuUsage() throws Exception {
File file = new File("/proc/stat");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
StringTokenizer token = new StringTokenizer(br.readLine());
token.nextToken();
long user1 = Long.parseLong(token.nextToken());
long nice1 = Long.parseLong(token.nextToken());
long sys1 = Long.parseLong(token.nextToken());
long idle1 = Long.parseLong(token.nextToken());
Thread.sleep(1000);
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
token = new StringTokenizer(br.readLine());
token.nextToken();
long user2 = Long.parseLong(token.nextToken());
long nice2 = Long.parseLong(token.nextToken());
long sys2 = Long.parseLong(token.nextToken());
long idle2 = Long.parseLong(token.nextToken());
return ((float) ((user2 + sys2 + nice2) - (user1 + sys1 + nice1))
/ (float) ((user2 + nice2 + sys2 + idle2) - (user1 + nice1
+ sys1 + idle1)))*100;
}
}
读取数据
response.setCharacterEncoding("gb2312");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String name");
StringBuffer time=new StringBuffer();
StringBuffer data=new StringBuffer("1,");
String t="";
String d="";
try{
int size=StakeoutCommand.getInstance().getStakeoutPlug(name).getMaxNum();
Iterator it=StakeoutCommand.getInstance().getStakeoutPlug(name).getData().getData().iterator();
for(int i=0;i<size;i++){
if(it.hasNext()){
CPU cpu=(CPU)it.next();
time.append(cpu.getUse()+",");
data.append(cpu.getUse()+",");
}else{
time.append("-,");
data.append(0+",");
}
}
}catch(Exception e){
e.printStackTrace();
}
if(time.toString().endsWith(","))
t=time.substring(0,time.length()-1);
if(data.toString().endsWith(","))
d=data.substring(0,data.length()-1);
//System.out.println(t);
//System.out.println(d);
out.println();
out.println("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
out.println("<response>");
out.println("<time>"+t+"</time>");
out.println("<data>"+d+"</data>");
out.println("</response>");
out.flush();
out.close();
HTML:
<script language=javascript>function table2(total,table_x,table_y,all_width,all_height,line_no)
{
var content="";
var line_color = "#69f";
var left_width = 30;
var total_no = total[1].length
var temp1,temp2,temp3
temp1 = 90;
//for(var i=1;i<total_no;i++) {
// for(var j=1;j<=line_no;j++) {
// if(temp1<total[j][i])
// temp1 = total[j][i];
// }
//}
temp1 = parseInt(temp1);
//temp1 =90;
if(temp1>9)
{
temp2 = temp1.toString().substr(1,1);
if(temp2>4)
{
temp3 = (parseInt(temp1/(Math.pow(10,(temp1.toString().length-1))))+1)*Math.pow(10,(temp1.toString().length-1));
}
else
{
//temp3 = (praseInt(temp1/(Math.pow(10,(temp1.toString().length-1))))+0.5)*Math.pow(10,(temp1.toString().length-1))
temp3 = (parseInt(temp1/(Math.pow(10,(temp1.toString().length-1))))+1)*Math.pow(10,(temp1.toString().length-1));
}
} else {
if(temp1>4)
{
temp3 = 10;
}else{
temp3 = 5;
}
}
temp4 = temp3;
try{
content=content+"<v:rect alt='' style=';left:" + (table_x + left_width) + "px;top:" + table_y + "px;width:" + all_width + "px;height:" + all_height + "px;z-index:-1' fillcolor='#9cf' stroked='f'><v:fill rotate='t' angle='-45' focus='100%' type='gradient'/></v:rect>";
}catch(ex){
alert("inside the exception handler:"+ex.description);
}
for(var i=0;i<all_height;i += all_height/5)
{
//背景横线
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width + length) + "px," + (table_y + all_height - length - i) + "px' to='" + (table_x + all_width + left_width) + "px," + (table_y + all_height - length - i) + "px' strokecolor='" + line_color + "'/>";
//数字横线
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + (left_width - 15)) + "px," + (table_y + i) + "px' to='" + (table_x + left_width) + "px," + (table_y + i) + "px'/>";
//数字
content=content+"<v:shape type='#_x0000_t202' alt='' style=';left:" + table_x + "px;top:" + (table_y + i) + "px;width:" + left_width + "px;height:18px;z-index:1'>";
content=content+"<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + temp4 + "</td></tr></table></v:textbox></v:shape>";
temp4 = temp4 - temp3/5;
}
//底横线
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width) + "px," + (table_y + all_height) + "px' to='" + (table_x + all_width + left_width) + "px," + (table_y + all_height) + "px'/>";
//左竖线
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width) + "px," + table_y + "px' to='" + (table_x + left_width) + "px," + (table_y + all_height) + "px'/>";
var tmpStr = ""
for(i=1;i<=line_no;i++) {
var re = /,/g;
tmpStr += ",[/"" + total[i][0].replace(re,"/",/"") + "/"]"
}
tmpStr = tmpStr.substr(1,tmpStr.length-1)
var line_code = eval("new Array(" + tmpStr + ")")
for(var j=1;j<=line_no;j++)
{
for(var i=1;i<total_no-1;i++){
var x1 = table_x + left_width + all_width * (i - 1)/(total_no-1)
var y1 = table_y + (temp3 - total[j][i]) * (all_height/temp3)
var x2 = table_x + left_width + all_width * i/(total_no-1)
var y2 = table_y + (temp3 - total[j][i+1]) * (all_height/temp3)
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;z-index:1' from='" + x1 + "px," + y1 + "px' to='" + x2 + "px," + y2 + "px' coordsize='21600,21600' strokecolor='" + line_code[j-1][0] + "' strokeweight='" + line_code[j-1][1] + "'>";
switch (parseInt(line_code[j-1][2]))
{
case 1:
break;
case 2:
content=content+ "<v:stroke dashstyle='1 1'/>";
break;
case 3:
content=content+ "<v:stroke dashstyle='dash'/>";
break;
case 4:
content=content+ "<v:stroke dashstyle='dashDot'/>";
break;
case 5:
content=content+ "<v:stroke dashstyle='dashDot'/>";
break;
case 6:
content=content+ "<v:stroke dashstyle='longDashDot'/>";
break;
case 7:
content=content+ "<v:stroke dashstyle='longDashDotDot'/>";
break;
}
content=content+ "</v:line>";
switch (parseInt(line_code[j-1][3])) {
case 1:
break;
case 2:
content=content+ "<v:rect style=';left:" + (x1 - 2) + "px;top:" + (y1 - 2) + "px;width:4px;height:4px; z-index:2' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>";
break;
case 3:
content=content+"<v:oval style=';left:" + (x1 - 2) + "px;top:" + (y1 - 2) + "px;width:4px;height:4px;z-index:1' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>";
break;
}
}
switch (parseInt(line_code[j-1][3]))
{
case 1:
break;
case 2:
content=content+"<v:rect style=';left:" + (x2 - 2) + "px;top:" + (y2 - 2) + "px;width:4px;height:4px; z-index:2' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>";
break;
case 3:
content=content+"<v:oval style=';left:" + (x2 - 2) + "px;top:" + (y2 - 2) + "px;width:4px;height:4px;z-index:1' fillcolor='" + line_code[j-1][0] + "' strokecolor='" + line_code[j-1][0] + "'/>";
break;
}
}
for(var i=0;i<total_no-1;i++)
{
content=content+"<v:line alt='' style=';left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px," + (table_y + all_height) + "px' to='" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px," + (table_y + all_height + 15) + "px'/>";
content=content+"<v:shape type='#_x0000_t202' alt='' style=';left:" + (table_x + left_width + all_width * (i)/(total_no-1)) + "px;top:" + (table_y + all_height) + "px;width:" + (all_width/(total_no-1)) + "px;height:18px;z-index:1'>";
content=content+"<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='left'>" + total[0][i] + "</td></tr></table></v:textbox></v:shape>";
}
var tb_height = 30
for(var i=0;i<line_no;i++)
{
content=content+"<v:shape type='#_x0000_t202' alt='' style=';left:" + (table_x + all_width + 25) + "px;top:" + (table_y + 10+(i) * tb_height) + "px;width:60px;height:" + tb_height + "px;z-index:1'>";
content=content+"<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='left'>" + line_code[i][4] + "</td></tr></table></v:textbox></v:shape>";
}
document.getElementById("content").innerHTML= content;
}
</script>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<!--[if !mso]>
<style>
v/:* { behavior: url(#default#VML) }
o/:* { behavior: url(#default#VML) }
.shape { behavior: url(#default#VML) }
</style>
<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style>
TD { FONT-SIZE: 9pt}
</style></head>
<body topmargin=5 leftmargin=0 scroll=AUTO>
<div ></div>
<script type="text/javascript">
var xmlHttp;
function createHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function getCPUInfo(){
createHttpRequest();
var url="smsc.CPUStatus?name=CpuStakeout";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
function callback(){
var time;
var data;
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var timeTag=xmlHttp.responseXML.getElementsByTagName("time");
var dataTag=xmlHttp.responseXML.getElementsByTagName("data");
time=timeTag[0].firstChild.nodeValue;
data=dataTag[0].firstChild.nodeValue;
setMessage(time,data)
}else{
}
}else{
}
}
function setMessage(time,data){
var t=new Array();
var d=new Array();
t=time.split(",");
d=data.split(",");
var baifen=Math.round(d[d.length-1]*100)/100
d[0]="#0000FF,1,1,2,"+baifen+"%";
var total = new Array(t,d)
table2(total,100,90,600,250,1);
}
getCPUInfo();
setInterval("getCPUInfo()",3000);
</script>
</body>
</html>