Search in sources :

Example 1 with ControlMetric

use of org.onosproject.cpman.ControlMetric in project onos by opennetworkinglab.

the class SystemMetricsCollectorWebResource method memoryMetrics.

/**
 * Collects memory metrics.
 *
 * @param stream JSON stream
 * @return 200 OK
 * @onos.rsModel MemoryMetricsPost
 */
@POST
@Path("memory_metrics")
@Consumes(MediaType.APPLICATION_JSON)
public Response memoryMetrics(InputStream stream) {
    ObjectNode root = mapper().createObjectNode();
    ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
    MetricsService metricsService = get(MetricsService.class);
    ControlMetric cm;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        if (jsonTree == null || !checkFields(jsonTree, MEMORY_FIELD_SET)) {
            return ok(root).build();
        }
        long memUsed = nullIsIllegal(jsonTree.get("memoryUsed").asLong(), INVALID_REQUEST);
        long memFree = nullIsIllegal(jsonTree.get("memoryFree").asLong(), INVALID_REQUEST);
        long memTotal = memUsed + memFree;
        long memUsedRatio = memTotal == 0L ? 0L : (memUsed * PERCENT_CONSTANT) / memTotal;
        long memFreeRatio = memTotal == 0L ? 0L : (memFree * PERCENT_CONSTANT) / memTotal;
        aggregator.setMetricsService(metricsService);
        aggregator.addMetrics(Optional.empty(), SYSTEM_TYPE);
        cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO, new MetricValue.Builder().load(memUsedRatio).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio);
        cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO, new MetricValue.Builder().load(memFreeRatio).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio);
        cm = new ControlMetric(ControlMetricType.MEMORY_USED, new MetricValue.Builder().load(memUsed).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.MEMORY_USED, memUsed);
        cm = new ControlMetric(ControlMetricType.MEMORY_FREE, new MetricValue.Builder().load(memFree).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.MEMORY_FREE, memFree);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return ok(root).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControlPlaneMonitorService(org.onosproject.cpman.ControlPlaneMonitorService) MetricsService(org.onlab.metrics.MetricsService) ControlMetric(org.onosproject.cpman.ControlMetric) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 2 with ControlMetric

use of org.onosproject.cpman.ControlMetric in project onos by opennetworkinglab.

the class SystemMetricsCollectorWebResource method diskMetrics.

/**
 * Collects disk metrics.
 *
 * @param stream JSON stream
 * @return 200 OK
 * @onos.rsModel DiskMetricsPost
 */
@POST
@Path("disk_metrics")
@Consumes(MediaType.APPLICATION_JSON)
public Response diskMetrics(InputStream stream) {
    ObjectNode root = mapper().createObjectNode();
    ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
    MetricsService metricsService = get(MetricsService.class);
    ControlMetric cm;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        ArrayNode diskRes = jsonTree.get("disks") == null ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks");
        for (JsonNode node : diskRes) {
            JsonNode resourceName = node.get("resourceName");
            nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
            aggregator.setMetricsService(metricsService);
            aggregator.addMetrics(Optional.of(resourceName.asText()), DISK_TYPE);
            long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST);
            long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST);
            cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, new MetricValue.Builder().load(readBytes).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), DISK_TYPE, ControlMetricType.DISK_READ_BYTES, readBytes);
            cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, new MetricValue.Builder().load(writeBytes).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), DISK_TYPE, ControlMetricType.DISK_WRITE_BYTES, writeBytes);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return ok(root).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControlPlaneMonitorService(org.onosproject.cpman.ControlPlaneMonitorService) MetricsService(org.onlab.metrics.MetricsService) ControlMetric(org.onosproject.cpman.ControlMetric) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 3 with ControlMetric

use of org.onosproject.cpman.ControlMetric in project onos by opennetworkinglab.

the class SystemMetricsCollectorWebResource method cpuMetrics.

/**
 * Collects CPU metrics.
 *
 * @param stream JSON stream
 * @return 200 OK
 * @onos.rsModel CpuMetricsPost
 */
@POST
@Path("cpu_metrics")
@Consumes(MediaType.APPLICATION_JSON)
public Response cpuMetrics(InputStream stream) {
    ObjectNode root = mapper().createObjectNode();
    ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
    MetricsService metricsService = get(MetricsService.class);
    ControlMetric cm;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        if (jsonTree == null || !checkFields(jsonTree, CPU_FIELD_SET)) {
            return ok(root).build();
        }
        long cpuLoad = nullIsIllegal((long) (jsonTree.get("cpuLoad").asDouble() * PERCENT_CONSTANT), INVALID_REQUEST);
        long totalCpuTime = nullIsIllegal(jsonTree.get("totalCpuTime").asLong(), INVALID_REQUEST);
        long sysCpuTime = nullIsIllegal(jsonTree.get("sysCpuTime").asLong(), INVALID_REQUEST);
        long userCpuTime = nullIsIllegal(jsonTree.get("userCpuTime").asLong(), INVALID_REQUEST);
        long cpuIdleTime = nullIsIllegal(jsonTree.get("cpuIdleTime").asLong(), INVALID_REQUEST);
        aggregator.setMetricsService(metricsService);
        aggregator.addMetrics(Optional.empty(), SYSTEM_TYPE);
        cm = new ControlMetric(ControlMetricType.CPU_LOAD, new MetricValue.Builder().load(cpuLoad).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.CPU_LOAD, cpuLoad);
        cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, new MetricValue.Builder().load(totalCpuTime).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime);
        cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME, new MetricValue.Builder().load(sysCpuTime).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime);
        cm = new ControlMetric(ControlMetricType.USER_CPU_TIME, new MetricValue.Builder().load(userCpuTime).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.USER_CPU_TIME, userCpuTime);
        cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME, new MetricValue.Builder().load(cpuIdleTime).add());
        monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
        aggregator.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return ok(root).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControlPlaneMonitorService(org.onosproject.cpman.ControlPlaneMonitorService) MetricsService(org.onlab.metrics.MetricsService) ControlMetric(org.onosproject.cpman.ControlMetric) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 4 with ControlMetric

use of org.onosproject.cpman.ControlMetric in project onos by opennetworkinglab.

the class ControlPlaneMonitorTest method testUpdateMetricWithoutId.

private void testUpdateMetricWithoutId(ControlMetricType cmt, MetricValue mv) {
    ControlMetric cm = new ControlMetric(cmt, mv);
    monitor.updateMetric(cm, UPDATE_INTERVAL, Optional.empty());
}
Also used : ControlMetric(org.onosproject.cpman.ControlMetric)

Example 5 with ControlMetric

use of org.onosproject.cpman.ControlMetric in project onos by opennetworkinglab.

the class SystemMetricsCollectorWebResource method networkMetrics.

/**
 * Collects network metrics.
 *
 * @param stream JSON stream
 * @return 200 OK
 * @onos.rsModel NetworkMetricsPost
 */
@POST
@Path("network_metrics")
@Consumes(MediaType.APPLICATION_JSON)
public Response networkMetrics(InputStream stream) {
    ObjectNode root = mapper().createObjectNode();
    ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
    MetricsService metricsService = get(MetricsService.class);
    ControlMetric cm;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        ArrayNode networkRes = jsonTree.get("networks") == null ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("networks");
        for (JsonNode node : networkRes) {
            JsonNode resourceName = node.get("resourceName");
            nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
            aggregator.setMetricsService(metricsService);
            aggregator.addMetrics(Optional.of(resourceName.asText()), NETWORK_TYPE);
            long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST);
            long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST);
            long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST);
            long outPackets = nullIsIllegal(node.get("outgoingPackets").asLong(), INVALID_REQUEST);
            cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, new MetricValue.Builder().load(inBytes).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), NETWORK_TYPE, ControlMetricType.NW_INCOMING_BYTES, inBytes);
            cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, new MetricValue.Builder().load(outBytes).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), NETWORK_TYPE, ControlMetricType.NW_OUTGOING_BYTES, outBytes);
            cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, new MetricValue.Builder().load(inPackets).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), NETWORK_TYPE, ControlMetricType.NW_INCOMING_PACKETS, inPackets);
            cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, new MetricValue.Builder().load(outPackets).add());
            monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
            aggregator.increment(resourceName.asText(), NETWORK_TYPE, ControlMetricType.NW_OUTGOING_PACKETS, outPackets);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return ok(root).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControlPlaneMonitorService(org.onosproject.cpman.ControlPlaneMonitorService) MetricsService(org.onlab.metrics.MetricsService) ControlMetric(org.onosproject.cpman.ControlMetric) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

ControlMetric (org.onosproject.cpman.ControlMetric)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 IOException (java.io.IOException)4 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 MetricsService (org.onlab.metrics.MetricsService)4 ControlPlaneMonitorService (org.onosproject.cpman.ControlPlaneMonitorService)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2