Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.cli; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 18 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 19 | import com.codahale.metrics.Counter; |
| 20 | import com.codahale.metrics.Gauge; |
| 21 | import com.codahale.metrics.Histogram; |
| 22 | import com.codahale.metrics.Meter; |
| 23 | import com.codahale.metrics.Metric; |
| 24 | import com.codahale.metrics.MetricFilter; |
| 25 | import com.codahale.metrics.Snapshot; |
| 26 | import com.codahale.metrics.Timer; |
| 27 | import com.google.common.base.Strings; |
| 28 | import com.google.common.collect.Ordering; |
| 29 | import com.google.common.collect.TreeMultimap; |
Flavio Castro | 6baf95d | 2015-08-11 14:48:49 -0700 | [diff] [blame] | 30 | import org.apache.karaf.shell.commands.Argument; |
| 31 | import org.apache.karaf.shell.commands.Command; |
| 32 | import org.joda.time.LocalDateTime; |
| 33 | import org.onlab.metrics.MetricsService; |
| 34 | |
| 35 | import java.util.Comparator; |
| 36 | import java.util.Map; |
| 37 | import java.util.Map.Entry; |
| 38 | |
| 39 | import static java.lang.String.format; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Prints metrics in the system. |
| 43 | */ |
| 44 | @Command(scope = "onos", name = "metrics", |
| 45 | description = "Prints metrics in the system") |
| 46 | public class MetricsListCommand extends AbstractShellCommand { |
| 47 | |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 48 | @Argument(index = 0, name = "metricName", description = "Name of Metric", |
| 49 | required = false, multiValued = false) |
| 50 | String metricName = null; |
| 51 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 52 | @Override |
| 53 | protected void execute() { |
| 54 | MetricsService metricsService = get(MetricsService.class); |
| 55 | |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 56 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 57 | |
| 58 | TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter); |
| 59 | matched.asMap().forEach((name, metrics) -> { |
| 60 | for (Metric metric : metrics) { |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 61 | 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 Castro | 6baf95d | 2015-08-11 14:48:49 -0700 | [diff] [blame] | 120 | 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 HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 124 | 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 | } |