blob: 921d9d42f07652e97ba5d61d92302f04a6bdd023 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070019package org.onlab.onos.metrics.topology.cli;
20
21import java.io.IOException;
22import java.util.concurrent.TimeUnit;
23
24import com.codahale.metrics.Gauge;
25import com.codahale.metrics.Meter;
26import com.codahale.metrics.json.MetricsModule;
27import com.fasterxml.jackson.core.JsonProcessingException;
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070032import org.onlab.metrics.EventMetric;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070033import org.onlab.onos.cli.AbstractShellCommand;
34import org.onlab.onos.metrics.topology.TopologyMetricsService;
35
36/**
37 * Command to show the topology events metrics.
38 */
39@Command(scope = "onos", name = "topology-events-metrics",
40 description = "Lists topology events metrics")
41public class TopologyEventsMetricsCommand extends AbstractShellCommand {
42
43 private static final String FORMAT_GAUGE =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070044 "Topology %s Event Timestamp (ms from epoch)=%d";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070045 private static final String FORMAT_METER =
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070046 "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070047
48 @Override
49 protected void execute() {
50 TopologyMetricsService service = get(TopologyMetricsService.class);
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070051
52 if (outputJson()) {
53 ObjectMapper mapper = new ObjectMapper()
54 .registerModule(new MetricsModule(TimeUnit.SECONDS,
55 TimeUnit.MILLISECONDS,
56 false));
57 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070058 result = json(mapper, result, "topologyDeviceEvent",
59 service.topologyDeviceEventMetric());
60 result = json(mapper, result, "topologyHostEvent",
61 service.topologyHostEventMetric());
62 result = json(mapper, result, "topologyLinkEvent",
63 service.topologyLinkEventMetric());
64 result = json(mapper, result, "topologyGraphEvent",
65 service.topologyGraphEventMetric());
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 Radoslavov64d9e472014-10-21 22:01:08 -070072 }
73 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070074
75 /**
76 * Produces JSON node for an Event Metric.
77 *
78 * @param mapper the JSON object mapper to use
79 * @param objectNode the JSON object node to use
80 * @param propertyPrefix the property prefix to use
81 * @param eventMetric the Event Metric with the data
82 * @return JSON object node for the Event Metric
83 */
84 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
85 String propertyPrefix, EventMetric eventMetric) {
86 String gaugeName = propertyPrefix + "Timestamp";
87 String meterName = propertyPrefix + "Rate";
88 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
89 Meter meter = eventMetric.eventRateMeter();
90
91 objectNode.put(gaugeName, json(mapper, gauge));
92 objectNode.put(meterName, json(mapper, meter));
93 return objectNode;
94 }
95
96 /**
97 * Produces JSON node for an Object.
98 *
99 * @param mapper the JSON object mapper to use
100 * @param object the Object with the data
101 * @return JSON node for the Object
102 */
103 private JsonNode json(ObjectMapper mapper, Object object) {
104 //
105 // NOTE: The API for custom serializers is incomplete,
106 // hence we have to parse the JSON string to create JsonNode.
107 //
108 try {
109 final String objectJson = mapper.writeValueAsString(object);
110 JsonNode jsonNode = mapper.readTree(objectJson);
111 return jsonNode;
112 } catch (JsonProcessingException e) {
113 log.error("Error writing value as JSON string", e);
114 } catch (IOException e) {
115 log.error("Error writing value as JSON string", e);
116 }
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}