用java也可以轻便实现收集系统信息:Sigar介绍

用java也可以轻松实现收集系统信息:Sigar介绍

Sigar简介

今天意外发现了一个开源工具包 SIGAR - System Information Gatherer And Reporter,即 系统信息收集和报告。

官方站点:

http://support.hyperic.com/display/SIGAR/Home;jsessionid=A9705887A07D20BBAC9A96500BC60822

 

 

SIGAR 官方站点 写道
The Sigar API provides a portable interface for gathering system information such as:

System memory, swap, cpu, load average, uptime, logins
Per-process memory, cpu, credential info, state, arguments, environment, open files
File system detection and metrics
Network interface detection, configuration info and metrics
TCP and UDP connection tables
Network route table

This information is available in most operating systems, but each OS has their own way(s) providing it.
SIGAR provides developers with one API to access this information regardless of the underlying platform.
The core API is implemented in pure C with bindings currently implemented for Java, Perl, Ruby, Python, Erlang, PHP and C#.

 

几乎所有的硬件信息、网络情况都可以得到。

 

1, CPU信息,包括基本信息(vendor、model、mhz、cacheSize)和统计信息(user、sys、idle、nice、wait)

2, 文件系统信息,包括Filesystem、Size、Used、Avail、Use%、Type

3, 事件信息,类似Service Control Manager

4, 内存信息,物理内存和交换内存的总数、使用数、剩余数;RAM的大小

5, 网络信息,包括网络接口信息和网络路由信息

6, 进程信息,包括每个进程的内存、CPU占用数、状态、参数、句柄

7, IO信息,包括IO的状态,读写大小等

8, 服务状态信息

9, 系统信息,包括操作系统版本,系统资源限制情况,系统运行时间以及负载,JAVA的版本信息等.

 

Linux下Sigar的安装步骤

wget http://downloads.sourceforge.net/project/sigar/sigar/1.6/hyperic-sigar-1.6.4-src.tar.gz

tar zxf hyperic-sigar-1.6.4-src.tar.gz

cd hyperic-sigar-1.6.4-src

cd src

cd bindings

cd java

ant

 

Sigar提供的命令行工具

它还提供了一个命令行的使用方式,如下所示:

[root@node16 java]# java -jar sigar-bin/lib/sigar.jar

Loaded rc file: ./.sigar_shellrc

sigar> help

Available commands:

        alias          - Create alias command

        cpuinfo        - Display cpu information

        df             - Report filesystem disk space usage

        du             - Display usage for a directory recursively

        free           - Display information about free and used memory

        get            - Get system properties

        help           - Gives help on shell commands

        ifconfig       - Network interface information

        iostat         - Report filesystem disk i/o

        kill           - Send signal to a process

        ls             - simple FileInfo test at the moment (like ls -l)

        mps            - Show multi process status

        netinfo        - Display network info

        netstat        - Display network connections

        nfsstat        - Display nfs stats

        pargs          - Show process command line arguments

        penv           - Show process environment

        pfile          - Display process file info

        pidof          - Find the process ID of a running program

        pinfo          - Display all process info

        pmodules       - Display process module info

        ps             - Show process status

        quit           - Terminate the shell

        route          - Kernel IP routing table

        set            - Set system properties

        sleep          - Delay execution for the a number of seconds 

        source         - Read a file, executing the contents

        sysinfo        - Display system information

        time           - Time command

        ulimit         - Display system resource limits

        uptime         - Display how long the system has been running

        version        - Display sigar and system version info

        who            - Show who is logged on

sigar> df

Filesystem      Size Used Avail Use% Mounted on      Type

/dev/mapper/VolGroup00-LogVol00  29G  24G  4.3G  85% /               ext3/local

proc              0    0     0     - /proc           proc/none

sysfs             0    0     0     - /sys            sysfs/none

devpts            0    0     0     - /dev/pts        devpts/none

/dev/sda1        99M  12M   82M  13% /boot           ext3/local

tmpfs           378M   0   378M    - /dev/shm        tmpfs/none

none              0    0     0     - /proc/sys/fs/binfmt_misc binfmt_misc/none

sigar> cpuinfo

Vendor.........Intel

Model..........Core(TM)2 Duo CPU     E8400  @ 3.00GHz

Mhz............2993

Total CPUs.....1

Cache size....6144

 

CPU 0.........

User Time.....0.0%

Sys Time......0.0%

Idle Time.....100.0%

Wait Time.....0.0%

Nice Time.....0.0%

Combined......0.0%

Irq Time......0.0%

SoftIrq Time..0.0%

Stolen Time....0.0%

 

Totals........

User Time.....0.0%

--More-- (Page 1 of 2) 

Sys Time......0.0%

Idle Time.....100.0%

Wait Time.....0.0%

Nice Time.....0.0%

Combined......0.0%

Irq Time......0.0%

SoftIrq Time..0.0%

Stolen Time....0.0%

 

sigar> quit

Goodbye.

[root@node16 java]# 

 

Sigar提供的java的命令行工具,实现了常用的Linux命令,如下所示:

 

SIGAR 官方站点 写道
The shell and commands are implemented in Java, the source code is located in
bindings/java/src/org/hyperic/sigar/cmd/.
Including implementations of well-known commands such as:

df
du
free
ifconfig
iostat
netstat
ps
route
top
ulimit
uptime
who

 

 

PTQL (Process Table Query Language)  

这是sigar的内部查询语言(类似于数据库的 SQL)

http://support.hyperic.com/display/SIGAR/PTQL

 

SIGAR 官方站点 写道
Hyperic SIGAR provides a mechanism to identify processes called Process Table Query Language. All operating systems assign a unique id (PID) to each running process. However, the PID is a random number that may also change at any point in time when a process is restarted. PTQL uses process attributes that will persist over time to identify a process.
 

 

 

[root@node16 java]# java -jar sigar-bin/lib/sigar.jar 

Loaded rc file: ./.sigar_shellrc

sigar> ps "State.Name.eq=java"

4338    root    15:02   402M    144M    6.7M    S       0:52    java:org.apache.catalina.startup.Bootstrap

4775    root    16:16   309M     16M    5.7M    S       0:0     java:org.hyperic.sigar.cmd.Runner

sigar> 

 

 

 

 

 

 

在 java 中如何使用

由于时间关系,只列出一些很好的网络资料:

 

sigar官方javadoc

http://www.hyperic.com/support/docs/sigar/

评论:用sigar写程序的时候肯定用得着

 

snmp采集服务器信息(sigar.jar实现) 

http://blog.csdn.net/ocelight/article/details/4147278

评论:列举了获取常用系统信息的java代码

 

Sigar系统监控 

http://blog.csdn.net/yaerfeng/article/details/7018362

评论:另外一篇介绍资料,也包括一些java示例代码。