blob: f0c5becdc7c3dffbb49673d64636ce660bf21d30 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070016package org.onlab.onos.metrics.topology.cli;
17
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;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070030import org.onlab.onos.cli.AbstractShellCommand;
31import org.onlab.onos.metrics.topology.TopologyMetricsService;
32
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 Radoslavov64d9e472014-10-21 22:01:08 -070063 print("%s", result);
64 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070065 printEventMetric("Device", service.topologyDeviceEventMetric());
66 printEventMetric("Host", service.topologyHostEventMetric());
67 printEventMetric("Link", service.topologyLinkEventMetric());
68 printEventMetric("Graph", service.topologyGraphEventMetric());
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070069 }
70 }
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070071
72 /**
73 * Produces JSON node for an Event Metric.
74 *
75 * @param mapper the JSON object mapper to use
76 * @param objectNode the JSON object node to use
77 * @param propertyPrefix the property prefix to use
78 * @param eventMetric the Event Metric with the data
79 * @return JSON object node for the Event Metric
80 */
81 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
82 String propertyPrefix, EventMetric eventMetric) {
83 String gaugeName = propertyPrefix + "Timestamp";
84 String meterName = propertyPrefix + "Rate";
85 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
86 Meter meter = eventMetric.eventRateMeter();
87
88 objectNode.put(gaugeName, json(mapper, gauge));
89 objectNode.put(meterName, json(mapper, meter));
90 return objectNode;
91 }
92
93 /**
94 * Produces JSON node for an Object.
95 *
96 * @param mapper the JSON object mapper to use
97 * @param object the Object with the data
98 * @return JSON node for the Object
99 */
100 private JsonNode json(ObjectMapper mapper, Object object) {
101 //
102 // NOTE: The API for custom serializers is incomplete,
103 // hence we have to parse the JSON string to create JsonNode.
104 //
105 try {
106 final String objectJson = mapper.writeValueAsString(object);
107 JsonNode jsonNode = mapper.readTree(objectJson);
108 return jsonNode;
109 } catch (JsonProcessingException e) {
110 log.error("Error writing value as JSON string", e);
111 } catch (IOException e) {
112 log.error("Error writing value as JSON string", e);
113 }
114 return null;
115 }
116
117 /**
118 * Prints an Event Metric.
119 *
120 * @param operationStr the string with the intent operation to print
121 * @param eventMetric the Event Metric to print
122 */
123 private void printEventMetric(String operationStr,
124 EventMetric eventMetric) {
125 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
126 Meter meter = eventMetric.eventRateMeter();
127 TimeUnit rateUnit = TimeUnit.SECONDS;
128 double rateFactor = rateUnit.toSeconds(1);
129
130 // Print the Gauge
131 print(FORMAT_GAUGE, operationStr, gauge.getValue());
132
133 // Print the Meter
134 print(FORMAT_METER, operationStr, meter.getCount(),
135 meter.getMeanRate() * rateFactor,
136 meter.getOneMinuteRate() * rateFactor,
137 meter.getFiveMinuteRate() * rateFactor,
138 meter.getFifteenMinuteRate() * rateFactor);
139 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700140}