Java Code Examples for org.hyperic.sigar.CpuPerc

The following examples show how to use org.hyperic.sigar.CpuPerc. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: JobX   Source File: Monitor.java    License: Apache License 2.0 6 votes vote down vote up
public CPU(int index, CpuInfo info, CpuPerc perc) {
    this.index = index;
    this.cacheSize = info.getCacheSize();
    this.coresPerSocket = info.getCoresPerSocket();
    this.totalCores = info.getTotalCores();
    this.totalSockets = info.getTotalSockets();
    this.mhz = info.getMhz();
    this.model = info.getModel();
    this.vendor = info.getVendor();

    this.user = perc.getUser();
    this.sys = perc.getSys();
    this.nice = perc.getNice();
    this.idle = perc.getIdle();
    this.wait = perc.getWait();
    this.irq = perc.getIrq();
    this.softIrq = perc.getSoftIrq();
    this.stolen = perc.getStolen();
    this.combined = perc.getCombined();
}
 
Example 2
Source Project: sigar-system_runtime   Source File: sigar.java    License: MIT License 6 votes vote down vote up
private static void cpu() throws SigarException {
    Sigar sigar = new Sigar();
    
    CpuPerc perc = sigar.getCpuPerc();
    System.out.println("整体cpu的占用情况:");
    System.out.println("空闲率: " + CpuPerc.format(perc.getIdle()));//获取当前cpu的空闲率
    System.out.println("占用率: "+ CpuPerc.format(perc.getCombined()));//获取当前cpu的占用率
    
    
    CpuInfo infos[] = sigar.getCpuInfoList();
    CpuPerc cpuList[] = null;
    cpuList = sigar.getCpuPercList();
    for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
        CpuInfo info = infos[i];
        System.out.println("第" + (i + 1) + "块CPU信息");
        System.out.println("CPU的总量MHz:    " + info.getMhz());// CPU的总量MHz
        System.out.println("CPU生产商:    " + info.getVendor());// 获得CPU的卖主,如:Intel
        System.out.println("CPU类别:    " + info.getModel());// 获得CPU的类别,如:Celeron
        System.out.println("CPU缓存数量:    " + info.getCacheSize());// 缓冲存储器数量
        printCpuPerc(cpuList[i]);
    }
}
 
Example 3
Source Project: kieker   Source File: CPUsDetailedPercSampler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringController) throws SigarException {
	if (!monitoringController.isMonitoringEnabled()) {
		return;
	}
	if (!monitoringController.isProbeActivated(SignatureFactory.createCPUSignature())) {
		return;
	}

	final CpuPerc[] cpus = this.sigar.getCpuPercList();
	final ITimeSource timesource = monitoringController.getTimeSource();
	for (int i = 0; i < cpus.length; i++) {
		if (monitoringController.isProbeActivated(SignatureFactory.createCPUSignature(i))) {
			final CpuPerc curCPU = cpus[i];
			// final double combinedUtilization = curCPU.getCombined();
			final CPUUtilizationRecord r = new CPUUtilizationRecord(timesource.getTime(),
					monitoringController.getHostname(), Integer.toString(i), curCPU.getUser(), curCPU.getSys(),
					curCPU.getWait(), curCPU.getNice(), curCPU.getIrq(), curCPU.getCombined(), curCPU.getIdle());
			monitoringController.newMonitoringRecord(r);
			// CPUsDetailedPercSampler.log.info("Sigar utilization: " + combinedUtilization
			// + "; " + " Record: " + r);
		}
	}
}
 
Example 4
Source Project: kieker   Source File: CPUsCombinedPercSampler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringController) throws SigarException {
	if (!monitoringController.isMonitoringEnabled()) {
		return;
	}
	if (!monitoringController.isProbeActivated(SignatureFactory.createCPUSignature())) {
		return;
	}

	final CpuPerc[] cpus = this.sigar.getCpuPercList();
	final ITimeSource timesource = monitoringController.getTimeSource();
	for (int i = 0; i < cpus.length; i++) {
		if (monitoringController.isProbeActivated(SignatureFactory.createCPUSignature(i))) {
			final CpuPerc curCPU = cpus[i];
			final double combinedUtilization = curCPU.getCombined();
			final ResourceUtilizationRecord r = new ResourceUtilizationRecord(timesource.getTime(), monitoringController.getHostname(),
					CPU_RESOURCE_NAME_PREFIX + i, combinedUtilization);
			monitoringController.newMonitoringRecord(r);
			// CPUsCombinedPercSampler.log.info("Sigar utilization: " + combinedUtilization + "; " + " Record: " + r);
		}
	}
}
 
Example 5
/**
 * Gets the cpu usage.
 * 
 * @return the cpu usage
 */
private Object getCpuUsage() {
	try {
		// cpu percentage object.
		CpuPerc per = sigar.getCpuPerc();

		// getting the string value of percentage.
		String p = String.valueOf(per.getCombined() * 100.0);
		// getting index of the .
		int ix = p.indexOf('.') + 1;
		// getting only one digit after .
		return p.substring(0, ix) + p.substring(ix, ix + 1);
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
	}
	return null;
}
 
Example 6
Source Project: onos   Source File: NodeMetricsManager.java    License: Apache License 2.0 6 votes vote down vote up
private void pollMetrics() {
    try {
        CpuPerc cpu = sigar.getCpuPerc();
        Mem mem = sigar.getMem();
        FileSystemUsage disk = sigar.getFileSystemUsage(SLASH);

        NodeMemoryUsage memoryNode = new NodeMemoryUsage.Builder().free(mem.getFree())
                .used(mem.getUsed()).total(mem.getTotal()).withUnit(Units.BYTES)
                .withNode(localNodeId).build();
        NodeCpuUsage cpuNode = new NodeCpuUsage.Builder().withNode(localNodeId)
                .usage(cpu.getCombined() * PERCENTAGE_MULTIPLIER).build();
        NodeDiskUsage diskNode = new NodeDiskUsage.Builder().withNode(localNodeId)
                .free(disk.getFree()).used(disk.getUsed()).withUnit(Units.KBYTES)
                .total(disk.getTotal()).build();
        diskStore.put(localNodeId, diskNode);
        memoryStore.put(localNodeId, memoryNode);
        cpuStore.put(localNodeId, cpuNode);

    } catch (SigarException e) {
        log.error("Exception occurred ", e);
    }

}
 
Example 7
Source Project: WeBASE-Front   Source File: PerformanceService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * getConfigInfo.
 * 
 * @return
 */
public Map<String, String> getConfigInfo() throws UnknownHostException, SigarException {
    log.info("getConfigInfo.");
    Map<String, String> configMap = new HashMap<>();
    String ip = getIp();
    log.debug("local ip:    " + ip);
    configMap.put("ip", ip);
    Mem mem = sigar.getMem();
    log.debug("memory total:    " + mem.getTotal() / 1024L + "K av");
    log.debug("memory used now:    " + mem.getUsed() / 1024L + "K used");
    configMap.put("memoryTotalSize", Long.toString(mem.getTotal() / 1024L));
    configMap.put("memoryUsedSize", Long.toString(mem.getUsed() / 1024L));
    CpuPerc cpu = sigar.getCpuPerc();
    CpuInfo[] infos = sigar.getCpuInfoList();
    log.debug("CPU mhz:    " + infos[0].getMhz());
    log.debug("CPU core number:    " + infos.length);
    configMap.put("cpuSize", Integer.toString(infos[0].getMhz()));
    configMap.put("cpuAmount", Integer.toString(infos.length));
    long total;
    long use;
    FileSystem[] fslist = sigar.getFileSystemList();
    log.debug("****fs " + fslist.length);
    use = sigar.getFileSystemUsage(constants.getMonitorDisk()).getUsed();
    total = sigar.getFileSystemUsage(constants.getMonitorDisk()).getTotal();
    log.debug("diskTotalSize:    " + total);
    log.debug("diskUsedSize:    " + use);
    configMap.put("diskTotalSize", Long.toString(total));
    configMap.put("diskUsedSize", Long.toString(use));
    return configMap;
}
 
Example 8
Source Project: sigar-system_runtime   Source File: sigar.java    License: MIT License 5 votes vote down vote up
private static void printCpuPerc(CpuPerc cpu) {
    System.out.println("CPU用户使用率:    " + CpuPerc.format(cpu.getUser()));// 用户使用率
    System.out.println("CPU系统使用率:    " + CpuPerc.format(cpu.getSys()));// 系统使用率
    System.out.println("CPU当前等待率:    " + CpuPerc.format(cpu.getWait()));// 当前等待率
    System.out.println("CPU当前错误率:    " + CpuPerc.format(cpu.getNice()));//
    System.out.println("CPU当前空闲率:    " + CpuPerc.format(cpu.getIdle()));// 当前空闲率
    System.out.println("CPU总的使用率:    " + CpuPerc.format(cpu.getCombined()));// 总的使用率
}
 
Example 9
Source Project: sigar-system_runtime   Source File: SystemRuntime.java    License: MIT License 5 votes vote down vote up
public CpuPerc cpu() throws SigarException {
        CpuPerc perc = sigar.getCpuPerc();
//        System.out.println("整体cpu的占用情况:");
//        System.out.println("空闲率: " + CpuPerc.format(perc.getIdle()));//获取当前cpu的空闲率
//        System.out.println("占用率: "+ CpuPerc.format(perc.getCombined()));//获取当前cpu的占用率
        return perc;
    }
 
Example 10
Source Project: micro-server   Source File: MachineStatsCheckerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	sigar = mock(Sigar.class);
	swap = mock(Swap.class);
	cpuPerc = mock(CpuPerc.class);
	cpuInfo = mock(CpuInfo.class);
	mem = mock(Mem.class);
	
	machineStatsChecker = new MachineStatsChecker();		
}
 
Example 11
Source Project: WeBASE-Front   Source File: PerformanceService.java    License: Apache License 2.0 4 votes vote down vote up
private BigDecimal getCpuRatio() throws SigarException {
    CpuPerc cpuPerc = sigar.getCpuPerc();
    return BigDecimal.valueOf(cpuPerc.getCombined() * 100);

}
 
Example 12
Source Project: maintain   Source File: HttpClientTool.java    License: MIT License 4 votes vote down vote up
public static CpuPerc getCpuPercJson(String url) {
	return getJson(url + "cpuPerc", CpuPerc.class);
}
 
Example 13
Source Project: maintain   Source File: ServerInfo.java    License: MIT License 4 votes vote down vote up
public CpuPerc getCpuPerc() {
	return cpuPerc;
}
 
Example 14
Source Project: maintain   Source File: ServerInfo.java    License: MIT License 4 votes vote down vote up
public void setCpuPerc(CpuPerc cpuPerc) {
	this.cpuPerc = cpuPerc;
}
 
Example 15
Source Project: maintain   Source File: Cpu.java    License: MIT License 4 votes vote down vote up
public Cpu(CpuInfo cpuInfo, CpuPerc cpuPerc) {
	this.cpuInfo = cpuInfo;
	this.cpuPerc = cpuPerc;
}
 
Example 16
Source Project: maintain   Source File: Cpu.java    License: MIT License 4 votes vote down vote up
public CpuPerc getCpuPerc() {
	return cpuPerc;
}
 
Example 17
Source Project: maintain   Source File: Cpu.java    License: MIT License 4 votes vote down vote up
public void setCpuPerc(CpuPerc cpuPerc) {
	this.cpuPerc = cpuPerc;
}
 
Example 18
Source Project: perfmon-agent   Source File: CPUTotalMetric.java    License: Apache License 2.0 4 votes vote down vote up
public void getValue(StringBuffer res) throws SigarException {
    CpuPerc cpu;
    if (coreID < 0) {
        cpu = sigarProxy.getCpuPerc();
    } else {
        cpu = sigarProxy.getCpuPercList()[coreID];
    }

    double val;
    switch (type) {

        case COMBINED:
            val = cpu.getCombined();
            break;
        case IDLE:
            val = cpu.getIdle();
            break;
        case IRQ:
            val = cpu.getIrq();
            break;
        case NICE:
            val = cpu.getNice();
            break;
        case SOFTIRQ:
            val = cpu.getSoftIrq();
            break;
        case STOLEN:
            val = cpu.getStolen();
            break;
        case SYSTEM:
            val = cpu.getSys();
            break;
        case USER:
            val = cpu.getUser();
            break;
        case IOWAIT:
            val = cpu.getWait();
            break;
        default:
            throw new SigarException("Unknown proc total type " + type);
    }

    if (!Double.isNaN(val)) {
        res.append(Double.toString(100 * val));
    } else {
        log.warn("Failed to get total cpu metric: " + types[type]);
    }
}