Hyperic-Sigar是一个收集系统各项底层信息的工具集.他有如下特点:
1. 收集信息全面
收集CPU,MEM,NETWORK,PROCESS,IOSTAT等
使用Sigar,你完全可以模仿出cpuinfo,meminfo,top,free,ifconfig,ipconfig,netstat,route,df,du,ps,ls等多种unix平台和windows平台的指令.
2.跨平台,支持多数平台
支持的平台包括:windows系列(32系列,IA64系列,AMD64系列),linux系列,freeBsd系列,HPUnix系列,Sun solaris/Sparc/Sparc64系列,macOs系列,AIX系列等

3.提供的API接口全面
sigar本身由C语言开发而成,提供了丰富的API接口,包括:JAVA,.NET,PERL,PHP,PYTHON,RUBY.Sigar.jar包是通过本地方法来调用操作系统API来获取系统相关数据。Windows操作系统下Sigar.jar依赖sigar-amd64-winnt.dll或sigar-x86-winnt.dll,linux操作系统下则依赖libsigar-amd64-linux.so或libsigar-x86-linux.so……而Sigar.jar还依赖于jug-asl-2.0.0.jar、log4j-1.2.14.jar、Junit.jar,

Hyperic-hq官方网站:http://www.hyperic.com

Sigar.jar下载地址:http://sigar.hyperic.com

Sigar.jar包的使用方法:

 

1、             CPU资源信息

a)        CPU数量(单位:个)

privatestaticint getCpuCount()throws SigarException {

    Sigar sigar = new Sigar();

    try {

        returnsigar.getCpuInfoList().length;

    } finally {

        sigar.close();

    }

}

b)       CPU的总量(单位:HZ)及CPU的相关信息

Sigar sigar = getSigar();

      CpuInfo infos[] = sigar.getCpuInfoList();

      for (int i = 0; i< infos.length; i++){//不管是单块CPU还是多CPU都适用

          CpuInfo info = infos[i];

          traceln("mhz=" +info.getMhz());//CPU的总量MHz

          traceln("vendor=" +info.getVendor());//获得CPU的卖主,如:Intel

          traceln("model=" +info.getModel());//获得CPU的类别,如:Celeron

          traceln("cache size=" +info.getCacheSize());//缓冲存储器数量

}

c)        CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等(单位:100%)

publicvoid testCpuPerc() {

          Sigar sigar= new Sigar();

         // 方式一,主要是针对一块CPU的情况

          CpuPerccpu;

         try {

              cpu =sigar.getCpuPerc();

             printCpuPerc(cpu);

          }catch (SigarException e) {

             e.printStackTrace();

          }

         //方式二,不管是单块CPU还是多CPU都适用

          CpuPerccpuList[] = null;

         try {

              cpuList =sigar.getCpuPercList();

          }catch (SigarException e) {

             e.printStackTrace();

             return;

          }

         for (int i = 0; i< cpuList.length; i++) {

             printCpuPerc(cpuList[i]);

          }

       }

      privatevoid printCpuPerc(CpuPerc cpu) {

         println("User :" +CpuPerc.format(cpu.getUser()));//用户使用率

         println("Sys :" +CpuPerc.format(cpu.getSys()));// 系统使用率

         println("Wait :" +CpuPerc.format(cpu.getWait()));//当前等待率

         println("Nice :" +CpuPerc.format(cpu.getNice()));//

         println("Idle :" +CpuPerc.format(cpu.getIdle()));//当前空闲率

         println("Total :" +CpuPerc.format(cpu.getCombined()));//总的使用率

}

d)       ……

2、             内存资源信息

a)       物理内存信息

Sigar sigar = new Sigar();

Mem mem = sigar.getMem();

// 内存总量

      System.out.println("Total = " +mem.getTotal() / 1024L + "K av");

      // 当前内存使用量

      System.out.println("Used = " +mem.getUsed() / 1024L + "K used");

      // 当前内存剩余量

System.out.println("Free = "+ mem.getFree() / 1024L + "K free");

b)       系统页面文件交换区信息

Sigar sigar = new Sigar();

Swap swap = sigar.getSwap();

// 交换区总量

      System.out.println("Total = " +swap.getTotal() / 1024L + "K av");

      // 当前交换区使用量

      System.out.println("Used = " +swap.getUsed() / 1024L + "K used");

      // 当前交换区剩余量

System.out.println("Free = "+ swap.getFree() / 1024L + "K free");

c)     ……

3、             操作系统信息

a)     取到当前操作系统的名称:

 private StringgetPlatformName() {

          Stringhostname = "";

         try {

              hostname =InetAddress.getLocalHost().getHostName();

          }catch (Exception exc) {

              Sigarsigar = new Sigar();

             try {

                 hostname= sigar.getNetInfo().getHostName();

              }catch (SigarException e) {

                 hostname= "localhost.unknown";

              }finally {

                sigar.close();

              }

          }

         return hostname;

}

b)       取当前操作系统的信息

      publicvoid testGetOSInfo() {

         OperatingSystemOS =OperatingSystem.getInstance();

         // 操作系统内核类型如:386、486、586等x86

         System.out.println("OS.getArch() =" + OS.getArch());

         System.out.println("OS.getCpuEndian() =" + OS.getCpuEndian());//

         System.out.println("OS.getDataModel() =" + OS.getDataModel());//

         // 系统描述

         System.out.println("OS.getDescription()= " + OS.getDescription());

         System.out.println("OS.getMachine() =" + OS.getMachine());//

         // 操作系统类型

         System.out.println("OS.getName() =" + OS.getName());

         System.out.println("OS.getPatchLevel()= " + OS.getPatchLevel());//

         // 操作系统的卖主

         System.out.println("OS.getVendor() =" + OS.getVendor());

         // 卖主名称

         System.out.println("OS.getVendorCodeName()= " + OS.getVendorCodeName());

         // 操作系统名称

         System.out.println("OS.getVendorName()= " + OS.getVendorName());

         // 操作系统卖主类型

         System.out.println("OS.getVendorVersion()= " + OS.getVendorVersion());

         // 操作系统的版本号

         System.out.println("OS.getVersion() =" + OS.getVersion());

}

c)        取当前系统进程表中的用户信息

publicvoid testWho() {

         try {

              Sigar sigar= new Sigar();

              Who who[] =sigar.getWhoList();

             if (who != null&& who.length> 0) {

                for (int i = 0; i< who.length; i++) {

                    Sysout.out.println("\n~~~~~~~~~" +String.valueOf(i) + "~~~~~~~~~");

                     Who _who =who[i];

                    Sysout.out.println ("getDevice() = " +_who.getDevice());

                    Sysout.out.println ("getHost() = " +_who.getHost());

                    Sysout.out.println ("getTime() = " +_who.getTime());

                    //当前系统进程表中的用户名

                   Sysout.out.println ("getUser() = " +_who.getUser());

                 }

              }

          }catch (SigarException e) {

             e.printStackTrace();

          }

}

d)       ……

4、             资源信息(主要是硬盘)

a)     取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象,然后对其进行编历):

publicvoid testFileSystemInfo()throws Exception {

          Sigar sigar= getSigar();

          FileSystemfslist[] = sigar.getFileSystemList();

          String dir =System.getProperty("user.home");//当前用户文件夹路径

         for (int i = 0; i< fslist.length; i++) {

             System.out.println("\n~~~~~~~~~~"+ i + "~~~~~~~~~~");

              FileSystemfs = fslist[i];

             // 分区的盘符名称

             System.out.println("fs.getDevName() =" + fs.getDevName());

             // 分区的盘符名称

             System.out.println("fs.getDirName() =" + fs.getDirName());

             System.out.println("fs.getFlags() =" + fs.getFlags());//

             // 文件系统类型,比如 FAT32、NTFS

             System.out.println("fs.getSysTypeName()= " + fs.getSysTypeName());

             // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等

             System.out.println("fs.getTypeName() =" + fs.getTypeName());

             // 文件系统类型

             System.out.println("fs.getType() =" + fs.getType());

             FileSystemUsage usage = null;

             try {

                 usage =sigar.getFileSystemUsage(fs.getDirName());

              }catch (SigarException e) {

                if (fs.getType() == 2)

                    throw e;

                continue;

              }

             switch (fs.getType()) {

             case 0: // TYPE_UNKNOWN:未知

                break;

             case 1: // TYPE_NONE

                break;

             case 2: // TYPE_LOCAL_DISK :本地硬盘

                // 文件系统总大小

                System.out.println(" Total= " + usage.getTotal() + "KB");

                // 文件系统剩余大小

                System.out.println(" Free= " + usage.getFree() + "KB");

                // 文件系统可用大小

                System.out.println(" Avail= " + usage.getAvail() + "KB");

                // 文件系统已经使用量

                System.out.println(" Used= " + usage.getUsed() + "KB");

                double usePercent = usage.getUsePercent() *100D;

                // 文件系统资源的利用率

                System.out.println(" Usage= " + usePercent + "%");

break;

             case 3:// TYPE_NETWORK:网络

                break;

             case 4:// TYPE_RAM_DISK:闪存

                break;

             case 5:// TYPE_CDROM :光驱

                break;

             case 6:// TYPE_SWAP :页面交换

                break;

              }

             System.out.println(" DiskReads= " + usage.getDiskReads());

             System.out.println(" DiskWrites= " + usage.getDiskWrites());

          }

         return;

}

b)     ……

5、             网络信息

a)     当前机器的正式域名

public StringgetFQDN(){

         try {

             returnInetAddress.getLocalHost().getCanonicalHostName();

          }catch (UnknownHostException e) {

             try {

                 Sigar sigar= new Sigar();

                return sigar.getFQDN();

              }catch (SigarException ex) {

                returnnull;

              }finally {

                  sigar.close();

              }

          }

}

b)       取到当前机器的IP地址

public StringgetDefaultIpAddress() {

           String address = null;

           try {

               address =InetAddress.getLocalHost().getHostAddress();

               //没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回

             // 否则再通过Sigar工具包中的方法来获取

if(!NetFlags.LOOPBACK_ADDRESS.equals(address)){

                    return address;

               }

           } catch (UnknownHostException e) {

               //hostname not in DNS or /etc/hosts

           }

           Sigar sigar = new Sigar();

           try {

               address = sigar.getNetInterfaceConfig().getAddress();

           } catch (SigarException e) {

               address = NetFlags.LOOPBACK_ADDRESS;

           } finally {

               sigar.close();

           }

return address;

}

c)        取到当前机器的MAC地址

public String getMAC() {

          Sigar sigar= null;

         try {

              sigar =new Sigar();

              String[]ifaces = sigar.getNetInterfaceList();

              Stringhwaddr = null;

             for (int i = 0; i< ifaces.length; i++){

                NetInterfaceConfig cfg =sigar.getNetInterfaceConfig(ifaces[i]);

                if(NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())

                        ||(cfg.getFlags() &NetFlags.IFF_LOOPBACK) != 0

                        ||NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())){

                    continue;

                 }

                 

                 hwaddr = cfg.getHwaddr();

                break;

              }

             return hwaddr != null ?hwaddr : null;

          }catch (Exception e) {

             returnnull;

          }finally {

             if (sigar !=null)

                sigar.close();

          }

}

d)       根据MAC地址来获得一个GUID号

public String getGUID(String mac) {

         if (mac ==null)

             returnnull;

         EthernetAddress eAddr = newEthernetAddress(mac);

         returnUUIDGenerator.getInstance().generateTimeBasedUUID(eAddr)

                .toString();

}

e)        获取网络流量等信息

publicvoid testNetIfList()throws Exception {

          Sigar sigar= new Sigar();

          StringifNames[] = sigar.getNetInterfaceList();

         for (int i = 0; i< ifNames.length; i++) {

              String name= ifNames[i];

             NetInterfaceConfig ifconfig =sigar.getNetInterfaceConfig(name);

             print("\nname = " +name);//网络设备名

             print("Address = "+ifconfig.getAddress());//IP地址

             print("Netmask = "+ifconfig.getNetmask());//子网掩码

             if ( (ifconfig.getFlags()& 1L) <= 0L) {

                print("!IFF_UP...skippinggetNetInterfaceStat");

                continue;

              }

             try {

                NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);

                print("RxPackets = " +ifstat.getRxPackets());//接收的总包裹数

                print("TxPackets = " +ifstat.getTxPackets());//发送的总包裹数

                print("RxBytes = " +ifstat.getRxBytes());//接收到的总字节数

                print("TxBytes = " +ifstat.getTxBytes());//发送的总字节数

                print("RxErrors = " +ifstat.getRxErrors());//接收到的错误包数

                print("TxErrors = " +ifstat.getTxErrors());//发送数据包时的错误数

                print("RxDropped = " +ifstat.getRxDropped());//接收时丢弃的包数

                print("TxDropped = " +ifstat.getTxDropped());//发送时丢弃的包数

              }catch (SigarNotImplementedException e){

              }catch (SigarException e) {

                print(e.getMessage());

              }

          }

}

void print(String msg){

         System.out.println(msg);

}

f)        一些其他的信息

      privatevoid getEthernetInfo(){

          Sigar sigar= null;

         try {

              sigar =new Sigar();

              String[]ifaces = sigar.getNetInterfaceList();

             for (int i = 0; i< ifaces.length; i++) {

                NetInterfaceConfig cfg =sigar.getNetInterfaceConfig(ifaces[i]);

                if(NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())

                        ||(cfg.getFlags() &NetFlags.IFF_LOOPBACK) != 0

                        ||NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())){

                    continue;

                 }

                System.out.println("cfg.getAddress() =" + cfg.getAddress());//IP地址

                System.out.println("cfg.getBroadcast()= " + cfg.getBroadcast());//网关广播地址

                System.out.println("cfg.getHwaddr() =" +cfg.getHwaddr());//网卡MAC地址

                System.out.println("cfg.getNetmask() =" + cfg.getNetmask());//子网掩码

                System.out.println("cfg.getDescription()= " + cfg.getDescription());//网卡描述信息

                System.out.println("cfg.getType() =" + cfg.getType());//

                System.out.println("cfg.getDestination()= " + cfg.getDestination());

                System.out.println("cfg.getFlags() =" + cfg.getFlags());//

                System.out.println("cfg.getMetric() =" + cfg.getMetric());

                System.out.println("cfg.getMtu() =" + cfg.getMtu());

                System.out.println("cfg.getName() =" + cfg.getName());

                System.out.println();

              }

          }catch (Exception e) {

             System.out.println("Error whilecreating GUID" + e);

          }finally {

             if (sigar !=null)

                sigar.close();

          }

}

Hyperic-Sigar简介的更多相关文章

  1. java sigar获取本地信息以及org.hyperic.sigar.SigarException: The device is not ready报错解决

    window下,使用java sigar 获取磁盘使用率,cpu使用率以及内存使用情况等信息时. 一:首先需要下载jar包和相关文件 sigar-1.6.4.zip 如果想了解更多可以去 sigar官 ...

  2. Hyperic Sigar API 举例

    Hyperic HQ 是什么? Hyperic HQ 是一个开源的(General Public License,GPL授权)IT资源管理框架,让用户使用统一的界面来管理各种不同的IT资源的管理,Hy ...

  3. 1 [main] DEBUG Sigar - no sigar-amd64-winnt.dll in java.library.path org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path

    github上一个java项目,在myeclipse中运行正常,生成jar后,运行报错: 1 [main] DEBUG Sigar - no sigar-amd64-winnt.dll in java ...

  4. Sigar简介

    大家好,我是Sigar.也许好多人还不认识我.下面就介绍一下我自己,好让大家对我有一个大致的了解. 我的全名是System Information Gatherer And Reporter,中文名是 ...

  5. 使用Sigar获取服务器信息

    Sigar简介 Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件.它用来从许多平台收集系统和处理信息. 这些平台包括:Linux, Windows, Solari ...

  6. Sigar使用

    Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API,核心由C语言实现的.项目中使用Sigar进行服务器 ...

  7. ava如何实现系统监控、系统信息收集、sigar开源API的学习(转)

    ava如何实现系统监控.系统信息收集.sigar开源API的学习(转) 转自:http://liningjustsoso.iteye.com/blog/1254584 首先给大家介绍一个开源工具Sig ...

  8. Sigar.jar获取系统信息

    Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件. 它用来从许多平台收集系统和处理信息,这些平台包括:Linux, Windows, Solaris, AIX, ...

  9. sigar监控

    相关参照博客: http://liningjustsoso.iteye.com/blog/1254584 http://blog.csdn.net/aoxida/article/category/12 ...

  10. sigar获取Windows系统的硬件信息进行JAVA后台系统资源监控

    windows下,将sigar-amd64-winnt.dll复制到jdk的bin目录下或者拷贝到WEB-INF/lib下 linux下,将libsigar-amd64-linux.so拷贝到jdk的 ...

随机推荐

  1. man手册语法格式

    Linux命令很多,但对格式本身解读的文章几乎是空白,都在凭对格式的猜测来写命令,就此在网上搜集此类资料都很少而且很不全面,想找官方的,也没找到.根据自己的理解写一篇出来,希望对初学者有用.   一. ...

  2. 关于NavigationItem.rightBarButtonItem设置

    转自:http://blog.csdn.net/zhuzhihai1988/article/details/7701998 第一种: UIImage *searchimage=[UIImage ima ...

  3. web 安全问题(一):CSRF 攻击

    什么是CSRF CSRF是怎么产生的 CSRF的攻击对象 CSRG的攻击手段 CSRF的防御措施 什么是CSRF 全称是(Cross Site Request Forgery)跨站请求伪造.也就是恶意 ...

  4. 二分箭术--G&K稳健过神思路

    自从资料片开始,一直在思考稳定可靠的过神思路,现在有眉目了,试验了几把感觉不错,先分享如下: 1)只开2个分城,特殊情况除外.Re: 经过多次打的经验,开2个分城比开3个分城更容易选址,政策更快,快乐 ...

  5. [未解决]Ajax-读取并解析XML,动态生成select下拉列表框

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. Android实例-红外线操作(XE10.2+小米5)

    相关资料: http://blog.csdn.net/qq_21752153/article/details/50244717https://developer.xamarin.com/api/typ ...

  7. JavaScript高级 面向对象(3)--字符编码

    说明(2017.3.29): 1. 数字+上下文=信息,如果是做数学运算,就是数字,如果是输出字符串给用户,就是码表字符. 2. ASCII码,共128个数字,128个数字刚好7个二进制位(2的7次方 ...

  8. 按关键字搜索自己的CSDN博客

    CSDN居然没有搜索自己博客的功能!如果博客写多了后将很难查找. 搜到的办法居然要靠谷歌... 在谷歌输入关键词,格式如下: <要检索的关键词> site:blog.csdn.net/&l ...

  9. 【C#】访问泛型中的List列表数据

    光看标题的确不好说明问题,下面描述一下问题场景: 已知后端自定义的返回的Json数据结构如下: response: { "message": "返回成功", & ...

  10. EFM32 DMA/PRS例程

    /**************************************************************************//**  * @file  * @brief H ...