blob: 6f8d013b86394ae74347ac1730057148758c65ca [file] [log] [blame]
Pavlin Radoslavov295b2962014-10-23 01:12:41 -07001package org.onlab.onos.metrics.intent.cli;
2
3import java.io.IOException;
4import java.util.concurrent.TimeUnit;
5
6import com.codahale.metrics.Gauge;
7import com.codahale.metrics.Meter;
8import com.codahale.metrics.json.MetricsModule;
9import com.fasterxml.jackson.core.JsonProcessingException;
10import com.fasterxml.jackson.databind.JsonNode;
11import com.fasterxml.jackson.databind.ObjectMapper;
12import com.fasterxml.jackson.databind.node.ObjectNode;
13import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070014import org.onlab.metrics.EventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070015import org.onlab.onos.cli.AbstractShellCommand;
16import org.onlab.onos.metrics.intent.IntentMetricsService;
17
18/**
19 * Command to show the intent events metrics.
20 */
21@Command(scope = "onos", name = "intents-events-metrics",
22 description = "Lists intent events metrics")
23public class IntentEventsMetricsCommand extends AbstractShellCommand {
24
25 private static final String FORMAT_GAUGE =
26 "Intent %s Event Timestamp (ms from epoch)=%d";
27 private static final String FORMAT_METER =
28 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
29
30 @Override
31 protected void execute() {
32 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070033
34 if (outputJson()) {
35 ObjectMapper mapper = new ObjectMapper()
36 .registerModule(new MetricsModule(TimeUnit.SECONDS,
37 TimeUnit.MILLISECONDS,
38 false));
39 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070040 result = json(mapper, result, "intentSubmitted",
41 service.intentSubmittedEventMetric());
42 result = json(mapper, result, "intentInstalled",
43 service.intentInstalledEventMetric());
44 result = json(mapper, result, "intentWithdrawRequested",
45 service.intentWithdrawRequestedEventMetric());
46 result = json(mapper, result, "intentWithdrawn",
47 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070048 print("%s", result);
49 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070050 printEventMetric("Submitted",
51 service.intentSubmittedEventMetric());
52 printEventMetric("Installed",
53 service.intentInstalledEventMetric());
54 printEventMetric("Withdraw Requested",
55 service.intentWithdrawRequestedEventMetric());
56 printEventMetric("Withdrawn",
57 service.intentWithdrawnEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070058 }
59 }
60
61 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070062 * Produces JSON node for an Event Metric.
63 *
64 * @param mapper the JSON object mapper to use
65 * @param objectNode the JSON object node to use
66 * @param propertyPrefix the property prefix to use
67 * @param eventMetric the Event Metric with the data
68 * @return JSON object node for the Event Metric
69 */
70 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
71 String propertyPrefix, EventMetric eventMetric) {
72 String gaugeName = propertyPrefix + "Timestamp";
73 String meterName = propertyPrefix + "Rate";
74 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
75 Meter meter = eventMetric.eventRateMeter();
76
77 objectNode.put(gaugeName, json(mapper, gauge));
78 objectNode.put(meterName, json(mapper, meter));
79 return objectNode;
80 }
81
82 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070083 * Produces JSON node for an Object.
84 *
85 * @param mapper the JSON object mapper to use
86 * @param object the Object with the data
87 * @return JSON node for the Object
88 */
89 private JsonNode json(ObjectMapper mapper, Object object) {
90 //
91 // NOTE: The API for custom serializers is incomplete,
92 // hence we have to parse the JSON string to create JsonNode.
93 //
94 try {
95 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070096 JsonNode jsonNode = mapper.readTree(objectJson);
97 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070098 } catch (JsonProcessingException e) {
99 log.error("Error writing value as JSON string", e);
100 } catch (IOException e) {
101 log.error("Error writing value as JSON string", e);
102 }
103 return null;
104 }
105
106 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700107 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700108 *
109 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700110 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700111 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700112 private void printEventMetric(String operationStr,
113 EventMetric eventMetric) {
114 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
115 Meter meter = eventMetric.eventRateMeter();
116 TimeUnit rateUnit = TimeUnit.SECONDS;
117 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700118
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700119 // Print the Gauge
120 print(FORMAT_GAUGE, operationStr, gauge.getValue());
121
122 // Print the Meter
123 print(FORMAT_METER, operationStr, meter.getCount(),
124 meter.getMeanRate() * rateFactor,
125 meter.getOneMinuteRate() * rateFactor,
126 meter.getFiveMinuteRate() * rateFactor,
127 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700128 }
129}