免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

用java取得linux系統(tǒng)cpu、內(nèi)存的實(shí)時(shí)信息

 軟件團(tuán)隊(duì)頭目 2012-07-30
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.InetAddress;  
  7. import java.util.StringTokenizer;  
  8.   
  9. import org.apache.log4j.Logger;  
  10.   
  11. /** 
  12.  * 取得linux系統(tǒng)下的cpu、內(nèi)存信息 
  13.  *  
  14.  * <p> 
  15.  *  
  16.  * @author leo 
  17.  *         </p> 
  18.  * @date 2008 
  19.  */  
  20. public final class LinuxSystemTool implements Runnable{  
  21.     private Logger log = Logger.getLogger(LinuxSystemTool.class);  
  22.     private Config config=Config.getInstance();  
  23.     /** 
  24.      * get memory by used info 
  25.      *  
  26.      * @return int[] result 
  27.      *         result.length==4;int[0]=MemTotal;int[1]=MemFree;int[2]=SwapTotal;int[3]=SwapFree; 
  28.      * @throws IOException 
  29.      * @throws InterruptedException 
  30.      */  
  31.     public void run() {  
  32.         // TODO Auto-generated method stub  
  33.   
  34.         while (true) {  
  35.               
  36.             try {  
  37.                 exec();  
  38.                 Thread.sleep(config.getThreadTime());  
  39.             } catch (Exception e) {  
  40.                 // TODO Auto-generated catch block  
  41.                 log.error("Performance Monitoring error:"+e.getMessage());  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.     }  
  46.     public void exec() throws Exception {  
  47. //      ServerStatus ss = new ServerStatus();  
  48.   
  49.         InetAddress inet = InetAddress.getLocalHost();  
  50.         System.out.println("Performance Monitoring ip:"+inet.toString());  
  51.           
  52.         String ip=inet.toString().substring(inet.toString().indexOf("/")+1);  
  53.         log.info("Performance Monitoring ip:"+ip);  
  54.           
  55.         int[] memInfo = LinuxSystemTool.getMemInfo();  
  56.         System.out.println("MemTotal:" + memInfo[0]);  
  57.         System.out.println("MemFree:" + memInfo[1]);  
  58.           
  59.         SnmpUtil util=new SnmpUtil();  
  60.         util.setCPU(getCpuInfo());  
  61. //      util.setDISK(1);  
  62.         util.setMEM(memInfo[0]/memInfo[1]);  
  63.         util.setIP(ip);  
  64.   
  65.     }  
  66.     public static int[] getMemInfo() throws IOException, InterruptedException {  
  67.         File file = new File("/proc/meminfo");  
  68.         BufferedReader br = new BufferedReader(new InputStreamReader(  
  69.                 new FileInputStream(file)));  
  70.         int[] result = new int[4];  
  71.         String str = null;  
  72.         StringTokenizer token = null;  
  73.         while ((str = br.readLine()) != null) {  
  74.             token = new StringTokenizer(str);  
  75.             if (!token.hasMoreTokens())  
  76.                 continue;  
  77.   
  78.             str = token.nextToken();  
  79.             if (!token.hasMoreTokens())  
  80.                 continue;  
  81.   
  82.             if (str.equalsIgnoreCase("MemTotal:"))  
  83.                 result[0] = Integer.parseInt(token.nextToken());  
  84.             else if (str.equalsIgnoreCase("MemFree:"))  
  85.                 result[1] = Integer.parseInt(token.nextToken());  
  86.             else if (str.equalsIgnoreCase("SwapTotal:"))  
  87.                 result[2] = Integer.parseInt(token.nextToken());  
  88.             else if (str.equalsIgnoreCase("SwapFree:"))  
  89.                 result[3] = Integer.parseInt(token.nextToken());  
  90.         }  
  91.   
  92.         return result;  
  93.     }  
  94.   
  95.     /** 
  96.      * get memory by used info 
  97.      *  
  98.      * @return float efficiency 
  99.      * @throws IOException 
  100.      * @throws InterruptedException 
  101.      */  
  102.     public static float getCpuInfo() throws IOException, InterruptedException {  
  103.         File file = new File("/proc/stat");  
  104.         BufferedReader br = new BufferedReader(new InputStreamReader(  
  105.                 new FileInputStream(file)));  
  106.         StringTokenizer token = new StringTokenizer(br.readLine());  
  107.         token.nextToken();  
  108.         int user1 = Integer.parseInt(token.nextToken());  
  109.         int nice1 = Integer.parseInt(token.nextToken());  
  110.         int sys1 = Integer.parseInt(token.nextToken());  
  111.         int idle1 = Integer.parseInt(token.nextToken());  
  112.   
  113.         Thread.sleep(1000);  
  114.   
  115.         br = new BufferedReader(  
  116.                 new InputStreamReader(new FileInputStream(file)));  
  117.         token = new StringTokenizer(br.readLine());  
  118.         token.nextToken();  
  119.         int user2 = Integer.parseInt(token.nextToken());  
  120.         int nice2 = Integer.parseInt(token.nextToken());  
  121.         int sys2 = Integer.parseInt(token.nextToken());  
  122.         int idle2 = Integer.parseInt(token.nextToken());  
  123.   
  124.         return (float) ((user2 + sys2 + nice2) - (user1 + sys1 + nice1))  
  125.                 / (float) ((user2 + nice2 + sys2 + idle2) - (user1 + nice1  
  126.                         + sys1 + idle1));  
  127.     }  
  128.   
  129.     /** 
  130.      * 測試類 
  131.      *  
  132.      * <p> 
  133.      *  
  134.      * @author 
  135.      * </p> 
  136.      * @date 
  137.      */  
  138.   
  139.     public static void main(String[] args) throws Exception {  
  140.         int[] memInfo = LinuxSystemTool.getMemInfo();  
  141.         System.out.println("MemTotal:" + memInfo[0]);  
  142.         System.out.println("MemFree:" + memInfo[1]);  
  143.         System.out.println("SwapTotal:" + memInfo[2]);  
  144.         System.out.println("SwapFree:" + memInfo[3]);  
  145.   
  146.         System.out.println("CPU利用率:" + LinuxSystemTool.getCpuInfo());  
  147.     }  
  148. }  

 CPU

 

在文件"/proc/stat"里面就包含了CPU的信息。每一個(gè)CPU的每一tick用在什么地方都在這個(gè)文件里面記著。后面的數(shù)字含義分別是: user、nice、sys、idle、iowait。有些版本的kernel沒有iowait這一項(xiàng)。這些數(shù)值表示從開機(jī)到現(xiàn)在,CPU的每tick用在了哪里。例如:

cpu0 256279030 0 11832528 1637168262

就是cpu0從開機(jī)到現(xiàn)在有 256279030 tick用在了user消耗,11832528用在了sys消耗。所以如果想計(jì)算單位時(shí)間(例如1s)里面CPU的負(fù)載,那只需要計(jì)算1秒前后數(shù)值的差除以每一秒的tick數(shù)量就可以了。gkrellm就是這樣實(shí)現(xiàn)的:((200 * (v2 - v1) / CPU_TICKS_PER_SECOND) + 1) /2

例如,第一次讀取/proc/stat,user的值是256279030;一秒以后再讀一次,值是256289030,那么CPU在這一秒的user消耗就是:((200 * (256289030 - 256279030) / CPU_TICKS_PER_SECOND) + 1) /2 = ((10000 * 200 / 1000000) + 1) / 2 = 1%了。

 

2、內(nèi)存消耗

 

文件"/proc/meminfo"里面包含的就是內(nèi)存的信息,還包括了swap的信息。例如:

 

 

$ cat /proc/meminfo

total:    used:    free: shared: buffers: cached:
Mem: 1057009664 851668992 205340672        0 67616768 367820800
Swap: 2146787328 164429824 1982357504
MemTotal:      1032236 kB
MemFree:        200528 kB
MemShared:           0 kB
……

不過從gkrellm的源代碼看,有些版本沒有前面那兩行統(tǒng)計(jì)的信息,只能夠根據(jù)下面的Key: Value這種各式的數(shù)據(jù)收集。

 

3、磁盤空間

 

從gkrellm的源代碼看,這個(gè)是一個(gè)很復(fù)雜的數(shù)據(jù)。磁盤分區(qū)的數(shù)據(jù)有可能分布在:/proc/mounts、/proc/diskstats、 /proc/partitions等等。而且如果想要檢查某幾個(gè)特定的路徑,還需要通過mount、df等命令的幫助。為了減少麻煩,這個(gè)數(shù)據(jù)我就直接用 statfs函數(shù)直接獲得了。

 

int statfs(const char *path, struct statfs *buf);

這個(gè)函數(shù)只需要輸入需要檢查的路徑名稱,就可以返回這個(gè)路徑所在的分區(qū)的空間使用情況:

總空間:buf.f_bsize * buf.f_blocks

空余空間:buf.f_bsize * buf.f_bavail

 

4、磁盤I/O

 

磁盤I/O的數(shù)據(jù)也同樣比較復(fù)雜,有些版本看/proc/diskstats,有些版本看/proc/partitions,還有些版本至今我也不知道在那里看……不過可以看到數(shù)據(jù)的版本也像CPU那樣,需要隔一段時(shí)間取值,兩次取值的差就是流量。

 

5、網(wǎng)絡(luò)流量

 

網(wǎng)絡(luò)流量也是五花八門,不過基本上都可以在/proc/net/dev里面獲得。同樣也是需要兩次取值取其差作為流量值。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多