|
| 1 | +package scouter.agent.counter.task; |
| 2 | +/* |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + * original from - https://svn.apache.org/repos/asf/chukwa/trunk/src/main/java/org/apache/hadoop/chukwa/datacollection/adaptor/sigar/SigarRunner.java |
| 20 | + * |
| 21 | + */ |
| 22 | +/** |
| 23 | + * Net Usage, Disk Usage |
| 24 | + |
| 25 | + */ |
| 26 | + |
| 27 | + |
| 28 | +import org.hyperic.sigar.*; |
| 29 | +import scouter.agent.Logger; |
| 30 | +import scouter.agent.counter.CounterBasket; |
| 31 | +import scouter.agent.counter.anotation.Counter; |
| 32 | +import scouter.lang.counters.CounterConstants; |
| 33 | + |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +public class HostNetDiskPerf { |
| 38 | + static int SLEEP_TIME = 2000; |
| 39 | + static Sigar sigarImpl = new Sigar(); |
| 40 | + static SigarProxy sigar = SigarProxyCache.newInstance(sigarImpl, SLEEP_TIME); |
| 41 | + |
| 42 | + private static String[] netIf = null; |
| 43 | + private static FileSystem[] fs = null; |
| 44 | + |
| 45 | + private static HashMap<String, Map<String, Long>> previousNetworkStats = new HashMap<String, Map<String, Long>>(); |
| 46 | + private static HashMap<String, Map<String, Long>> previousDiskStats = new HashMap<String, Map<String, Long>>(); |
| 47 | + private static final String RX_DELTA = "rxD"; |
| 48 | + private static final String TX_DELTA = "txD"; |
| 49 | + private static final String READ_DELTA = "rdD"; |
| 50 | + private static final String WRITE_DELTA = "wrD"; |
| 51 | + private static volatile long rxTotalBytesPerSec = 0L; |
| 52 | + private static volatile long txTotalBytesPerSec = 0L; |
| 53 | + private static volatile long readTotalBytesPerSec = 0L; |
| 54 | + private static volatile long writeTotalBytesPerSec = 0L; |
| 55 | + |
| 56 | + public static long getRxTotalBytesPerSec() { |
| 57 | + return HostNetDiskPerf.rxTotalBytesPerSec; |
| 58 | + } |
| 59 | + |
| 60 | + public static long getTxTotalBytesPerSec() { |
| 61 | + return HostNetDiskPerf.txTotalBytesPerSec; |
| 62 | + } |
| 63 | + |
| 64 | + public static long getReadTotalBytesPerSec() { |
| 65 | + return HostNetDiskPerf.readTotalBytesPerSec; |
| 66 | + } |
| 67 | + |
| 68 | + public static long getWriteTotalBytesPerSec() { |
| 69 | + return HostNetDiskPerf.writeTotalBytesPerSec; |
| 70 | + } |
| 71 | + |
| 72 | + @Counter(interval = 10000) |
| 73 | + public void process(CounterBasket pw) { |
| 74 | + try { |
| 75 | + netUsage(10); |
| 76 | + diskIO(10); |
| 77 | + SigarProxyCache.clear(sigar); |
| 78 | + } catch (Exception e) { |
| 79 | + Logger.println("A141", 10, "HostPerfProcess10s", e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void netUsage(int checkIntervalSec) { |
| 84 | + try { |
| 85 | + if (netIf == null) { |
| 86 | + netIf = sigar.getNetInterfaceList(); |
| 87 | + } |
| 88 | + long tmpRxTotal = 0L; |
| 89 | + long tmpTxTotal = 0L; |
| 90 | + |
| 91 | + for (int i = 0; i < netIf.length; i++) { |
| 92 | + NetInterfaceStat net = null; |
| 93 | + try { |
| 94 | + net = sigar.getNetInterfaceStat(netIf[i]); |
| 95 | + } catch (SigarException e) { |
| 96 | + // Ignore the exception when trying to stat network interface |
| 97 | + Logger.println("A143", 300, "SigarException trying to stat network device " + netIf[i], e); |
| 98 | + continue; |
| 99 | + } |
| 100 | + Map<String, Long> netMap = new HashMap<String, Long>(); |
| 101 | + long rxBytes = net.getRxBytes(); |
| 102 | + long txBytes = net.getTxBytes(); |
| 103 | + |
| 104 | + netMap.put(CounterConstants.HOST_NET_RX_BYTES, rxBytes); |
| 105 | + netMap.put(CounterConstants.HOST_NET_TX_BYTES, txBytes); |
| 106 | + |
| 107 | + Map<String, Long> preMap = previousNetworkStats.get(netIf[i]); |
| 108 | + |
| 109 | + if (preMap != null) { |
| 110 | + long rxDelta = (rxBytes - preMap.get(CounterConstants.HOST_NET_RX_BYTES)) / checkIntervalSec; // per sec delta |
| 111 | + long txDelta = (txBytes - preMap.get(CounterConstants.HOST_NET_TX_BYTES)) / checkIntervalSec; // per sec delta |
| 112 | + |
| 113 | + netMap.put(this.RX_DELTA, rxDelta); |
| 114 | + netMap.put(this.TX_DELTA, txDelta); |
| 115 | + |
| 116 | + tmpRxTotal += rxDelta; |
| 117 | + tmpTxTotal += txDelta; |
| 118 | + } |
| 119 | + previousNetworkStats.put(netIf[i], netMap); |
| 120 | + } |
| 121 | + |
| 122 | + HostNetDiskPerf.rxTotalBytesPerSec = tmpRxTotal; |
| 123 | + HostNetDiskPerf.txTotalBytesPerSec = tmpTxTotal; |
| 124 | + |
| 125 | + } catch (SigarException se) { |
| 126 | + Logger.println("A144", 60, "SigarException on net usage", se); |
| 127 | + HostNetDiskPerf.rxTotalBytesPerSec = 0; |
| 128 | + HostNetDiskPerf.txTotalBytesPerSec = 0; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void diskIO(int checkIntervalSec) { |
| 133 | + try { |
| 134 | + if (fs == null) { |
| 135 | + fs = sigar.getFileSystemList(); |
| 136 | + } |
| 137 | + long tmpReadTotal = 0L; |
| 138 | + long tmpWriteTotal = 0L; |
| 139 | + |
| 140 | + for (int i = 0; i < fs.length; i++) { |
| 141 | + FileSystemUsage usage = null; |
| 142 | + try { |
| 143 | + usage = sigar.getFileSystemUsage(fs[i].getDirName()); |
| 144 | + } catch (SigarException e) { |
| 145 | + // Ignore the exception when trying to stat file interface |
| 146 | + Logger.println("A145", 300, "SigarException trying to stat file system device " + fs[i], e); |
| 147 | + continue; |
| 148 | + } |
| 149 | + Map<String, Long> fsMap = new HashMap<String, Long>(); |
| 150 | + long readBytes = usage.getDiskReadBytes(); |
| 151 | + long writeBytes = usage.getDiskWriteBytes(); |
| 152 | + |
| 153 | + fsMap.put(CounterConstants.HOST_DISK_READ_BYTES, readBytes); |
| 154 | + fsMap.put(CounterConstants.HOST_DISK_WRITE_BYTES, writeBytes); |
| 155 | + |
| 156 | + Map<String, Long> preMap = previousDiskStats.get(fs[i].getDevName()); |
| 157 | + |
| 158 | + if (preMap != null) { |
| 159 | + long readDelta = (readBytes - preMap.get(CounterConstants.HOST_DISK_READ_BYTES)) / checkIntervalSec; // per sec delta |
| 160 | + long writeDelta = (writeBytes - preMap.get(CounterConstants.HOST_DISK_WRITE_BYTES)) / checkIntervalSec; // per sec delta |
| 161 | + |
| 162 | + fsMap.put(this.READ_DELTA, readDelta); |
| 163 | + fsMap.put(this.WRITE_DELTA, writeDelta); |
| 164 | + |
| 165 | + tmpReadTotal += readDelta; |
| 166 | + tmpWriteTotal += writeDelta; |
| 167 | + } |
| 168 | + previousDiskStats.put(fs[i].getDevName(), fsMap); |
| 169 | + } |
| 170 | + |
| 171 | + HostNetDiskPerf.readTotalBytesPerSec = tmpReadTotal; |
| 172 | + HostNetDiskPerf.writeTotalBytesPerSec = tmpWriteTotal; |
| 173 | + |
| 174 | + } catch (SigarException se) { |
| 175 | + Logger.println("A144", 60, "SigarException on net usage", se); |
| 176 | + HostNetDiskPerf.rxTotalBytesPerSec = 0; |
| 177 | + HostNetDiskPerf.txTotalBytesPerSec = 0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments