喜欢编程的鱼
oop_ming
全部博文(606)
UML(1)
文本分类(2)
Apache Http serv(2)
JProfiler(3)
Apache Lucene Ma(1)
googleapi(3)
nutch(10)
heritrix(1)
lucene(7)
J2EE模式(2)
行为模式(4)
结构模式(1)
创建模式(3)
FreeMarker(1)
Weblogic(3)
JSF(1)
Ajax(12)
Tomcat(5)
xml(6)
servlet(4)
JSP(19)
css(2)
javascript(30)
html(5)
Flex Builder(10)
NetBeans(0)
eclipse(28)
Linux(24)
windows(2)
Solaris(4)
Memcached(2)
NoSQL(0)
MS SQL(18)
MySQL(13)
Oracle(32)
iBATIS(1)
junit(3)
J2ME(1)
SWT(2)
webservice(17)
quartz(6)
J2EE(11)
J2SE(80)
面试题集(2)
问题集锦(1)
其他(8)
ANT(3)
jboss(1)
hibernate(56)
spring(33)
struts(23)
2011年(10)
2010年(67)
2009年(155)
2008年(386)
时间看来
梦醒潇湘
妤傛ê鍋
a8050366
abin1703
cynthia
Bsolar
1234fifa
dgsucces
belaxx
ousky
xutlong
分类: Java
2010-05-20 16:29:20
import java.io.BufferedReader;import java.io.InputStreamReader;/* * Copyright xuanwu.com All right reserved. *//** * @author fisher * @version 1.0 2010-5-20 下午04:09:51 */public class JavaLinux { /** * 获取cpu使用情况 * @return * @throws Exception */ public double getCpuUsage() throws Exception { double cpuUsed = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 && strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; if (++m == 9) {// 第9列为CPU的使用百分比(RedHat cpuUsed += Double.parseDouble(tmp); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return cpuUsed; } /** * 内存监控 * @return * @throws Exception */ public double getMemUsage() throws Exception { double menUsed = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; if (str.indexOf(" R ") != -1) { // 只分析正在运行的进程,top进程本身除外 && System.out.println("------------------3-----------------"); strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; if (++m == 10) { // 9)--第10列为mem的使用百分比(RedHat 9) menUsed += Double.parseDouble(tmp); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return menUsed; } /** * 获取磁盘空间大小 * * @return * @throws Exception */ public double getDeskUsage() throws Exception { double totalHD = 0; double usedHD = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("df -hl /home");//df -hl 查看硬盘空间 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; ++m; System.out.println("----tmp----" + tmp); if (tmp.indexOf("G") != -1) { if (m == 2) { System.out.println("---G----" + tmp); if (!tmp.equals("") && !tmp.equals("0")) totalHD += Double.parseDouble(tmp .substring(0, tmp.length() - 1)) * 1024; } if (m == 3) { System.out.println("---G----" + tmp); if (!tmp.equals("none") && !tmp.equals("0")) usedHD += Double.parseDouble(tmp.substring( 0, tmp.length() - 1)) * 1024; } } if (tmp.indexOf("M") != -1) { if (m == 2) { System.out.println("---M---" + tmp); if (!tmp.equals("") && !tmp.equals("0")) totalHD += Double.parseDouble(tmp .substring(0, tmp.length() - 1)); } if (m == 3) { System.out.println("---M---" + tmp); if (!tmp.equals("none") && !tmp.equals("0")) usedHD += Double.parseDouble(tmp.substring( 0, tmp.length() - 1)); System.out.println("----3----" + usedHD); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return (usedHD / totalHD) * 100; } public static void main(String[] args) throws Exception { JavaLinux cpu = new JavaLinux(); System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%"); System.out.println("---------------mem used:" + cpu.getMemUsage() + "%"); System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%"); System.out.println("------------jvm监控----------------------"); Runtime lRuntime = Runtime.getRuntime(); System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K"); System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K"); System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K"); System.out.println("---------------Available Processors :" + lRuntime.availableProcessors()); } }
上一篇:线程中的current thread not owner异常错误
下一篇:MySql中文乱码问题总结
登录 注册