Permalink
Browse files

Added ulimit metrics

  • Loading branch information...
cb372 authored and Chris Birchall committed Feb 28, 2012
1 parent 0eb5317 commit 620c194ebffc8412f3a62da6b2b56c94810fd0e6
View
@@ -26,7 +26,16 @@ Currently the following data can be collected:
* Disk I/O (iostat)
* TODO
* Resource limits (ulimit)
- * TODO
+ * Core file size
+ * Data segment size
+ * File size
+ * Pipe size
+ * Memory size
+ * Open files
+ * Stack size
+ * CPU time
+ * User processes
+ * Virtual memory
* JVM process
* TODO
@@ -16,6 +16,7 @@ public static SigarMetrics getInstance() {
private final CpuMetrics cpu = new CpuMetrics(sigar);
private final MemoryMetrics memory = new MemoryMetrics(sigar);
private final FilesystemMetrics fs = new FilesystemMetrics(sigar);
+ private final UlimitMetrics ulimit = new UlimitMetrics(sigar);
private SigarMetrics() {
// singleton
@@ -31,6 +32,8 @@ public void registerGauges() {
public void registerGauges(MetricsRegistry registry) {
cpu.registerGauges(registry);
memory.registerGauges(registry);
+ fs.registerGauges(registry);
+ ulimit.registerGauges(registry);
}
public CpuMetrics cpu() {
@@ -44,4 +47,8 @@ public MemoryMetrics memory() {
public FilesystemMetrics filesystems() {
return fs;
}
+
+ public UlimitMetrics ulimit() {
+ return ulimit;
+ }
}
@@ -0,0 +1,106 @@
+package com.yammer.metrics.sigar;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import com.yammer.metrics.Metrics;
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.MetricsRegistry;
+
+import org.hyperic.sigar.ResourceLimit;
+import org.hyperic.sigar.Sigar;
+import org.hyperic.sigar.SigarException;
+
+public class UlimitMetrics extends AbstractSigarMetric {
+ private final long infinity;
+
+ protected UlimitMetrics(Sigar sigar) {
+ super(sigar);
+ infinity = ResourceLimit.INFINITY();
+ }
+
+ public static final class Ulimit {
+ private final long coreFileSize, dataSegSize,
+ fileSize, pipeSize,
+ memSize, openFiles,
+ stackSize, cpuTime,
+ processes, virtMem;
+
+ private Ulimit(//
+ long coreFileSize, long dataSegSize, //
+ long fileSize, long pipeSize, //
+ long memSize, long openFiles, //
+ long stackSize, long cpuTime, //
+ long processes, long virtMem) {
+ this.coreFileSize = coreFileSize;
+ this.dataSegSize = dataSegSize;
+ this.fileSize = fileSize;
+ this.pipeSize = pipeSize;
+ this.memSize = memSize;
+ this.openFiles = openFiles;
+ this.stackSize = stackSize;
+ this.cpuTime = cpuTime;
+ this.processes = processes;
+ this.virtMem = virtMem;
+ }
+
+ public static Ulimit fromSigarBean(ResourceLimit lim, long infinity) {
+ return new Ulimit( //
+ replaceInfinity(lim.getCoreCur(), infinity), //
+ replaceInfinity(lim.getDataCur(), infinity), //
+ replaceInfinity(lim.getFileSizeCur(), infinity), //
+ replaceInfinity(lim.getPipeSizeCur(), infinity), //
+ replaceInfinity(lim.getMemoryCur(), infinity), //
+ replaceInfinity(lim.getOpenFilesCur(), infinity), //
+ replaceInfinity(lim.getStackCur(), infinity), //
+ replaceInfinity(lim.getCpuCur(), infinity), //
+ replaceInfinity(lim.getProcessesCur(), infinity), //
+ replaceInfinity(lim.getVirtualMemoryCur(), infinity));
+ }
+
+ public static Ulimit undef() {
+ return new Ulimit(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L);
+ }
+
+ public long coreFileSize() { return coreFileSize; }
+ public long dataSegSize() { return dataSegSize; }
+ public long fileSize() { return fileSize; }
+ public long pipeSize() { return pipeSize; }
+ public long memSize() { return memSize; }
+ public long openFiles() { return openFiles; }
+ public long stackSize() { return stackSize; }
+ public long cpuTime() { return cpuTime; }
+ public long processes() { return processes; }
+ public long virtMemSize() { return virtMem; }
+
+ private static long replaceInfinity(long value, long infinity) {
+ if (value == infinity) {
+ return -1L;
+ } else {
+ return value;
+ }
+ }
+ }
+
+ public Ulimit ulimit() {
+ try {
+ return Ulimit.fromSigarBean(sigar.getResourceLimit(), infinity);
+ } catch (SigarException e) {
+ return Ulimit.undef();
+ }
+ }
+
+ public void registerGauges(MetricsRegistry registry) {
+ registry.newGauge(getClass(), "ulimit-open-files", new Gauge<Long>() {
+ public Long value() {
+ return ulimit().openFiles();
+ }
+ });
+ registry.newGauge(getClass(), "ulimit-stack-size", new Gauge<Long>() {
+ public Long value() {
+ return ulimit().stackSize();
+ }
+ });
+ }
+
+}
@@ -0,0 +1,24 @@
+package com.yammer.metrics.sigar.tests;
+
+import com.yammer.metrics.sigar.UlimitMetrics;
+import com.yammer.metrics.sigar.UlimitMetrics.Ulimit;
+import com.yammer.metrics.sigar.SigarMetrics;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.closeTo;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
+
+import static org.junit.Assert.assertThat;
+
+public class UlimitMetricsTest {
+ private final UlimitMetrics um = SigarMetrics.getInstance().ulimit();
+
+ @Test
+ public void openFilesLimitIsGreaterThanZero() throws Exception {
+ assertThat(um.ulimit().openFiles(), is(greaterThan(0L)));
+ }
+}

0 comments on commit 620c194

Please sign in to comment.