天天看點

系統資訊收集API : Java Sigar

Sigar簡介

開源工具包 SIGAR - System Information Gatherer And Reporter,即 系統資訊收集和報告。

官方站點:

https://support.hyperic.com/display/SIGAR/Home

考慮到官網的不穩定,把相關系摘錄如下:

簡單的說,他能收集的資訊包括:

  • 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

幾乎所有的硬體資訊、網絡情況都可以得到。

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的版本資訊等.

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#.

支援的作業系統平台:

Operating System Architecture Versions Binary Included
Linux x86 2.2, 2.4, 2.6 kernels     支援
Linux amd64 2.6 kernel
支援
Linux ppc 2.6 kernel
支援
Linux ppc64 2.6 kernel
支援
Linux ia64 2.6 kernel
支援
Linux s390 2.6 kernel
不支援
Linux s390x 2.6 kernel
支援
Windows x86 NT 4.0, 2000 Pro/Server, 2003 Server, XP, Vista, 2008 Server, 7
支援
Windows x64 2003 Server, Vista, 2008 Server, 7
支援
Solaris sparc-32 2.6, 7, 8, 9, 10
支援
Solaris sparc-64 "
支援
Solaris x86 8, 9, 10
支援
支援
Solaris x64 "
支援
AIX ppc 4.3, 5.1, 5.2, 5.3, 6.1
支援
AIX ppc64 5.2,5.3,6.1
支援
HP-UX PA-RISC 11
支援
HP-UX ia64 11
支援
FreeBSD x86 4.x 不支援
FreeBSD x86 5.x, 6.x
支援
FreeBSD x64 6.x
支援
FreeBSD x86,x64 7.x,8.x 不支援
OpenBSD x86 4.x,5.x 不支援
NetBSD x86 3.1 不支援
Mac OS X PowerPC 10.3, 10.4 支援
Mac OS X x86 10.4, 10.5, 10.6 支援
Mac OS X x64 10.5, 10.6 支援

While SIGAR only depends on the Linux kernel version, the following distributions have been certified:

Distribution Versions
Red Hat 6.2, 7.3, 8.0, 9.0
RHEL 3, 4, 5, 6
CentOS 3, 4, 5
Fedora 2, 3, 4, 5, 6, 7, 8, 9, 10
SuSE 8, 9, 10, 11
Ubuntu 6.06, 8.04, 8.10, 9.04
Debian 2.6, 3.0, 3.1, 3.2, 4.0, 5.0
VMware ESX 2.x, 3.0
XenServer 3.1, 3.2, 4.0, 4.1, 5.0
Slackware 10, 11
Mandrake 10
Scientific Linux 5
Gentoo

To test drive run the following command:

?

% java -jar sigar-bin/lib/sigar.jar

sigar> help

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提供的指令行工具

它還提供了一個指令行的使用方式,如下所示:

[[email protected] 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.

[[email protected] java]#

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

Perl, Ruby, Python, Erlang, PHP and C# interfaces are still a work in progress.

To try the Perl examples:

?

% cd bindings/perl

% perl Makefile.PL && make

\

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.

[[email protected] 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/fred_lzy/article/details/8512624

評論:重點介紹了Hyperic 平台,這個平台又有開源版和企業版。

http://blog.csdn.net/fred_lzy/article/details/12273699