blob: 78ff7af676a5bcb196d1cc7831330fd5df5ecb29 [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
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070018import java.util.concurrent.TimeUnit;
19
20import com.codahale.metrics.Gauge;
21import com.codahale.metrics.Meter;
22import com.codahale.metrics.json.MetricsModule;
23import com.fasterxml.jackson.core.JsonProcessingException;
24import com.fasterxml.jackson.databind.JsonNode;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070027import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070028import org.apache.karaf.shell.api.action.lifecycle.Service;
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 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070037@Command(scope = "onos", name = "topology-events-metrics",
38 description = "Lists topology events metrics")
39public class TopologyEventsMetricsCommand extends AbstractShellCommand {
40
41 private static final String FORMAT_GAUGE =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070042 "Topology %s Event Timestamp (ms from epoch)=%d";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070043 private static final String FORMAT_METER =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070044 "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070045
46 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070047 protected void doExecute() {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070048 TopologyMetricsService service = get(TopologyMetricsService.class);
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070049
50 if (outputJson()) {
51 ObjectMapper mapper = new ObjectMapper()
52 .registerModule(new MetricsModule(TimeUnit.SECONDS,
53 TimeUnit.MILLISECONDS,
54 false));
55 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070056 result = json(mapper, result, "topologyDeviceEvent",
57 service.topologyDeviceEventMetric());
58 result = json(mapper, result, "topologyHostEvent",
59 service.topologyHostEventMetric());
60 result = json(mapper, result, "topologyLinkEvent",
61 service.topologyLinkEventMetric());
62 result = json(mapper, result, "topologyGraphEvent",
63 service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070064 result = json(mapper, result, "topologyGraphReasonsEvent",
65 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070066 print("%s", result);
67 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070068 printEventMetric("Device", service.topologyDeviceEventMetric());
69 printEventMetric("Host", service.topologyHostEventMetric());
70 printEventMetric("Link", service.topologyLinkEventMetric());
71 printEventMetric("Graph", service.topologyGraphEventMetric());
Pavlin Radoslavov6aaa1e02015-03-18 11:12:06 -070072 printEventMetric("Graph Reasons",
73 service.topologyGraphReasonsEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070074 }
75 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070076
77 /**
78 * Produces JSON node for an Event Metric.
79 *
80 * @param mapper the JSON object mapper to use
81 * @param objectNode the JSON object node to use
82 * @param propertyPrefix the property prefix to use
83 * @param eventMetric the Event Metric with the data
84 * @return JSON object node for the Event Metric
85 */
86 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
87 String propertyPrefix, EventMetric eventMetric) {
88 String gaugeName = propertyPrefix + "Timestamp";
89 String meterName = propertyPrefix + "Rate";
90 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
91 Meter meter = eventMetric.eventRateMeter();
92
Ray Milkey9d810f62015-02-13 11:20:58 -080093 objectNode.set(gaugeName, json(mapper, gauge));
94 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070095 return objectNode;
96 }
97
98 /**
99 * Produces JSON node for an Object.
100 *
101 * @param mapper the JSON object mapper to use
102 * @param object the Object with the data
103 * @return JSON node for the Object
104 */
105 private JsonNode json(ObjectMapper mapper, Object object) {
106 //
107 // NOTE: The API for custom serializers is incomplete,
108 // hence we have to parse the JSON string to create JsonNode.
109 //
110 try {
111 final String objectJson = mapper.writeValueAsString(object);
112 JsonNode jsonNode = mapper.readTree(objectJson);
113 return jsonNode;
114 } catch (JsonProcessingException e) {
115 log.error("Error writing value as JSON string", e);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700116 }
117 return null;
118 }
119
120 /**
121 * Prints an Event Metric.
122 *
123 * @param operationStr the string with the intent operation to print
124 * @param eventMetric the Event Metric to print
125 */
126 private void printEventMetric(String operationStr,
127 EventMetric eventMetric) {
128 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
129 Meter meter = eventMetric.eventRateMeter();
130 TimeUnit rateUnit = TimeUnit.SECONDS;
131 double rateFactor = rateUnit.toSeconds(1);
132
133 // Print the Gauge
134 print(FORMAT_GAUGE, operationStr, gauge.getValue());
135
136 // Print the Meter
137 print(FORMAT_METER, operationStr, meter.getCount(),
138 meter.getMeanRate() * rateFactor,
139 meter.getOneMinuteRate() * rateFactor,
140 meter.getFiveMinuteRate() * rateFactor,
141 meter.getFifteenMinuteRate() * rateFactor);
142 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700143}