zoukankan      html  css  js  c++  java
  • Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集

    实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器.

    web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机上当初这个方案被否决了,只好专心写自己的socket.

    系统的cpu信息的获得可以用cat /proc/cpuinfo

    下面是我截取的命令执行部分结果

    这里面有几个非常重要的参数   

    • processor     逻辑处理器的id。
    • physical id    物理封装的处理器的id。
    • core id           每个核心的id。
    • cpu cores     位于相同物理封装的处理器中的内核数量。
    • siblings         位于相同物理封装的处理器中的逻辑处理器的数量

    系统内存的使用率我用的是free命令

    总内存 已用内存 和剩余内存都可以得到 使用率的获得也就是用这几个参数做一些计算

    系统的磁盘Io我用的是iostat -x -k 1 2  这里取得是第二次刷新的数据  原因是第一次的数据是从系统开机到现在的累计值 不是需求要得到的 这里截取命令的截取结果

    各个参数的含义这里就不详细解释了  在网上都解释的很详细

    网卡信息的采集 我用的是lspci

    Ethernet controller 既是系统的网卡

    系统的运行时间 用到的命令是cat /proc/uptime 

    第一个参数1188788.89既是系统运行了多少S

    采集的命令 都知道了  接下来就是用java程序采集了  不多说  贴上自己写的部分代码 由于要用socket传给客户端 所以对字符都有一些分隔符处理 方便客户端对字符串解析

      1     public static String getmachineinfo() throws IOException {
      2         StringBuffer Machineinfo = new StringBuffer();
      3         StringBuffer Machineio = new StringBuffer();
      4         StringBuffer MachineUptime = new StringBuffer();
      5         StringBuffer MachineCpu = new StringBuffer();
      6         StringBuffer MachineNetworkcard = new StringBuffer();
      7         StringBuffer MachineMemory = new StringBuffer();
      8         StringBuffer MachineHostname = new StringBuffer();
      9         double Machinerunningvm=0;
     10         String[] cmd = new String[] { "iostat", "-x", "-k", "1", "2" };
     11         Process process = Runtime.getRuntime().exec(cmd);
     12         int count = 0;// 计数
     13         double ioreadspeedsum = 0;// 统计实体机每个磁盘io的和
     14         double iowritespeedsum = 0;
     15         double ioreadnumsum = 0;
     16         double iowritenumsum = 0;
     17         LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
     18         String line;
     19         while ((line = br.readLine()) != null) {
     20             String[] words = line.split("\\s+");
     21             if (words.length > 3) {
     22                 if (words[0].equals("Device:"))// 用来判断取第二次刷新的数据
     23                     count += 1;
     24                 if (words[0].contains("sd") && count == 2)// 取第二次刷新的数据
     25                                                             // 获得Io信息并取得累加值
     26                 {
     27                     ioreadnumsum += Double.valueOf(words[3]);
     28                     iowritenumsum += Double.valueOf(words[4]);
     29                     ioreadspeedsum += Double.valueOf(words[5]);
     30                     iowritespeedsum += Double.valueOf(words[6]);
     31                 }
     32             }
     33         }
     34         Machineio.append(ioreadnumsum + " " + iowritenumsum + " " + ioreadspeedsum + " " + iowritespeedsum + " ");
     35         cmd = new String[] { "cat", "/proc/uptime" };
     36         process = Runtime.getRuntime().exec(cmd);
     37         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
     38         if ((line = br.readLine()) != null) {
     39             String[] words = line.split("\\s+");
     40             MachineUptime.append(words[0] + " ");
     41         }
     42         ArrayList<String> cpuidjudge = new ArrayList<String>();
     43         String cpuid = new String();
     44         String cpumodelname = new String();
     45         String cpucore = new String();
     46         String cpusiblings = new String();
     47         cmd = new String[] { "cat", "/proc/cpuinfo" };
     48         process = Runtime.getRuntime().exec(cmd);
     49         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
     50         while ((line = br.readLine()) != null) {
     51             String[] words = line.split("\\s+");
     52             if (words.length > 1) {
     53                 if (words[0].equals("physical"))// 用来判断Physical id
     54                 {
     55                     cpuid = words[3];
     56                 }
     57                 if (words[0].equals("model") && words[1].equals("name"))// 用来判断cpu
     58                                                                         // model
     59                                                                         // name
     60                 {
     61                     cpumodelname = line.toString().replaceAll("model name    :", "").replaceAll("\\s+", " ");
     62                 }
     63                 if (words[0].equals("siblings"))// 用来判断siblings
     64                 {
     65                     cpusiblings = words[2];
     66                 }
     67                 if (words[0].equals("cpu") && words[1].equals("cores"))// 用来判断cpu
     68                                                                         // cores
     69                 {
     70                     cpucore = words[3];
     71                     if (!(cpuidjudge.contains(cpuid))) {
     72                         cpuidjudge.add(cpuid);
     73                         MachineCpu.append(cpuid + "-" + cpumodelname + "-" + cpusiblings + "-" + cpucore + "|");// 加的横杠和竖杠是为了客户端接收后对字符的分解处理
     74                     }
     75                 }
     76             }
     77 
     78         }
     79         cmd = new String[] { "lspci" };
     80         process = Runtime.getRuntime().exec(cmd);
     81         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
     82         while ((line = br.readLine()) != null) {
     83             String[] words = line.split("\\s+");
     84             if (words[1].equals("Ethernet") && words[2].contains("controller")) {// 用来判断网关
     85                 for (int i = 3; i < words.length; i++) {
     86                     MachineNetworkcard.append(words[i] + " ");
     87                 }
     88                 MachineNetworkcard.append("-");
     89             }
     90         }
     91         cmd = new String[] { "free" };
     92         process = Runtime.getRuntime().exec(cmd);
     93         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
     94         while ((line = br.readLine()) != null) {
     95             String[] words = line.split("\\s+");
     96             if (words[0].equals("Mem:"))// 用来判断内存
     97             {
     98                 MachineMemory.append(words[1] + " " + words[2] + " " + words[3]);
     99             }
    100         }
    101         cmd = new String[] { "hostname" };
    102         process = Runtime.getRuntime().exec(cmd);
    103         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
    104         MachineHostname.append(br.readLine());
    105         System.out.println(MachineHostname.toString());
    106         cmd = new String[] { "virsh","list"};
    107         process = Runtime.getRuntime().exec(cmd);
    108         br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
    109         while ((line = br.readLine()) != null) {
    110             String[] words = line.split("\\s+");
    111             if (words.length>2&&words[3].contains("running"))// 用来判断
    112             {
    113                 Machinerunningvm+=1;
    114             }
    115         }        
    116         Machineinfo.append(Machineio + "\n" + MachineUptime + "\n" + MachineCpu + "\n" + MachineNetworkcard+"\n"+MachineMemory+"\n"+MachineHostname+"\n"+Machinerunningvm);
    117         return Machineinfo.toString();
    118     }

    暂时先写这么多吧 

  • 相关阅读:
    java锁类型
    Java线程池
    1 Navicat12 过期了怎么办
    N1-二维数组查找
    SQL-59 按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推。
    SQL-58 获取有奖金的员工相关信息。
    SQL-57 使用含有关键字exists查找未分配具体部门的员工的所有信息。
    SQL-56 获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和recevied,没有分配具体的员工不显示
    SQL-55 分页查询employees表,每5行一页,返回第2页的数据
    SQL-54 查找排除当前最大、最小salary之后的员工的平均工资avg_salary。
  • 原文地址:https://www.cnblogs.com/Mrjie/p/5803360.html
Copyright © 2011-2022 走看看