1. 程式人生 > >用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提供的命令列工具

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

[[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]# 

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.  

[[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/yaerfeng/article/details/7018362

評論:另外一篇介紹資料,也包括一些java示例程式碼。

相關推薦

java可以輕鬆實現收集系統資訊Sigar介紹

Sigar簡介 今天意外發現了一個開源工具包 SIGAR - System Information Gatherer And Reporter,即 系統資訊收集和報告。 官方站點: http://support.hyperic.com/display/SIGAR/Home;j

驚奇!Java實現比特幣系統

代碼 ring str 之間 lin 再次 action 入門 rev 最近區塊鏈技術突然爆火,身邊做技術的朋友茶余飯後不談點區塊鏈什麽的都被認為是跟不上時代了,為啥會這樣了? 這其實跟比特幣價格去年的突飛猛進是分不開的,比特幣價格從去年初不到一千美金到今年初最高接近兩萬美

所謂的網頁爬蟲java程式碼來實現,此程式碼適合在maven專案中使用中使用,因為,程式碼中的類所對應的依賴可以讓maven下載。

//獲得httpClient物件 CloseableHttpClient httpClient = HttpClients.createDefault(); //url公司域名隨便 String url = "https://www.baidu.co

JAVA連線SQL實現查詢資料

顯示所有學生程式碼 <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE html>

JAVA連線SQL實現更新資料

在程式碼中更新資料 <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE html>

JAVA連線SQL實現插入資料

         直接由程式碼來決定插入的資料。 <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql

JAVA連線SQL實現刪除資料

刪除一條資料 <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE html> &l

JAVA和Jquery實現掃碼登入的原理和程式碼

貼上前端程式碼,供參考 <script type="text/javascript" src="js/jquery-3.3.1.js"></script> <script type="text/javascript" src="js/jq

Spark中RDD轉換成DataFrame的兩種方式(分別Java和scala實現

 一:準備資料來源       在專案下新建一個student.txt檔案,裡面的內容為: print? <code class="language-java">1,zhangsan,20   2,lisi,21   3,wanger,1

第67課Spark SQL下采Java和Scala實現Join的案例綜合實戰(鞏固前面學習的Spark SQL知識)

內容:     1.SparkSQL案例分析     2.SparkSQL下采用Java和Scala實現案例 一、SparkSQL下采用Java和Scala實現案例 學生成績: {"name":"Michael","score":98} {"name":"Andy"

java內部類實現多重繼承

package test;   /**   * class one: Class1   * @author TJ   */ publicclass Class1 {       /**       * method: getName       * @return 

99乘法表分別java和python實現

如何用java和python實現九九乘法表 java python python一行實現 java class ChengFaBiao { public static void main(String[] args) { for

java模擬銀行賬戶儲存系統

package cn.mdln.study2; /**  * 模擬銀行賬戶儲存系統  * @author Administrator  *  */ public class TestDemo16 {public static void main(String[] args)

java.util.Observable實現Observer模式

Observer模式  主要是觀察者與被觀察者之間的關係 觀察者為羊,被觀察者為狼  模仿的場景為狼叫羊跑 程式碼如下: 1.被觀察者類 package test.pattern.observer; import java.util.Observable; public class Wolf

Java泛型實現折半插入排序

package ch10; /** * successful-折半插入排序 * @author songjie * */ public class BinaryInsertSort { public static <T extends Comparable

Java和Jquery實現了一個砸金蛋例子

之前在網上找到了一個Jquery+PHP實現的砸金蛋的例子,正好公司也要我寫一個類似的砸金蛋的功能,於是在網上找了找參考,用Jsp程式也弄了一個出來,首先是Html的效果,我打算每次在頁面上只顯示一個金蛋,點選的時候用Ajax提交,Java在後臺處理一下返回結果

Java泛型實現快速排序

package ch10; public class QuickSort { /** * successfully-快速排序 * 使用了泛型-自然排序 * @param <T> * @param t */ public static &

if else 太多?看我 Java 8 輕鬆幹掉!

之前我用 Java 8 寫了一段邏輯,就是類似下面這樣的例子: ``` /* * 來源公眾號:Java技術棧 */ if(xxxOrder != null){ if(xxxOrder.getXxxShippingInfo() != null){ if(xxxOrder.getXxxShippingI

Java架構-詳解分散式系統本質“分治”和“冗餘”

站在全域性角度看,分散式系統的本質是什麼?其實說白了,就是兩點:“分治”和“冗餘”。 分治和冗餘使得分散式系統具備了核心價值,那麼它的價值是什麼? 分散式系統的價值 談到分散式系統的價值,可能就得從 1953 年說起了。在這一年,埃布·格羅希(Herb Grosch)提

java集合類實現簡單的學生資訊管理系統

package jihe; import java.util.Scanner; public class Student { private String sno; private String sname; private int grade; private int age; private S