add os level stats and info (still some more info left...)

This commit is contained in:
kimchy 2010-05-06 23:46:53 +03:00
parent 4cfd55c1dc
commit b54f6b77de
12 changed files with 696 additions and 4 deletions

View File

@ -21,6 +21,7 @@
<w>committable</w> <w>committable</w>
<w>configurator</w> <w>configurator</w>
<w>coord</w> <w>coord</w>
<w>cpus</w>
<w>datagram</w> <w>datagram</w>
<w>desc</w> <w>desc</w>
<w>deserialize</w> <w>deserialize</w>

View File

@ -30,6 +30,10 @@ import org.elasticsearch.monitor.jvm.JvmService;
import org.elasticsearch.monitor.memory.MemoryMonitor; import org.elasticsearch.monitor.memory.MemoryMonitor;
import org.elasticsearch.monitor.memory.MemoryMonitorService; import org.elasticsearch.monitor.memory.MemoryMonitorService;
import org.elasticsearch.monitor.memory.alpha.AlphaMemoryMonitor; import org.elasticsearch.monitor.memory.alpha.AlphaMemoryMonitor;
import org.elasticsearch.monitor.os.JmxOsProbe;
import org.elasticsearch.monitor.os.OsProbe;
import org.elasticsearch.monitor.os.OsService;
import org.elasticsearch.monitor.os.SigarOsProbe;
import org.elasticsearch.monitor.process.JmxProcessProbe; import org.elasticsearch.monitor.process.JmxProcessProbe;
import org.elasticsearch.monitor.process.ProcessProbe; import org.elasticsearch.monitor.process.ProcessProbe;
import org.elasticsearch.monitor.process.ProcessService; import org.elasticsearch.monitor.process.ProcessService;
@ -76,6 +80,7 @@ public class MonitorModule extends AbstractModule {
if (sigarService.sigarAvailable()) { if (sigarService.sigarAvailable()) {
bind(SigarService.class).toInstance(sigarService); bind(SigarService.class).toInstance(sigarService);
bind(ProcessProbe.class).to(SigarProcessProbe.class).asEagerSingleton(); bind(ProcessProbe.class).to(SigarProcessProbe.class).asEagerSingleton();
bind(OsProbe.class).to(SigarOsProbe.class).asEagerSingleton();
sigarLoaded = true; sigarLoaded = true;
} }
} catch (Throwable e) { } catch (Throwable e) {
@ -84,9 +89,11 @@ public class MonitorModule extends AbstractModule {
if (!sigarLoaded) { if (!sigarLoaded) {
// bind non sigar implementations // bind non sigar implementations
bind(ProcessProbe.class).to(JmxProcessProbe.class).asEagerSingleton(); bind(ProcessProbe.class).to(JmxProcessProbe.class).asEagerSingleton();
bind(OsProbe.class).to(JmxOsProbe.class).asEagerSingleton();
} }
// bind other services // bind other services
bind(ProcessService.class).asEagerSingleton(); bind(ProcessService.class).asEagerSingleton();
bind(OsService.class).asEagerSingleton();
bind(JvmService.class).asEagerSingleton(); bind(JvmService.class).asEagerSingleton();
bind(JvmMonitorService.class).asEagerSingleton(); bind(JvmMonitorService.class).asEagerSingleton();

View File

@ -0,0 +1,44 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
import org.elasticsearch.util.component.AbstractComponent;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
/**
* @author kimchy (shay.banon)
*/
public class JmxOsProbe extends AbstractComponent implements OsProbe {
@Inject public JmxOsProbe(Settings settings) {
super(settings);
}
@Override public OsInfo osInfo() {
return new OsInfo();
}
@Override public OsStats osStats() {
OsStats stats = new OsStats();
stats.timestamp = System.currentTimeMillis();
return stats;
}
}

View File

@ -0,0 +1,157 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import java.io.IOException;
import java.io.Serializable;
/**
* @author kimchy (shay.banon)
*/
public class OsInfo implements Streamable, Serializable {
String cpuVendor = "";
String cpuModel = "";
int cpuMhz = -1;
int cpuTotalCores = -1;
int cpuTotalSockets = -1;
int cpuCoresPerSocket = -1;
long cpuCacheSize = -1;
long memTotal = -1;
long swapTotal = -1;
OsInfo() {
}
public String cpuVendor() {
return cpuVendor;
}
public String getCpuVendor() {
return cpuVendor();
}
public String cpuModel() {
return cpuModel;
}
public String getCpuModel() {
return cpuModel();
}
public int cpuMhz() {
return cpuMhz;
}
public int getCpuMhz() {
return cpuMhz();
}
public int cpuTotalCores() {
return cpuTotalCores;
}
public int getCpuTotalCores() {
return cpuTotalCores();
}
public int cpuTotalSockets() {
return cpuTotalSockets;
}
public int getCpuTotalSockets() {
return cpuTotalSockets();
}
public int cpuCoresPerSocket() {
return cpuCoresPerSocket;
}
public int getCpuCoresPerSocket() {
return cpuCoresPerSocket();
}
public SizeValue cpuCacheSize() {
return new SizeValue(cpuCacheSize);
}
public SizeValue getCpuCacheSize() {
return cpuCacheSize();
}
public SizeValue memTotal() {
return new SizeValue(memTotal);
}
public SizeValue getMemTotal() {
return memTotal();
}
public SizeValue swapTotal() {
return new SizeValue(swapTotal);
}
public SizeValue getSwapTotal() {
return swapTotal();
}
public static OsInfo readOsInfo(StreamInput in) throws IOException {
OsInfo info = new OsInfo();
info.readFrom(in);
return info;
}
@Override public void readFrom(StreamInput in) throws IOException {
cpuVendor = in.readUTF();
cpuModel = in.readUTF();
cpuMhz = in.readInt();
cpuTotalCores = in.readInt();
cpuTotalSockets = in.readInt();
cpuCoresPerSocket = in.readInt();
cpuCacheSize = in.readLong();
memTotal = in.readLong();
swapTotal = in.readLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(cpuVendor);
out.writeUTF(cpuModel);
out.writeInt(cpuMhz);
out.writeInt(cpuTotalCores);
out.writeInt(cpuTotalSockets);
out.writeInt(cpuCoresPerSocket);
out.writeLong(cpuCacheSize);
out.writeLong(memTotal);
out.writeLong(swapTotal);
}
}

View File

@ -0,0 +1,30 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
/**
* @author kimchy (shay.banon)
*/
public interface OsProbe {
OsInfo osInfo();
OsStats osStats();
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
import org.elasticsearch.util.component.AbstractComponent;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
/**
* @author kimchy (shay.banon)
*/
public class OsService extends AbstractComponent {
private final OsProbe probe;
private final OsInfo info;
@Inject public OsService(Settings settings, OsProbe probe) {
super(settings);
this.probe = probe;
this.info = probe.osInfo();
logger.trace("Using probe [{}]", probe);
}
public OsInfo info() {
return this.info;
}
public OsStats stats() {
return probe.osStats();
}
}

View File

@ -0,0 +1,229 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
import org.elasticsearch.util.Percent;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
/**
* @author kimchy (shay.banon)
*/
public class OsStats implements Streamable, Serializable {
public static final double[] EMPTY_LOAD = new double[0];
long timestamp;
double[] loadAverage = EMPTY_LOAD;
long uptime = -1;
double cpuSys = -1;
double cpuUser = -1;
double cpuIdle = -1;
long memFree = -1;
double memFreePercent = -1;
long memUsed = -1;
double memUsedPercent = -1;
long memActualFree = -1;
long memActualUsed = -1;
long swapFree = -1;
long swapUsed = -1;
OsStats() {
}
public long timestamp() {
return timestamp;
}
public long getTimestamp() {
return timestamp();
}
public double[] loadAverage() {
return loadAverage;
}
public double[] getLoadAverage() {
return loadAverage();
}
public TimeValue uptime() {
return new TimeValue(uptime, TimeUnit.SECONDS);
}
public TimeValue getUptime() {
return uptime();
}
public Percent cpuSys() {
return new Percent(cpuSys);
}
public Percent getCpuSys() {
return cpuSys();
}
public Percent cpuUser() {
return new Percent(cpuUser);
}
public Percent getCpuUser() {
return cpuUser();
}
public Percent cpuIdle() {
return new Percent(cpuIdle);
}
public Percent getCpuIdle() {
return cpuIdle();
}
public SizeValue memUsed() {
return new SizeValue(memUsed);
}
public SizeValue getMemUsed() {
return memUsed();
}
public Percent memUsedPercent() {
return new Percent(memUsedPercent);
}
public Percent getMemUsedPercent() {
return memUsedPercent();
}
public SizeValue memFree() {
return new SizeValue(memFree);
}
public SizeValue getMemFree() {
return memFree();
}
public Percent memFreePercent() {
return new Percent(memFreePercent);
}
public Percent getMemFreePercent() {
return memFreePercent();
}
public SizeValue memActualFree() {
return new SizeValue(memActualFree);
}
public SizeValue getMemActualFree() {
return memActualFree();
}
public SizeValue memActualUsed() {
return new SizeValue(memActualUsed);
}
public SizeValue getMemActualUsed() {
return memActualUsed();
}
public SizeValue swapUsed() {
return new SizeValue(swapUsed);
}
public SizeValue getSwapUsed() {
return swapUsed();
}
public SizeValue swapFree() {
return new SizeValue(swapFree);
}
public SizeValue getSwapFree() {
return swapFree();
}
public static OsStats readOsStats(StreamInput in) throws IOException {
OsStats stats = new OsStats();
stats.readFrom(in);
return stats;
}
@Override public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong();
loadAverage = new double[in.readVInt()];
for (int i = 0; i < loadAverage.length; i++) {
loadAverage[i] = in.readDouble();
}
uptime = in.readLong();
cpuSys = in.readDouble();
cpuUser = in.readDouble();
cpuIdle = in.readDouble();
memFree = in.readLong();
memFreePercent = in.readDouble();
memUsed = in.readLong();
memUsedPercent = in.readDouble();
memActualFree = in.readLong();
memActualUsed = in.readLong();
swapFree = in.readLong();
swapUsed = in.readLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
out.writeVInt(loadAverage.length);
for (double val : loadAverage) {
out.writeDouble(val);
}
out.writeLong(uptime);
out.writeDouble(cpuSys);
out.writeDouble(cpuUser);
out.writeDouble(cpuIdle);
out.writeLong(memFree);
out.writeDouble(memFreePercent);
out.writeLong(memUsed);
out.writeDouble(memUsedPercent);
out.writeLong(memActualFree);
out.writeLong(memActualUsed);
out.writeLong(swapFree);
out.writeLong(swapFree);
}
}

View File

@ -0,0 +1,123 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.monitor.os;
import org.elasticsearch.monitor.sigar.SigarService;
import org.elasticsearch.util.component.AbstractComponent;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
import org.hyperic.sigar.*;
/**
* @author kimchy (shay.banon)
*/
public class SigarOsProbe extends AbstractComponent implements OsProbe {
private final SigarService sigarService;
@Inject public SigarOsProbe(Settings settings, SigarService sigarService) {
super(settings);
this.sigarService = sigarService;
}
@Override public OsInfo osInfo() {
Sigar sigar = sigarService.sigar();
OsInfo info = new OsInfo();
try {
CpuInfo[] infos = sigar.getCpuInfoList();
info.cpuVendor = infos[0].getVendor();
info.cpuModel = infos[0].getModel();
info.cpuMhz = infos[0].getMhz();
info.cpuTotalCores = infos[0].getTotalCores();
info.cpuTotalSockets = infos[0].getTotalSockets();
info.cpuCoresPerSocket = infos[0].getCoresPerSocket();
if (infos[0].getCacheSize() != Sigar.FIELD_NOTIMPL) {
info.cpuCacheSize = infos[0].getCacheSize();
}
} catch (SigarException e) {
// ignore
}
try {
Mem mem = sigar.getMem();
info.memTotal = mem.getTotal();
} catch (SigarException e) {
// ignore
}
try {
Swap swap = sigar.getSwap();
info.swapTotal = swap.getTotal();
} catch (SigarException e) {
// ignore
}
return info;
}
@Override public OsStats osStats() {
Sigar sigar = sigarService.sigar();
OsStats stats = new OsStats();
stats.timestamp = System.currentTimeMillis();
try {
stats.loadAverage = sigar.getLoadAverage();
} catch (SigarException e) {
// ignore
}
try {
stats.uptime = (long) sigar.getUptime().getUptime();
} catch (SigarException e) {
// ignore
}
try {
CpuPerc cpuPerc = sigar.getCpuPerc();
stats.cpuSys = cpuPerc.getSys();
stats.cpuUser = cpuPerc.getUser();
stats.cpuIdle = cpuPerc.getIdle();
} catch (SigarException e) {
// ignore
}
try {
Mem mem = sigar.getMem();
stats.memFree = mem.getFree();
stats.memFreePercent = mem.getFreePercent() / 100;
stats.memUsed = mem.getUsed();
stats.memUsedPercent = mem.getUsedPercent() / 100;
stats.memActualFree = mem.getActualFree();
stats.memActualUsed = mem.getActualUsed();
} catch (SigarException e) {
// ignore
}
try {
Swap swap = sigar.getSwap();
stats.swapFree = swap.getFree();
stats.swapUsed = swap.getUsed();
} catch (SigarException e) {
// ignore
}
return stats;
}
}

View File

@ -33,7 +33,7 @@ public class ProcessInfo implements Streamable, Serializable {
private long id; private long id;
private ProcessInfo() { ProcessInfo() {
} }

View File

@ -22,11 +22,19 @@ package org.elasticsearch.monitor.process;
import org.elasticsearch.util.Percent; import org.elasticsearch.util.Percent;
import org.elasticsearch.util.SizeValue; import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.TimeValue; import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import java.io.IOException;
import java.io.Serializable;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class ProcessStats { public class ProcessStats implements Streamable, Serializable {
long timestamp = -1;
double cpuPercent = -1; double cpuPercent = -1;
@ -44,6 +52,17 @@ public class ProcessStats {
long fd; long fd;
ProcessStats() {
}
public long timestamp() {
return this.timestamp;
}
public long getTimestamp() {
return timestamp();
}
/** /**
* Get the Process cpu usage. * Get the Process cpu usage.
* *
@ -152,4 +171,34 @@ public class ProcessStats {
public long getFd() { public long getFd() {
return fd(); return fd();
} }
public static ProcessStats readProcessStats(StreamInput in) throws IOException {
ProcessStats stats = new ProcessStats();
stats.readFrom(in);
return stats;
}
@Override public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong();
cpuPercent = in.readDouble();
cpuSys = in.readLong();
cpuUser = in.readLong();
cpuTotal = in.readLong();
memTotalVirtual = in.readLong();
memResident = in.readLong();
memShare = in.readLong();
fd = in.readLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
out.writeDouble(cpuPercent);
out.writeLong(cpuSys);
out.writeLong(cpuUser);
out.writeLong(cpuTotal);
out.writeLong(memTotalVirtual);
out.writeLong(memResident);
out.writeLong(memShare);
out.writeLong(fd);
}
} }

View File

@ -37,13 +37,14 @@ public class SigarProcessProbe extends AbstractComponent implements ProcessProbe
this.sigarService = sigarService; this.sigarService = sigarService;
} }
@Override public ProcessInfo processInfo() { @Override public synchronized ProcessInfo processInfo() {
return new ProcessInfo(sigarService.sigar().getPid()); return new ProcessInfo(sigarService.sigar().getPid());
} }
@Override public ProcessStats processStats() { @Override public synchronized ProcessStats processStats() {
Sigar sigar = sigarService.sigar(); Sigar sigar = sigarService.sigar();
ProcessStats stats = new ProcessStats(); ProcessStats stats = new ProcessStats();
stats.timestamp = System.currentTimeMillis();
try { try {
ProcCpu cpu = sigar.getProcCpu(sigar.getPid()); ProcCpu cpu = sigar.getProcCpu(sigar.getPid());