java 对LINUX 进行带宽使用率监控

因为项目需要使用java对视频服务器进行带宽使用率监控,故找相关代码来研究以及使用,系统为centos7.2

借鉴了 https://blog.csdn.net/blue_jjw/article/details/8741000 该文章的代码,进行了修改

首先使用 ifconfig 命令看看自己有几块网卡

ifconfig

然后使用

sudo ethtool eth0

这里写图片描述
找到自己的带宽是多少M
后续代码需要用到该参数

接下来你需要使用下面的指令获得网卡当前的速率

cat /proc/net/dev

结果见结构图
这里写图片描述
过会你需要对照自己的结构图修改相关数据的数组下标,见代码里的注释

然后就是java代码

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.io.PrintWriter;  
import java.io.StringWriter;

/** 
 * 采集网络带宽使用率 
 */  
public class NetUsage{  


    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 
     */  
    public float get() {  
        System.out.println("开始收集带宽率");
        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")){  

                    System.out.println(line);
                    String[] temp = line.split("\\s+");
                    System.out.println("temp: "+temp.length+"temp[0]="+temp[0]);
                    //这里可能因为不同操作系统 展示的结果不同,导致下标不同,
                    //自己对照 cat /proc/net/dev 该指令执行后展示出的结构 找到Receive bytes 是数组里第几个元素,替换下标即可
                    inSize1 = Long.parseLong(temp[1].substring(5)); //Receive bytes,单位为Byte  
                    outSize1 = Long.parseLong(temp[9]);             //Transmit bytes,单位为Byte  
                    break;  
                }                 
            }     
            in1.close();  
            pro1.destroy();  
            try {  
                Thread.sleep(500);  
            } catch (InterruptedException e) {  
                StringWriter sw = new StringWriter();  
                e.printStackTrace(new PrintWriter(sw));  
                System.out.println("NetUsage休眠时发生InterruptedException. " + e.getMessage());  
                System.out.println(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")){  

                    System.out.println(line);
                    String[] temp = line.split("\\s+");
                    //这里数组下标也需要修改 
                    inSize2 = Long.parseLong(temp[1].substring(5));  
                    outSize2 = Long.parseLong(temp[9]);  
                    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;  
                System.out.println("本节点网口速度为: " + curRate + "Mbps");
                System.out.println("本节点网络带宽使用率为: " + netUsage);

            }                 
            in2.close();  
            pro2.destroy();  
        } catch (IOException e) {  
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw));  
            System.out.println("NetUsage发生InstantiationException. " + e.getMessage());
            System.out.println(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);  
//        }  
    }  
}

自己运行后没什么问题
运行结果为
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013781343/article/details/80421789