Sigar(System Information Gatherer And Reporter),是一個開源的工具,提供了跨平台的系統信息收集的API,核心由C語言實現的。項目中使用Sigar進行服務器監控。很多人都對它進行了介紹:
上面兩個鏈接中有對Sigar的介紹和使用方法。官方的介紹在這里,英語好的話還是看這個吧:
- https://support.hyperic.com/display/SIGAR/Home
- http://cpansearch.perl.org/src/DOUGM/hyperic-sigar-1.6.3-src/docs/javadoc/org/hyperic/sigar/Sigar.html
通過上面的鏈接,測試Sigar或者寫個Sigar的Demo應該沒問題了。另外,Sigar這個項目已經不再活躍了,2014年的commit只有5個,不知道是否有其他監控服務器工具,希望大家不吝賜教!
現在要在項目中使用Sigar,我們項目使用maven進行構建,網上找到了如何在maven中配置Sigar的帖子:
各位大神可以試試,我功底太淺(確切的說是沒有功底),沒試出來。后來發現個簡單辦法,雖然不甘心,先用着,也是網上找到的:
在使用的時候注意,上面鏈接給出了一部分代碼,其他的代碼在:
里面有個OsCheck類,還有就是代碼中使用的Resources是guava中的Resources而不是java中的,所有如果使用maven的話,pom.xml中要加
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
我們的項目是一個J2EE項目,開發時使用的中間件是Tomcat,在Windows和Linux上跑都沒有問,可是當部署到服務器上的時候,就開始報錯:
java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getNativeVersion()Ljava/lang/String;
或者
NoClassDefFoundError: Could not initialize class
如果在服務器上直接使用java -jar sigar-bin/lib/sigar.jar的方式使用,是不會報錯的。
服務器上用的是TongWeb(我知道大家都沒聽說過),據說他和GlassFlash很像。
在查找原因時發現這個帖子:
回答是這樣的:
On Redhat linux with JBoss server adding sigar jar to the LDPATH has resolve the problem.
我想LDPATH指的就是LD_LIBRARY_PATH吧,同時發現了這個帖子:
這位樓主解決了這個問題,好的,那應該就是LD_LIBRARY_PATH的問題了,但是先不要按照他說的來,一個一個加載動態庫太麻煩了。發現了這個問題出現的具體原因:
是出於安全原因,Linux系統做了限制(Because of security reasons, Linux system to the limit. LD_LIBRARY_PATH not loading from .profile nor /etc/environment)。有人已經翻譯好了:
好的,只要我們在每次運行前:
export LD_LIBRARY_PATH=DIR_where_Your_Sigar_Lib_is:$LD_LIBRARY_PATH
或者改/etc/ld.so.conf就可以了。
綜合上面所提到的解決方法,最終項目中是這樣改的:
-
pom.xml中增加Sigar與Guava依賴:
- <dependency>
- <groupId>org.fusesource</groupId>
- <artifactId>sigar</artifactId>
- <version>1.6.4</version>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>18.0</version>
- </dependency>
- 在 http://sourceforge.net/projects/sigar/files/ 下載Sigar-1.6.4,將hyperic-sigar-1.6.4\sigar-bin\lib這個文件夾重命名為sigar放到項目的\src\main\resources\目錄下
-
增加如下OsCheck類:
- public final class OsCheck {
- /**
* types of Operating Systems
*/ - public enum OSType {
- Windows, MacOS, Linux, Other
- }
- protected static OSType detectedOS;
- /**
* detected the operating system from the os.name System property and cache
* the result
*
* @returns - the operating system detected
*/ - public static OSType getOperatingSystemType() {
- if (detectedOS == null) {
- String OS = System.getProperty("os.name", "generic").toLowerCase();
- if (OS.indexOf("win") >= 0) {
- detectedOS = OSType.Windows;
- } else if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
- detectedOS = OSType.MacOS;
- } else if (OS.indexOf("nux") >= 0) {
- detectedOS = OSType.Linux;
- } else {
- detectedOS = OSType.Other;
- }
- }
- return detectedOS;
- }
- }
-
增加SigarUtil類:
- import java.io.File;
- import com.google.common.io.Resources;
- import org.hyperic.sigar.Sigar;
- public class SigarUtil {
- private static class SigarUtilHolder{
- private static final SigarUtil INSTANCE = new SigarUtil();
- private static final Sigar Sigar = new Sigar();
- }
- private SigarUtil (){
- try {
- String file = Resources.getResource("sigar/.sigar_shellrc").getFile();
- File classPath = new File(file).getParentFile();
- String path = System.getProperty("java.library.path");
- if (OsCheck.getOperatingSystemType() == OsCheck.OSType.Windows) {
- path += ";" + classPath.getCanonicalPath();
- } else {
- path += ":" + classPath.getCanonicalPath();
- }
- System.setProperty("java.library.path", path);
- System.out.println(path);
- } catch (Exception e) {
- }
- }
- public static final Sigar getInstance(){
- return SigarUtilHolder.Sigar;
- }
- public static final SigarUtil getSigarUtilInstance(){
- return SigarUtilHolder.INSTANCE;
- }
- }
-
按如下方式調用:
- public void testLib() {
- try {
- Sigar sigar = SigarUtil.getInstance();
- CpuPerc cpu = sigar.getCpuPerc();
- System.out.println(String.valueOf(cpu.getCombined()));
- } catch (SigarException e) {
- e.printStackTrace();
- }
- }
-
如果是Tomcat,現在項目已經可以使用了,但是如果使用其他中間件時報錯的話就在終端輸入
export LD_LIBRARY_PATH=DIR_where_Your_Sigar_Lib_is:$LD_LIBRARY_PATH
或者改/etc/ld.so.conf就可以了。如果使用第一種方式。服務器重啟后就需要重新設置,所以我們還是修改/etc/ld.so.conf吧。/etc/ld.so.conf下面加一行sigar的路徑,保存過后為了讓動態鏈接庫為系統所共享,還需運行動態鏈接庫的管理命令ldconfig一下。
用fusioncharts做實時顯示: