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 |
1 |
import click |
17 |
1 |
from osmclient.common.exceptions import ClientException |
18 |
1 |
from osmclient.cli_commands import utils |
19 |
1 |
from prettytable import PrettyTable |
20 |
1 |
import json |
21 |
1 |
import os |
22 |
1 |
import logging |
23 |
|
|
24 |
1 |
logger = logging.getLogger("osmclient") |
25 |
|
|
26 |
|
|
27 |
1 |
@click.command(name="ns-alarm-create") |
28 |
1 |
@click.argument("name") |
29 |
1 |
@click.option("--ns", prompt=True, help="NS instance id or name") |
30 |
1 |
@click.option( |
31 |
|
"--vnf", prompt=True, help="VNF name (VNF member index as declared in the NSD)" |
32 |
|
) |
33 |
1 |
@click.option("--vdu", prompt=True, help="VDU name (VDU name as declared in the VNFD)") |
34 |
1 |
@click.option("--metric", prompt=True, help="Name of the metric (e.g. cpu_utilization)") |
35 |
1 |
@click.option( |
36 |
|
"--severity", |
37 |
|
default="WARNING", |
38 |
|
help="severity of the alarm (WARNING, MINOR, MAJOR, CRITICAL, INDETERMINATE)", |
39 |
|
) |
40 |
1 |
@click.option( |
41 |
|
"--threshold_value", |
42 |
|
prompt=True, |
43 |
|
help="threshold value that, when crossed, an alarm is triggered", |
44 |
|
) |
45 |
1 |
@click.option( |
46 |
|
"--threshold_operator", |
47 |
|
prompt=True, |
48 |
|
help="threshold operator describing the comparison (GE, LE, GT, LT, EQ)", |
49 |
|
) |
50 |
1 |
@click.option( |
51 |
|
"--statistic", |
52 |
|
default="AVERAGE", |
53 |
|
help="statistic (AVERAGE, MINIMUM, MAXIMUM, COUNT, SUM)", |
54 |
|
) |
55 |
1 |
@click.pass_context |
56 |
1 |
def ns_alarm_create( |
57 |
|
ctx, |
58 |
|
name, |
59 |
|
ns, |
60 |
|
vnf, |
61 |
|
vdu, |
62 |
|
metric, |
63 |
|
severity, |
64 |
|
threshold_value, |
65 |
|
threshold_operator, |
66 |
|
statistic, |
67 |
|
): |
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)? |
71 |
0 |
logger.debug("") |
72 |
0 |
ns_instance = ctx.obj.ns.get(ns) |
73 |
0 |
alarm = {} |
74 |
0 |
alarm["alarm_name"] = name |
75 |
0 |
alarm["ns_id"] = ns_instance["_id"] |
76 |
0 |
alarm["correlation_id"] = ns_instance["_id"] |
77 |
0 |
alarm["vnf_member_index"] = vnf |
78 |
0 |
alarm["vdu_name"] = vdu |
79 |
0 |
alarm["metric_name"] = metric |
80 |
0 |
alarm["severity"] = severity |
81 |
0 |
alarm["threshold_value"] = int(threshold_value) |
82 |
0 |
alarm["operation"] = threshold_operator |
83 |
0 |
alarm["statistic"] = statistic |
84 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
85 |
0 |
ctx.obj.ns.create_alarm(alarm) |
86 |
|
|
87 |
|
|
88 |
1 |
@click.command(name="alarm-show", short_help="show alarm details") |
89 |
1 |
@click.argument("uuid") |
90 |
1 |
@click.pass_context |
91 |
1 |
def alarm_show(ctx, uuid): |
92 |
|
"""Show alarm's detail information""" |
93 |
|
|
94 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
95 |
0 |
resp = ctx.obj.ns.get_alarm(uuid=uuid) |
96 |
0 |
alarm_filter = [ |
97 |
|
"uuid", |
98 |
|
"name", |
99 |
|
"metric", |
100 |
|
"statistic", |
101 |
|
"threshold", |
102 |
|
"operation", |
103 |
|
"ns-id", |
104 |
|
"vnf-id", |
105 |
|
"vdu_name", |
106 |
|
"action", |
107 |
|
"status", |
108 |
|
] |
109 |
0 |
table = PrettyTable(["key", "attribute"]) |
110 |
0 |
try: |
111 |
|
# Arrange and return the response data |
112 |
0 |
alarm = resp.replace("ObjectId", "") |
113 |
0 |
for key in alarm_filter: |
114 |
0 |
if key == "uuid": |
115 |
0 |
value = alarm.get(key) |
116 |
0 |
key = "alarm-id" |
117 |
0 |
elif key == "name": |
118 |
0 |
value = alarm.get(key) |
119 |
0 |
key = "alarm-name" |
120 |
0 |
elif key == "ns-id": |
121 |
0 |
value = alarm["tags"].get("ns_id") |
122 |
0 |
elif key == "vdu_name": |
123 |
0 |
value = alarm["tags"].get("vdu_name") |
124 |
0 |
elif key == "status": |
125 |
0 |
value = alarm["alarm_status"] |
126 |
|
else: |
127 |
0 |
value = alarm[key] |
128 |
0 |
table.add_row( |
129 |
|
[key, utils.wrap_text(text=json.dumps(value, indent=2), width=100)] |
130 |
|
) |
131 |
0 |
table.align = "l" |
132 |
0 |
print(table) |
133 |
0 |
except Exception: |
134 |
0 |
print(resp) |
135 |
|
# TODO: check the reason for the try-except |
136 |
|
|
137 |
|
|
138 |
|
# List alarm |
139 |
1 |
@click.command(name="alarm-list", short_help="list all alarms") |
140 |
1 |
@click.option( |
141 |
|
"--ns_id", default=None, required=False, help="List out alarm for given ns id" |
142 |
|
) |
143 |
1 |
@click.pass_context |
144 |
1 |
def alarm_list(ctx, ns_id): |
145 |
|
"""list all alarms""" |
146 |
|
|
147 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
148 |
0 |
project_name = os.getenv("OSM_PROJECT", "admin") |
149 |
|
# TODO: check the reason for this^ |
150 |
0 |
resp = ctx.obj.ns.get_alarm(project_name=project_name, ns_id=ns_id) |
151 |
|
|
152 |
0 |
table = PrettyTable( |
153 |
|
["alarm-id", "metric", "threshold", "operation", "action", "status"] |
154 |
|
) |
155 |
0 |
if resp: |
156 |
|
# return the response data in a table |
157 |
0 |
resp = resp.replace("ObjectId", "") |
158 |
0 |
for alarm in resp: |
159 |
0 |
table.add_row( |
160 |
|
[ |
161 |
|
utils.wrap_text(text=str(alarm["uuid"]), width=38), |
162 |
|
alarm["metric"], |
163 |
|
alarm["threshold"], |
164 |
|
alarm["operation"], |
165 |
|
utils.wrap_text(text=alarm["action"], width=25), |
166 |
|
alarm["alarm_status"], |
167 |
|
] |
168 |
|
) |
169 |
0 |
table.align = "l" |
170 |
0 |
print(table) |
171 |
|
|
172 |
|
|
173 |
|
# Update alarm |
174 |
1 |
@click.command(name="alarm-update", short_help="Update a alarm") |
175 |
1 |
@click.argument("uuid") |
176 |
1 |
@click.option("--threshold", default=None, help="Alarm threshold") |
177 |
1 |
@click.option("--is_enable", default=None, type=bool, help="enable or disable alarm") |
178 |
1 |
@click.pass_context |
179 |
1 |
def alarm_update(ctx, uuid, threshold, is_enable): |
180 |
|
""" |
181 |
|
Update alarm |
182 |
|
|
183 |
|
""" |
184 |
0 |
if not threshold and is_enable is None: |
185 |
0 |
raise ClientException( |
186 |
|
"Please provide option to update i.e threshold or is_enable" |
187 |
|
) |
188 |
0 |
ctx.obj.ns.update_alarm(uuid, threshold, is_enable) |