blob: 39e0b7f57146a7a64058329dd1007f37eb31ea03 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070028import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070029import org.apache.karaf.shell.api.action.lifecycle.Service;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070030import org.onlab.metrics.EventMetric;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cli.AbstractShellCommand;
32import org.onosproject.metrics.topology.TopologyMetricsService;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070033
34/**
35 * Command to show the topology events metrics.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070038@Command(scope = "onos", name = "topology-events-metrics",
39 description = "Lists topology events metrics")
40public class TopologyEventsMetricsCommand extends AbstractShellCommand {
41
42 private static final String FORMAT_GAUGE =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070043 "Topology %s Event Timestamp (ms from epoch)=%d";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070044 private static final String FORMAT_METER =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070045 "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070046
47 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070048 protected void doExecute() {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070049 TopologyMetricsService service = get(TopologyMetricsService.class);
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070050
51 if (outputJson()) {
52 ObjectMapper mapper = new ObjectMapper()
53 .registerModule(new MetricsModule(TimeUnit.SECONDS,
54 TimeUnit.MILLISECONDS,
55 false));
56 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070057 result = json(mapper, result, "topologyDeviceEvent",
58 service.topologyDeviceEventMetric());
59 result = json(mapper, result, "topologyHostEvent",
60 service.topologyHostEventMetric());
61 result = json(mapper, result, "topologyLinkEvent",
62 service.topologyLinkEventMetric());
63 result = json(mapper, result, "topologyGraphEvent",
64 service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070065 result = json(mapper, result, "topologyGraphReasonsEvent",
66 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070067 print("%s", result);
68 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070069 printEventMetric("Device", service.topologyDeviceEventMetric());
70 printEventMetric("Host", service.topologyHostEventMetric());
71 printEventMetric("Link", service.topologyLinkEventMetric());
72 printEventMetric("Graph", service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070073 printEventMetric("Graph Reasons",
74 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070075 }
76 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070077
78 /**
79 * Produces JSON node for an Event Metric.
80 *
81 * @param mapper the JSON object mapper to use
82 * @param objectNode the JSON object node to use
83 * @param propertyPrefix the property prefix to use
84 * @param eventMetric the Event Metric with the data
85 * @return JSON object node for the Event Metric
86 */
87 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
88 String propertyPrefix, EventMetric eventMetric) {
89 String gaugeName = propertyPrefix + "Timestamp";
90 String meterName = propertyPrefix + "Rate";
91 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
92 Meter meter = eventMetric.eventRateMeter();
93
Ray Milkey9d810f62015-02-13 11:20:58 -080094 objectNode.set(gaugeName, json(mapper, gauge));
95 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070096 return objectNode;
97 }
98
99 /**
100 * Produces JSON node for an Object.
101 *
102 * @param mapper the JSON object mapper to use
103 * @param object the Object with the data
104 * @return JSON node for the Object
105 */
106 private JsonNode json(ObjectMapper mapper, Object object) {
107 //
108 // NOTE: The API for custom serializers is incomplete,
109 // hence we have to parse the JSON string to create JsonNode.
110 //
111 try {
112 final String objectJson = mapper.writeValueAsString(object);
113 JsonNode jsonNode = mapper.readTree(objectJson);
114 return jsonNode;
115 } catch (JsonProcessingException e) {
116 log.error("Error writing value as JSON string", e);
117 } catch (IOException e) {
118 log.error("Error writing value as JSON string", e);
119 }
120 return null;
121 }
122
123 /**
124 * Prints an Event Metric.
125 *
126 * @param operationStr the string with the intent operation to print
127 * @param eventMetric the Event Metric to print
128 */
129 private void printEventMetric(String operationStr,
130 EventMetric eventMetric) {
131 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
132 Meter meter = eventMetric.eventRateMeter();
133 TimeUnit rateUnit = TimeUnit.SECONDS;
134 double rateFactor = rateUnit.toSeconds(1);
135
136 // Print the Gauge
137 print(FORMAT_GAUGE, operationStr, gauge.getValue());
138
139 // Print the Meter
140 print(FORMAT_METER, operationStr, meter.getCount(),
141 meter.getMeanRate() * rateFactor,
142 meter.getOneMinuteRate() * rateFactor,
143 meter.getFiveMinuteRate() * rateFactor,
144 meter.getFifteenMinuteRate() * rateFactor);
145 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700146}