Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.metrics.topology.cli; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.concurrent.TimeUnit; |
| 5 | |
| 6 | import com.codahale.metrics.Gauge; |
| 7 | import com.codahale.metrics.Meter; |
| 8 | import com.codahale.metrics.json.MetricsModule; |
| 9 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | import com.fasterxml.jackson.databind.JsonNode; |
| 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 13 | import org.apache.karaf.shell.commands.Command; |
| 14 | import org.onlab.onos.cli.AbstractShellCommand; |
| 15 | import org.onlab.onos.metrics.topology.TopologyMetricsService; |
| 16 | |
| 17 | /** |
| 18 | * Command to show the topology events metrics. |
| 19 | */ |
| 20 | @Command(scope = "onos", name = "topology-events-metrics", |
| 21 | description = "Lists topology events metrics") |
| 22 | public class TopologyEventsMetricsCommand extends AbstractShellCommand { |
| 23 | |
| 24 | private static final String FORMAT_GAUGE = |
| 25 | "Last Topology Event Timestamp (ms from epoch)=%d"; |
| 26 | private static final String FORMAT_METER = |
| 27 | "Topology Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; |
| 28 | |
| 29 | @Override |
| 30 | protected void execute() { |
| 31 | TopologyMetricsService service = get(TopologyMetricsService.class); |
| 32 | Gauge<Long> gauge = service.lastEventTimestampEpochMsGauge(); |
| 33 | Meter meter = service.eventRateMeter(); |
| 34 | |
| 35 | if (outputJson()) { |
| 36 | ObjectMapper mapper = new ObjectMapper() |
| 37 | .registerModule(new MetricsModule(TimeUnit.SECONDS, |
| 38 | TimeUnit.MILLISECONDS, |
| 39 | false)); |
| 40 | ObjectNode result = mapper.createObjectNode(); |
| 41 | try { |
| 42 | // |
| 43 | // NOTE: The API for custom serializers is incomplete, |
| 44 | // hence we have to parse the JSON string to create JsonNode. |
| 45 | // |
| 46 | final String gaugeJson = mapper.writeValueAsString(gauge); |
| 47 | final String meterJson = mapper.writeValueAsString(meter); |
| 48 | JsonNode gaugeNode = mapper.readTree(gaugeJson); |
| 49 | JsonNode meterNode = mapper.readTree(meterJson); |
| 50 | result.put("lastTopologyEventTimestamp", gaugeNode); |
Pavlin Radoslavov | 6bc5594 | 2014-10-21 22:16:53 -0700 | [diff] [blame] | 51 | result.put("topologyEventRate", meterNode); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 52 | } catch (JsonProcessingException e) { |
| 53 | log.error("Error writing value as JSON string", e); |
| 54 | } catch (IOException e) { |
| 55 | log.error("Error writing value as JSON string", e); |
| 56 | } |
| 57 | print("%s", result); |
| 58 | } else { |
| 59 | TimeUnit rateUnit = TimeUnit.SECONDS; |
| 60 | double rateFactor = rateUnit.toSeconds(1); |
| 61 | print(FORMAT_GAUGE, gauge.getValue()); |
| 62 | print(FORMAT_METER, meter.getCount(), |
| 63 | meter.getMeanRate() * rateFactor, |
| 64 | meter.getOneMinuteRate() * rateFactor, |
| 65 | meter.getFiveMinuteRate() * rateFactor, |
| 66 | meter.getFifteenMinuteRate() * rateFactor); |
| 67 | } |
| 68 | } |
| 69 | } |