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