您的位置:首页 > 运维架构

主机系统监控指标获取方法

2014-08-09 21:41 232 查看
最近项目需要根据不同主机相关监控指标下发任务,由于考虑到尽量减少很多第三方软件依赖给系统部署及运维带来的问题,不打算安装ganglia来做。经过一天时间的调研发现了Sigar这个功能强大的开源项目。Sigar本身是C语言开发完成的,通过本地方法调用获取系统指标:

1、操作系统信息;

2、CPU信息;

3、内存信息;

4、进程信息;

5、文件系统信息;

6、网络接口信息;

7、网络路由和链接表信息。

项目中提供一个简单工厂类,获取Sigar对象。同时需要将链接库文件放入src/resources/lib目录下,最终这些链接文件会通过maven打包放入classes/lib。需要注意的是要对这些链接文件打包的时候设置resouce filter进行处理,防止maven打包的时候修改这些文件内容。

<build>

<!-- 通过设置两个resource解决maven编译打包的时候对依赖的二进制链接文件修改的问题 -->

<resources>

<resource>

<directory>src/main/resources</directory>

<filtering>true</filtering>

<includes>

<include>**/*.xml</include>

</includes>

</resource>

<resource>

<directory>src/main/resources</directory>

<filtering>false</filtering>

<excludes>

<exclude>**/*.xml</exclude>

</excludes>

</resource>

</resources>

</build>

简单工厂类:

public class SigarFactory {
private static final Logger LOG = LoggerFactory.getLogger(SigarFactory.class);
private static String LIB_PATH = SigarFactory.class.getProtectionDomain().getCodeSource().getLocation().getFile();
private static Sigar instance = null;

static {
String os = System.getProperty("os.name");
if(!StringUtils.isEmpty(os) && os.toLowerCase().contains("windows")) {
try {
LIB_PATH = URLDecoder.decode(LIB_PATH, "UTF-8").substring(1) + "lib";
} catch (UnsupportedEncodingException e) {
LOG.error("fetch path of link files which are used by Sigar met errors.", e);
}
}
}

private SigarFactory() { }

public static Sigar getInstance() {
if (instance == null) {
// 同步块,线程安全的创建实例
synchronized (SigarFactory.class) {
if (instance == null) {
System.setProperty("org.hyperic.sigar.path", LIB_PATH);
instance = new Sigar();
}
}
}
return instance;

}
}


sigar依赖的本地库下载地址:http://url.cn/PSyCnF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: