`

java监控cup、系统的使用率(必须是JDK1.6)

阅读更多
package com.cvicse.test;
/**
 * @author jgao1 
 * Bytes类用来处理字符串
 */
public class Bytes {
	public static String substring(String src, int start_idx, int end_idx) {
		byte[] b = src.getBytes();
		String tgt = "";
		for (int i = start_idx; i <= end_idx; i++) {
			tgt += (char) b[i];
		}
		return tgt;
	}
}
package com.cvicse.test;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CountDate {

	private String currentMonth;
	private String nextMonth;
	private String currentYear;
	private String nextYear;
	private String currentYM;
	private String currentYMDHMS;
	private Date currentYMDHMSs;
	private String nextYMDHMS;

	/* 根据系统日期获得当月 */
	public String getCurrentMonth() {
		Calendar rightNow = Calendar.getInstance();
		if (String.valueOf(rightNow.get(Calendar.MONTH) + 1).length() > 1) {
			this.currentMonth = String
					.valueOf(rightNow.get(Calendar.MONTH) + 1);
			return currentMonth;
		}
		this.currentMonth = "0"
				+ String.valueOf(rightNow.get(Calendar.MONTH) + 1);

		return currentMonth;
	}

	/* 根据系统日期获得当年 */
	public String getCurrentYear() {
		Calendar rightNow = Calendar.getInstance();
		this.currentYear = String.valueOf(rightNow.get(Calendar.YEAR));
		return currentYear;
	}

	/* 根据系统日期获得数据库日期 */
	public String getCurrentYMDHMS() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
		this.currentYMDHMS = sdf.format(t);
		return currentYMDHMS;
	}

	public Date getCurrentYMDHMSs() {
		Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
		currentYMDHMSs = t;
		return currentYMDHMSs;
	}

	/* 根据系统日期获得下个月 */
	public String getNextMonth() {
		Calendar rightNow = Calendar.getInstance();
		if ((rightNow.get(Calendar.MONTH) + 1) == 12) {
			this.nextMonth = "01";
			this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR) + 1);
			return nextMonth;
		}
		if (String.valueOf(rightNow.get(Calendar.MONTH) + 2).length() > 1) {
			this.nextMonth = String.valueOf(rightNow.get(Calendar.MONTH) + 2);
			return nextMonth;
		}
		this.nextMonth = "0" + String.valueOf(rightNow.get(Calendar.MONTH) + 2);
		return nextMonth;
	}

	/* 根据系统日期获得下个月的年分 */
	public String getNextYear() {
		Calendar rightNow = Calendar.getInstance();
		if ((rightNow.get(Calendar.MONTH) + 1) == 12) {
			this.nextMonth = "01";
			this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR) + 1);
			return nextYear;
		}
		this.nextYear = String.valueOf(rightNow.get(Calendar.YEAR));
		return nextYear;
	}

	/* 根据系统日期获得下个月的数据库日期 */
	public String getNextYMDHMS() {
		nextYear = getNextYear();
		nextMonth = getNextMonth();
		this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
		return nextYMDHMS;
	}

	/* 根据给定的月参数获得当月日期 */
	public String getCurrentMonth(int pcurrentMonth) {
		if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
				return currentMonth;
			}
		}
		if (String.valueOf(pcurrentMonth).length() > 1) {
			this.currentMonth = String.valueOf(pcurrentMonth);
		} else {
			this.currentMonth = "0" + String.valueOf(pcurrentMonth);
		}
		return currentMonth;

	}

	/* 根据给定的月参数和年参数获得当年 */
	public String getCurrentYear(int pcurrentMonth, int pcurrentYear) {
		if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
			}
			this.currentYear = String
					.valueOf(pcurrentMonth / 12 + pcurrentYear);
			return currentYear;
		}
		return this.currentYear = String.valueOf(pcurrentYear);
	}

	/* 根据给定的月参数和年参数获得数据库日期 */
	public String getCurrentYMDHMS(int pcurrentMonth, int pcurrentYear) {
		if ((pcurrentMonth % 12 > 1) || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
			}
			this.currentYear = String
					.valueOf(pcurrentMonth / 12 + pcurrentYear);
			this.currentYMDHMS = currentYear + "-" + currentMonth + "-"
					+ "01 00:00:00";
			return currentYMDHMS;
		}
		if (String.valueOf(pcurrentMonth).length() > 1) {
			this.currentMonth = String.valueOf(pcurrentMonth);
		} else {
			this.currentMonth = "0" + String.valueOf(pcurrentMonth);
		}
		this.currentYear = String.valueOf(pcurrentYear);
		this.currentYMDHMS = currentYear + "-" + currentMonth + "-"
				+ "01 00:00:00";
		return currentYMDHMS;
	}

	public Date getCurrentYMDHMSs(int pcurrentMonth, int pcurrentYear) {
		if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
			}
			this.currentYear = String
					.valueOf(pcurrentMonth / 12 + pcurrentYear);
			this.currentYMDHMSs = Timestamp.valueOf(currentYear + "-"
					+ currentMonth + "-" + "01 00:00:00.0");
			return currentYMDHMSs;
		}
		if (String.valueOf(pcurrentMonth).length() > 1) {
			this.currentMonth = String.valueOf(pcurrentMonth);
		} else {
			this.currentMonth = "0" + String.valueOf(pcurrentMonth);
		}
		this.currentYear = String.valueOf(pcurrentYear);
		this.currentYMDHMSs = Timestamp.valueOf(currentYear + "-"
				+ currentMonth + "-" + "01 00:00:00.0");
		return currentYMDHMSs;
	}

	/* 根据给定的月参数和年参数获得下个月的月份和年份 */
	public String getNextYMDHMS(int pcurrentMonth, int pcurrentYear) {
		if ((pcurrentMonth % 12 > 1) || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf((pcurrentMonth % 12)).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
			}
			this.currentYear = String
					.valueOf(pcurrentMonth / 12 + pcurrentYear);
			if (String.valueOf(Integer.parseInt(currentMonth) + 1).length() > 1) {
				this.nextMonth = String
						.valueOf(Integer.parseInt(currentMonth) + 1);
			} else {
				this.nextMonth = "0"
						+ String.valueOf(Integer.parseInt(currentMonth) + 1);
			}
			this.nextYear = currentYear;
			this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
			return nextYMDHMS;
		}
		if (pcurrentMonth == 12) {
			this.nextMonth = "01";
			this.nextYear = String.valueOf(pcurrentYear + 1);
			this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
			return nextYMDHMS;
		}
		if (String.valueOf(pcurrentMonth + 1).length() > 1) {
			this.nextMonth = String.valueOf(pcurrentMonth + 1);
			this.nextYear = String.valueOf(pcurrentYear);
			this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
			return nextYMDHMS;
		}
		this.nextMonth = "0" + String.valueOf(pcurrentMonth + 1);
		this.nextYear = String.valueOf(pcurrentYear);
		this.nextYMDHMS = nextYear + "-" + nextMonth + "-" + "01 00:00:00";
		return nextYMDHMS;
	}

	/* 根据系统日期获得下个月的年份和月份 */
	public String getCurrentYM() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
		this.currentYM = sdf.format(t);
		return currentYM;
	}

	/* 根据系统日期获得当月的年份和月份 */
	public String getCurrentYM(int pcurrentMonth, int pcurrentYear) {
		if (pcurrentMonth % 12 > 1 || (pcurrentMonth % 12 == 1)) {
			if (String.valueOf(pcurrentMonth % 12).length() > 1) {
				this.currentMonth = String.valueOf((pcurrentMonth % 12));
			} else {
				this.currentMonth = "0" + String.valueOf((pcurrentMonth % 12));
			}
			this.currentYear = String
					.valueOf(pcurrentMonth / 12 + pcurrentYear);
			this.currentYM = currentYear + "年" + currentMonth + "月";
			return currentYM;
		}
		if (String.valueOf(pcurrentMonth).length() > 1) {
			this.currentMonth = String.valueOf(pcurrentMonth);
		} else {
			this.currentMonth = "0" + String.valueOf(pcurrentMonth);
		}
		this.currentYear = String.valueOf(pcurrentYear);
		this.currentYM = currentYear + "年" + currentMonth + "月";
		return currentYM;
	}

	public static void main(String args[]) {
		CountDate c = new CountDate();
		System.out.println(c.getCurrentYMDHMSs(12, 2029));
	}
}

 

package com.cvicse.test;

import java.sql.Timestamp;

public class MonitorInfoBean implements Comparable<MonitorInfoBean> {
	/** 操作系统. */
	private String osName;

	/** 总的物理内存. */
	private float totalMemorySize;

	/** 已使用的物理内存. */
	private float usedMemory;

	/** cpu使用率. */
	private double cpuRatio;

	/** 主机IP地址 */
	private String mIpAddress;
	/** 数据存储时间 */
	private String dDateTime;

	/** 内存使用率 */
	private float memoryRatio;

	/** linux下Buffers内存 */
	private float buffersMemory;

	/** linux下Cached内存 */
	private float cachedMemory;

	public float getBuffersMemory() {
		return buffersMemory;
	}

	public float getCachedMemory() {
		return cachedMemory;
	}

	public String getDDateTime() {
		return dDateTime;
	}

	public void setDDateTime(String dateTime) {
		dDateTime = dateTime;
	}

	public String getMIpAddress() {
		return mIpAddress;
	}

	public void setMIpAddress(String ipAddress) {
		mIpAddress = ipAddress;
	}

	public String getOsName() {
		return osName;
	}

	public void setOsName(String osName) {
		this.osName = osName;
	}

	public float getTotalMemorySize() {
		return totalMemorySize;
	}

	public void setTotalMemorySize(float totalMemorySize) {
		this.totalMemorySize = totalMemorySize;
	}

	public float getUsedMemory() {
		return usedMemory;
	}

	public void setUsedMemory(long usedMemory) {
		this.usedMemory = usedMemory;
	}

	public double getCpuRatio() {
		return cpuRatio;
	}

	public void setCpuRatio(double cpuRatio) {
		this.cpuRatio = cpuRatio;
	}

	public int compareTo(MonitorInfoBean m) {
		String stra = this.getDDateTime();
		String strb = m.getDDateTime();
		Timestamp a = Timestamp.valueOf(stra);
		Timestamp b = Timestamp.valueOf(strb);
		if (a.before(b)) {
			return -1;
		} else if (a.after(b)) {
			return 1;
		} else {
			return 0;
		}

	}

	public float getMemoryRatio() {
		return memoryRatio;
	}

	public void setMemoryRatio(float memoryRatio) {
		this.memoryRatio = memoryRatio;
	}

	public void setUsedMemory(float usedMemory) {
		this.usedMemory = usedMemory;
	}

	public void setBuffersMemory(float buffersMemory) {
		this.buffersMemory = buffersMemory;
	}

	public void setCachedMemory(float cachedMemory) {
		this.cachedMemory = cachedMemory;
	}

}

 

package com.cvicse.test;

public interface IMonitorService {
	public MonitorInfoBean getMonitorInfoBean() throws Exception;
}

 

package com.cvicse.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import com.sun.management.OperatingSystemMXBean;
import sun.management.ManagementFactory;

public class MonitorServiceImpl implements IMonitorService {

	private static final int CPUTIME = 30;
	private static final int PERCENT = 100;
	private static final int FAULTLENGTH = 10;
	private static String osVersion = null;

	private String mIpAddress = null;
	private String dDateTime = null;
	private float totalMemorySize = 0.0f;
	private float buffersMemory = 0.0f;
	private float cachedMemory = 0.0f;
	private float usedMemory = 0.0f;
	private float memoryRatio;

	/**
	 * 获得当前的监控对象.
	 * 
	 * @return 返回构造好的监控对象
	 * @throws Exception
	 * @author YINLIANG
	 */
	public MonitorInfoBean getMonitorInfoBean() throws Exception {
		int kb = 1024;
		CountDate ddate = new CountDate();
		osVersion = System.getProperty("os.version");
		OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
				.getOperatingSystemMXBean();

		// 操作系统
		String osName = System.getProperty("os.name");
		// 主机IP
		if (osName.toLowerCase().startsWith("windows")) {
			mIpAddress = this.getWindowsIp();
		} else {
			mIpAddress = this.getLinuxIP();
		}
		if (osName.toLowerCase().startsWith("windows")) {
			// 总的物理内存
			float totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize()
					/ kb;
			float usedPhysicalMemorySize = (osmxb.getTotalPhysicalMemorySize() - osmxb
					.getFreePhysicalMemorySize())
					/ kb;
			totalMemorySize = Float.parseFloat(String.format("%.1f",
					totalPhysicalMemorySize));
			// 已使用的物理内存
			usedMemory = Float.parseFloat(String.format("%.1f",
					usedPhysicalMemorySize));
			// windows内存使用率
			memoryRatio = Float.parseFloat(String.format("%.1f",
					(usedMemory / totalMemorySize) * 100));
		} else {
			float[] result = null;
			result = getLinuxMemInfo();

			totalMemorySize = Float
					.parseFloat(String.format("%.1f", result[0]));
			buffersMemory = Float.parseFloat(String.format("%.1f", result[1]));
			cachedMemory = Float.parseFloat(String.format("%.1f", result[2]));
			usedMemory = totalMemorySize - result[3];
			// linux内存使用率
			memoryRatio = Float
					.parseFloat(String
							.format(
									"%.1f",
									((usedMemory - (cachedMemory + buffersMemory)) / totalMemorySize) * 100));
		}

		// 获得cpu频率
		double cpuRatio = 0;
		if (osName.toLowerCase().startsWith("windows")) {
			cpuRatio = this.getCpuRatioForWindows();
		} else {
			cpuRatio = this.getCpuRateForLinux();
		}
		/* 取得数据时间 */
		dDateTime = ddate.getCurrentYMDHMS();
		// 构造返回对象
		MonitorInfoBean infoBean = new MonitorInfoBean();

		infoBean.setOsName(osName);
		infoBean.setCpuRatio(cpuRatio);
		infoBean.setMIpAddress(mIpAddress);
		infoBean.setDDateTime(dDateTime);
		infoBean.setBuffersMemory(buffersMemory);
		infoBean.setCachedMemory(cachedMemory);
		infoBean.setUsedMemory(usedMemory);
		infoBean.setTotalMemorySize(totalMemorySize);
		infoBean.setMemoryRatio(memoryRatio);
		return infoBean;
	}

	/**
	 * 获得Linux下IP地址.
	 * 
	 * @return 返回Linux下IP地址
	 * @author yinliang
	 */
	public String getLinuxIP() {
		String ip = "";
		try {
			Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
					.getNetworkInterfaces();
			while (e1.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) e1.nextElement();
				if (!ni.getName().equals("eth0")) {
					continue;
				} else {
					Enumeration<?> e2 = ni.getInetAddresses();
					while (e2.hasMoreElements()) {
						InetAddress ia = (InetAddress) e2.nextElement();
						if (ia instanceof Inet6Address)
							continue;
						ip = ia.getHostAddress();
					}
					break;
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		return ip;
	}

	public String getWindowsIp() {
		String ip = "";
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}

	/**
	 * 获得Linux下CPU使用率.
	 * 
	 * @return 返回Linux下cpu使用率
	 * @author yinliang
	 */
	private double getCpuRateForLinux() {
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader brStat = null;
		StringTokenizer tokenStat = null;
		try {

			Process process = Runtime.getRuntime().exec("top -b -n 1");
			is = process.getInputStream();
			isr = new InputStreamReader(is);
			brStat = new BufferedReader(isr);

			if (osVersion.startsWith("2.4")) {
				brStat.readLine();
				brStat.readLine();
				brStat.readLine();
				brStat.readLine();

				tokenStat = new StringTokenizer(brStat.readLine());
				tokenStat.nextToken();
				tokenStat.nextToken();
				String user = tokenStat.nextToken();
				tokenStat.nextToken();
				String system = tokenStat.nextToken();
				tokenStat.nextToken();
				String nice = tokenStat.nextToken();

				user = user.substring(0, user.indexOf("%"));
				system = system.substring(0, system.indexOf("%"));
				nice = nice.substring(0, nice.indexOf("%"));

				float userUsage = new Float(user).floatValue();
				float systemUsage = new Float(system).floatValue();
				float niceUsage = new Float(nice).floatValue();

				return (userUsage + systemUsage + niceUsage) / 100;
			} else {
				brStat.readLine();
				brStat.readLine();

				tokenStat = new StringTokenizer(brStat.readLine());
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				String cpuUsage = tokenStat.nextToken();

				Float usage = new Float(cpuUsage.substring(0, cpuUsage
						.indexOf("%")));

				return ((1 - usage.floatValue() / 100) * 100);
			}

		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
			freeResource(is, isr, brStat);
			return 1;
		} finally {
			freeResource(is, isr, brStat);
		}

	}

	private static void freeResource(InputStream is, InputStreamReader isr,
			BufferedReader br) {
		try {
			if (is != null)
				is.close();
			if (isr != null)
				isr.close();
			if (br != null)
				br.close();
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}
	}

	/**
	 * 获得windows下CPU使用率.
	 * 
	 * @return 返回windows下cpu使用率
	 * @author YINLIANG
	 */
	private double getCpuRatioForWindows() {
		try {
			mIpAddress = InetAddress.getLocalHost().getHostAddress();
			String procCmd = System.getenv("windir")
					+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
					+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
			// 取进程信息
			long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
			Thread.sleep(CPUTIME);
			long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
			if (c0 != null && c1 != null) {
				long idletime = c1[0] - c0[0];
				long busytime = c1[1] - c0[1];
				return Double.valueOf(
						PERCENT * (busytime) / (busytime + idletime))
						.doubleValue();
			} else {
				return 0.0;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0.0;
		}
	}

	/**
	 * 
	 * 读取CPU信息.
	 * 
	 * @param proc
	 * @return
	 * @author GuoHuang
	 */
	private long[] readCpu(final Process proc) {
		long[] retn = new long[2];
		try {
			proc.getOutputStream().close();
			InputStreamReader ir = new InputStreamReader(proc.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);
			String line = input.readLine();
			if (line == null || line.length() < FAULTLENGTH) {
				return null;
			}
			int capidx = line.indexOf("Caption");
			int cmdidx = line.indexOf("CommandLine");
			int rocidx = line.indexOf("ReadOperationCount");
			int umtidx = line.indexOf("UserModeTime");
			int kmtidx = line.indexOf("KernelModeTime");
			int wocidx = line.indexOf("WriteOperationCount");
			long idletime = 0;
			long kneltime = 0;
			long usertime = 0;
			while ((line = input.readLine()) != null) {
				if (line.length() < wocidx) {
					continue;
				}
				// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
				// ThreadCount,UserModeTime,WriteOperation
				String caption = Bytes.substring(line, capidx, cmdidx - 1)
						.trim();
				String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
				if (cmd.indexOf("wmic.exe") >= 0) {
					continue;
				}
				// log.info("line="+line);
				if (caption.equals("System Idle Process")
						|| caption.equals("System")) {
					idletime += Long.valueOf(
							Bytes.substring(line, kmtidx, rocidx - 1).trim())
							.longValue();
					idletime += Long.valueOf(
							Bytes.substring(line, umtidx, wocidx - 1).trim())
							.longValue();
					continue;
				}

				kneltime += Long.valueOf(
						Bytes.substring(line, kmtidx, rocidx - 1).trim())
						.longValue();
				usertime += Long.valueOf(
						Bytes.substring(line, umtidx, wocidx - 1).trim())
						.longValue();
			}
			retn[0] = idletime;
			retn[1] = kneltime + usertime;
			return retn;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				proc.getInputStream().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public float[] getLinuxMemInfo() {
		File file = new File("/proc/meminfo");
		float result[] = new float[4];
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			String str = null;
			StringTokenizer token = null;
			while ((str = br.readLine()) != null) {
				token = new StringTokenizer(str);
				if (!token.hasMoreTokens()) {
					continue;
				}
				str = token.nextToken();
				if (!token.hasMoreTokens()) {
					continue;
				}
				if (str.equalsIgnoreCase("MemTotal:")) {
					result[0] = Long.parseLong(token.nextToken());

				}
				if (str.equalsIgnoreCase("Buffers:")) {
					result[1] = Long.parseLong(token.nextToken());

				}
				if (str.equalsIgnoreCase("Cached:")) {
					result[2] = Long.parseLong(token.nextToken());

				}
				if (str.equalsIgnoreCase("MemFree:")) {
					result[3] = Long.parseLong(token.nextToken());

				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 测试方法.
	 * 
	 * @param args
	 * @throws Exception
	 * @author YINLIANG
	 */
	public static void information() {
		MonitorInfoBean monitorInfo = null;
		IMonitorService service = new MonitorServiceImpl();
		try {
			monitorInfo = service.getMonitorInfoBean();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String osName = monitorInfo.getOsName();
		String cpuRatio = String.valueOf(monitorInfo.getCpuRatio());
		String mIpAddress = monitorInfo.getMIpAddress();
		String memoryRatio = String.valueOf(monitorInfo.getMemoryRatio());
		String dDateTime = monitorInfo.getDDateTime();
		System.out.println("操作系统名字:" + osName);
		System.out.println("cpu使用频率:" + cpuRatio);
		System.out.println("内存使用率:" + memoryRatio);
		System.out.println("主机IP地址:" + mIpAddress);
		System.out.println("获取数据时间:" + dDateTime);
	}

	public static void main(String[] args) {
		MonitorServiceImpl.information();
	}
}

 

分享到:
评论
1 楼 kakarottoz 2013-06-04  
楼主太厉害了

相关推荐

    苹果电脑安装jdk1.6 mac for jdk1.6 jdk6 安装版

    mac for jdk1.6 jdk6 安装版 里面有两个jdk1.6的安装包,都可以用 如果电脑上安装有1.7,1.8等高版本jdk就不要再下安装包了,安装包安装会报错 命令是这个:brew install ...如果看不懂 移步搜怎么使用brew安装jdk1.6

    jdk1.6集成jjwt的问题

    标题中的“jdk1.6集成jjwt的问题”指的是在Java Development Kit (JDK) 版本1.6的环境下,尝试整合JSON Web Token (JWT) 库jjwt时遇到的挑战。JWT是一种开放标准(RFC 7519),用于在各方之间安全地传输信息作为 ...

    jdk1.6 32位免安装纯绿色版

    值得注意的是,虽然JDK 1.6已经比较老旧,许多现代的Java应用和框架可能不再支持这个版本,开发者通常会被建议使用更新的JDK版本,例如JDK 8或更高。然而,在某些特殊情况下,如旧项目维护或与特定版本的软件兼容性...

    jdk1.6 解压版-windows

    在Windows环境下使用JDK1.6,你需要设置环境变量,主要包括`JAVA_HOME`指向JDK的安装目录,以及`PATH`添加%JAVA_HOME%\bin,确保系统可以找到JDK的可执行文件。 JDK1.6相对于更早的版本,引入了一些新特性,如: -...

    JDK1.6版本下载

    10. **Java EE集成**:JDK 1.6对Java企业版(Java EE)的集成有了更好的支持,使得开发Web应用和企业级应用更为便捷。 总的来说,JDK 1.6在提升开发效率、运行性能以及安全性等方面都有显著的进步,是Java发展历程...

    JDK1.6百度云下载

    JDK1.6是Java发展史上的一个重要版本,它在JDK1.5的基础上进行了大量的改进和优化,提供了更多的特性和功能。 ### 二、JDK1.6的主要特性 1. **性能优化**:JDK1.6对垃圾回收机制进行了改进,提升了程序运行时的...

    java-jdk1.6-jdk-6u45-windows-x64.zip

    1. 解压缩"java-jdk1.6-jdk-6u45-windows-x64.zip"文件,这将释放出"jdk-6u45-windows-x64.exe"可执行文件。 2. 双击运行"jdk-6u45-windows-x64.exe",安装向导会引导你完成安装过程。通常,你需要选择安装路径,...

    okhttp3.8源码使用jdk1.6重新编译_okhttp3.8.0-jdk1.6.zip

    1.okhttp3.8源码使用jdk1.6重新编译,已集成了okio,在javaweb项目中使用,未在安卓项目中使用 2.okhttp3.8源码使用jdk1.6重新编译_okhttp3.8.0-jdk1.6.jar

    jdk1.6压缩包,下载即用

    - **下载**:从Oracle官网或其他可信源下载JDK 1.6的安装文件,如"jdk1.6.exe"。 - **安装**:双击执行安装文件,按照向导步骤进行安装,选择合适的安装路径。 - **环境变量设置**:安装完成后,需要在系统环境变量...

    jdk1.6下载可以直接用(不用安装),下了可以直接在百度搜索jdk配置环境,方便快捷.zip

    这个压缩包提供的JDK1.6版本无需安装,用户可以直接解压后使用,这对于开发者来说是一个便捷的选择,特别是在某些旧项目或者特定环境下仍需使用这个版本的JDK。 配置JDK环境变量是使用Java开发工具的必要步骤,主要...

    aspose-words-15.8.0-jdk1.6

    aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-15.8.0-jdk1.6aspose-words-...

    jdk1.6jar包

    这个特定的更新主要集中在错误修复和安全补丁上,确保开发者在使用JDK 1.6时,能避免已知的安全风险,同时提高系统的稳定性。这些更新可能包括对Java插件、Java运行时环境、Javadoc工具、Java编译器等各个组件的改进...

    jdk1.6解压版(免安装版)

    在给定的"jdk1.6解压版(免安装版)"中,用户无需经历传统意义上的安装过程,只需将压缩包解压到指定位置,然后配置系统环境变量,就可以开始使用这个版本的JDK进行Java程序的开发和运行。 1. **JDK1.6**:这是Java的...

    解决JDK1.6下的Base64报错问题

    在资源文件中的`解决JDK1.6下的Base64.jar`可能是包含了一个兼容JDK 1.6的Base64实现的第三方库,你可以直接将这个jar包添加到你的项目类路径中,这样就可以使用其中提供的Base64类而无需修改代码。添加方法通常包括...

    jdk 1.6 64位

    Java Development Kit(JDK)是...请注意,JDK 1.6已过时,不再接收安全更新,因此对于生产环境,建议使用更新的JDK版本,以保证系统的安全性和稳定性。对于学习和测试目的,了解旧版本的特性和历史变迁也是有价值的。

    jdk1.6解决base64

    因此,在JDK 1.6下使用这些类时可能会遇到兼容性问题,比如编译错误或者运行时异常等。 #### 解决方案 为了解决这个问题,可以考虑引入Apache Commons Codec库。Apache Commons Codec库提供了一系列编码器和解码器...

    如何解决JDK1.6下的Base64

    提供的压缩包文件"解决JDK1.6下的Base64.txt"可能包含了如何集成和使用这些第三方库的详细步骤,或者可能是自定义的Base64实现。在实际应用中,你需要根据文件内容来调整你的项目配置。 总的来说,尽管JDK 1.6没有...

    IBMJDK1.6linux.zip

    IBM JDK 1.6是IBM公司为Java开发者提供的一个针对Linux 64位操作系统的Java开发工具包。这个版本的JDK是Java平台标准版(Java SE)的一部分,主要用于开发和运行Java应用程序、Web应用程序以及企业级应用。IBM JDK与...

    java jdk 1.6 Linux版本

    java jdk 1.6 Linux版本

    jdk1.6 官方安装包window平台32位

    **正文** 《深入理解JDK1.6 32位在Windows...通过以上步骤,您将在32位Windows系统上成功安装并配置JDK1.6,从而可以愉快地进行Java编程。在实际应用中,了解JDK的特性并熟练掌握环境配置,对于提升开发效率至关重要。

Global site tag (gtag.js) - Google Analytics