use of org.onlab.metrics.MetricsService 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();
}
use of org.onlab.metrics.MetricsService 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();
}
use of org.onlab.metrics.MetricsService 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();
}
use of org.onlab.metrics.MetricsService in project onos by opennetworkinglab.
the class PacketStatsWebResource method getPacketStatsCountersJson.
private ObjectNode getPacketStatsCountersJson() {
MetricsService service = get(MetricsService.class);
ObjectNode node = mapper.createObjectNode();
ObjectNode pktCounterNode = mapper.createObjectNode();
Map<String, Counter> counters = service.getCounters(filter);
Counter arpCounter = counters.get("packetStatisticsComponent.arpFeature.arpPC");
Counter lldpCounter = counters.get("packetStatisticsComponent.lldpFeature.lldpPC");
Counter nsCounter = counters.get("packetStatisticsComponent.nbrSolicitFeature.nbrSolicitPC");
Counter naCounter = counters.get("packetStatisticsComponent.nbrAdvertFeature.nbrAdvertPC");
pktCounterNode.put("arpCounter", arpCounter.getCount());
pktCounterNode.put("lldpCounter", lldpCounter.getCount());
pktCounterNode.put("nsCounter", nsCounter.getCount());
pktCounterNode.put("naCounter", naCounter.getCount());
node.put("packet_stats_counters", pktCounterNode);
return node;
}
use of org.onlab.metrics.MetricsService in project onos by opennetworkinglab.
the class MetricsHelper method createTimer.
/**
* Creates a Timer instance with given name.
*
* @param component component name
* @param feature feature name
* @param name timer name
* @return Timer instance
*/
default Timer createTimer(String component, String feature, String name) {
final MetricsService metricsService = metricsService();
if (metricsService != null) {
MetricsComponent c = metricsService.registerComponent(component);
MetricsFeature f = c.registerFeature(feature);
return metricsService.createTimer(c, f, name);
}
return null;
}
Aggregations