blob: 52b07e390c1dcd0f37ccae098f3bb417fc2450d6 [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
17package org.onlab.onos.cli;
18
19import static java.lang.String.format;
20
21import java.util.Comparator;
22import java.util.Map;
23import java.util.Map.Entry;
24
25import org.apache.karaf.shell.commands.Command;
26import org.joda.time.LocalDateTime;
27import org.onlab.metrics.MetricsService;
28
29import com.codahale.metrics.Counter;
30import com.codahale.metrics.Gauge;
31import com.codahale.metrics.Histogram;
32import com.codahale.metrics.Meter;
33import com.codahale.metrics.Metric;
34import com.codahale.metrics.MetricFilter;
35import com.codahale.metrics.Snapshot;
36import com.codahale.metrics.Timer;
37import com.google.common.base.Strings;
38import com.google.common.collect.Ordering;
39import com.google.common.collect.TreeMultimap;
40
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
48 @Override
49 protected void execute() {
50 MetricsService metricsService = get(MetricsService.class);
51
52 // TODO support filter condition
53 MetricFilter filter = MetricFilter.ALL;
54
55 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
56 matched.asMap().forEach((name, metrics) -> {
57 for (Metric metric : metrics) {
58 // TODO JSON version
59 printMetric(name, metric);
60 }
61 });
62 }
63
64 /**
65 * Print metric object.
66 *
67 * @param name metric name
68 * @param metric metric object
69 */
70 private void printMetric(String name, Metric metric) {
71 final String heading;
72
73 if (metric instanceof Counter) {
74 heading = format("-- %s : [%s] --", name, "Counter");
75 print(heading);
76 Counter counter = (Counter) metric;
77 print(" count = %d", counter.getCount());
78
79 } else if (metric instanceof Gauge) {
80 heading = format("-- %s : [%s] --", name, "Gauge");
81 print(heading);
82 @SuppressWarnings("rawtypes")
83 Gauge gauge = (Gauge) metric;
84 final Object value = gauge.getValue();
85 if (name.endsWith("EpochMs") && value instanceof Long) {
86 print(" value = %s (%s)", value, new LocalDateTime(value));
87 } else {
88 print(" value = %s", value);
89 }
90
91 } else if (metric instanceof Histogram) {
92 heading = format("-- %s : [%s] --", name, "Histogram");
93 print(heading);
94 final Histogram histogram = (Histogram) metric;
95 final Snapshot snapshot = histogram.getSnapshot();
96 print(" count = %d", histogram.getCount());
97 print(" min = %d", snapshot.getMin());
98 print(" max = %d", snapshot.getMax());
99 print(" mean = %f", snapshot.getMean());
100 print(" stddev = %f", snapshot.getStdDev());
101
102 } else if (metric instanceof Meter) {
103 heading = format("-- %s : [%s] --", name, "Meter");
104 print(heading);
105 final Meter meter = (Meter) metric;
106 print(" count = %d", meter.getCount());
107 print(" mean rate = %f", meter.getMeanRate());
108 print(" 1-minute rate = %f", meter.getOneMinuteRate());
109 print(" 5-minute rate = %f", meter.getFiveMinuteRate());
110 print(" 15-minute rate = %f", meter.getFifteenMinuteRate());
111
112 } else if (metric instanceof Timer) {
113 heading = format("-- %s : [%s] --", name, "Timer");
114 print(heading);
115 final Timer timer = (Timer) metric;
116 final Snapshot snapshot = timer.getSnapshot();
117 print(" count = %d", timer.getCount());
118 print(" mean rate = %f", timer.getMeanRate());
119 print(" 1-minute rate = %f", timer.getOneMinuteRate());
120 print(" 5-minute rate = %f", timer.getFiveMinuteRate());
121 print(" 15-minute rate = %f", timer.getFifteenMinuteRate());
122 print(" min = %f ms", nanoToMs(snapshot.getMin()));
123 print(" max = %f ms", nanoToMs(snapshot.getMax()));
124 print(" mean = %f ms", nanoToMs(snapshot.getMean()));
125 print(" stddev = %f ms", nanoToMs(snapshot.getStdDev()));
126 } else {
127 heading = format("-- %s : [%s] --", name, metric.getClass().getCanonicalName());
128 print(heading);
129 print("Unknown Metric type:{}", metric.getClass().getCanonicalName());
130 }
131 print(Strings.repeat("-", heading.length()));
132 }
133
134 @SuppressWarnings("rawtypes")
135 private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
136 TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
137
138 Map<String, Counter> counters = metricsService.getCounters(filter);
139 for (Entry<String, Counter> entry : counters.entrySet()) {
140 metrics.put(entry.getKey(), entry.getValue());
141 }
142 Map<String, Gauge> gauges = metricsService.getGauges(filter);
143 for (Entry<String, Gauge> entry : gauges.entrySet()) {
144 metrics.put(entry.getKey(), entry.getValue());
145 }
146 Map<String, Histogram> histograms = metricsService.getHistograms(filter);
147 for (Entry<String, Histogram> entry : histograms.entrySet()) {
148 metrics.put(entry.getKey(), entry.getValue());
149 }
150 Map<String, Meter> meters = metricsService.getMeters(filter);
151 for (Entry<String, Meter> entry : meters.entrySet()) {
152 metrics.put(entry.getKey(), entry.getValue());
153 }
154 Map<String, Timer> timers = metricsService.getTimers(filter);
155 for (Entry<String, Timer> entry : timers.entrySet()) {
156 metrics.put(entry.getKey(), entry.getValue());
157 }
158
159 return metrics;
160 }
161
162 private double nanoToMs(double nano) {
163 return nano / 1_000_000D;
164 }
165}