使用Sigar采系统信息

使用Sigar采系统信息nbsp 简介 Sigar SystemInform 开源的跨平台系统信息收集工具 C 语言实现 可以监控服务器性能信息 例如 cpu mem disk 等使用信息 nbsp 下载 nbsp Hyperic hq 官方网站 http www hyperic comSigar jar 下载地址 http sigar hyperic com nbsp 使用

 

简介
Sigar(System Information Gatherer And Reporter),开源的跨平台系统信息收集工具,C语言实现。
可以监控服务器性能信息,例如cpu、mem、disk等使用信息。




 

下载 
Hyperic-hq官方网站:http://www.hyperic.com
Sigar.jar下载地址:http://sigar.hyperic.com




 

使用

非Maven:直接拷贝下载压缩包中的Sigar.jar 到你的项目lib目录

 Sigar中提供的基本类

/* * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. * This file is part of SIGAR. * * SIGAR is free software; you can redistribute it and/or modify * it under the terms version 2 of the GNU General Public License as * published by the Free Software Foundation. This program is distributed * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ package com.study.sigar; import java.io.PrintStream; import java.util.Arrays; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarProxy; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.cmd.Shell; import org.hyperic.sigar.pager.PageControl; import org.hyperic.sigar.pager.PageFetchException; import org.hyperic.sigar.pager.StaticPageFetcher; import org.hyperic.sigar.util.GetlineCompleter; import org.hyperic.sigar.util.PrintfFormat; import org.hyperic.sigar.shell.CollectionCompleter; import org.hyperic.sigar.shell.ProcessQueryCompleter; import org.hyperic.sigar.shell.ShellCommandBase; import org.hyperic.sigar.shell.ShellCommandExecException; import org.hyperic.sigar.shell.ShellCommandUsageException; public abstract class SigarCommandBase extends ShellCommandBase implements GetlineCompleter { protected Shell shell; protected PrintStream out = System.out; protected PrintStream err = System.err; protected Sigar sigar; protected SigarProxy proxy; protected List output = new ArrayList(); private CollectionCompleter completer; private GetlineCompleter ptqlCompleter; private Collection completions = new ArrayList(); private PrintfFormat formatter; private ArrayList printfItems = new ArrayList(); public SigarCommandBase(Shell shell) { this.shell = shell; this.out = shell.getOutStream(); this.err = shell.getErrStream(); this.sigar = shell.getSigar(); this.proxy = shell.getSigarProxy(); //provide simple way for handlers to implement tab completion this.completer = new CollectionCompleter(shell); if (isPidCompleter()) { this.ptqlCompleter = new ProcessQueryCompleter(shell); } } public SigarCommandBase() { this(new Shell()); this.shell.setPageSize(PageControl.SIZE_UNLIMITED); } public void setOutputFormat(String format) { this.formatter = new PrintfFormat(format); } public PrintfFormat getFormatter() { return this.formatter; } public String sprintf(String format, Object[] items) { return new PrintfFormat(format).sprintf(items); } public void printf(String format, Object[] items) { println(sprintf(format, items)); } public void printf(Object[] items) { PrintfFormat formatter = getFormatter(); if (formatter == null) { //see flushPrintfItems this.printfItems.add(items); } else { println(formatter.sprintf(items)); } } public void printf(List items) { printf((Object[])items.toArray(new Object[0])); } public void println(String line) { if (this.shell.isInteractive()) { this.output.add(line); } else { this.out.println(line); } } private void flushPrintfItems() { if (this.printfItems.size() == 0) { return; } //no format was specified, just line up the columns int[] max = null; for (Iterator it=this.printfItems.iterator(); it.hasNext();) { Object[] items = (Object[])it.next(); if (max == null) { max = new int[items.length]; Arrays.fill(max, 0); } for (int i=0; i 
  
    max[i]) { max[i] = len; } } } StringBuffer format = new StringBuffer(); for (int i=0; i 
   
     = 1) && Character.isDigit(line.charAt(0))) { return line; } return this.ptqlCompleter.complete(line); } public String complete(String line) { if (isPidCompleter()) { return completePid(line); } GetlineCompleter c = getCompleter(); if (c != null) { return c.complete(line); } this.completer.setCollection(getCompletions()); return this.completer.complete(line); } } 
    
  

  采集系统信息

package com.study.sigar; import java.sql.Connection; import java.sql.PreparedStatement; import java.text.DecimalFormat; import java.util.HashMap; import org.apache.log4j.Logger; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.FileSystem; import org.hyperic.sigar.FileSystemUsage; import org.hyperic.sigar.Mem; import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.NfsFileSystem; import org.hyperic.sigar.SigarException; / * @Title: * @author Administrator * @version 1.0 */ public class SigarCollect extends SigarCommandBase { private Logger log = Logger.getLogger(SigarCollect.class); private HashMap info = new HashMap(); // Probe Name public static void main(String[] args) throws Exception { new SigarCollect().processCommand(args); } public static void collect() throws Exception { String[] args = new String[]{}; new SigarCollect().processCommand(args); } public void output(String[] args) throws SigarException { info.clear(); log.info("Sigar collect probe host info..."); //IP MAC NetInterfaceConfig config = this.sigar.getNetInterfaceConfig(null); info.put("CollectorIP", config.getAddress()); info.put("Mac", config.getHwaddr()); //采集机名称 org.hyperic.sigar.NetInfo netInfo = this.sigar.getNetInfo(); info.put("CollectorName", netInfo.getHostName()); //内存大小单位G Mem mem = this.sigar.getMem(); info.put("hMemory", mem.getTotal()/1024/1024/1024); //Mem占用百分比 String sMemory = format( mem.getUsedPercent()); info.put("Memory", sMemory); //CPU占用百分比 CpuPerc cpu = sigar.getCpuPerc(); String sCpu = format(cpu.getCombined() * 100); info.put("Cpu", sCpu); //CPU大小 格式如 3*4Core2.67GHz org.hyperic.sigar.CpuInfo[] cpuInfos = this.sigar.getCpuInfoList(); org.hyperic.sigar.CpuInfo cpuInfo = cpuInfos[0]; info.put("hCPU", cpuInfo.getModel()); //磁盘/文件系统 outputFile(); info.put("Time_stamp", System.currentTimeMillis() + ""); log.info("Collected probe host info " + info); } / * 采文件系统 */ public void outputFile() throws SigarException { FileSystem[] fslist = this.proxy.getFileSystemList(); double totalSize = 0l; double totalUsed = 0l; for (int i = 0; i < fslist.length; i++) { FileSystem fs = fslist[i]; try { FileSystemUsage usage = null; if (fs instanceof NfsFileSystem) { NfsFileSystem nfs = (NfsFileSystem) fs; if (!nfs.ping()) { log.warn(nfs.getUnreachableMessage()); return; } } try { usage = this.sigar.getFileSystemUsage(fs.getDirName()); } catch(Exception e){ continue; } totalSize += Double.parseDouble(usage.getTotal() + ""); // 总大小 totalUsed += Double.parseDouble(usage.getUsed() + ""); // 总已使用 } catch (Exception e) { log.error("Get host file system info error", e); } } //磁盘占用百分比 double percent = (double)(totalUsed/totalSize*100); String sPercent = format(percent); info.put("Disk", sPercent); //磁盘大小单位G double dTotalFileSize = totalSize/1024/1024; long iTotalFileSize = Math.round(dTotalFileSize); info.put("hDisk", iTotalFileSize); } / * 保留2位小数 * @param d * @return */ private String format(double d) { DecimalFormat fnum = new DecimalFormat("0.00"); String dd = fnum.format(d); return dd; } } 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210854.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:45
下一篇 2026年3月18日 下午11:46


相关推荐

  • acwing-240. 食物链(并查集+边权值)[通俗易懂]

    acwing-240. 食物链(并查集+边权值)[通俗易懂]动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。A 吃 B,B 吃 C,C 吃 A。现有 N 个动物,以 1∼N 编号。每个动物都是 A,B,C 中的一种,但是我们并不知道它到底是哪一种。有人用两种说法对这 N 个动物所构成的食物链关系进行描述:第一种说法是 1 X Y,表示 X 和 Y 是同类。第二种说法是 2 X Y,表示 X 吃 Y。此人对 N 个动物,用上述两种说法,一句接一句地说出 K 句话,这 K 句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句

    2022年8月10日
    18
  • mac Hadoop环境变量配置

    mac Hadoop环境变量配置Mac打开、编辑.bash_profile文件一般在Mac上配置环境变量时经常要创建、编辑.bash_profile文件。创建该文件时一般都会选择在当前用户目录下,即Mac下的.bash_profile文件的路径是/Users/YourMacUserName/.bash_profile(如果该文件已经创建过的话)默认情况下,系统不显示隐藏文件,可在终端中输如下代码,即可显示…

    2022年6月21日
    78
  • 蒙特卡洛算法案例_蒙特卡洛原理

    蒙特卡洛算法案例_蒙特卡洛原理从今天开始要研究SamplingMethods,主要是MCMC算法。本文是开篇文章,先来了解蒙特卡洛算法。Contents1.蒙特卡洛介绍2.蒙特卡洛的应用3.蒙特卡洛积分1.蒙特

    2022年8月1日
    11
  • javah命令详解「建议收藏」

    javah命令详解「建议收藏」概述:最近在写c++/c的一个小的项目,需要打成动态库,供java使用。就对java调用c++/c代码做了简答了解,在此做记录。jni开发第一步,就是用javah命令生成生成c\c++头文件。javah命令参数详解cmd(默认配置jdkpath)执行javah-help如下图:-d和-o这两个参数用于设置生成的C\C++头文件的指定,该两参数选项不能同时使…

    2026年3月10日
    3
  • 使用JS读取本地文件

    使用JS读取本地文件使用 XMLHttpReque 发起请求 读取本地文件 主要总结了三种方法 如下所示 创建 XMLHttpReque 请求 constxhrFile newXMLHttpRe 建立连接 第三个参数为同步或异步请求 xhrFile open GET filePath false method1xhrFi onload function constallText xhrFile response callback al

    2026年3月19日
    3
  • IT速查手册[通俗易懂]

    IT速查手册[通俗易懂]发现一个很好的点评公司网站,很有创意。里面大部分的评论都来自公司内部的员工。  想找IT工作的,可以先到那里了解公司的待遇、环境、内部等问题。  公司点评网:

    2022年7月1日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号