linux性能收集

这个是我写的系统采集类实现了CPU个数,CPU频率,内存信息,磁盘信息,CPU利用率,内存利用率,磁盘利用率,系统启动时间的获得
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.fenghuo.jsnmp.logic.task;

  6. import java.io.*;
  7. import java.util.StringTokenizer;

  8. /**
  9.  * 用于执行linux命令 
  10.  * @author huangxf
  11.  *
  12.  */
  13. public class LinuxExec {

  14.     /**
  15.      * 获取cpu使用情况
  16.      * @return
  17.      * @throws Exception
  18.      */
  19.     public double getCpuUsage() throws Exception {
  20.         double cpuUsed = 0;

  21.         Runtime rt = Runtime.getRuntime();
  22.         Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令

  23.         BufferedReader in = null;
  24.         try {
  25.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  26.             String str = null;
  27.             String[] strArray = null;

  28.             while ((str = in.readLine()) != null) {
  29.                 int m = 0;

  30.                 if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&

  31.                     strArray = str.split(" ");
  32.                     for (String tmp : strArray) {
  33.                         if (tmp.trim().length() == 0) {
  34.                             continue;
  35.                         }
  36.                         if (++m == 9) {// 第9列为CPU的使用百分比(RedHat

  37.                             cpuUsed += Double.parseDouble(tmp);
  38.                         }

  39.                     }

  40.                 }
  41.             }
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         } finally {
  45.             in.close();
  46.         }
  47.         return cpuUsed;
  48.     }

  49.     /**
  50.      * 内存监控
  51.      * @return
  52.      * @throws Exception
  53.      */
  54.     public double getMemUsage() throws Exception {

  55.         double menUsed = 0;
  56.         Runtime rt = Runtime.getRuntime();
  57.         Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令

  58.         BufferedReader in = null;
  59.         try {
  60.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  61.             String str = null;
  62.             String[] strArray = null;

  63.             while ((str = in.readLine()) != null) {
  64.                 int m = 0;

  65.                 if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
  66.                     // 
  67.                     // System.out.println("------------------3-----------------");

  68.                     strArray = str.split(" ");
  69.                     for (String tmp : strArray) {
  70.                         if (tmp.trim().length() == 0) {
  71.                             continue;
  72.                         }
  73.                         if (++m == 10) {
  74.                             // 9)--第10列为mem的使用百分比(RedHat 9)

  75.                             menUsed += Double.parseDouble(tmp);

  76.                         }
  77.                     }

  78.                 }
  79.             }
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         } finally {
  83.             in.close();

  84.         }
  85.         return menUsed;
  86.     }

  87.     /**
  88.      * 获取磁盘空间大小
  89.      * 
  90.      * @return
  91.      * @throws Exception
  92.      */
  93.     public double getDeskUsage() throws Exception {
  94.         double totalHD = 0;
  95.         double usedHD = 0;
  96.         Runtime rt = Runtime.getRuntime();
  97.         Process p = rt.exec("df -hl");//df -hl 查看硬盘空间

  98.         BufferedReader in = null;
  99.         try {
  100.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  101.             String str = null;
  102.             String[] strArray = null;
  103.             while ((str = in.readLine()) != null) {
  104.                 int m = 0;
  105.                 strArray = str.split(" ");
  106.                 for (String tmp : strArray) {
  107.                     if (tmp.trim().length() == 0) {
  108.                         continue;
  109.                     }
  110.                     ++m;
  111. //          System.out.println("----tmp----" + tmp);
  112.                     if (tmp.indexOf("G") != -1) {
  113.                         if (m == 2) {
  114. //                          System.out.println("---G----" + tmp);
  115.                             if (!tmp.equals("") && !tmp.equals("0")) {
  116.                                 totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
  117.                             }
  118.                         }
  119.                         if (m == 3) {
  120. //                          System.out.println("---G----" + tmp);
  121.                             if (!tmp.equals("none") && !tmp.equals("0")) {
  122.                                 usedHD += Double.parseDouble(tmp.substring(
  123.                                         0, tmp.length() - 1)) * 1024;
  124.                             }
  125.                         }
  126.                     }
  127.                     if (tmp.indexOf("M") != -1) {
  128.                         if (m == 2) {
  129. //                          System.out.println("---M---" + tmp);
  130.                             if (!tmp.equals("") && !tmp.equals("0")) {
  131.                                 totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
  132.                             }
  133.                         }
  134.                         if (m == 3) {
  135. //                          System.out.println("---M---" + tmp);
  136.                             if (!tmp.equals("none") && !tmp.equals("0")) {
  137.                                 usedHD += Double.parseDouble(tmp.substring(
  138.                                         0, tmp.length() - 1));
  139.                             // System.out.println("----3----" + usedHD);
  140.                             }
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.         } catch (Exception e) {
  146.             e.printStackTrace();
  147.         } finally {
  148.             in.close();
  149.         }
  150.         return (usedHD / totalHD) * 100;
  151.     }

  152.     /**
  153.      * 获得CPU的个数
  154.      * @return
  155.      * @throws java.lang.Exception
  156.      */
  157.     public int getIntCpuNum() throws Exception {
  158.         int rs = 0;
  159.         Runtime rt = Runtime.getRuntime();
  160. //        Process p = rt.exec("cat /proc/cpuinfo | grep processor | wc -l");//获得CPU个数
  161.         Process p = rt.exec("cat /proc/cpuinfo");//获得CPU个数

  162.         BufferedReader in = null;
  163.         try {
  164.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  165.             String str = null;
  166.             String[] strarr = null;
  167.             while ((str = in.readLine()) != null) {
  168.                 strarr = str.split(":");
  169.                 if (strarr[0].trim().equals("processor")) {
  170.                     rs = Integer.valueOf(strarr[1].trim())+1;
  171.                 }
  172.             }
  173. //            String str = in.readLine();
  174. //            System.out.println("============================================");
  175. //            System.out.println(str);
  176. //            str = str.trim();
  177. //            System.out.println(str);
  178. //            System.out.println("============================================");
  179. //            rs = Integer.valueOf(str);
  180.         } catch (Exception e) {
  181.             e.printStackTrace();
  182.         } finally {
  183.             in.close();
  184.         }
  185.         return rs;
  186.     }

  187.     /**
  188.      * 获得内存的大小
  189.      * @return
  190.      * @throws java.lang.Exception
  191.      */
  192.     public int getIntMemMax() throws Exception {
  193.         int rs = 0;
  194.         Runtime rt = Runtime.getRuntime();
  195. //        Process p = rt.exec("cat /proc/meminfo|grep -i /"^memtotal/"|cut -d/":/" -f2|cut -d/"k/" -f1");//获得内存大小
  196.         Process p = rt.exec("cat /proc/meminfo");//获得内存大小

  197.         BufferedReader in = null;
  198.         try {
  199.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  200.             int[] result = new int[4];
  201.             String str = null;
  202.             StringTokenizer token = null;
  203.             while ((str = in.readLine()) != null) {
  204.                 token = new StringTokenizer(str);
  205.                 if (!token.hasMoreTokens()) {
  206.                     continue;
  207.                 }
  208.                 str = token.nextToken();
  209.                 if (!token.hasMoreTokens()) {
  210.                     continue;
  211.                 }
  212.                 if (str.equalsIgnoreCase("MemTotal:")) {
  213.                     result[0] = Integer.parseInt(token.nextToken());
  214.                     rs = result[0];
  215.                 } else if (str.equalsIgnoreCase("MemFree:")) {
  216.                     result[1] = Integer.parseInt(token.nextToken());
  217.                 } else if (str.equalsIgnoreCase("SwapTotal:")) {
  218.                     result[2] = Integer.parseInt(token.nextToken());
  219.                 } else if (str.equalsIgnoreCase("SwapFree:")) {
  220.                     result[3] = Integer.parseInt(token.nextToken());
  221.                 }
  222.             }
  223.         } catch (Exception e) {
  224.             e.printStackTrace();
  225.         } finally {
  226.             in.close();
  227.         }
  228.         return rs / 1000;
  229.     }

  230.     /**
  231.      * 获得的CPUHMZ
  232.      * @return
  233.      * @throws java.lang.Exception
  234.      */
  235.     public int getIntCpuHMZ() throws Exception {
  236.         int cpuhmz = 0;
  237.         Runtime rt = Runtime.getRuntime();
  238.         Process p = rt.exec("cat /proc/cpuinfo ");//读CPU信息文件

  239.         BufferedReader in = null;
  240.         try {
  241.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  242.             String str = null;
  243.             String[] strarr = null;
  244.             while ((str = in.readLine()) != null) {
  245.                 strarr = str.split(":");
  246.                 if (strarr.length > 0) {
  247.                     if (strarr[0].trim().equals("cpu MHz")) {
  248.                         cpuhmz = Double.valueOf(strarr[1].trim()).intValue();
  249.                     }
  250.                 }
  251.             }
  252.         } catch (Exception e) {
  253.             e.printStackTrace();
  254.         } finally {
  255.             in.close();
  256.         }
  257.         return cpuhmz;
  258.     }

  259.     /**
  260.      * 获取磁盘空间大小(总)
  261.      * 
  262.      * @return
  263.      * @throws Exception
  264.      */
  265.     public int getDeskAll() throws Exception {
  266.         double totalHD = 0;
  267.         Runtime rt = Runtime.getRuntime();
  268.         Process p = rt.exec("df -hl");//df -hl 查看硬盘空间

  269.         BufferedReader in = null;
  270.         try {
  271.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  272.             String str = null;
  273.             String[] strArray = null;
  274.             while ((str = in.readLine()) != null) {
  275.                 int m = 0;
  276.                 strArray = str.split(" ");
  277.                 for (String tmp : strArray) {
  278.                     if (tmp.trim().length() == 0) {
  279.                         continue;
  280.                     }
  281.                     ++m;
  282. //          System.out.println("----tmp----" + tmp);
  283.                     if (tmp.indexOf("G") != -1) {
  284.                         if (m == 2) {
  285. //              System.out.println("---G----" + tmp);
  286.                             if (!tmp.equals("") && !tmp.equals("0")) {
  287.                                 totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
  288.                             }
  289.                         }
  290.                         if (m == 3) {
  291. //              System.out.println("---G----" + tmp);
  292.                         }
  293.                     }
  294.                     if (tmp.indexOf("M") != -1) {
  295.                         if (m == 2) {
  296. //              System.out.println("---M---" + tmp);
  297.                             if (!tmp.equals("") && !tmp.equals("0")) {
  298.                                 totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
  299.                             }
  300.                         }
  301.                         if (m == 3) {
  302. //              System.out.println("---M---" + tmp);
  303.                         }
  304.                     }

  305.                 }

  306.             }
  307.         } catch (Exception e) {
  308.             e.printStackTrace();
  309.         } finally {
  310.             in.close();
  311.         }
  312.         Double d = new Double(totalHD);
  313.         return d.intValue();
  314.     }

  315.     /**
  316.      * 查询系统时间
  317.      * @return
  318.      * @throws java.io.IOException
  319.      */
  320.     public long getSysStartTime() throws IOException, InterruptedException {
  321.         long statime = 0;
  322.         Runtime rt = Runtime.getRuntime();
  323.         Process p = rt.exec("uptime");//查看系统启动时间

  324. //        System.out.println(p.exitValue());
  325. //        p.waitFor();
  326.         BufferedReader in = null;
  327.         try {
  328.             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  329.             String str = in.readLine();
  330.             String[] strs = str.split(",");
  331.             String[] rss = strs[1].trim().split(" ");
  332.             if (rss.length == 1) {
  333.                 String[] times = rss[0].split(":");
  334.                 int hour = Integer.valueOf(times[0]);
  335.                 int min = Integer.valueOf(times[1]);
  336.                 statime = (hour * 60 + min) * 60 * 1000;
  337.             } else if (rss.length == 2) {
  338.                 int min = Integer.valueOf(rss[0]);
  339.                 statime = min * 60 * 1000;
  340.             }
  341.         } catch (Exception e) {
  342.             e.printStackTrace();
  343.         } finally {
  344.             in.close();
  345.         }
  346.         return statime;
  347.     }

  348.     public void shutdownh() throws IOException {
  349.         Runtime rt = Runtime.getRuntime();
  350.         Process p = rt.exec("shutdown -h");
  351.     }

  352.     public void shutdownr() throws IOException {
  353.         Runtime rt = Runtime.getRuntime();
  354.         Process p = rt.exec("shutdown -r");
  355.     }

  356.     public static void main(String[] args) throws Exception {
  357.         LinuxExec cpu = new LinuxExec();
  358.         System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
  359.         System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
  360.         System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
  361.         System.out.println("---------------CPU NUM:" + cpu.getIntCpuNum());
  362.         System.out.println("---------------CPU HMZ:" + cpu.getIntCpuHMZ());
  363.         System.out.println("---------------MEN MAX:" + cpu.getIntMemMax());
  364.         System.out.println("---------------DISK MAX:" + cpu.getDeskAll());
  365.         System.out.println("---------------STARTTIME:" + cpu.getSysStartTime());
  366.         System.out.println("------------jvm监控----------------------");
  367.         Runtime lRuntime = Runtime.getRuntime();
  368.         System.out.println("--------------Free Momery:" + lRuntime.freeMemory() + "K");
  369.         System.out.println("--------------Max Momery:" + lRuntime.maxMemory() + "K");
  370.         System.out.println("--------------Total Momery:" + lRuntime.totalMemory() + "K");
  371.         System.out.println("---------------Available Processors :" + lRuntime.availableProcessors());
  372.     }
  373. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值