blob: 2fb8c4f0363678027951a47a874e2974901a0630 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.intent.cli;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070017
18import java.io.IOException;
19import java.util.concurrent.TimeUnit;
20
21import com.codahale.metrics.Gauge;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.json.MetricsModule;
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070029import org.onlab.metrics.EventMetric;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.cli.AbstractShellCommand;
31import org.onosproject.metrics.intent.IntentMetricsService;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070032
33/**
34 * Command to show the intent events metrics.
35 */
36@Command(scope = "onos", name = "intents-events-metrics",
37 description = "Lists intent events metrics")
38public class IntentEventsMetricsCommand extends AbstractShellCommand {
39
40 private static final String FORMAT_GAUGE =
41 "Intent %s Event Timestamp (ms from epoch)=%d";
42 private static final String FORMAT_METER =
43 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
44
45 @Override
46 protected void execute() {
47 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070048
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 Radoslavovccc2e332014-10-23 13:46:28 -070055 result = json(mapper, result, "intentSubmitted",
56 service.intentSubmittedEventMetric());
57 result = json(mapper, result, "intentInstalled",
58 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080059 result = json(mapper, result, "intentFailed",
60 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070061 result = json(mapper, result, "intentWithdrawRequested",
62 service.intentWithdrawRequestedEventMetric());
63 result = json(mapper, result, "intentWithdrawn",
64 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070065 result = json(mapper, result, "intentPurged",
66 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070067 print("%s", result);
68 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070069 printEventMetric("Submitted",
70 service.intentSubmittedEventMetric());
71 printEventMetric("Installed",
72 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080073 printEventMetric("Failed",
74 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070075 printEventMetric("Withdraw Requested",
76 service.intentWithdrawRequestedEventMetric());
77 printEventMetric("Withdrawn",
78 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070079 printEventMetric("Purged",
80 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070081 }
82 }
83
84 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070085 * Produces JSON node for an Event Metric.
86 *
87 * @param mapper the JSON object mapper to use
88 * @param objectNode the JSON object node to use
89 * @param propertyPrefix the property prefix to use
90 * @param eventMetric the Event Metric with the data
91 * @return JSON object node for the Event Metric
92 */
93 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
94 String propertyPrefix, EventMetric eventMetric) {
95 String gaugeName = propertyPrefix + "Timestamp";
96 String meterName = propertyPrefix + "Rate";
97 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
98 Meter meter = eventMetric.eventRateMeter();
99
Ray Milkey9d810f62015-02-13 11:20:58 -0800100 objectNode.set(gaugeName, json(mapper, gauge));
101 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700102 return objectNode;
103 }
104
105 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700106 * Produces JSON node for an Object.
107 *
108 * @param mapper the JSON object mapper to use
109 * @param object the Object with the data
110 * @return JSON node for the Object
111 */
112 private JsonNode json(ObjectMapper mapper, Object object) {
113 //
114 // NOTE: The API for custom serializers is incomplete,
115 // hence we have to parse the JSON string to create JsonNode.
116 //
117 try {
118 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700119 JsonNode jsonNode = mapper.readTree(objectJson);
120 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700121 } catch (JsonProcessingException e) {
122 log.error("Error writing value as JSON string", e);
123 } catch (IOException e) {
124 log.error("Error writing value as JSON string", e);
125 }
126 return null;
127 }
128
129 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700130 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700131 *
132 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700133 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700134 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700135 private void printEventMetric(String operationStr,
136 EventMetric eventMetric) {
137 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
138 Meter meter = eventMetric.eventRateMeter();
139 TimeUnit rateUnit = TimeUnit.SECONDS;
140 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700141
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700142 // Print the Gauge
143 print(FORMAT_GAUGE, operationStr, gauge.getValue());
144
145 // Print the Meter
146 print(FORMAT_METER, operationStr, meter.getCount(),
147 meter.getMeanRate() * rateFactor,
148 meter.getOneMinuteRate() * rateFactor,
149 meter.getFiveMinuteRate() * rateFactor,
150 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700151 }
152}