Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / metrics.py
1 # Copyright ETSI Contributors and Others.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import click
17 from osmclient.cli_commands import utils
18 import time
19 import logging
20
21 logger = logging.getLogger("osmclient")
22
23
24 @click.command(
25 name="ns-metric-export",
26 short_help="exports a metric to the internal OSM bus, which can be read by other apps",
27 )
28 @click.option("--ns", prompt=True, help="NS instance id or name")
29 @click.option(
30 "--vnf", prompt=True, help="VNF name (VNF member index as declared in the NSD)"
31 )
32 @click.option("--vdu", prompt=True, help="VDU name (VDU name as declared in the VNFD)")
33 @click.option("--metric", prompt=True, help="name of the metric (e.g. cpu_utilization)")
34 # @click.option('--period', default='1w',
35 # help='metric collection period (e.g. 20s, 30m, 2h, 3d, 1w)')
36 @click.option(
37 "--interval", help="periodic interval (seconds) to export metrics continuously"
38 )
39 @click.pass_context
40 def ns_metric_export(ctx, ns, vnf, vdu, metric, interval):
41 """exports a metric to the internal OSM bus, which can be read by other apps"""
42 # TODO: Check how to validate interval.
43 # Should it be an integer (seconds), or should a suffix (s,m,h,d,w) also be permitted?
44 logger.debug("")
45 ns_instance = ctx.obj.ns.get(ns)
46 metric_data = {}
47 metric_data["ns_id"] = ns_instance["_id"]
48 metric_data["correlation_id"] = ns_instance["_id"]
49 metric_data["vnf_member_index"] = vnf
50 metric_data["vdu_name"] = vdu
51 metric_data["metric_name"] = metric
52 metric_data["collection_unit"] = "WEEK"
53 metric_data["collection_period"] = 1
54 utils.check_client_version(ctx.obj, ctx.command.name)
55 if not interval:
56 print("{}".format(ctx.obj.ns.export_metric(metric_data)))
57 else:
58 i = 1
59 while True:
60 print("{} {}".format(ctx.obj.ns.export_metric(metric_data), i))
61 time.sleep(int(interval))
62 i += 1