1、CPUhtml
使用proc文件系統,"proc文件系統是一個僞文件系統,它只存在內存當中,而不佔用外存空間。它以文件系統的方式爲訪問系統內核數據的操做提供接口。用戶和應用程序能夠經過proc獲得系統的信息,並能夠改變內核的某些參數。"java
從/proc文件系統獲取cpu使用狀況: cat /proc/statlinux
在Linux的內核中,有一個全 局變量:Jiffies。 Jiffies表明時間。它的單位隨硬件平臺的不一樣而不一樣。系統裏定義了一個常數HZ,表明每秒種最小時間間隔的數目。這樣jiffies的單位就是 1/HZ。Intel平臺jiffies的單位是1/100秒,這就是系統所能分辨的最小時間間隔了。每一個CPU時間片,Jiffies都要加1。 CPU的利用率就是用執行用戶態+系統態的Jiffies除以總的Jifffies來表示。ios
[root@localhost LoadBalanceAlg]# cat /proc/stat
cpu 71095 55513 76751 2545622893 303185 4160 47722 0
cpu0 3855 1134 4284 159122519 3882 0 717 0
cpu1 4236 770 5837 159113370 11291 6 865 0
cpu2 4934 1142 5048 158991321 130622 362 2939 0
cpu3 2320 14774 5177 159111528 1417 8 1138 0
cpu4 2694 405 3086 159071174 56284 235 2477 0
cpu5 1701 886 2560 159129034 1316 2 849 0
cpu6 2937 450 2863 159068480 59183 228 2198 0
cpu7 916 316 2426 159130057 1682 1 933 0
cpu8 2543 50 3509 159122844 4467 1 2911 0
cpu9 4761 827 6296 159118849 4490 8 1086 0
cpu10 8517 4236 9148 159102063 9791 173 2382 0
cpu11 22001 29737 14602 159065992 2583 6 1382 0
cpu12 3453 150 3075 159113794 5387 1162 9276 0
cpu13 2120 424 3403 159126526 2608 7 1199 0
cpu14 2637 65 2663 159107796 6704 1914 14503 0
cpu15 1462 142 2763 159127539 1470 39 2859 0
intr 1636622296 1591605869 4 0 4 4 0 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1005479 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32763528 0 0 0 0 0 0 0 1697776 0 0 0 0 0 0 0 1556158 2 0 0 0 0 0 0 1598011 0 0 0 0 0 0 0 1287622 0 0 0 0 0 0 0 1522517 0 0 0 0 0 0 0 2467360 0 0 0 0 0 0 0 1116999 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 431778894
btime 1363058934
processes 279394
procs_running 1
procs_blocked 0shell
public class Runtimeextends Object
每一個 Java 應用程序都有一個 Runtime
類實例,使應用程序可以與其運行的環境相鏈接。能夠經過getRuntime
方法獲取當前運行時。apache
應用程序不能建立本身的 Runtime 類實例。 網絡
public abstract class Processextends Object
ProcessBuilder.start()
和Runtime.exec
方法建立一個本機進程,並返回 Process
子類的一個實例,該實例可用來控制進程並得到相關信息。app
<span style="font-size:14px;">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import org.apache.log4j.Logger; /** * 採集CPU使用率 */ public class CpuUsage extends ResourceUsage { private static Logger log = Logger.getLogger(CpuUsage.class); private static CpuUsage INSTANCE = new CpuUsage(); private CpuUsage(){ } public static CpuUsage getInstance(){ return INSTANCE; } /** * Purpose:採集CPU使用率 * @param args * @return float,CPU使用率,小於1 */ @Override public float get() { log.info("開始收集cpu使用率"); float cpuUsage = 0; Process pro1,pro2; Runtime r = Runtime.getRuntime(); try { String command = "cat /proc/stat"; //第一次採集CPU時間 long startTime = System.currentTimeMillis(); pro1 = r.exec(command); BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream())); String line = null; long idleCpuTime1 = 0, totalCpuTime1 = 0; //分別爲系統啓動後空閒的CPU時間和總的CPU時間 while((line=in1.readLine()) != null){ if(line.startsWith("cpu")){ line = line.trim(); log.info(line); String[] temp = line.split("\\s+"); idleCpuTime1 = Long.parseLong(temp[4]); for(String s : temp){ if(!s.equals("cpu")){ totalCpuTime1 += Long.parseLong(s); } } log.info("IdleCpuTime: " + idleCpuTime1 + ", " + "TotalCpuTime" + totalCpuTime1); break; } } in1.close(); pro1.destroy(); try { Thread.sleep(100); } catch (InterruptedException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("CpuUsage休眠時發生InterruptedException. " + e.getMessage()); log.error(sw.toString()); } //第二次採集CPU時間 long endTime = System.currentTimeMillis(); pro2 = r.exec(command); BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream())); long idleCpuTime2 = 0, totalCpuTime2 = 0; //分別爲系統啓動後空閒的CPU時間和總的CPU時間 while((line=in2.readLine()) != null){ if(line.startsWith("cpu")){ line = line.trim(); log.info(line); String[] temp = line.split("\\s+"); idleCpuTime2 = Long.parseLong(temp[4]); for(String s : temp){ if(!s.equals("cpu")){ totalCpuTime2 += Long.parseLong(s); } } log.info("IdleCpuTime: " + idleCpuTime2 + ", " + "TotalCpuTime" + totalCpuTime2); break; } } if(idleCpuTime1 != 0 && totalCpuTime1 !=0 && idleCpuTime2 != 0 && totalCpuTime2 !=0){ cpuUsage = 1 - (float)(idleCpuTime2 - idleCpuTime1)/(float)(totalCpuTime2 - totalCpuTime1); log.info("本節點CPU使用率爲: " + cpuUsage); } in2.close(); pro2.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("CpuUsage發生InstantiationException. " + e.getMessage()); log.error(sw.toString()); } return cpuUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(CpuUsage.getInstance().get()); Thread.sleep(5000); } } }</span>
<span style="font-size:14px;">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import org.apache.log4j.Logger; /** * 採集內存使用率 */ public class MemUsage extends ResourceUsage{ private static Logger log = Logger.getLogger(MemUsage.class); private static MemUsage INSTANCE = new MemUsage(); private MemUsage(){ } public static MemUsage getInstance(){ return INSTANCE; } /** * Purpose:採集內存使用率 * @param args * @return float,內存使用率,小於1 */ @Override public float get() { log.info("開始收集memory使用率"); float memUsage = 0.0f; Process pro = null; Runtime r = Runtime.getRuntime(); try { String command = "cat /proc/meminfo"; pro = r.exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream())); String line = null; int count = 0; long totalMem = 0, freeMem = 0; while((line=in.readLine()) != null){ log.info(line); String[] memInfo = line.split("\\s+"); if(memInfo[0].startsWith("MemTotal")){ totalMem = Long.parseLong(memInfo[1]); } if(memInfo[0].startsWith("MemFree")){ freeMem = Long.parseLong(memInfo[1]); } memUsage = 1- (float)freeMem/(float)totalMem; log.info("本節點內存使用率爲: " + memUsage); if(++count == 2){ break; } } in.close(); pro.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("MemUsage發生InstantiationException. " + e.getMessage()); log.error(sw.toString()); } return memUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(MemUsage.getInstance().get()); Thread.sleep(5000); } } }</span>
<span style="font-size:14px;">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import org.apache.log4j.Logger; /** * 採集磁盤IO使用率 */ public class IoUsage extends ResourceUsage{ private static Logger log = Logger.getLogger(IoUsage.class); private static IoUsage INSTANCE = new IoUsage(); private IoUsage(){ } public static IoUsage getInstance(){ return INSTANCE; } /** * @Purpose:採集磁盤IO使用率 * @param args * @return float,磁盤IO使用率,小於1 */ @Override public float get() { log.info("開始收集磁盤IO使用率"); float ioUsage = 0.0f; Process pro = null; Runtime r = Runtime.getRuntime(); try { String command = "iostat -d -x"; pro = r.exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream())); String line = null; int count = 0; while((line=in.readLine()) != null){ if(++count >= 4){ // log.info(line); String[] temp = line.split("\\s+"); if(temp.length > 1){ float util = Float.parseFloat(temp[temp.length-1]); ioUsage = (ioUsage>util)?ioUsage:util; } } } if(ioUsage > 0){ log.info("本節點磁盤IO使用率爲: " + ioUsage); ioUsage /= 100; } in.close(); pro.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("IoUsage發生InstantiationException. " + e.getMessage()); log.error(sw.toString()); } return ioUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(IoUsage.getInstance().get()); Thread.sleep(5000); } } }</span>
<span style="font-size:14px;">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import org.apache.log4j.Logger; /** * 採集網絡帶寬使用率 */ public class NetUsage extends ResourceUsage { private static Logger log = Logger.getLogger(NetUsage.class); private static NetUsage INSTANCE = new NetUsage(); private final static float TotalBandwidth = 1000; //網口帶寬,Mbps private NetUsage(){ } public static NetUsage getInstance(){ return INSTANCE; } /** * @Purpose:採集網絡帶寬使用率 * @param args * @return float,網絡帶寬使用率,小於1 */ @Override public float get() { log.info("開始收集網絡帶寬使用率"); float netUsage = 0.0f; Process pro1,pro2; Runtime r = Runtime.getRuntime(); try { String command = "cat /proc/net/dev"; //第一次採集流量數據 long startTime = System.currentTimeMillis(); pro1 = r.exec(command); BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream())); String line = null; long inSize1 = 0, outSize1 = 0; while((line=in1.readLine()) != null){ line = line.trim(); if(line.startsWith("eth0")){ log.info(line); String[] temp = line.split("\\s+"); inSize1 = Long.parseLong(temp[0].substring(5)); //Receive bytes,單位爲Byte outSize1 = Long.parseLong(temp[8]); //Transmit bytes,單位爲Byte break; } } in1.close(); pro1.destroy(); try { Thread.sleep(1000); } catch (InterruptedException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("NetUsage休眠時發生InterruptedException. " + e.getMessage()); log.error(sw.toString()); } //第二次採集流量數據 long endTime = System.currentTimeMillis(); pro2 = r.exec(command); BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream())); long inSize2 = 0 ,outSize2 = 0; while((line=in2.readLine()) != null){ line = line.trim(); if(line.startsWith("eth0")){ log.info(line); String[] temp = line.split("\\s+"); inSize2 = Long.parseLong(temp[0].substring(5)); outSize2 = Long.parseLong(temp[8]); break; } } if(inSize1 != 0 && outSize1 !=0 && inSize2 != 0 && outSize2 !=0){ float interval = (float)(endTime - startTime)/1000; //網口傳輸速度,單位爲bps float curRate = (float)(inSize2 - inSize1 + outSize2 - outSize1)*8/(1000000*interval); netUsage = curRate/TotalBandwidth; log.info("本節點網口速度爲: " + curRate + "Mbps"); log.info("本節點網絡帶寬使用率爲: " + netUsage); } in2.close(); pro2.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); log.error("NetUsage發生InstantiationException. " + e.getMessage()); log.error(sw.toString()); } return netUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(NetUsage.getInstance().get()); Thread.sleep(5000); } } }</span>