blob: ad642c900549538670f1d6a19f9564fa45c80326 [file] [log] [blame]
Yuta HIGUCHI6a462832014-11-23 23:56:03 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 */
16
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.cli;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080018
19import static java.lang.String.format;
20
21import java.util.Comparator;
22import java.util.Map;
23import java.util.Map.Entry;
24
Flavio Castro4b519412015-07-24 12:57:59 -070025import org.apache.karaf.shell.commands.Argument;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080026import org.apache.karaf.shell.commands.Command;
27import org.joda.time.LocalDateTime;
28import org.onlab.metrics.MetricsService;
29
30import com.codahale.metrics.Counter;
31import com.codahale.metrics.Gauge;
32import com.codahale.metrics.Histogram;
33import com.codahale.metrics.Meter;
34import com.codahale.metrics.Metric;
35import com.codahale.metrics.MetricFilter;
36import com.codahale.metrics.Snapshot;
37import com.codahale.metrics.Timer;
38import com.google.common.base.Strings;
39import com.google.common.collect.Ordering;
40import com.google.common.collect.TreeMultimap;
41
42/**
43 * Prints metrics in the system.
44 */
45@Command(scope = "onos", name = "metrics",
46 description = "Prints metrics in the system")
47public class MetricsListCommand extends AbstractShellCommand {
48
Flavio Castro4b519412015-07-24 12:57:59 -070049 @Argument(index = 0, name = "metricName", description = "Name of Metric",
50 required = false, multiValued = false)
51 String metricName = null;
52
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080053 @Override
54 protected void execute() {
55 MetricsService metricsService = get(MetricsService.class);
56
Flavio Castro4b519412015-07-24 12:57:59 -070057 MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080058
59 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
60 matched.asMap().forEach((name, metrics) -> {
61 for (Metric metric : metrics) {
Yuta HIGUCHI6a462832014-11-23 23:56:03 -080062 printMetric(name, metric);
63 }
64 });
65 }
66
67 /**
68 * Print metric object.
69 *
70 * @param name metric name
71 * @param metric metric object
72 */
73 private void printMetric(String name, Metric metric) {
74 final String heading;
75
76 if (metric instanceof Counter) {
77 heading = format("-- %s : [%s] --", name, "Counter");
78 print(heading);
79 Counter counter = (Counter) metric;
80 print(" count = %d", counter.getCount());
81
82 } else if (metric instanceof Gauge) {
83 heading = format("-- %s : [%s] --", name, "Gauge");
84 print(heading);
85 @SuppressWarnings("rawtypes")
86 Gauge gauge = (Gauge) metric;
87 final Object value = gauge.getValue();
88 if (name.endsWith("EpochMs") && value instanceof Long) {
89 print(" value = %s (%s)", value, new LocalDateTime(value));
90 } else {
91 print(" value = %s", value);
92 }
93
94 } else if (metric instanceof Histogram) {
95 heading = format("-- %s : [%s] --", name, "Histogram");
96 print(heading);
97 final Histogram histogram = (Histogram) metric;
98 final Snapshot snapshot = histogram.getSnapshot();
99 print(" count = %d", histogram.getCount());
100 print(" min = %d", snapshot.getMin());
101 print(" max = %d", snapshot.getMax());
102 print(" mean = %f", snapshot.getMean());
103 print(" stddev = %f", snapshot.getStdDev());
104
105 } else if (metric instanceof Meter) {
106 heading = format("-- %s : [%s] --", name, "Meter");
107 print(heading);
108 final Meter meter = (Meter) metric;
109 print(" count = %d", meter.getCount());
110 print(" mean rate = %f", meter.getMeanRate());
111 print(" 1-minute rate = %f", meter.getOneMinuteRate());
112 print(" 5-minute rate = %f", meter.getFiveMinuteRate());
113 print(" 15-minute rate = %f", meter.getFifteenMinuteRate());
114
115 } else if (metric instanceof Timer) {
116 heading = format("-- %s : [%s] --", name, "Timer");
117 print(heading);
118 final Timer timer = (Timer) metric;
119 final Snapshot snapshot = timer.getSnapshot();
120 print(" count = %d", timer.getCount());
121 print(" mean rate = %f", timer.getMeanRate());
122 print(" 1-minute rate = %f", timer.getOneMinuteRate());
123 print(" 5-minute rate = %f", timer.getFiveMinuteRate());
124 print(" 15-minute rate = %f", timer.getFifteenMinuteRate());
125 print(" min = %f ms", nanoToMs(snapshot.getMin()));
126 print(" max = %f ms", nanoToMs(snapshot.getMax()));
127 print(" mean = %f ms", nanoToMs(snapshot.getMean()));
128 print(" stddev = %f ms", nanoToMs(snapshot.getStdDev()));
129 } else {
130 heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName());
131 print(heading);
132 print("Unknown Metric type:{}", metric.getClass().getCanonicalName());
133 }
134 print(Strings.repeat("-", heading.length()));
135 }
136
137 @SuppressWarnings("rawtypes")
138 private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
139 TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
140
141 Map<String, Counter> counters = metricsService.getCounters(filter);
142 for (Entry<String, Counter> entry : counters.entrySet()) {
143 metrics.put(entry.getKey(), entry.getValue());
144 }
145 Map<String, Gauge> gauges = metricsService.getGauges(filter);
146 for (Entry<String, Gauge> entry : gauges.entrySet()) {
147 metrics.put(entry.getKey(), entry.getValue());
148 }
149 Map<String, Histogram> histograms = metricsService.getHistograms(filter);
150 for (Entry<String, Histogram> entry : histograms.entrySet()) {
151 metrics.put(entry.getKey(), entry.getValue());
152 }
153 Map<String, Meter> meters = metricsService.getMeters(filter);
154 for (Entry<String, Meter> entry : meters.entrySet()) {
155 metrics.put(entry.getKey(), entry.getValue());
156 }
157 Map<String, Timer> timers = metricsService.getTimers(filter);
158 for (Entry<String, Timer> entry : timers.entrySet()) {
159 metrics.put(entry.getKey(), entry.getValue());
160 }
161
162 return metrics;
163 }
164
165 private double nanoToMs(double nano) {
166 return nano / 1_000_000D;
167 }
168}