Search in sources :

Example 1 with InterfaceUsage

use of org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage in project java-chassis by ServiceComb.

the class NetMeter method refreshNet.

/*
   * Inter-|   Receive                                                            |  Transmit
   *  face |bytes      packets     errs drop fifo  frame      compressed multicast|bytes       packets     errs   drop  fifo colls carrier compressed
   *  eth0: 2615248100 32148518    0    0    0     0          0          0         87333034794 21420267    0      0     0     0    0    0
   *        0          1           2    3    4     5          6          7          8          9
   */
protected void refreshNet(long secondInterval) {
    try {
        File file = new File("/proc/net/dev");
        List<String> netInfo = FileUtils.readLines(file, StandardCharsets.UTF_8);
        Set<String> nameSet = new HashSet<>();
        // the first two lines is useless
        for (int i = 2; i < netInfo.size(); i++) {
            String interfaceData = netInfo.get(i);
            String[] strings = interfaceData.split(":");
            if (strings.length != 2) {
                LOGGER.warn(" there is something wrong with {} ", interfaceData);
                continue;
            }
            String name = strings[0].trim();
            nameSet.add(name);
            InterfaceUsage interfaceUsage = interfaceUsageMap.computeIfAbsent(name, key -> new InterfaceUsage(id, key));
            interfaceUsage.update(strings[1], secondInterval);
        }
        // clear deleted interfaces
        for (String interfaceName : interfaceUsageMap.keySet()) {
            if (!nameSet.contains(interfaceName)) {
                this.interfaceUsageMap.remove(interfaceName);
            }
        }
    } catch (IOException e) {
        LOGGER.error("Failed to read net info/", e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) HashSet(java.util.HashSet)

Example 2 with InterfaceUsage

use of org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage in project java-chassis by ServiceComb.

the class TestNetMeter method testNetRefreshUnchanged.

@Test
public void testNetRefreshUnchanged(@Mocked Id id) {
    List<String> list = new ArrayList<>();
    list.add("useless");
    list.add("useless");
    list.add("eth0: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    new MockUp<FileUtils>() {

        @Mock
        public List<String> readLines(File file, Charset encoding) {
            return list;
        }
    };
    NetMeter netMeter = new NetMeter(id);
    list.remove(2);
    list.add("eth0: 1 1    0    0    0     0          0          1         1 1    1      0     0     0    0    0");
    netMeter.refreshNet(1);
    Map<String, InterfaceUsage> meterInterfaceInfoMap = netMeter.getInterfaceUsageMap();
    Assert.assertTrue(meterInterfaceInfoMap.containsKey("eth0"));
    InterfaceUsage eth0 = meterInterfaceInfoMap.get("eth0");
    Assert.assertEquals("eth0", eth0.getName());
    Assert.assertEquals(4, eth0.getNetStats().size());
    // recv Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(0).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(1).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(1L, eth0.getNetStats().get(2).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(1L, eth0.getNetStats().get(3).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
}
Also used : ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) File(java.io.File) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) Test(org.junit.Test)

Example 3 with InterfaceUsage

use of org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage in project java-chassis by ServiceComb.

the class TestOsMeterInitializer method init.

@Test
public void init(@Mocked Runtime runtime, @Mocked RuntimeMXBean mxBean) {
    List<String> list = new ArrayList<>();
    list.add("13  1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1");
    list.add("useless");
    list.add("eth0: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    new MockUp<Files>() {

        // Files.readFirstLine
        @Mock
        public String readFirstLine(File file, Charset encoding) {
            return list.get(0);
        }
    };
    new MockUp<FileUtils>() {

        @Mock
        public List<String> readLines(File file, Charset encoding) {
            return list;
        }
    };
    new MockUp<CpuUtils>() {

        @Mock
        public int calcHertz() {
            return 100;
        }
    };
    new MockUp<ManagementFactory>() {

        @Mock
        RuntimeMXBean getRuntimeMXBean() {
            return mxBean;
        }
    };
    new MockUp<Runtime>() {

        @Mock
        public Runtime getRuntime() {
            return runtime;
        }
    };
    new Expectations() {

        {
            runtime.availableProcessors();
            result = 2;
        }
    };
    globalRegistry.add(registry);
    OsMetersInitializer osMetersInitializer = new OsMetersInitializer();
    osMetersInitializer.setOsLinux(true);
    osMetersInitializer.init(globalRegistry, eventBus, null);
    OsMeter osMeter = osMetersInitializer.getOsMeter();
    Assert.assertNotNull(osMeter);
    Assert.assertNotNull(osMeter.getCpuMeter());
    Assert.assertNotNull(osMeter.getNetMeter());
    CpuMeter cpuMeter = osMeter.getCpuMeter();
    NetMeter netMeter = osMeter.getNetMeter();
    Assert.assertEquals(0.0, cpuMeter.getProcessCpuUsage().getUsage(), 0.0);
    Assert.assertEquals(0.0, cpuMeter.getAllCpuUsage().getUsage(), 0.0);
    Map<String, InterfaceUsage> interfaceInfoMap = netMeter.getInterfaceUsageMap();
    Assert.assertEquals(1, interfaceInfoMap.size());
    InterfaceUsage eth0 = interfaceInfoMap.get("eth0");
    Assert.assertEquals(4, eth0.getNetStats().size());
    // recv Bps
    Assert.assertEquals(0L, eth0.getNetStats().get(0).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(0L, eth0.getNetStats().get(1).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(0L, eth0.getNetStats().get(2).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(0L, eth0.getNetStats().get(3).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
}
Also used : Expectations(mockit.Expectations) NetMeter(org.apache.servicecomb.metrics.core.meter.os.NetMeter) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) OsMeter(org.apache.servicecomb.metrics.core.meter.os.OsMeter) File(java.io.File) CpuMeter(org.apache.servicecomb.metrics.core.meter.os.CpuMeter) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) Test(org.junit.Test)

Example 4 with InterfaceUsage

use of org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage in project java-chassis by ServiceComb.

the class TestNetMeter method testNetRefreshRemove.

@Test
public void testNetRefreshRemove(@Mocked Id id) {
    List<String> list = new ArrayList<>();
    list.add("useless");
    list.add("useless");
    list.add("eth0: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    list.add("lo: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    new MockUp<FileUtils>() {

        @Mock
        public List<String> readLines(File file, Charset encoding) {
            return list;
        }
    };
    NetMeter netMeter = new NetMeter(id);
    Map<String, InterfaceUsage> netMap = netMeter.getInterfaceUsageMap();
    Assert.assertEquals(2, netMap.size());
    InterfaceUsage lo = netMap.get("lo");
    InterfaceUsage eth0 = netMap.get("eth0");
    Assert.assertEquals("lo", lo.getName());
    Assert.assertEquals(4, lo.getNetStats().size());
    // recv Bps
    Assert.assertEquals(0L, lo.getNetStats().get(0).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, lo.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(0L, lo.getNetStats().get(1).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, lo.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(0L, lo.getNetStats().get(2).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, lo.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(0L, lo.getNetStats().get(3).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, lo.getNetStats().get(3).getIndex());
    Assert.assertEquals("eth0", eth0.getName());
    Assert.assertEquals(4, eth0.getNetStats().size());
    // recv Bps
    Assert.assertEquals(0L, eth0.getNetStats().get(0).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(0L, eth0.getNetStats().get(1).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(0L, eth0.getNetStats().get(2).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(0L, eth0.getNetStats().get(3).getLastValue());
    Assert.assertEquals(0, eth0.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
    list.remove(2);
    list.remove(2);
    list.add("eth0: 1 1    0    0    0     0          0          1         1 1    1      0     0     0    0    0");
    netMeter.refreshNet(1);
    Assert.assertNull(netMap.get("lo"));
    Assert.assertEquals(1, netMap.size());
    Assert.assertEquals("eth0", eth0.getName());
    Assert.assertEquals(4, eth0.getNetStats().size());
    // recv Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(0).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(1).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(1L, eth0.getNetStats().get(2).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(1L, eth0.getNetStats().get(3).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
}
Also used : ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) File(java.io.File) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) Test(org.junit.Test)

Example 5 with InterfaceUsage

use of org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage in project java-chassis by ServiceComb.

the class TestNetMeter method testNetRefreshAdd.

@Test
public void testNetRefreshAdd(@Mocked Id id) {
    List<String> list = new ArrayList<>();
    list.add("useless");
    list.add("useless");
    list.add("eth0: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    new MockUp<FileUtils>() {

        @Mock
        public List<String> readLines(File file, Charset encoding) {
            return list;
        }
    };
    NetMeter netMeter = new NetMeter(id);
    Map<String, InterfaceUsage> netMap = netMeter.getInterfaceUsageMap();
    Assert.assertEquals(1, netMap.size());
    list.remove(2);
    list.add("eth0: 1 1    0    0    0     0          0          1         1 1    1      0     0     0    0    0");
    list.add("lo: 0 0    0    0    0     0          0          0         0 0    0      0     0     0    0    0");
    netMeter.refreshNet(1);
    Assert.assertEquals(2, netMap.size());
    InterfaceUsage eth0 = netMap.get("eth0");
    Assert.assertEquals("eth0", eth0.getName());
    Assert.assertEquals(4, eth0.getNetStats().size());
    // recv Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(0).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, eth0.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(1L, eth0.getNetStats().get(1).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, eth0.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(1L, eth0.getNetStats().get(2).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, eth0.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(1L, eth0.getNetStats().get(3).getLastValue());
    Assert.assertEquals(1, eth0.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, eth0.getNetStats().get(3).getIndex());
    InterfaceUsage lo = netMap.get("lo");
    Assert.assertEquals("lo", lo.getName());
    Assert.assertEquals(4, lo.getNetStats().size());
    // recv Bps
    Assert.assertEquals(0L, lo.getNetStats().get(0).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(0).getRate(), 0.0);
    Assert.assertEquals(0, lo.getNetStats().get(0).getIndex());
    // send Bps
    Assert.assertEquals(0L, lo.getNetStats().get(1).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(1).getRate(), 0.0);
    Assert.assertEquals(8, lo.getNetStats().get(1).getIndex());
    // recv pps
    Assert.assertEquals(0L, lo.getNetStats().get(2).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(2).getRate(), 0.0);
    Assert.assertEquals(1, lo.getNetStats().get(2).getIndex());
    // send pps
    Assert.assertEquals(0L, lo.getNetStats().get(3).getLastValue());
    Assert.assertEquals(0, lo.getNetStats().get(3).getRate(), 0.0);
    Assert.assertEquals(9, lo.getNetStats().get(3).getIndex());
}
Also used : ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) MockUp(mockit.MockUp) File(java.io.File) InterfaceUsage(org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage) Test(org.junit.Test)

Aggregations

File (java.io.File)5 InterfaceUsage (org.apache.servicecomb.metrics.core.meter.os.net.InterfaceUsage)5 Charset (java.nio.charset.Charset)4 ArrayList (java.util.ArrayList)4 MockUp (mockit.MockUp)4 Test (org.junit.Test)4 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Expectations (mockit.Expectations)1 CpuMeter (org.apache.servicecomb.metrics.core.meter.os.CpuMeter)1 NetMeter (org.apache.servicecomb.metrics.core.meter.os.NetMeter)1 OsMeter (org.apache.servicecomb.metrics.core.meter.os.OsMeter)1