blob: b7e0401d4281276cca113330d708ac9d637502bb [file] [log] [blame]
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -07001package org.onlab.onos.metrics.topology.cli;
2
3import java.io.IOException;
4import java.util.concurrent.TimeUnit;
5
6import com.codahale.metrics.Gauge;
7import com.codahale.metrics.Meter;
8import com.codahale.metrics.json.MetricsModule;
9import com.fasterxml.jackson.core.JsonProcessingException;
10import com.fasterxml.jackson.databind.JsonNode;
11import com.fasterxml.jackson.databind.ObjectMapper;
12import com.fasterxml.jackson.databind.node.ObjectNode;
13import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070014import org.onlab.metrics.EventMetric;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070015import org.onlab.onos.cli.AbstractShellCommand;
16import org.onlab.onos.metrics.topology.TopologyMetricsService;
17
18/**
19 * Command to show the topology events metrics.
20 */
21@Command(scope = "onos", name = "topology-events-metrics",
22 description = "Lists topology events metrics")
23public class TopologyEventsMetricsCommand extends AbstractShellCommand {
24
25 private static final String FORMAT_GAUGE =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070026 "Topology %s Event Timestamp (ms from epoch)=%d";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070027 private static final String FORMAT_METER =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070028 "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070029
30 @Override
31 protected void execute() {
32 TopologyMetricsService service = get(TopologyMetricsService.class);
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070033
34 if (outputJson()) {
35 ObjectMapper mapper = new ObjectMapper()
36 .registerModule(new MetricsModule(TimeUnit.SECONDS,
37 TimeUnit.MILLISECONDS,
38 false));
39 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070040 result = json(mapper, result, "topologyDeviceEvent",
41 service.topologyDeviceEventMetric());
42 result = json(mapper, result, "topologyHostEvent",
43 service.topologyHostEventMetric());
44 result = json(mapper, result, "topologyLinkEvent",
45 service.topologyLinkEventMetric());
46 result = json(mapper, result, "topologyGraphEvent",
47 service.topologyGraphEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070048 print("%s", result);
49 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070050 printEventMetric("Device", service.topologyDeviceEventMetric());
51 printEventMetric("Host", service.topologyHostEventMetric());
52 printEventMetric("Link", service.topologyLinkEventMetric());
53 printEventMetric("Graph", service.topologyGraphEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070054 }
55 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070056
57 /**
58 * Produces JSON node for an Event Metric.
59 *
60 * @param mapper the JSON object mapper to use
61 * @param objectNode the JSON object node to use
62 * @param propertyPrefix the property prefix to use
63 * @param eventMetric the Event Metric with the data
64 * @return JSON object node for the Event Metric
65 */
66 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
67 String propertyPrefix, EventMetric eventMetric) {
68 String gaugeName = propertyPrefix + "Timestamp";
69 String meterName = propertyPrefix + "Rate";
70 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
71 Meter meter = eventMetric.eventRateMeter();
72
73 objectNode.put(gaugeName, json(mapper, gauge));
74 objectNode.put(meterName, json(mapper, meter));
75 return objectNode;
76 }
77
78 /**
79 * Produces JSON node for an Object.
80 *
81 * @param mapper the JSON object mapper to use
82 * @param object the Object with the data
83 * @return JSON node for the Object
84 */
85 private JsonNode json(ObjectMapper mapper, Object object) {
86 //
87 // NOTE: The API for custom serializers is incomplete,
88 // hence we have to parse the JSON string to create JsonNode.
89 //
90 try {
91 final String objectJson = mapper.writeValueAsString(object);
92 JsonNode jsonNode = mapper.readTree(objectJson);
93 return jsonNode;
94 } catch (JsonProcessingException e) {
95 log.error("Error writing value as JSON string", e);
96 } catch (IOException e) {
97 log.error("Error writing value as JSON string", e);
98 }
99 return null;
100 }
101
102 /**
103 * Prints an Event Metric.
104 *
105 * @param operationStr the string with the intent operation to print
106 * @param eventMetric the Event Metric to print
107 */
108 private void printEventMetric(String operationStr,
109 EventMetric eventMetric) {
110 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
111 Meter meter = eventMetric.eventRateMeter();
112 TimeUnit rateUnit = TimeUnit.SECONDS;
113 double rateFactor = rateUnit.toSeconds(1);
114
115 // Print the Gauge
116 print(FORMAT_GAUGE, operationStr, gauge.getValue());
117
118 // Print the Meter
119 print(FORMAT_METER, operationStr, meter.getCount(),
120 meter.getMeanRate() * rateFactor,
121 meter.getOneMinuteRate() * rateFactor,
122 meter.getFiveMinuteRate() * rateFactor,
123 meter.getFifteenMinuteRate() * rateFactor);
124 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700125}