blob: 0fbbc05390b72fea6b2e4530572c3ad47d39449b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.topology.cli;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070017
18import java.io.IOException;
19import java.util.concurrent.TimeUnit;
20
21import com.codahale.metrics.Gauge;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.json.MetricsModule;
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070029import org.onlab.metrics.EventMetric;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.cli.AbstractShellCommand;
31import org.onosproject.metrics.topology.TopologyMetricsService;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070032
33/**
34 * Command to show the topology events metrics.
35 */
36@Command(scope = "onos", name = "topology-events-metrics",
37 description = "Lists topology events metrics")
38public class TopologyEventsMetricsCommand extends AbstractShellCommand {
39
40 private static final String FORMAT_GAUGE =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070041 "Topology %s Event Timestamp (ms from epoch)=%d";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070042 private static final String FORMAT_METER =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070043 "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070044
45 @Override
46 protected void execute() {
47 TopologyMetricsService service = get(TopologyMetricsService.class);
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070048
49 if (outputJson()) {
50 ObjectMapper mapper = new ObjectMapper()
51 .registerModule(new MetricsModule(TimeUnit.SECONDS,
52 TimeUnit.MILLISECONDS,
53 false));
54 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070055 result = json(mapper, result, "topologyDeviceEvent",
56 service.topologyDeviceEventMetric());
57 result = json(mapper, result, "topologyHostEvent",
58 service.topologyHostEventMetric());
59 result = json(mapper, result, "topologyLinkEvent",
60 service.topologyLinkEventMetric());
61 result = json(mapper, result, "topologyGraphEvent",
62 service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070063 result = json(mapper, result, "topologyGraphReasonsEvent",
64 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070065 print("%s", result);
66 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070067 printEventMetric("Device", service.topologyDeviceEventMetric());
68 printEventMetric("Host", service.topologyHostEventMetric());
69 printEventMetric("Link", service.topologyLinkEventMetric());
70 printEventMetric("Graph", service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070071 printEventMetric("Graph Reasons",
72 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070073 }
74 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070075
76 /**
77 * Produces JSON node for an Event Metric.
78 *
79 * @param mapper the JSON object mapper to use
80 * @param objectNode the JSON object node to use
81 * @param propertyPrefix the property prefix to use
82 * @param eventMetric the Event Metric with the data
83 * @return JSON object node for the Event Metric
84 */
85 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
86 String propertyPrefix, EventMetric eventMetric) {
87 String gaugeName = propertyPrefix + "Timestamp";
88 String meterName = propertyPrefix + "Rate";
89 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
90 Meter meter = eventMetric.eventRateMeter();
91
Ray Milkey9d810f62015-02-13 11:20:58 -080092 objectNode.set(gaugeName, json(mapper, gauge));
93 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070094 return objectNode;
95 }
96
97 /**
98 * Produces JSON node for an Object.
99 *
100 * @param mapper the JSON object mapper to use
101 * @param object the Object with the data
102 * @return JSON node for the Object
103 */
104 private JsonNode json(ObjectMapper mapper, Object object) {
105 //
106 // NOTE: The API for custom serializers is incomplete,
107 // hence we have to parse the JSON string to create JsonNode.
108 //
109 try {
110 final String objectJson = mapper.writeValueAsString(object);
111 JsonNode jsonNode = mapper.readTree(objectJson);
112 return jsonNode;
113 } catch (JsonProcessingException e) {
114 log.error("Error writing value as JSON string", e);
115 } catch (IOException e) {
116 log.error("Error writing value as JSON string", e);
117 }
118 return null;
119 }
120
121 /**
122 * Prints an Event Metric.
123 *
124 * @param operationStr the string with the intent operation to print
125 * @param eventMetric the Event Metric to print
126 */
127 private void printEventMetric(String operationStr,
128 EventMetric eventMetric) {
129 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
130 Meter meter = eventMetric.eventRateMeter();
131 TimeUnit rateUnit = TimeUnit.SECONDS;
132 double rateFactor = rateUnit.toSeconds(1);
133
134 // Print the Gauge
135 print(FORMAT_GAUGE, operationStr, gauge.getValue());
136
137 // Print the Meter
138 print(FORMAT_METER, operationStr, meter.getCount(),
139 meter.getMeanRate() * rateFactor,
140 meter.getOneMinuteRate() * rateFactor,
141 meter.getFiveMinuteRate() * rateFactor,
142 meter.getFifteenMinuteRate() * rateFactor);
143 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700144}