blob: aa7f0365fc8ede31722db4f9f8809602db8ba1cd [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070019package org.onlab.onos.metrics.intent.cli;
20
21import java.io.IOException;
22import java.util.concurrent.TimeUnit;
23
24import com.codahale.metrics.Gauge;
25import com.codahale.metrics.Meter;
26import com.codahale.metrics.json.MetricsModule;
27import com.fasterxml.jackson.core.JsonProcessingException;
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070032import org.onlab.metrics.EventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070033import org.onlab.onos.cli.AbstractShellCommand;
34import org.onlab.onos.metrics.intent.IntentMetricsService;
35
36/**
37 * Command to show the intent events metrics.
38 */
39@Command(scope = "onos", name = "intents-events-metrics",
40 description = "Lists intent events metrics")
41public class IntentEventsMetricsCommand extends AbstractShellCommand {
42
43 private static final String FORMAT_GAUGE =
44 "Intent %s Event Timestamp (ms from epoch)=%d";
45 private static final String FORMAT_METER =
46 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
47
48 @Override
49 protected void execute() {
50 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070051
52 if (outputJson()) {
53 ObjectMapper mapper = new ObjectMapper()
54 .registerModule(new MetricsModule(TimeUnit.SECONDS,
55 TimeUnit.MILLISECONDS,
56 false));
57 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070058 result = json(mapper, result, "intentSubmitted",
59 service.intentSubmittedEventMetric());
60 result = json(mapper, result, "intentInstalled",
61 service.intentInstalledEventMetric());
62 result = json(mapper, result, "intentWithdrawRequested",
63 service.intentWithdrawRequestedEventMetric());
64 result = json(mapper, result, "intentWithdrawn",
65 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070066 print("%s", result);
67 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070068 printEventMetric("Submitted",
69 service.intentSubmittedEventMetric());
70 printEventMetric("Installed",
71 service.intentInstalledEventMetric());
72 printEventMetric("Withdraw Requested",
73 service.intentWithdrawRequestedEventMetric());
74 printEventMetric("Withdrawn",
75 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070076 }
77 }
78
79 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070080 * Produces JSON node for an Event Metric.
81 *
82 * @param mapper the JSON object mapper to use
83 * @param objectNode the JSON object node to use
84 * @param propertyPrefix the property prefix to use
85 * @param eventMetric the Event Metric with the data
86 * @return JSON object node for the Event Metric
87 */
88 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
89 String propertyPrefix, EventMetric eventMetric) {
90 String gaugeName = propertyPrefix + "Timestamp";
91 String meterName = propertyPrefix + "Rate";
92 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
93 Meter meter = eventMetric.eventRateMeter();
94
95 objectNode.put(gaugeName, json(mapper, gauge));
96 objectNode.put(meterName, json(mapper, meter));
97 return objectNode;
98 }
99
100 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700101 * Produces JSON node for an Object.
102 *
103 * @param mapper the JSON object mapper to use
104 * @param object the Object with the data
105 * @return JSON node for the Object
106 */
107 private JsonNode json(ObjectMapper mapper, Object object) {
108 //
109 // NOTE: The API for custom serializers is incomplete,
110 // hence we have to parse the JSON string to create JsonNode.
111 //
112 try {
113 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700114 JsonNode jsonNode = mapper.readTree(objectJson);
115 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700116 } catch (JsonProcessingException e) {
117 log.error("Error writing value as JSON string", e);
118 } catch (IOException e) {
119 log.error("Error writing value as JSON string", e);
120 }
121 return null;
122 }
123
124 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700125 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700126 *
127 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700128 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700129 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700130 private void printEventMetric(String operationStr,
131 EventMetric eventMetric) {
132 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
133 Meter meter = eventMetric.eventRateMeter();
134 TimeUnit rateUnit = TimeUnit.SECONDS;
135 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700136
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700137 // Print the Gauge
138 print(FORMAT_GAUGE, operationStr, gauge.getValue());
139
140 // Print the Meter
141 print(FORMAT_METER, operationStr, meter.getCount(),
142 meter.getMeanRate() * rateFactor,
143 meter.getOneMinuteRate() * rateFactor,
144 meter.getFiveMinuteRate() * rateFactor,
145 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700146 }
147}