club.zhcs.sigar.SigarMetrics Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of axe-sigar Show documentation
Show all versions of axe-sigar Show documentation
spring-boot使用 sigar增加监控信息
package club.zhcs.sigar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
/**
* @author kerbores
*
*/
public class SigarMetrics implements PublicMetrics {
Log logger = Logs.get();
public Collection> metrics() {
List> metrics = new ArrayList>();
try {
final Sigar sigar = SigarFactory.load();
// CPU
metrics.add(new Metric("cpu.total", sigar.getCpu().getTotal()));
metrics.add(new Metric("cpu.idle", sigar.getCpu().getIdle()));
metrics.add(new Metric("cpu.irq", sigar.getCpu().getIrq()));
metrics.add(new Metric("cpu.nice", sigar.getCpu().getNice()));
metrics.add(new Metric("cpu.soft.irq", sigar.getCpu().getSoftIrq()));
metrics.add(new Metric("cpu.stolen", sigar.getCpu().getStolen()));
metrics.add(new Metric("cpu.sys", sigar.getCpu().getSys()));
metrics.add(new Metric("cpu.user", sigar.getCpu().getUser()));
metrics.add(new Metric("cpu.wait", sigar.getCpu().getWait()));
metrics.add(new Metric("cpu.usage", sigar.getCpuPerc().getCombined() * 100));
// MEM
metrics.add(new Metric("mem.free", sigar.getMem().getFree()));
metrics.add(new Metric("mem.actual.free", sigar.getMem().getActualFree()));
metrics.add(new Metric("mem.actual.used", sigar.getMem().getActualUsed()));
metrics.add(new Metric("mem.free.percent", sigar.getMem().getFreePercent()));
metrics.add(new Metric("mem.ram", sigar.getMem().getRam()));
metrics.add(new Metric("mem.total", sigar.getMem().getTotal()));
metrics.add(new Metric("mem.used", sigar.getMem().getUsed()));
metrics.add(new Metric("mem.user.percent", sigar.getMem().getUsedPercent()));
// NET
metrics.add(new Metric("tcp.active.opens", sigar.getTcp().getActiveOpens()));
metrics.add(new Metric("tcp.attempt.fails", sigar.getTcp().getAttemptFails()));
metrics.add(new Metric("tcp.curre.stab", sigar.getTcp().getCurrEstab()));
metrics.add(new Metric("tcp.estab.resets", sigar.getTcp().getEstabResets()));
metrics.add(new Metric("tcp.in.errs", sigar.getTcp().getInErrs()));
metrics.add(new Metric("tcp.in.segs", sigar.getTcp().getInSegs()));
metrics.add(new Metric("tcp.out.rsts", sigar.getTcp().getOutRsts()));
metrics.add(new Metric("tcp.out.segs", sigar.getTcp().getOutSegs()));
metrics.add(new Metric("tcp.passive.opens", sigar.getTcp().getPassiveOpens()));
metrics.add(new Metric("tcp.retrans.segs", sigar.getTcp().getRetransSegs()));
// SWAP
metrics.add(new Metric("swap.free", sigar.getSwap().getFree()));
metrics.add(new Metric("swap.page.in", sigar.getSwap().getPageIn()));
metrics.add(new Metric("swap.page.out", sigar.getSwap().getPageOut()));
metrics.add(new Metric("swap.total", sigar.getSwap().getTotal()));
metrics.add(new Metric("swap.used", sigar.getSwap().getUsed()));
metrics.add(new Metric("swap.usage", sigar.getSwap().getUsed() * 100 / sigar.getSwap().getTotal()));
// JVM
Runtime runtime = Runtime.getRuntime();
long max = runtime.maxMemory();
long total = runtime.totalMemory();
long free = runtime.freeMemory();
long usable = max - total + free;
double freePercent = 100 * usable / max;
double usedPercent = 100 - freePercent;
metrics.add(new Metric("jvm.max", max));
metrics.add(new Metric("jvm.free", free));
metrics.add(new Metric("jvm.total", total));
metrics.add(new Metric("jvm.usable", usable));
metrics.add(new Metric("jvm.free.percent", freePercent));
metrics.add(new Metric("jvm.usage", usedPercent));
long read = 0;
long write = 0;
for (FileSystem fs : sigar.getFileSystemList()) {
if (fs.getType() == FileSystem.TYPE_LOCAL_DISK) {
try {
FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
read += usage.getDiskReadBytes();
write += usage.getDiskWriteBytes();
} catch (SigarException e) {
e.printStackTrace();
}
}
}
metrics.add(new Metric("disk.read", read));
metrics.add(new Metric("disk.write", write));
} catch (SigarException e) {
e.printStackTrace();
}
return metrics;
}
}