Skip to content
Merged

Dev #165

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions scouter.agent.host/src/scouter/agent/counter/task/HostNetDiskPerf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package scouter.agent.counter.task;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.
*
* original from - https://svn.apache.org/repos/asf/chukwa/trunk/src/main/java/org/apache/hadoop/chukwa/datacollection/adaptor/sigar/SigarRunner.java
*
*/
/**
* Net Usage, Disk Usage
* author: gunlee01@gmail.com
*/


import org.hyperic.sigar.*;
import scouter.agent.Logger;
import scouter.agent.counter.CounterBasket;
import scouter.agent.counter.anotation.Counter;
import scouter.lang.counters.CounterConstants;

import java.util.HashMap;
import java.util.Map;

public class HostNetDiskPerf {
static int SLEEP_TIME = 2000;
static Sigar sigarImpl = new Sigar();
static SigarProxy sigar = SigarProxyCache.newInstance(sigarImpl, SLEEP_TIME);

private static String[] netIf = null;
private static FileSystem[] fs = null;

private static HashMap<String, Map<String, Long>> previousNetworkStats = new HashMap<String, Map<String, Long>>();
private static HashMap<String, Map<String, Long>> previousDiskStats = new HashMap<String, Map<String, Long>>();
private static final String RX_DELTA = "rxD";
private static final String TX_DELTA = "txD";
private static final String READ_DELTA = "rdD";
private static final String WRITE_DELTA = "wrD";
private static volatile long rxTotalBytesPerSec = 0L;
private static volatile long txTotalBytesPerSec = 0L;
private static volatile long readTotalBytesPerSec = 0L;
private static volatile long writeTotalBytesPerSec = 0L;

public static long getRxTotalBytesPerSec() {
return HostNetDiskPerf.rxTotalBytesPerSec;
}

public static long getTxTotalBytesPerSec() {
return HostNetDiskPerf.txTotalBytesPerSec;
}

public static long getReadTotalBytesPerSec() {
return HostNetDiskPerf.readTotalBytesPerSec;
}

public static long getWriteTotalBytesPerSec() {
return HostNetDiskPerf.writeTotalBytesPerSec;
}

@Counter(interval = 10000)
public void process(CounterBasket pw) {
try {
netUsage(10);
diskIO(10);
SigarProxyCache.clear(sigar);
} catch (Exception e) {
Logger.println("A141", 10, "HostPerfProcess10s", e);
}
}

private void netUsage(int checkIntervalSec) {
try {
if (netIf == null) {
netIf = sigar.getNetInterfaceList();
}
long tmpRxTotal = 0L;
long tmpTxTotal = 0L;

for (int i = 0; i < netIf.length; i++) {
NetInterfaceStat net = null;
try {
net = sigar.getNetInterfaceStat(netIf[i]);
} catch (SigarException e) {
// Ignore the exception when trying to stat network interface
Logger.println("A143", 300, "SigarException trying to stat network device " + netIf[i], e);
continue;
}
Map<String, Long> netMap = new HashMap<String, Long>();
long rxBytes = net.getRxBytes();
long txBytes = net.getTxBytes();

netMap.put(CounterConstants.HOST_NET_RX_BYTES, rxBytes);
netMap.put(CounterConstants.HOST_NET_TX_BYTES, txBytes);

Map<String, Long> preMap = previousNetworkStats.get(netIf[i]);

if (preMap != null) {
long rxDelta = (rxBytes - preMap.get(CounterConstants.HOST_NET_RX_BYTES)) / checkIntervalSec; // per sec delta
long txDelta = (txBytes - preMap.get(CounterConstants.HOST_NET_TX_BYTES)) / checkIntervalSec; // per sec delta

netMap.put(this.RX_DELTA, rxDelta);
netMap.put(this.TX_DELTA, txDelta);

tmpRxTotal += rxDelta;
tmpTxTotal += txDelta;
}
previousNetworkStats.put(netIf[i], netMap);
}

HostNetDiskPerf.rxTotalBytesPerSec = tmpRxTotal;
HostNetDiskPerf.txTotalBytesPerSec = tmpTxTotal;

} catch (SigarException se) {
Logger.println("A144", 60, "SigarException on net usage", se);
HostNetDiskPerf.rxTotalBytesPerSec = 0;
HostNetDiskPerf.txTotalBytesPerSec = 0;
}
}

private void diskIO(int checkIntervalSec) {
try {
if (fs == null) {
fs = sigar.getFileSystemList();
}
long tmpReadTotal = 0L;
long tmpWriteTotal = 0L;

for (int i = 0; i < fs.length; i++) {
FileSystemUsage usage = null;
try {
usage = sigar.getFileSystemUsage(fs[i].getDirName());
} catch (SigarException e) {
// Ignore the exception when trying to stat file interface
Logger.println("A145", 300, "SigarException trying to stat file system device " + fs[i], e);
continue;
}
Map<String, Long> fsMap = new HashMap<String, Long>();
long readBytes = usage.getDiskReadBytes();
long writeBytes = usage.getDiskWriteBytes();

fsMap.put(CounterConstants.HOST_DISK_READ_BYTES, readBytes);
fsMap.put(CounterConstants.HOST_DISK_WRITE_BYTES, writeBytes);

Map<String, Long> preMap = previousDiskStats.get(fs[i].getDevName());

if (preMap != null) {
long readDelta = (readBytes - preMap.get(CounterConstants.HOST_DISK_READ_BYTES)) / checkIntervalSec; // per sec delta
long writeDelta = (writeBytes - preMap.get(CounterConstants.HOST_DISK_WRITE_BYTES)) / checkIntervalSec; // per sec delta

fsMap.put(this.READ_DELTA, readDelta);
fsMap.put(this.WRITE_DELTA, writeDelta);

tmpReadTotal += readDelta;
tmpWriteTotal += writeDelta;
}
previousDiskStats.put(fs[i].getDevName(), fsMap);
}

HostNetDiskPerf.readTotalBytesPerSec = tmpReadTotal;
HostNetDiskPerf.writeTotalBytesPerSec = tmpWriteTotal;

} catch (SigarException se) {
Logger.println("A144", 60, "SigarException on net usage", se);
HostNetDiskPerf.rxTotalBytesPerSec = 0;
HostNetDiskPerf.txTotalBytesPerSec = 0;
}
}

}
48 changes: 35 additions & 13 deletions scouter.agent.host/src/scouter/agent/counter/task/HostPerf.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package scouter.agent.counter.task;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.NfsFileSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;
import org.hyperic.sigar.SigarProxyCache;
import org.hyperic.sigar.Swap;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 scouter.agent.counter.task;

import org.hyperic.sigar.*;
import scouter.agent.Configure;
import scouter.agent.Logger;
import scouter.agent.counter.CounterBasket;
Expand All @@ -36,7 +46,7 @@ public class HostPerf {
MeterResource cpuMeter = new MeterResource();
MeterResource sysCpuMeter = new MeterResource();
MeterResource userCpuMeter = new MeterResource();

@Counter
public void process(CounterBasket pw) {
try {
Expand Down Expand Up @@ -101,6 +111,17 @@ void domain(CounterBasket pw) throws SigarException {
p.put(CounterConstants.HOST_TCPSTAT_TIM, new DecimalValue(tcpstat_time));
p.put(CounterConstants.HOST_TCPSTAT_EST, new DecimalValue(tcpstat_est));

p.put(CounterConstants.HOST_NET_RX_BYTES, new DecimalValue(HostNetDiskPerf.getRxTotalBytesPerSec()));
p.put(CounterConstants.HOST_NET_TX_BYTES, new DecimalValue(HostNetDiskPerf.getTxTotalBytesPerSec()));

p.put(CounterConstants.HOST_DISK_READ_BYTES, new DecimalValue(HostNetDiskPerf.getReadTotalBytesPerSec()));
p.put(CounterConstants.HOST_DISK_WRITE_BYTES, new DecimalValue(HostNetDiskPerf.getWriteTotalBytesPerSec()));

// System.out.println("rx:" + HostNetDiskPerf.getRxTotalBytesPerSec());
// System.out.println("tx:" + HostNetDiskPerf.getTxTotalBytesPerSec());
// System.out.println("read:" + HostNetDiskPerf.getReadTotalBytesPerSec());
// System.out.println("write:" + HostNetDiskPerf.getWriteTotalBytesPerSec());

p = pw.getPack(conf.getObjName(), TimeTypeEnum.FIVE_MIN);
p.put(CounterConstants.HOST_CPU, new FloatValue(cpu));
p.put(CounterConstants.HOST_SYSCPU, new FloatValue(sysCpu));
Expand All @@ -120,6 +141,7 @@ void domain(CounterBasket pw) throws SigarException {
p.put(CounterConstants.HOST_TCPSTAT_FIN, new DecimalValue(tcpstat_fin1 + tcpstat_fin2));
p.put(CounterConstants.HOST_TCPSTAT_TIM, new DecimalValue(tcpstat_time));
p.put(CounterConstants.HOST_TCPSTAT_EST, new DecimalValue(tcpstat_est));

SigarProxyCache.clear(sigar);
}

Expand Down
Loading