use of org.hyperic.sigar.SigarException in project graylog2-server by Graylog2.
the class SigarOsProbe method osStats.
@Override
public OsStats osStats() {
final Sigar sigar = sigarService.sigar();
double[] loadAverage;
try {
loadAverage = sigar.getLoadAverage();
} catch (SigarException e) {
loadAverage = OsStats.EMPTY_LOAD;
}
long uptime;
try {
uptime = (long) sigar.getUptime().getUptime();
} catch (SigarException e) {
uptime = -1L;
}
Processor processor;
try {
final CpuInfo[] cpuInfos = sigar.getCpuInfoList();
final String vendor = cpuInfos[0].getVendor();
final String model = cpuInfos[0].getModel();
final int mhz = cpuInfos[0].getMhz();
final int totalCores = cpuInfos[0].getTotalCores();
final int totalSockets = cpuInfos[0].getTotalSockets();
final int coresPerSocket = cpuInfos[0].getCoresPerSocket();
long cacheSize = -1L;
if (cpuInfos[0].getCacheSize() != Sigar.FIELD_NOTIMPL) {
cacheSize = cpuInfos[0].getCacheSize();
}
final CpuPerc cpuPerc = sigar.getCpuPerc();
final short sys = (short) (cpuPerc.getSys() * 100);
final short user = (short) (cpuPerc.getUser() * 100);
final short idle = (short) (cpuPerc.getIdle() * 100);
final short stolen = (short) (cpuPerc.getStolen() * 100);
processor = Processor.create(model, vendor, mhz, totalCores, totalSockets, coresPerSocket, cacheSize, sys, user, idle, stolen);
} catch (SigarException e) {
processor = Processor.create("Unknown", "Unknown", -1, -1, -1, -1, -1L, (short) -1, (short) -1, (short) -1, (short) -1);
}
Memory memory;
try {
Mem mem = sigar.getMem();
long total = mem.getTotal();
long free = mem.getFree();
short freePercent = (short) mem.getFreePercent();
long used = mem.getUsed();
short usedPercent = (short) mem.getUsedPercent();
long actualFree = mem.getActualFree();
long actualUsed = mem.getActualUsed();
memory = Memory.create(total, free, freePercent, used, usedPercent, actualFree, actualUsed);
} catch (SigarException e) {
memory = Memory.create(-1L, -1L, (short) -1, -1L, (short) -1, -1L, -1L);
}
Swap swap;
try {
org.hyperic.sigar.Swap sigSwap = sigar.getSwap();
long total = sigSwap.getTotal();
long free = sigSwap.getFree();
long used = sigSwap.getUsed();
swap = Swap.create(total, free, used);
} catch (SigarException e) {
swap = Swap.create(-1L, -1L, -1L);
}
return OsStats.create(loadAverage, uptime, processor, memory, swap);
}
use of org.hyperic.sigar.SigarException in project graylog2-server by Graylog2.
the class SigarProcessProbe method processStats.
@Override
public synchronized ProcessStats processStats() {
final Sigar sigar = sigarService.sigar();
final long pid = sigar.getPid();
final long openFileDescriptors = JmxProcessProbe.getOpenFileDescriptorCount();
final long maxFileDescriptorCount = JmxProcessProbe.getMaxFileDescriptorCount();
ProcessStats.Cpu cpu;
try {
ProcCpu procCpu = sigar.getProcCpu(pid);
cpu = ProcessStats.Cpu.create((short) (procCpu.getPercent() * 100), procCpu.getSys(), procCpu.getUser(), procCpu.getTotal());
} catch (SigarException e) {
cpu = null;
}
ProcessStats.Memory memory;
try {
ProcMem mem = sigar.getProcMem(sigar.getPid());
memory = ProcessStats.Memory.create(mem.getSize(), mem.getResident(), mem.getShare());
} catch (SigarException e) {
memory = null;
}
return ProcessStats.create(pid, openFileDescriptors, maxFileDescriptorCount, cpu, memory);
}
use of org.hyperic.sigar.SigarException in project ats-framework by Axway.
the class MatchedProcess method updateProcessesMatchingMap.
private static List<ReadingInstance> updateProcessesMatchingMap(Sigar sigar, List<FullReadingBean> initialProcessReadings, List<ReadingInstance> currentReadingInstances) throws SigarException {
// remember to user process regex and alias
Map<String, Pattern> processPatterns = new HashMap<String, Pattern>();
Map<String, String> processAliases = new HashMap<String, String>();
Map<String, String> processUsernames = new HashMap<String, String>();
for (FullReadingBean processReading : initialProcessReadings) {
String userRegex = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_RECOGNITION_PATTERN);
Pattern compiledPattern = Pattern.compile(userRegex);
processPatterns.put(userRegex, compiledPattern);
String userProcessAlias = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_ALIAS);
processAliases.put(userRegex, userProcessAlias);
String processUsername = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_USERNAME);
processUsernames.put(userRegex, processUsername);
}
// at the end only processes that are not alive will be in this list
Set<Long> finishedProcessesIds = new HashSet<Long>(matchedProcessesIds);
// iterate all system processes and remember the ones we want to monitor
for (long pid : sigar.getProcList()) {
// check if we know this process from a previous poll, we do not want to add it again
if (!matchedProcessesIds.contains(pid)) {
// we try to match a process by its start command
String processStartCommand = constructProcessStartCommand(sigar, pid);
if (processStartCommand != null && !processStartCommand.isEmpty()) {
String processUsername = null;
// check this process against all patterns
for (String userRegex : processPatterns.keySet()) {
// by default we search processes from all users
boolean isExpectedProcessUsername = true;
// check if it matters who started this process
String requestedProcessUsername = processUsernames.get(userRegex);
if (!StringUtils.isNullOrEmpty(requestedProcessUsername)) {
if (processUsername == null) {
// we still do not know the user of this process
try {
processUsername = sigar.getProcCredName(pid).getUser();
} catch (SigarException e) {
// a specific username is required, but we can not get the info about this process
isExpectedProcessUsername = false;
}
}
isExpectedProcessUsername = requestedProcessUsername.equalsIgnoreCase(processUsername);
}
if (isExpectedProcessUsername) {
Pattern processPattern = processPatterns.get(userRegex);
if (processPattern.matcher(processStartCommand.trim()).matches()) {
Map<Long, MatchedProcess> processesMatchedThisRegex = matchedProcessesMap.get(userRegex);
if (processesMatchedThisRegex == null) {
processesMatchedThisRegex = new HashMap<Long, MatchedProcess>();
}
int processIndexForThisPattern = getNextIndexForThisProcessPattern(userRegex);
MatchedProcess matchedProcess = new MatchedProcess(pid, userRegex, processAliases.get(userRegex), processIndexForThisPattern, processStartCommand);
processesMatchedThisRegex.put(pid, matchedProcess);
matchedProcessesMap.put(userRegex, processesMatchedThisRegex);
matchedProcessesIds.add(pid);
log.info("We will monitor process: " + matchedProcess.toString());
}
}
}
}
} else {
// the process is still alive
finishedProcessesIds.remove(pid);
}
}
// check if some processes have died, we do not want to monitor them anymore
for (Long finishedProcessId : finishedProcessesIds) {
// cleanup the maps
matchedProcessesIds.remove(finishedProcessId);
for (Map<Long, MatchedProcess> matchedProcessMap : matchedProcessesMap.values()) {
MatchedProcess removedProcess = matchedProcessMap.remove(finishedProcessId);
if (removedProcess != null) {
log.info("This process is no more alive: " + removedProcess.toString());
}
}
Iterator<String> it = processesReadingInstanceIdentifiers.iterator();
while (it.hasNext()) {
String processesReadingInstanceIdentifier = it.next();
if (Long.parseLong(processesReadingInstanceIdentifier.split("->")[1]) == finishedProcessId) {
it.remove();
}
}
}
// return the updated list of reading instances we poll
List<ReadingInstance> newReadingInstances = new ArrayList<ReadingInstance>();
for (ReadingInstance currentReadingInstance : currentReadingInstances) {
boolean stillActiveInstance = true;
for (Long finishedProcessId : finishedProcessesIds) {
if (finishedProcessId == currentReadingInstance.getPid()) {
stillActiveInstance = false;
break;
}
}
if (stillActiveInstance) {
newReadingInstances.add(currentReadingInstance);
}
}
return newReadingInstances;
}
use of org.hyperic.sigar.SigarException in project ats-framework by Axway.
the class MatchedProcess method getProcessCpuUsageRunningUser.
private static List<ReadingInstance> getProcessCpuUsageRunningUser(SigarWrapper sigarWrapper, FullReadingBean reading, ParentProcessReadingBean parentProcess) throws SigarException {
String readingProcessPattern = reading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_RECOGNITION_PATTERN);
// we can match more than 1 process
List<ReadingInstance> readingInstancesList = new ArrayList<ReadingInstance>();
// iterate all processes in the map
Map<Long, MatchedProcess> processesForThisPattern = matchedProcessesMap.get(readingProcessPattern);
if (processesForThisPattern != null) {
for (final MatchedProcess processInfo : processesForThisPattern.values()) {
// check if we are not already measuring this reading for this process
String processesReadingInstanceIdentifier = reading.getName() + "->" + processInfo.getPid();
if (!processesReadingInstanceIdentifiers.contains(processesReadingInstanceIdentifier)) {
processesReadingInstanceIdentifiers.add(processesReadingInstanceIdentifier);
// create a new reading for this process
Map<String, String> parameters = constructProcessParametersMap(processInfo, readingProcessPattern, "1");
readingInstancesList.add(new ReadingInstance(sigarWrapper, parentProcess, reading.getId() + "->" + processInfo.getPid(), processInfo.getPid(), reading.getMonitorName(), "[process] " + processInfo.getAlias() + " - CPU usage - User", reading.getUnit(), parameters, 1.0F) {
private static final long serialVersionUID = 1L;
@Override
public void init() throws SigarException {
this.lastLongValue = fixLongValue(fixOverflow(getName(), sigarWrapper.getProcessCpuTimeRunningUser(processInfo.getPid()), ReadingInstance.CPU_PROCESS_OVERFLOW_VALUE));
}
@Override
public float poll() throws SigarException {
long userTime = fixLongValue(fixOverflow(getName(), sigarWrapper.getProcessCpuTimeRunningUser(processInfo.getPid()), ReadingInstance.CPU_PROCESS_OVERFLOW_VALUE));
double deltaUserTime;
if (userTime > 0) {
deltaUserTime = (userTime - this.lastLongValue) / numberOfCPUs;
this.lastLongValue = userTime;
} else {
return -1;
}
float deltaUserPercentUsage = 0.0F;
long elapsedTime = getElapsedTime();
// moment of creating the ReadingInstance, it is normal for new processes
if (elapsedTime > 900) {
// calculate user code time for 1ms
deltaUserTime = deltaUserTime / elapsedTime;
// convert to percentage usage
deltaUserPercentUsage = new BigDecimal(deltaUserTime).setScale(2, BigDecimal.ROUND_DOWN).floatValue() * 100.0F;
}
addValueToParentProcess(deltaUserPercentUsage);
return deltaUserPercentUsage;
}
});
}
}
}
return readingInstancesList;
}
use of org.hyperic.sigar.SigarException in project ats-framework by Axway.
the class MatchedProcess method getNetworkTraffic.
private static List<ReadingInstance> getNetworkTraffic(SigarWrapper sigarWrapper, FullReadingBean reading) throws SigarException {
// this reading can actually be expanded in more than one reading
int nextUniqueReadingId = 1;
List<ReadingInstance> readingInstancesList = new ArrayList<ReadingInstance>();
String[] ifNames = sigarWrapper.getSigarInstance().getNetInterfaceList();
// if there are more than one IP addresses on a single interface, Sigar will show these interface names so many times
Set<String> uniqueIfNames = new HashSet<String>(Arrays.asList(ifNames));
for (final String ifName : uniqueIfNames) {
if (ifName.indexOf(':') > -1) {
// that's why we will skip it, also Sigar throws an exception with message: No such device or address
continue;
}
final long txBytes = sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName).getTxBytes();
readingInstancesList.add(new ReadingInstance(sigarWrapper, reading.getId() + ", " + (nextUniqueReadingId++), reading.getMonitorName(), reading.getName() + " " + ifName + " TX data", reading.getUnit(), 0) {
private static final long serialVersionUID = 1L;
@Override
public void init() throws SigarException {
applyMemoryNormalizationFactor();
this.lastLongValue = fixLongValue(fixOverflow(ifName, txBytes));
}
@Override
public float poll() throws SigarException {
NetInterfaceStat ifstat = this.sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName);
long txBytes = fixLongValue(fixOverflow(getName(), ifstat.getTxBytes()));
double result;
if (txBytes >= 0) {
result = (txBytes - this.lastLongValue) * normalizationFactor;
this.lastLongValue = txBytes;
} else {
return -1.0F;
}
// calculate TX bytes per second
result = result / ((double) getElapsedTime() / 1000);
return new BigDecimal(result).setScale(2, BigDecimal.ROUND_DOWN).floatValue();
}
});
final long rxBytes = sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName).getRxBytes();
readingInstancesList.add(new ReadingInstance(sigarWrapper, reading.getId() + ", " + (nextUniqueReadingId++), reading.getMonitorName(), reading.getName() + " " + ifName + " RX data", reading.getUnit(), 0) {
private static final long serialVersionUID = 1L;
@Override
public void init() throws SigarException {
applyMemoryNormalizationFactor();
this.lastLongValue = fixLongValue(fixOverflow(ifName, rxBytes));
}
@Override
public float poll() throws SigarException {
NetInterfaceStat ifstat = this.sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName);
long rxBytes = fixLongValue(fixOverflow(getName(), ifstat.getRxBytes()));
double result;
if (rxBytes >= 0) {
result = (rxBytes - this.lastLongValue) * normalizationFactor;
this.lastLongValue = rxBytes;
} else {
return -1.0F;
}
// calculate RX bytes per second
result = result / ((double) getElapsedTime() / 1000);
return new BigDecimal(result).setScale(2, BigDecimal.ROUND_DOWN).floatValue();
}
});
}
return readingInstancesList;
}
Aggregations