1 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from osmclient
.common
.exceptions
import ClientException
18 from osmclient
.cli_commands
import utils
19 from prettytable
import PrettyTable
24 logger
= logging
.getLogger("osmclient")
27 @click.command(name
="ns-alarm-create")
28 @click.argument("name")
29 @click.option("--ns", prompt
=True, help="NS instance id or name")
31 "--vnf", prompt
=True, help="VNF name (VNF member index as declared in the NSD)"
33 @click.option("--vdu", prompt
=True, help="VDU name (VDU name as declared in the VNFD)")
34 @click.option("--metric", prompt
=True, help="Name of the metric (e.g. cpu_utilization)")
38 help="severity of the alarm (WARNING, MINOR, MAJOR, CRITICAL, INDETERMINATE)",
43 help="threshold value that, when crossed, an alarm is triggered",
46 "--threshold_operator",
48 help="threshold operator describing the comparison (GE, LE, GT, LT, EQ)",
53 help="statistic (AVERAGE, MINIMUM, MAXIMUM, COUNT, SUM)",
68 """creates a new alarm for a NS instance"""
69 # TODO: Check how to validate threshold_value.
70 # Should it be an integer (1-100), percentage, or decimal (0.01-1.00)?
72 ns_instance
= ctx
.obj
.ns
.get(ns
)
74 alarm
["alarm_name"] = name
75 alarm
["ns_id"] = ns_instance
["_id"]
76 alarm
["correlation_id"] = ns_instance
["_id"]
77 alarm
["vnf_member_index"] = vnf
78 alarm
["vdu_name"] = vdu
79 alarm
["metric_name"] = metric
80 alarm
["severity"] = severity
81 alarm
["threshold_value"] = int(threshold_value
)
82 alarm
["operation"] = threshold_operator
83 alarm
["statistic"] = statistic
84 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
85 ctx
.obj
.ns
.create_alarm(alarm
)
88 @click.command(name
="alarm-show", short_help
="show alarm details")
89 @click.argument("uuid")
91 def alarm_show(ctx
, uuid
):
92 """Show alarm's detail information"""
94 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
95 resp
= ctx
.obj
.ns
.get_alarm(uuid
=uuid
)
109 table
= PrettyTable(["key", "attribute"])
111 # Arrange and return the response data
112 alarm
= resp
.replace("ObjectId", "")
113 for key
in alarm_filter
:
115 value
= alarm
.get(key
)
118 value
= alarm
.get(key
)
121 value
= alarm
["tags"].get("ns_id")
122 elif key
== "vdu_name":
123 value
= alarm
["tags"].get("vdu_name")
124 elif key
== "status":
125 value
= alarm
["alarm_status"]
129 [key
, utils
.wrap_text(text
=json
.dumps(value
, indent
=2), width
=100)]
135 # TODO: check the reason for the try-except
139 @click.command(name
="alarm-list", short_help
="list all alarms")
141 "--ns_id", default
=None, required
=False, help="List out alarm for given ns id"
144 def alarm_list(ctx
, ns_id
):
145 """list all alarms"""
147 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
148 project_name
= os
.getenv("OSM_PROJECT", "admin")
149 # TODO: check the reason for this^
150 resp
= ctx
.obj
.ns
.get_alarm(project_name
=project_name
, ns_id
=ns_id
)
153 ["alarm-id", "metric", "threshold", "operation", "action", "status"]
156 # return the response data in a table
157 resp
= resp
.replace("ObjectId", "")
161 utils
.wrap_text(text
=str(alarm
["uuid"]), width
=38),
165 utils
.wrap_text(text
=alarm
["action"], width
=25),
166 alarm
["alarm_status"],
174 @click.command(name
="alarm-update", short_help
="Update a alarm")
175 @click.argument("uuid")
176 @click.option("--threshold", default
=None, help="Alarm threshold")
177 @click.option("--is_enable", default
=None, type=bool, help="enable or disable alarm")
179 def alarm_update(ctx
, uuid
, threshold
, is_enable
):
184 if not threshold
and is_enable
is None:
185 raise ClientException(
186 "Please provide option to update i.e threshold or is_enable"
188 ctx
.obj
.ns
.update_alarm(uuid
, threshold
, is_enable
)