zoukankan      html  css  js  c++  java
  • Sigar使用

    Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API,核心由C语言实现的。项目中使用Sigar进行服务器监控。很多人都对它进行了介绍:

    上面两个链接中有对Sigar的介绍和使用方法。官方的介绍在这里,英语好的话还是看这个吧:

    通过上面的链接,测试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.4sigar-binlib这个文件夹重命名为sigar放到项目的srcmain esources目录下
    • 增加如下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做实时显示:

  • 相关阅读:
    2016年会有感之测试解决方案
    APP测试走过的那些坑
    2016年终总结——测试基础篇(二)
    2016年终总结——测试基础篇(一)
    分享篇——我的Java学习路线
    selenium使用笔记(三)——元素定位
    selenium使用笔记(二)——Tesseract OCR
    selenium使用笔记(一)——selenium你该知道的
    对新手学习自动化的一些感想
    Maven的配置和使用(三)
  • 原文地址:https://www.cnblogs.com/mr-totoro/p/4974979.html
Copyright © 2011-2022 走看看