首页 > 技术文章 > sigar监控

cczhoufeng 2014-01-15 10:10 原文

相关参照博客:

http://liningjustsoso.iteye.com/blog/1254584

http://blog.csdn.net/aoxida/article/category/1259086

jar包

我这里是一个使用timer+sigar的例子,之后用quartz做了后台任务修改,可以看另外那篇文章

实现功能是每秒查一下网卡的流量

package com.dsideal.Util;

import java.util.Timer;
import java.util.TimerTask;

import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;

import com.jfinal.plugin.memcached.MemcachedKit;
import com.jfinal.plugin.memcached.MemcachedPlugin;


public class SigarTest extends TimerTask{
    public static void main(String[] args) throws Exception {
          
        Timer timer = new Timer();
        timer.schedule(new SigarTest(), 1000, 1000);
        
    }
    
    public static int getCpuCount() throws SigarException {  
        Sigar sigar = new Sigar();  
        try {  
            return sigar.getCpuInfoList().length;  
        } finally {  
            sigar.close();  
        }  
    }  
    
    private static long dfRxBytes = 0;
    private static long dfTxBytes = 0;
    public static long dfRxBytesMb = 0; //每秒接收的总字节数
    public static long dfTxBytesMb = 0; //每秒发送的总字节数
    public void testNetIfList()  {  
        try {
             MemcachedPlugin memcachedPlugin = new MemcachedPlugin("10.10.3.151:11211");
             memcachedPlugin.start();
            Sigar sigar = new Sigar();  
            String ifNames[] = sigar.getNetInterfaceList();  
            for (int i = 0; i < ifNames.length; i++) {  
                String name = ifNames[i];  
                if(name.equals("eth6")) {
                    NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);  
//                    print("\nname(网络设备名) = " + name);// 网络设备名  
//                    print("Address(IP地址) = " + ifconfig.getAddress());// IP地址  
//                    print("Netmask(子网掩码) = " + ifconfig.getNetmask());// 子网掩码  
                    if ((ifconfig.getFlags() & 1L) <= 0L) {  
//                        print("!IFF_UP...skipping getNetInterfaceStat");  
                        continue;  
                    }  
                    try {  
                        NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);  
//                        print("RxPackets(接收的总包裹数) = " + ifstat.getRxPackets());// 接收的总包裹数  
//                        print("TxPackets(发送的总包裹数) = " + ifstat.getTxPackets());// 发送的总包裹数 
                        if (dfRxBytes == 0) {
                            dfRxBytes = ifstat.getRxBytes();
                        }
//                        print("RxBytes(接收到的总字节数) = " + ifstat.getRxBytes());// 接收到的总字节数  
//                        print("-------------------------------------------");
//                        print("RxBytes(每秒接收到的总字节数) = " + (ifstat.getRxBytes() - dfRxBytes) + "B");// 接收到的总字节数  
//                        print("RxBytes(每秒接收到的总字节数) = " + ((ifstat.getRxBytes() - dfRxBytes) /1024) + "KB");// 接收到的总字节数  
                        dfRxBytesMb = (ifstat.getRxBytes() - dfRxBytes) /1024 /1024;
                        print("RxBytes(每秒接收到的总字节数) = " + dfRxBytesMb + "MB");// 接收到的总字节数  
//                        print("-------------------------------------------");
                        dfRxBytes = ifstat.getRxBytes();
//                        print("TxBytes(发送的总字节数) = " + ifstat.getTxBytes());// 发送的总字节数  
//                        print("-------------------------------------------");
//                        print("TxBytes(每秒发送的总字节数) = " + (ifstat.getTxBytes() - dfTxBytes) + "B");// 发送的总字节数    
//                        print("TxBytes(每秒发送的总字节数) = " + ((ifstat.getTxBytes() - dfTxBytes) /1024) + "KB");// 发送的总字节数    
                        if (dfTxBytes != 0) {
                             dfTxBytesMb = (ifstat.getTxBytes() - dfTxBytes) /1024 /1024 ;
                             MemcachedKit.set("dfTxBytesMb", 0, dfTxBytesMb);
//                             System.out.println("dfTxBytesMb="+MemcachedKit.get("dfTxBytesMb"));
                        }
                        print("TxBytes(每秒发送的总字节数) = " + dfTxBytesMb + "MB");// 发送的总字节数   
//                        print("-------------------------------------------");
//                        if (((ifstat.getTxBytes() - dfTxBytes) /1024 /1024) <= 8) {
//                            this.printA();
//                        } else {
//                            this.printB();
//                        }
                        dfTxBytes = ifstat.getTxBytes();
//                        print("RxErrors(接收到的错误包数) = " + ifstat.getRxErrors());// 接收到的错误包数  
//                        print("TxErrors(发送数据包时的错误数) = " + ifstat.getTxErrors());// 发送数据包时的错误数  
//                        print("RxDropped(接收时丢弃的包数) = " + ifstat.getRxDropped());// 接收时丢弃的包数  
//                        print("TxDropped(发送时丢弃的包数) = " + ifstat.getTxDropped());// 发送时丢弃的包数  
                    } catch (SigarNotImplementedException e) {  
                         print(e.getMessage());  
                    } catch (SigarException e) {  
                        print(e.getMessage());  
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }  
        } catch (Exception e) {
            // TODO: handle exception
        }
        
    }
    
    void print(String msg) {  
        System.out.println(msg);  
    }

    @Override
    public void run() {
        SigarTest s =new SigarTest();
        s.testNetIfList();
        
    }  
    
    public static void printA() {
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
    }
    
    public static void printB() {
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBB");
    }
    
}

 

推荐阅读