Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.metrics.topology.cli; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.concurrent.TimeUnit; |
| 20 | |
| 21 | import com.codahale.metrics.Gauge; |
| 22 | import com.codahale.metrics.Meter; |
| 23 | import com.codahale.metrics.json.MetricsModule; |
| 24 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 25 | import com.fasterxml.jackson.databind.JsonNode; |
| 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 29 | import org.onlab.metrics.EventMetric; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cli.AbstractShellCommand; |
| 31 | import org.onosproject.metrics.topology.TopologyMetricsService; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 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") |
| 38 | public class TopologyEventsMetricsCommand extends AbstractShellCommand { |
| 39 | |
| 40 | private static final String FORMAT_GAUGE = |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 41 | "Topology %s Event Timestamp (ms from epoch)=%d"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 42 | private static final String FORMAT_METER = |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 43 | "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | TopologyMetricsService service = get(TopologyMetricsService.class); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 48 | |
| 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 Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 55 | 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 Radoslavov | 6aaa1e0 | 2015-03-18 11:12:06 -0700 | [diff] [blame] | 63 | result = json(mapper, result, "topologyGraphReasonsEvent", |
| 64 | service.topologyGraphReasonsEventMetric()); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 65 | print("%s", result); |
| 66 | } else { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 67 | printEventMetric("Device", service.topologyDeviceEventMetric()); |
| 68 | printEventMetric("Host", service.topologyHostEventMetric()); |
| 69 | printEventMetric("Link", service.topologyLinkEventMetric()); |
| 70 | printEventMetric("Graph", service.topologyGraphEventMetric()); |
Pavlin Radoslavov | 6aaa1e0 | 2015-03-18 11:12:06 -0700 | [diff] [blame] | 71 | printEventMetric("Graph Reasons", |
| 72 | service.topologyGraphReasonsEventMetric()); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Produces JSON node for an Event Metric. |
| 78 | * |
| 79 | * @param mapper the JSON object mapper to use |
| 80 | * @param objectNode the JSON object node to use |
| 81 | * @param propertyPrefix the property prefix to use |
| 82 | * @param eventMetric the Event Metric with the data |
| 83 | * @return JSON object node for the Event Metric |
| 84 | */ |
| 85 | private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode, |
| 86 | String propertyPrefix, EventMetric eventMetric) { |
| 87 | String gaugeName = propertyPrefix + "Timestamp"; |
| 88 | String meterName = propertyPrefix + "Rate"; |
| 89 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 90 | Meter meter = eventMetric.eventRateMeter(); |
| 91 | |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 92 | objectNode.set(gaugeName, json(mapper, gauge)); |
| 93 | objectNode.set(meterName, json(mapper, meter)); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 94 | return objectNode; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Produces JSON node for an Object. |
| 99 | * |
| 100 | * @param mapper the JSON object mapper to use |
| 101 | * @param object the Object with the data |
| 102 | * @return JSON node for the Object |
| 103 | */ |
| 104 | private JsonNode json(ObjectMapper mapper, Object object) { |
| 105 | // |
| 106 | // NOTE: The API for custom serializers is incomplete, |
| 107 | // hence we have to parse the JSON string to create JsonNode. |
| 108 | // |
| 109 | try { |
| 110 | final String objectJson = mapper.writeValueAsString(object); |
| 111 | JsonNode jsonNode = mapper.readTree(objectJson); |
| 112 | return jsonNode; |
| 113 | } catch (JsonProcessingException e) { |
| 114 | log.error("Error writing value as JSON string", e); |
| 115 | } catch (IOException e) { |
| 116 | log.error("Error writing value as JSON string", e); |
| 117 | } |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Prints an Event Metric. |
| 123 | * |
| 124 | * @param operationStr the string with the intent operation to print |
| 125 | * @param eventMetric the Event Metric to print |
| 126 | */ |
| 127 | private void printEventMetric(String operationStr, |
| 128 | EventMetric eventMetric) { |
| 129 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 130 | Meter meter = eventMetric.eventRateMeter(); |
| 131 | TimeUnit rateUnit = TimeUnit.SECONDS; |
| 132 | double rateFactor = rateUnit.toSeconds(1); |
| 133 | |
| 134 | // Print the Gauge |
| 135 | print(FORMAT_GAUGE, operationStr, gauge.getValue()); |
| 136 | |
| 137 | // Print the Meter |
| 138 | print(FORMAT_METER, operationStr, meter.getCount(), |
| 139 | meter.getMeanRate() * rateFactor, |
| 140 | meter.getOneMinuteRate() * rateFactor, |
| 141 | meter.getFiveMinuteRate() * rateFactor, |
| 142 | meter.getFifteenMinuteRate() * rateFactor); |
| 143 | } |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 144 | } |