<dependency> <groupId>org.fusesource</groupId> <artifactId>sigar</artifactId> <version>1.6.4</version> </dependency>
1、首先是Sigar中提供的DLL如何加载,可以通过设置其为CLASSPATH后,我们还可以通过程序来把只需要的DLL复制到默认的CLASSPATH路径下。
首先是通过参数的判断,是加载那种DLL,下面给出加载DLL的函数:
private static Logger logger = Logger.getLogger(ListenerMain.class); public static void main(String[] args) throws SigarException { try { logger.debug(System.getProperty("java.library.path")); InputStream inputStream = new FileInputStream("../config/log4j.properties"); PropertyConfigurator.configure(inputStream); if (null == args) { logger.error("请输入系统架构,例如Window 32位,则输入win32,win64,如果是Linux,则输入linux32,linux,目前只支持这2个系统"); return; } // String sys = args[0]; String sys = "win32"; if (null != args && args.length > 0) { sys = args[0]; } String dllPath = ""; String dllName = ""; if ("win32".equals(sys)) { dllPath = "/dll/sigar-x86-winnt.dll"; dllName = "sigar-x86-winnt.dll"; } else if ("win64".equals(sys)) { dllPath = "/dll/sigar-amd64-winnt.dll"; dllName = "sigar-amd64-winnt.dll"; } else if ("linux32".equals(sys)) { dllPath = "/dll/libsigar-x86-linux.so"; dllName = "libsigar-x86-linux.so"; } else if (("linux64").equals(sys)) { dllPath = "/dll/libsigar-amd64-linux.so"; dllName = "libsigar-amd64-linux.so"; } if (dllPath.equals("")) { logger.error("输入的系统架构信息不正确,请重新输入"); return; } //设置DLL setLibrary(dllPath,dllName); } catch (Exception e) { } } private static void setLibrary(String dllPath,String dllName) { try { // 获取到java.library.path String libpath = System.getProperty("java.library.path"); if (libpath == null || libpath.length() == 0) { throw new RuntimeException("java.library.path is null"); } String path = null; StringTokenizer st = new StringTokenizer(libpath, System.getProperty("path.separator")); if (st.hasMoreElements()) { path = st.nextToken(); } else { throw new RuntimeException("can not split library path:" + libpath); } // 把dll文件写入到java.library.path中该dll放在ConvertWord2HM相同目录下,这个可以是你的类名 InputStream inputStream = ListenerMain.class.getResource(dllPath).openStream(); final File dllFile = new File(new File(path), dllName); if (!dllFile.exists()) { FileOutputStream outputStream = new FileOutputStream(dllFile); byte[] array = new byte[8192]; int bytesRead = -1; while ((bytesRead = inputStream.read(array) )!= -1) { outputStream.write(array, 0, bytesRead); } outputStream.flush(); outputStream.close(); } // 动态加载dll System.load(dllFile.getPath()); // 在虚拟机关闭的时候删除dll // dllFile.deleteOnExit(); } catch (Throwable e) { logger.error("load dll error", e); } }
2、获取服务CPU、内存、硬盘的占用率
public double getDeviceCpuValue() throws SigarException { double combined = 0f; Sigar sigar = new Sigar(); // 方式二,不管是单块CPU还是多CPU都适用 CpuPerc cpuList[] = null; cpuList = sigar.getCpuPercList(); for (int i = 0; i < cpuList.length; i++) { combined = combined + cpuList[i].getCombined(); } logger.debug("device cpu = " + combined); return combined / cpuList.length; }
public float getDeviceMemValue() throws SigarException { // a)物理内存信息 Sigar sigar = new Sigar(); Mem mem; mem = sigar.getMem(); return (float)mem.getUsed() / (float)mem.getTotal(); }
public float getDeviceHdValue() throws SigarException { float hdValue = 0f; Sigar sigar = new Sigar(); FileSystem fslist[] = sigar.getFileSystemList(); float total = 0f; float used = 0f; for (int i = 0; i < fslist.length; i++) { FileSystem fs = fslist[i]; FileSystemUsage usage = null; try { usage = sigar.getFileSystemUsage(fs.getDirName()); } catch (SigarException e) { if (fs.getType() == 2) { throw e; } continue; } switch (fs.getType()) { case 0: // TYPE_UNKNOWN :未知 break; case 1: // TYPE_NONE break; case 2: // TYPE_LOCAL_DISK : 本地硬盘 // 文件系统总大小 total = total + usage.getTotal(); used = used + usage.getUsed(); break; case 3:// TYPE_NETWORK :网络 break; case 4:// TYPE_RAM_DISK :闪存 break; case 5:// TYPE_CDROM :光驱 break; case 6:// TYPE_SWAP :页面交换 break; } } hdValue = used / total; return hdValue; }
3、Sigar不能正确获取某个进程的占用率,只能通过Shell脚本来实现,但是可以获取某个进程内存的使用率:
float memValue = (float)procMem.getResident() / (float)sigar.getMem().getTotal();
4、如何判断网卡是否正常使用,在Sigar并没有提供这样的方法,但是通过判断是否存在正常的IP,则说明
该网卡是否正常使用。
public Map<String, String> getNeWorkCards() throws SigarException { Sigar sigar = null; sigar = new Sigar(); String[] ifaces = sigar.getNetInterfaceList(); Map<String, String> map = new HashMap<String, String>(); for (int i = 0; i < ifaces.length; i++) { NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]); if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || NetFlags.LOOPBACK_ADDRESS_V6.equals(cfg.getAddress())) { continue; } if ((NetFlags.ANY_ADDR.equals(cfg.getAddress()) && NetFlags.ANY_ADDR.equals(cfg.getBroadcast())) || (NetFlags.ANY_ADDR_V6.equals(cfg.getAddress()) && NetFlags.ANY_ADDR_V6.equals(cfg.getBroadcast()))) { map.put(ifaces[i], AlarmContants.ABNORMAL + ""); } else { map.put(ifaces[i], AlarmContants.NORMAL + ""); } } return map; }
5、在Window及Linux上,Sigar得到一些信息还是有很大区别的,一般来说,Window得到的信息更全些,
而Linux相对比较少,这就需要我们自己编写Shell脚本去获取一些需要的信息了。
6、Sigar不能获取硬件的信息
相关推荐
Sugarcrm开发指南英文版共享 很好的crm应用
SugarCRM开发帮助文档很好的介绍了SugarCRM的各种业务逻辑及其功能等
里边描述的比较详细,但是 能看懂英文才行
export.sugar百度大屏sugar模版下载
I started this book with the intention of bringing a new side of SugarCRM to light. Since I began working at SugarCRM, I saw the flexibility and extensibility that the application could provide. I ...
sugarcrm developer guide en sugarcrm developer guide en sugarcrm developer guide en
开源php CRM系统,而且很好用,请大家有时间可以研究一下。
Sugar QCT_SP
sugar.zip,使用MotionLayout实现高德地图bottomSheets效果
Soft Sugar
SugarCRM v3.5.1c 简体中文版 See the Installation Guide found on SugarForge.org for installation steps of Sugar Open Source. http://www.sugarcrm.com/crm/documentation.html How to Install a Sugar Patch ...
sugar时间建立在包括php开发环境,Mysql关系数据库,apache或者IIS web服务器和windows或者linux操作系统的基础上的稳定的开源程序,并且得到广泛的工业标准的支持。sugar支持LAMP(Linux,Apache,Mysql,PHP)和 WIMP...
SugarCRM ver 说明 SugarCRM Community Edition SugarCRM Professinal Edition SugarCRM Enterprise Edition
sugarcrm原版教程,国内现在sugarcrm的教程较少,网页上很多翻译粗看比较难理解,如果要系统了解,这个是很不错的教程。
Android数据库框架——Sugar
最新的sugarCRM6.2.0的汉化包!Delphi语言汉化后(更新算法,不过,据说应该用php汉化更简单的,可惜我不会的哈!),手动修改了少量,均通过测试,没问题! ★★★★使用方法: 第1步:记事本打开.\htdocs\...
sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引sugar操作指引
Sugar是一个价格合理并且容易使用的企业级CRM,Sugar的设计初衷是为了帮助您的企业于千载客户沟通,共享销售信息,促成交易以及保持客户开心。 数千家企业每天使用Sugar去管理销售,市场和客户支持。 作为一个开源,...
packt_用开源项目快速建站实践丛书之SugarCRM
sugar Android数据库框架jar包,欢迎同学们下载,sugar 框架用起来方便很多