| 1 |
# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP |
| 2 |
# Copyright 2017 FUJITSU LIMITED |
| 3 |
# |
| 4 |
# 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 |
| 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, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 |
# implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
import datetime |
| 18 |
import numbers |
| 19 |
import time |
| 20 |
|
| 21 |
from keystoneauth1 import exceptions as k_exc |
| 22 |
from osc_lib import exceptions as osc_exc |
| 23 |
|
| 24 |
from monascaclient.common import utils |
| 25 |
|
| 26 |
from oslo_serialization import jsonutils |
| 27 |
|
| 28 |
# Alarm valid types |
| 29 |
severity_types = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'] |
| 30 |
state_types = ['UNDETERMINED', 'ALARM', 'OK'] |
| 31 |
enabled_types = ['True', 'true', 'False', 'false'] |
| 32 |
group_by_types = ['alarm_definition_id', 'name', 'state', 'severity', |
| 33 |
'link', 'lifecycle_state', 'metric_name', |
| 34 |
'dimension_name', 'dimension_value'] |
| 35 |
allowed_notification_sort_by = {'id', 'name', 'type', 'address', 'created_at', 'updated_at'} |
| 36 |
allowed_alarm_sort_by = {'alarm_id', 'alarm_definition_id', |
| 37 |
'alarm_definition_name', 'state', 'severity', |
| 38 |
'lifecycle_state', 'link', |
| 39 |
'state_updated_timestamp', 'updated_timestamp', |
| 40 |
'created_timestamp'} |
| 41 |
allowed_definition_sort_by = {'id', 'name', 'severity', 'updated_at', 'created_at'} |
| 42 |
|
| 43 |
|
| 44 |
@utils.arg('name', metavar='<METRIC_NAME>', |
| 45 |
help='Name of the metric to create.') |
| 46 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 47 |
help='key value pair used to create a metric dimension. ' |
| 48 |
'This can be specified multiple times, or once with parameters ' |
| 49 |
'separated by a comma. ' |
| 50 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 51 |
'that confuse the CLI parser.', |
| 52 |
action='append') |
| 53 |
@utils.arg('--value-meta', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 54 |
help='key value pair for extra information about a value. ' |
| 55 |
'This can be specified multiple times, or once with parameters ' |
| 56 |
'separated by a comma. ' |
| 57 |
'value_meta need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 58 |
'that confuse the CLI parser.', |
| 59 |
action='append') |
| 60 |
@utils.arg('--time', metavar='<UNIX_TIMESTAMP>', |
| 61 |
default=time.time() * 1000, type=int, |
| 62 |
help='Metric timestamp in milliseconds. Default: current timestamp.') |
| 63 |
@utils.arg('--project-id', metavar='<CROSS_PROJECT_ID>', |
| 64 |
help='The Project ID to create metric on behalf of. ' |
| 65 |
'Requires monitoring-delegate role in keystone.') |
| 66 |
@utils.arg('value', metavar='<METRIC_VALUE>', |
| 67 |
type=float, |
| 68 |
help='Metric value.') |
| 69 |
def do_metric_create(mc, args): |
| 70 |
'''Create metric.''' |
| 71 |
fields = {} |
| 72 |
fields['name'] = args.name |
| 73 |
if args.dimensions: |
| 74 |
fields['dimensions'] = utils.format_parameters(args.dimensions) |
| 75 |
fields['timestamp'] = args.time |
| 76 |
fields['value'] = args.value |
| 77 |
if args.value_meta: |
| 78 |
fields['value_meta'] = utils.format_parameters(args.value_meta) |
| 79 |
if args.project_id: |
| 80 |
fields['tenant_id'] = args.project_id |
| 81 |
try: |
| 82 |
mc.metrics.create(**fields) |
| 83 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 84 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 85 |
else: |
| 86 |
print('Successfully created metric') |
| 87 |
|
| 88 |
|
| 89 |
@utils.arg('jsonbody', metavar='<JSON_BODY>', |
| 90 |
type=jsonutils.loads, |
| 91 |
help='The raw JSON body in single quotes. See api doc.') |
| 92 |
def do_metric_create_raw(mc, args): |
| 93 |
'''Create metric from raw json body.''' |
| 94 |
try: |
| 95 |
mc.metrics.create(**args.jsonbody) |
| 96 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 97 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 98 |
else: |
| 99 |
print('Successfully created metric') |
| 100 |
|
| 101 |
|
| 102 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 103 |
help='key value pair used to specify a metric dimension. ' |
| 104 |
'This can be specified multiple times, or once with parameters ' |
| 105 |
'separated by a comma. ' |
| 106 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 107 |
'that confuse the CLI parser.', |
| 108 |
action='append') |
| 109 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 110 |
help='The offset used to paginate the return data.') |
| 111 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 112 |
help='The amount of data to be returned up to the API maximum limit.') |
| 113 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 114 |
help="Retrieve data for the specified tenant/project id instead of " |
| 115 |
"the tenant/project from the user's Keystone credentials.") |
| 116 |
def do_metric_name_list(mc, args): |
| 117 |
'''List names of metrics.''' |
| 118 |
fields = {} |
| 119 |
if args.dimensions: |
| 120 |
fields['dimensions'] = utils.format_dimensions_query(args.dimensions) |
| 121 |
if args.limit: |
| 122 |
fields['limit'] = args.limit |
| 123 |
if args.offset: |
| 124 |
fields['offset'] = args.offset |
| 125 |
if args.tenant_id: |
| 126 |
fields['tenant_id'] = args.tenant_id |
| 127 |
|
| 128 |
try: |
| 129 |
metric_names = mc.metrics.list_names(**fields) |
| 130 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 131 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 132 |
else: |
| 133 |
if args.json: |
| 134 |
print(utils.json_formatter(metric_names)) |
| 135 |
return |
| 136 |
if isinstance(metric_names, list): |
| 137 |
utils.print_list(metric_names, ['Name'], formatters={'Name': lambda x: x['name']}) |
| 138 |
|
| 139 |
|
| 140 |
@utils.arg('--name', metavar='<METRIC_NAME>', |
| 141 |
help='Name of the metric to list.') |
| 142 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 143 |
help='key value pair used to specify a metric dimension. ' |
| 144 |
'This can be specified multiple times, or once with parameters ' |
| 145 |
'separated by a comma. ' |
| 146 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 147 |
'that confuse the CLI parser.', |
| 148 |
action='append') |
| 149 |
@utils.arg('--starttime', metavar='<UTC_START_TIME>', |
| 150 |
help='measurements >= UTC time. format: 2014-01-01T00:00:00Z. OR' |
| 151 |
' Format: -120 (previous 120 minutes).') |
| 152 |
@utils.arg('--endtime', metavar='<UTC_END_TIME>', |
| 153 |
help='measurements <= UTC time. format: 2014-01-01T00:00:00Z.') |
| 154 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 155 |
help='The offset used to paginate the return data.') |
| 156 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 157 |
help='The amount of data to be returned up to the API maximum limit.') |
| 158 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 159 |
help="Retrieve data for the specified tenant/project id instead of " |
| 160 |
"the tenant/project from the user's Keystone credentials.") |
| 161 |
def do_metric_list(mc, args): |
| 162 |
'''List metrics for this tenant.''' |
| 163 |
fields = {} |
| 164 |
if args.name: |
| 165 |
fields['name'] = args.name |
| 166 |
if args.dimensions: |
| 167 |
fields['dimensions'] = utils.format_dimensions_query(args.dimensions) |
| 168 |
if args.limit: |
| 169 |
fields['limit'] = args.limit |
| 170 |
if args.offset: |
| 171 |
fields['offset'] = args.offset |
| 172 |
if args.starttime: |
| 173 |
_translate_starttime(args) |
| 174 |
fields['start_time'] = args.starttime |
| 175 |
if args.endtime: |
| 176 |
fields['end_time'] = args.endtime |
| 177 |
if args.tenant_id: |
| 178 |
fields['tenant_id'] = args.tenant_id |
| 179 |
|
| 180 |
try: |
| 181 |
metric = mc.metrics.list(**fields) |
| 182 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 183 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 184 |
else: |
| 185 |
if args.json: |
| 186 |
print(utils.json_formatter(metric)) |
| 187 |
return |
| 188 |
cols = ['name', 'dimensions'] |
| 189 |
formatters = { |
| 190 |
'name': lambda x: x['name'], |
| 191 |
'dimensions': lambda x: utils.format_dict(x['dimensions']), |
| 192 |
} |
| 193 |
if isinstance(metric, list): |
| 194 |
# print the list |
| 195 |
utils.print_list(metric, cols, formatters=formatters) |
| 196 |
else: |
| 197 |
# add the dictionary to a list, so print_list works |
| 198 |
metric_list = list() |
| 199 |
metric_list.append(metric) |
| 200 |
utils.print_list( |
| 201 |
metric_list, |
| 202 |
cols, |
| 203 |
formatters=formatters) |
| 204 |
|
| 205 |
|
| 206 |
@utils.arg('--metric-name', metavar='<METRIC_NAME>', |
| 207 |
help='Name of the metric to report dimension name list.', |
| 208 |
action='append') |
| 209 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 210 |
help='The offset used to paginate the return data.') |
| 211 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 212 |
help='The amount of data to be returned up to the API maximum ' |
| 213 |
'limit.') |
| 214 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 215 |
help="Retrieve data for the specified tenant/project id instead of " |
| 216 |
"the tenant/project from the user's Keystone credentials.") |
| 217 |
def do_dimension_name_list(mc, args): |
| 218 |
'''List names of metric dimensions.''' |
| 219 |
fields = {} |
| 220 |
if args.metric_name: |
| 221 |
fields['metric_name'] = args.metric_name |
| 222 |
if args.limit: |
| 223 |
fields['limit'] = args.limit |
| 224 |
if args.offset: |
| 225 |
fields['offset'] = args.offset |
| 226 |
if args.tenant_id: |
| 227 |
fields['tenant_id'] = args.tenant_id |
| 228 |
|
| 229 |
try: |
| 230 |
dimension_names = mc.metrics.list_dimension_names(**fields) |
| 231 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 232 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 233 |
|
| 234 |
if args.json: |
| 235 |
print(utils.json_formatter(dimension_names)) |
| 236 |
return |
| 237 |
|
| 238 |
if isinstance(dimension_names, list): |
| 239 |
utils.print_list(dimension_names, ['Dimension Names'], formatters={ |
| 240 |
'Dimension Names': lambda x: x['dimension_name']}) |
| 241 |
|
| 242 |
|
| 243 |
@utils.arg('dimension_name', metavar='<DIMENSION_NAME>', |
| 244 |
help='Name of the dimension to list dimension values.') |
| 245 |
@utils.arg('--metric-name', metavar='<METRIC_NAME>', |
| 246 |
help='Name of the metric to report dimension value list.', |
| 247 |
action='append') |
| 248 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 249 |
help='The offset used to paginate the return data.') |
| 250 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 251 |
help='The amount of data to be returned up to the API maximum ' |
| 252 |
'limit.') |
| 253 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 254 |
help="Retrieve data for the specified tenant/project id instead of " |
| 255 |
"the tenant/project from the user's Keystone credentials.") |
| 256 |
def do_dimension_value_list(mc, args): |
| 257 |
'''List names of metric dimensions.''' |
| 258 |
fields = {} |
| 259 |
fields['dimension_name'] = args.dimension_name |
| 260 |
if args.metric_name: |
| 261 |
fields['metric_name'] = args.metric_name |
| 262 |
if args.limit: |
| 263 |
fields['limit'] = args.limit |
| 264 |
if args.offset: |
| 265 |
fields['offset'] = args.offset |
| 266 |
if args.tenant_id: |
| 267 |
fields['tenant_id'] = args.tenant_id |
| 268 |
|
| 269 |
try: |
| 270 |
dimension_values = mc.metrics.list_dimension_values(**fields) |
| 271 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 272 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 273 |
|
| 274 |
if args.json: |
| 275 |
print(utils.json_formatter(dimension_values)) |
| 276 |
return |
| 277 |
|
| 278 |
if isinstance(dimension_values, list): |
| 279 |
utils.print_list(dimension_values, ['Dimension Values'], formatters={ |
| 280 |
'Dimension Values': lambda x: x['dimension_value']}) |
| 281 |
|
| 282 |
|
| 283 |
def format_measure_timestamp(measurements): |
| 284 |
# returns newline separated times for the timestamp column |
| 285 |
return '\n'.join([str(m[0]) for m in measurements]) |
| 286 |
|
| 287 |
|
| 288 |
def format_measure_value(measurements): |
| 289 |
# returns newline separated values for the value column |
| 290 |
return '\n'.join(['{:12.3f}'.format(m[1]) for m in measurements]) |
| 291 |
|
| 292 |
|
| 293 |
def format_value_meta(measurements): |
| 294 |
# returns newline separated values for the value column |
| 295 |
measure_string_list = list() |
| 296 |
for measure in measurements: |
| 297 |
if len(measure) < 3: |
| 298 |
measure_string = "" |
| 299 |
else: |
| 300 |
meta_string_list = [] |
| 301 |
for k, v in measure[2].items(): |
| 302 |
if isinstance(v, numbers.Number): |
| 303 |
m_str = k + ': ' + str(v) |
| 304 |
else: |
| 305 |
m_str = k + ': ' + v |
| 306 |
meta_string_list.append(m_str) |
| 307 |
measure_string = ','.join(meta_string_list) |
| 308 |
measure_string_list.append(measure_string) |
| 309 |
return '\n'.join(measure_string_list) |
| 310 |
|
| 311 |
|
| 312 |
def format_statistic_timestamp(statistics, columns, name): |
| 313 |
# returns newline separated times for the timestamp column |
| 314 |
time_index = 0 |
| 315 |
if statistics: |
| 316 |
time_index = columns.index(name) |
| 317 |
time_list = list() |
| 318 |
for timestamp in statistics: |
| 319 |
time_list.append(str(timestamp[time_index])) |
| 320 |
return '\n'.join(time_list) |
| 321 |
|
| 322 |
|
| 323 |
def format_statistic_value(statistics, columns, stat_type): |
| 324 |
# find the index for column name |
| 325 |
stat_index = 0 |
| 326 |
if statistics: |
| 327 |
stat_index = columns.index(stat_type) |
| 328 |
value_list = list() |
| 329 |
for stat in statistics: |
| 330 |
value_str = '{:12.3f}'.format(stat[stat_index]) |
| 331 |
value_list.append(value_str) |
| 332 |
return '\n'.join(value_list) |
| 333 |
|
| 334 |
|
| 335 |
def format_metric_name(metrics): |
| 336 |
# returns newline separated metric names for the column |
| 337 |
metric_string_list = list() |
| 338 |
for metric in metrics: |
| 339 |
metric_name = metric['name'] |
| 340 |
metric_dimensions = metric['dimensions'] |
| 341 |
metric_string_list.append(metric_name) |
| 342 |
# need to line up with dimensions column |
| 343 |
rng = len(metric_dimensions) |
| 344 |
for i in range(rng): |
| 345 |
if i == rng - 1: |
| 346 |
# last one |
| 347 |
break |
| 348 |
metric_string_list.append(" ") |
| 349 |
return '\n'.join(metric_string_list) |
| 350 |
|
| 351 |
|
| 352 |
def format_metric_dimensions(metrics): |
| 353 |
# returns newline separated dimension key values for the column |
| 354 |
metric_string_list = list() |
| 355 |
for metric in metrics: |
| 356 |
metric_dimensions = metric['dimensions'] |
| 357 |
for k, v in metric_dimensions.items(): |
| 358 |
if isinstance(v, numbers.Number): |
| 359 |
d_str = k + ': ' + str(v) |
| 360 |
else: |
| 361 |
d_str = k + ': ' + v |
| 362 |
metric_string_list.append(d_str) |
| 363 |
return '\n'.join(metric_string_list) |
| 364 |
|
| 365 |
|
| 366 |
@utils.arg('name', metavar='<METRIC_NAME>', |
| 367 |
help='Name of the metric to list measurements.') |
| 368 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 369 |
help='key value pair used to specify a metric dimension. ' |
| 370 |
'This can be specified multiple times, or once with parameters ' |
| 371 |
'separated by a comma. ' |
| 372 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 373 |
'that confuse the CLI parser.', |
| 374 |
action='append') |
| 375 |
@utils.arg('starttime', metavar='<UTC_START_TIME>', |
| 376 |
help='measurements >= UTC time. format: 2014-01-01T00:00:00Z.' |
| 377 |
' OR Format: -120 (previous 120 minutes).') |
| 378 |
@utils.arg('--endtime', metavar='<UTC_END_TIME>', |
| 379 |
help='measurements <= UTC time. format: 2014-01-01T00:00:00Z.') |
| 380 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 381 |
help='The offset used to paginate the return data.') |
| 382 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 383 |
help='The amount of data to be returned up to the API maximum limit.') |
| 384 |
@utils.arg('--merge_metrics', action='store_const', |
| 385 |
const=True, |
| 386 |
help='Merge multiple metrics into a single result.') |
| 387 |
@utils.arg('--group_by', metavar='<KEY1,KEY2,...>', |
| 388 |
help='Select which keys to use for grouping. A \'*\' groups by all keys.') |
| 389 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 390 |
help="Retrieve data for the specified tenant/project id instead of " |
| 391 |
"the tenant/project from the user's Keystone credentials.") |
| 392 |
def do_measurement_list(mc, args): |
| 393 |
'''List measurements for the specified metric.''' |
| 394 |
fields = {} |
| 395 |
fields['name'] = args.name |
| 396 |
|
| 397 |
if args.dimensions: |
| 398 |
fields['dimensions'] = utils.format_dimensions_query(args.dimensions) |
| 399 |
_translate_starttime(args) |
| 400 |
fields['start_time'] = args.starttime |
| 401 |
if args.endtime: |
| 402 |
fields['end_time'] = args.endtime |
| 403 |
if args.limit: |
| 404 |
fields['limit'] = args.limit |
| 405 |
if args.offset: |
| 406 |
fields['offset'] = args.offset |
| 407 |
if args.merge_metrics: |
| 408 |
fields['merge_metrics'] = args.merge_metrics |
| 409 |
if args.group_by: |
| 410 |
fields['group_by'] = args.group_by |
| 411 |
if args.tenant_id: |
| 412 |
fields['tenant_id'] = args.tenant_id |
| 413 |
|
| 414 |
try: |
| 415 |
metric = mc.metrics.list_measurements(**fields) |
| 416 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 417 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 418 |
else: |
| 419 |
if args.json: |
| 420 |
print(utils.json_formatter(metric)) |
| 421 |
return |
| 422 |
cols = ['name', 'dimensions', 'timestamp', 'value', 'value_meta'] |
| 423 |
formatters = { |
| 424 |
'name': lambda x: x['name'], |
| 425 |
'dimensions': lambda x: utils.format_dict(x['dimensions']), |
| 426 |
'timestamp': lambda x: format_measure_timestamp(x['measurements']), |
| 427 |
'value': lambda x: format_measure_value(x['measurements']), |
| 428 |
'value_meta': lambda x: format_value_meta(x['measurements']), |
| 429 |
} |
| 430 |
if isinstance(metric, list): |
| 431 |
# print the list |
| 432 |
utils.print_list(metric, cols, formatters=formatters) |
| 433 |
else: |
| 434 |
# add the dictionary to a list, so print_list works |
| 435 |
metric_list = list() |
| 436 |
metric_list.append(metric) |
| 437 |
utils.print_list( |
| 438 |
metric_list, |
| 439 |
cols, |
| 440 |
formatters=formatters) |
| 441 |
|
| 442 |
|
| 443 |
@utils.arg('name', metavar='<METRIC_NAME>', |
| 444 |
help='Name of the metric to report measurement statistics.') |
| 445 |
@utils.arg('statistics', metavar='<STATISTICS>', |
| 446 |
help='Statistics is one or more (separated by commas) of ' |
| 447 |
'[AVG, MIN, MAX, COUNT, SUM].') |
| 448 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 449 |
help='key value pair used to specify a metric dimension. ' |
| 450 |
'This can be specified multiple times, or once with parameters ' |
| 451 |
'separated by a comma. ' |
| 452 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 453 |
'that confuse the CLI parser.', |
| 454 |
action='append') |
| 455 |
@utils.arg('starttime', metavar='<UTC_START_TIME>', |
| 456 |
help='measurements >= UTC time. format: 2014-01-01T00:00:00Z. OR' |
| 457 |
' Format: -120 (previous 120 minutes).') |
| 458 |
@utils.arg('--endtime', metavar='<UTC_END_TIME>', |
| 459 |
help='measurements <= UTC time. format: 2014-01-01T00:00:00Z.') |
| 460 |
@utils.arg('--period', metavar='<PERIOD>', |
| 461 |
help='number of seconds per interval (default is 300)') |
| 462 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 463 |
help='The offset used to paginate the return data.') |
| 464 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 465 |
help='The amount of data to be returned up to the API maximum limit.') |
| 466 |
@utils.arg('--merge_metrics', action='store_const', |
| 467 |
const=True, |
| 468 |
help='Merge multiple metrics into a single result.') |
| 469 |
@utils.arg('--group_by', metavar='<KEY1,KEY2,...>', |
| 470 |
help='Select which keys to use for grouping. A \'*\' groups by all keys.') |
| 471 |
@utils.arg('--tenant-id', metavar='<TENANT_ID>', |
| 472 |
help="Retrieve data for the specified tenant/project id instead of " |
| 473 |
"the tenant/project from the user's Keystone credentials.") |
| 474 |
def do_metric_statistics(mc, args): |
| 475 |
'''List measurement statistics for the specified metric.''' |
| 476 |
statistic_types = ['AVG', 'MIN', 'MAX', 'COUNT', 'SUM'] |
| 477 |
statlist = args.statistics.split(',') |
| 478 |
for stat in statlist: |
| 479 |
if stat.upper() not in statistic_types: |
| 480 |
errmsg = ('Invalid type, not one of [' + |
| 481 |
', '.join(statistic_types) + ']') |
| 482 |
raise osc_exc.CommandError(errmsg) |
| 483 |
|
| 484 |
fields = {} |
| 485 |
fields['name'] = args.name |
| 486 |
if args.dimensions: |
| 487 |
fields['dimensions'] = utils.format_dimensions_query(args.dimensions) |
| 488 |
_translate_starttime(args) |
| 489 |
fields['start_time'] = args.starttime |
| 490 |
if args.endtime: |
| 491 |
fields['end_time'] = args.endtime |
| 492 |
if args.period: |
| 493 |
fields['period'] = args.period |
| 494 |
fields['statistics'] = args.statistics |
| 495 |
if args.limit: |
| 496 |
fields['limit'] = args.limit |
| 497 |
if args.offset: |
| 498 |
fields['offset'] = args.offset |
| 499 |
if args.merge_metrics: |
| 500 |
fields['merge_metrics'] = args.merge_metrics |
| 501 |
if args.group_by: |
| 502 |
fields['group_by'] = args.group_by |
| 503 |
if args.tenant_id: |
| 504 |
fields['tenant_id'] = args.tenant_id |
| 505 |
|
| 506 |
try: |
| 507 |
metric = mc.metrics.list_statistics(**fields) |
| 508 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 509 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 510 |
else: |
| 511 |
if args.json: |
| 512 |
print(utils.json_formatter(metric)) |
| 513 |
return |
| 514 |
cols = ['name', 'dimensions'] |
| 515 |
# add dynamic column names |
| 516 |
if metric: |
| 517 |
column_names = metric[0]['columns'] |
| 518 |
for name in column_names: |
| 519 |
cols.append(name) |
| 520 |
else: |
| 521 |
# when empty set, print_list needs a col |
| 522 |
cols.append('timestamp') |
| 523 |
|
| 524 |
formatters = { |
| 525 |
'name': lambda x: x['name'], |
| 526 |
'dimensions': lambda x: utils.format_dict(x['dimensions']), |
| 527 |
'timestamp': lambda x: |
| 528 |
format_statistic_timestamp(x['statistics'], x['columns'], |
| 529 |
'timestamp'), |
| 530 |
'avg': lambda x: |
| 531 |
format_statistic_value(x['statistics'], x['columns'], 'avg'), |
| 532 |
'min': lambda x: |
| 533 |
format_statistic_value(x['statistics'], x['columns'], 'min'), |
| 534 |
'max': lambda x: |
| 535 |
format_statistic_value(x['statistics'], x['columns'], 'max'), |
| 536 |
'count': lambda x: |
| 537 |
format_statistic_value(x['statistics'], x['columns'], 'count'), |
| 538 |
'sum': lambda x: |
| 539 |
format_statistic_value(x['statistics'], x['columns'], 'sum'), |
| 540 |
} |
| 541 |
if isinstance(metric, list): |
| 542 |
# print the list |
| 543 |
utils.print_list(metric, cols, formatters=formatters) |
| 544 |
else: |
| 545 |
# add the dictionary to a list, so print_list works |
| 546 |
metric_list = list() |
| 547 |
metric_list.append(metric) |
| 548 |
utils.print_list( |
| 549 |
metric_list, |
| 550 |
cols, |
| 551 |
formatters=formatters) |
| 552 |
|
| 553 |
|
| 554 |
@utils.arg('name', metavar='<NOTIFICATION_NAME>', |
| 555 |
help='Name of the notification to create.') |
| 556 |
@utils.arg('type', metavar='<TYPE>', |
| 557 |
help='The notification type. See monasca notification-type-list for supported types.') |
| 558 |
@utils.arg('address', metavar='<ADDRESS>', |
| 559 |
help='A valid EMAIL Address, URL, or SERVICE KEY.') |
| 560 |
@utils.arg('--period', metavar='<PERIOD>', type=int, default=0, |
| 561 |
help='A period for the notification method.') |
| 562 |
def do_notification_create(mc, args): |
| 563 |
'''Create notification.''' |
| 564 |
|
| 565 |
fields = {} |
| 566 |
fields['name'] = args.name |
| 567 |
fields['type'] = args.type |
| 568 |
fields['address'] = args.address |
| 569 |
if args.period: |
| 570 |
fields['period'] = args.period |
| 571 |
try: |
| 572 |
notification = mc.notifications.create(**fields) |
| 573 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 574 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 575 |
else: |
| 576 |
print(jsonutils.dumps(notification, indent=2)) |
| 577 |
|
| 578 |
|
| 579 |
@utils.arg('id', metavar='<NOTIFICATION_ID>', |
| 580 |
help='The ID of the notification.') |
| 581 |
def do_notification_show(mc, args): |
| 582 |
'''Describe the notification.''' |
| 583 |
fields = {} |
| 584 |
fields['notification_id'] = args.id |
| 585 |
try: |
| 586 |
notification = mc.notifications.get(**fields) |
| 587 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 588 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 589 |
else: |
| 590 |
if args.json: |
| 591 |
print(utils.json_formatter(notification)) |
| 592 |
return |
| 593 |
formatters = { |
| 594 |
'name': utils.json_formatter, |
| 595 |
'id': utils.json_formatter, |
| 596 |
'type': utils.json_formatter, |
| 597 |
'address': utils.json_formatter, |
| 598 |
'period': utils.json_formatter, |
| 599 |
'links': utils.format_dictlist, |
| 600 |
} |
| 601 |
utils.print_dict(notification, formatters=formatters) |
| 602 |
|
| 603 |
|
| 604 |
@utils.arg('--sort-by', metavar='<SORT BY FIELDS>', |
| 605 |
help='Fields to sort by as a comma separated list. Valid values are id, ' |
| 606 |
'name, type, address, created_at, updated_at. ' |
| 607 |
'Fields may be followed by "asc" or "desc", ex "address desc", ' |
| 608 |
'to set the direction of sorting.') |
| 609 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 610 |
help='The offset used to paginate the return data.') |
| 611 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 612 |
help='The amount of data to be returned up to the API maximum limit.') |
| 613 |
def do_notification_list(mc, args): |
| 614 |
'''List notifications for this tenant.''' |
| 615 |
fields = {} |
| 616 |
if args.limit: |
| 617 |
fields['limit'] = args.limit |
| 618 |
if args.offset: |
| 619 |
fields['offset'] = args.offset |
| 620 |
if args.sort_by: |
| 621 |
sort_by = args.sort_by.split(',') |
| 622 |
for field in sort_by: |
| 623 |
field_values = field.lower().split() |
| 624 |
if len(field_values) > 2: |
| 625 |
print("Invalid sort_by value {}".format(field)) |
| 626 |
if field_values[0] not in allowed_notification_sort_by: |
| 627 |
print("Sort-by field name {} is not in [{}]".format(field_values[0], |
| 628 |
allowed_notification_sort_by)) |
| 629 |
return |
| 630 |
if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']: |
| 631 |
print("Invalid value {}, must be asc or desc".format(field_values[1])) |
| 632 |
fields['sort_by'] = args.sort_by |
| 633 |
|
| 634 |
try: |
| 635 |
notification = mc.notifications.list(**fields) |
| 636 |
except osc_exc.ClientException as he: |
| 637 |
raise osc_exc.CommandError( |
| 638 |
'ClientException code=%s message=%s' % |
| 639 |
(he.code, he.message)) |
| 640 |
else: |
| 641 |
if args.json: |
| 642 |
print(utils.json_formatter(notification)) |
| 643 |
return |
| 644 |
cols = ['name', 'id', 'type', 'address', 'period'] |
| 645 |
formatters = { |
| 646 |
'name': lambda x: x['name'], |
| 647 |
'id': lambda x: x['id'], |
| 648 |
'type': lambda x: x['type'], |
| 649 |
'address': lambda x: x['address'], |
| 650 |
'period': lambda x: x['period'], |
| 651 |
} |
| 652 |
if isinstance(notification, list): |
| 653 |
|
| 654 |
utils.print_list( |
| 655 |
notification, |
| 656 |
cols, |
| 657 |
formatters=formatters) |
| 658 |
else: |
| 659 |
notif_list = list() |
| 660 |
notif_list.append(notification) |
| 661 |
utils.print_list(notif_list, cols, formatters=formatters) |
| 662 |
|
| 663 |
|
| 664 |
@utils.arg('id', metavar='<NOTIFICATION_ID>', |
| 665 |
help='The ID of the notification.') |
| 666 |
def do_notification_delete(mc, args): |
| 667 |
'''Delete notification.''' |
| 668 |
fields = {} |
| 669 |
fields['notification_id'] = args.id |
| 670 |
try: |
| 671 |
mc.notifications.delete(**fields) |
| 672 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 673 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 674 |
else: |
| 675 |
print('Successfully deleted notification') |
| 676 |
|
| 677 |
|
| 678 |
@utils.arg('id', metavar='<NOTIFICATION_ID>', |
| 679 |
help='The ID of the notification.') |
| 680 |
@utils.arg('name', metavar='<NOTIFICATION_NAME>', |
| 681 |
help='Name of the notification.') |
| 682 |
@utils.arg('type', metavar='<TYPE>', |
| 683 |
help='The notification type. See monasca notification-type-list for supported types.') |
| 684 |
@utils.arg('address', metavar='<ADDRESS>', |
| 685 |
help='A valid EMAIL Address, URL, or SERVICE KEY.') |
| 686 |
@utils.arg('period', metavar='<PERIOD>', type=int, |
| 687 |
help='A period for the notification method.') |
| 688 |
def do_notification_update(mc, args): |
| 689 |
'''Update notification.''' |
| 690 |
fields = {} |
| 691 |
fields['notification_id'] = args.id |
| 692 |
fields['name'] = args.name |
| 693 |
|
| 694 |
fields['type'] = args.type |
| 695 |
fields['address'] = args.address |
| 696 |
fields['period'] = args.period |
| 697 |
try: |
| 698 |
notification = mc.notifications.update(**fields) |
| 699 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 700 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 701 |
else: |
| 702 |
print(jsonutils.dumps(notification, indent=2)) |
| 703 |
|
| 704 |
|
| 705 |
@utils.arg('id', metavar='<NOTIFICATION_ID>', |
| 706 |
help='The ID of the notification.') |
| 707 |
@utils.arg('--name', metavar='<NOTIFICATION_NAME>', |
| 708 |
help='Name of the notification.') |
| 709 |
@utils.arg('--type', metavar='<TYPE>', |
| 710 |
help='The notification type. See monasca notification-type-list for supported types.') |
| 711 |
@utils.arg('--address', metavar='<ADDRESS>', |
| 712 |
help='A valid EMAIL Address, URL, or SERVICE KEY.') |
| 713 |
@utils.arg('--period', metavar='<PERIOD>', type=int, |
| 714 |
help='A period for the notification method.') |
| 715 |
def do_notification_patch(mc, args): |
| 716 |
'''Patch notification.''' |
| 717 |
fields = {} |
| 718 |
fields['notification_id'] = args.id |
| 719 |
if args.name: |
| 720 |
fields['name'] = args.name |
| 721 |
|
| 722 |
if args.type: |
| 723 |
fields['type'] = args.type |
| 724 |
if args.address: |
| 725 |
fields['address'] = args.address |
| 726 |
if args.period or args.period == 0: |
| 727 |
fields['period'] = args.period |
| 728 |
try: |
| 729 |
notification = mc.notifications.patch(**fields) |
| 730 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 731 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 732 |
else: |
| 733 |
print(jsonutils.dumps(notification, indent=2)) |
| 734 |
|
| 735 |
|
| 736 |
def _validate_severity(severity): |
| 737 |
if severity.upper() not in severity_types: |
| 738 |
errmsg = ('Invalid severity, not one of [' + |
| 739 |
', '.join(severity_types) + ']') |
| 740 |
print(errmsg) |
| 741 |
return False |
| 742 |
return True |
| 743 |
|
| 744 |
|
| 745 |
@utils.arg('name', metavar='<ALARM_DEFINITION_NAME>', |
| 746 |
help='Name of the alarm definition to create.') |
| 747 |
@utils.arg('--description', metavar='<DESCRIPTION>', |
| 748 |
help='Description of the alarm.') |
| 749 |
@utils.arg('expression', metavar='<EXPRESSION>', |
| 750 |
help='The alarm expression to evaluate. Quoted.') |
| 751 |
@utils.arg('--severity', metavar='<SEVERITY>', |
| 752 |
help='Severity is one of [LOW, MEDIUM, HIGH, CRITICAL].') |
| 753 |
@utils.arg('--match-by', metavar='<MATCH_BY_DIMENSION_KEY1,MATCH_BY_DIMENSION_KEY2,' |
| 754 |
'...>', |
| 755 |
help='The metric dimensions to use to create unique alarms. ' |
| 756 |
'One or more dimension key names separated by a comma. ' |
| 757 |
'Key names need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 758 |
'that confuse the CLI parser.') |
| 759 |
@utils.arg('--alarm-actions', metavar='<NOTIFICATION-ID>', |
| 760 |
help='The notification method to use when an alarm state is ALARM. ' |
| 761 |
'This param may be specified multiple times.', |
| 762 |
action='append') |
| 763 |
@utils.arg('--ok-actions', metavar='<NOTIFICATION-ID>', |
| 764 |
help='The notification method to use when an alarm state is OK. ' |
| 765 |
'This param may be specified multiple times.', |
| 766 |
action='append') |
| 767 |
@utils.arg('--undetermined-actions', metavar='<NOTIFICATION-ID>', |
| 768 |
help='The notification method to use when an alarm state is ' |
| 769 |
'UNDETERMINED. This param may be specified multiple times.', |
| 770 |
action='append') |
| 771 |
def do_alarm_definition_create(mc, args): |
| 772 |
'''Create an alarm definition.''' |
| 773 |
fields = {} |
| 774 |
fields['name'] = args.name |
| 775 |
if args.description: |
| 776 |
fields['description'] = args.description |
| 777 |
fields['expression'] = args.expression |
| 778 |
if args.alarm_actions: |
| 779 |
fields['alarm_actions'] = args.alarm_actions |
| 780 |
if args.ok_actions: |
| 781 |
fields['ok_actions'] = args.ok_actions |
| 782 |
if args.undetermined_actions: |
| 783 |
fields['undetermined_actions'] = args.undetermined_actions |
| 784 |
if args.severity: |
| 785 |
if not _validate_severity(args.severity): |
| 786 |
return |
| 787 |
fields['severity'] = args.severity |
| 788 |
if args.match_by: |
| 789 |
fields['match_by'] = args.match_by.split(',') |
| 790 |
try: |
| 791 |
alarm = mc.alarm_definitions.create(**fields) |
| 792 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 793 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 794 |
else: |
| 795 |
print(jsonutils.dumps(alarm, indent=2)) |
| 796 |
|
| 797 |
|
| 798 |
@utils.arg('id', metavar='<ALARM_DEFINITION_ID>', |
| 799 |
help='The ID of the alarm definition.') |
| 800 |
def do_alarm_definition_show(mc, args): |
| 801 |
'''Describe the alarm definition.''' |
| 802 |
fields = {} |
| 803 |
fields['alarm_id'] = args.id |
| 804 |
try: |
| 805 |
alarm = mc.alarm_definitions.get(**fields) |
| 806 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 807 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 808 |
else: |
| 809 |
if args.json: |
| 810 |
print(utils.json_formatter(alarm)) |
| 811 |
return |
| 812 |
# print out detail of a single alarm |
| 813 |
formatters = { |
| 814 |
'name': utils.json_formatter, |
| 815 |
'id': utils.json_formatter, |
| 816 |
'expression': utils.json_formatter, |
| 817 |
'expression_data': utils.format_expression_data, |
| 818 |
'match_by': utils.json_formatter, |
| 819 |
'actions_enabled': utils.json_formatter, |
| 820 |
'alarm_actions': utils.json_formatter, |
| 821 |
'ok_actions': utils.json_formatter, |
| 822 |
'severity': utils.json_formatter, |
| 823 |
'undetermined_actions': utils.json_formatter, |
| 824 |
'description': utils.json_formatter, |
| 825 |
'links': utils.format_dictlist, |
| 826 |
} |
| 827 |
utils.print_dict(alarm, formatters=formatters) |
| 828 |
|
| 829 |
|
| 830 |
@utils.arg('--name', metavar='<ALARM_DEFINITION_NAME>', |
| 831 |
help='Name of the alarm definition.') |
| 832 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 833 |
help='key value pair used to specify a metric dimension. ' |
| 834 |
'This can be specified multiple times, or once with parameters ' |
| 835 |
'separated by a comma. ' |
| 836 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 837 |
'that confuse the CLI parser.', |
| 838 |
action='append') |
| 839 |
@utils.arg('--severity', metavar='<SEVERITY>', |
| 840 |
help='Severity is one of ["LOW", "MEDIUM", "HIGH", "CRITICAL"].') |
| 841 |
@utils.arg('--sort-by', metavar='<SORT BY FIELDS>', |
| 842 |
help='Fields to sort by as a comma separated list. Valid values are id, ' |
| 843 |
'name, severity, created_at, updated_at. ' |
| 844 |
'Fields may be followed by "asc" or "desc", ex "severity desc", ' |
| 845 |
'to set the direction of sorting.') |
| 846 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 847 |
help='The offset used to paginate the return data.') |
| 848 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 849 |
help='The amount of data to be returned up to the API maximum limit.') |
| 850 |
def do_alarm_definition_list(mc, args): |
| 851 |
'''List alarm definitions for this tenant.''' |
| 852 |
fields = {} |
| 853 |
if args.name: |
| 854 |
fields['name'] = args.name |
| 855 |
if args.dimensions: |
| 856 |
fields['dimensions'] = utils.format_dimensions_query(args.dimensions) |
| 857 |
if args.severity: |
| 858 |
if not _validate_severity(args.severity): |
| 859 |
return |
| 860 |
fields['severity'] = args.severity |
| 861 |
if args.sort_by: |
| 862 |
sort_by = args.sort_by.split(',') |
| 863 |
for field in sort_by: |
| 864 |
field_values = field.split() |
| 865 |
if len(field_values) > 2: |
| 866 |
print("Invalid sort_by value {}".format(field)) |
| 867 |
if field_values[0] not in allowed_definition_sort_by: |
| 868 |
print("Sort-by field name {} is not in [{}]".format(field_values[0], |
| 869 |
allowed_definition_sort_by)) |
| 870 |
return |
| 871 |
if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']: |
| 872 |
print("Invalid value {}, must be asc or desc".format(field_values[1])) |
| 873 |
fields['sort_by'] = args.sort_by |
| 874 |
if args.limit: |
| 875 |
fields['limit'] = args.limit |
| 876 |
if args.offset: |
| 877 |
fields['offset'] = args.offset |
| 878 |
try: |
| 879 |
alarm = mc.alarm_definitions.list(**fields) |
| 880 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 881 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 882 |
else: |
| 883 |
if args.json: |
| 884 |
print(utils.json_formatter(alarm)) |
| 885 |
return |
| 886 |
cols = ['name', 'id', 'expression', 'match_by', 'actions_enabled'] |
| 887 |
formatters = { |
| 888 |
'name': lambda x: x['name'], |
| 889 |
'id': lambda x: x['id'], |
| 890 |
'expression': lambda x: x['expression'], |
| 891 |
'match_by': lambda x: utils.format_list(x['match_by']), |
| 892 |
'actions_enabled': lambda x: x['actions_enabled'], |
| 893 |
} |
| 894 |
if isinstance(alarm, list): |
| 895 |
# print the list |
| 896 |
utils.print_list(alarm, cols, formatters=formatters) |
| 897 |
else: |
| 898 |
# add the dictionary to a list, so print_list works |
| 899 |
alarm_list = list() |
| 900 |
alarm_list.append(alarm) |
| 901 |
utils.print_list(alarm_list, cols, formatters=formatters) |
| 902 |
|
| 903 |
|
| 904 |
@utils.arg('id', metavar='<ALARM_DEFINITION_ID>', |
| 905 |
help='The ID of the alarm definition.') |
| 906 |
def do_alarm_definition_delete(mc, args): |
| 907 |
'''Delete the alarm definition.''' |
| 908 |
fields = {} |
| 909 |
fields['alarm_id'] = args.id |
| 910 |
try: |
| 911 |
mc.alarm_definitions.delete(**fields) |
| 912 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 913 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 914 |
else: |
| 915 |
print('Successfully deleted alarm definition') |
| 916 |
|
| 917 |
|
| 918 |
@utils.arg('id', metavar='<ALARM_DEFINITION_ID>', |
| 919 |
help='The ID of the alarm definition.') |
| 920 |
@utils.arg('name', metavar='<ALARM_DEFINITION_NAME>', |
| 921 |
help='Name of the alarm definition.') |
| 922 |
@utils.arg('description', metavar='<DESCRIPTION>', |
| 923 |
help='Description of the alarm.') |
| 924 |
@utils.arg('expression', metavar='<EXPRESSION>', |
| 925 |
help='The alarm expression to evaluate. Quoted.') |
| 926 |
@utils.arg('alarm_actions', metavar='<ALARM-NOTIFICATION-ID1,ALARM-NOTIFICATION-ID2,...>', |
| 927 |
help='The notification method(s) to use when an alarm state is ALARM ' |
| 928 |
'as a comma separated list.') |
| 929 |
@utils.arg('ok_actions', metavar='<OK-NOTIFICATION-ID1,OK-NOTIFICATION-ID2,...>', |
| 930 |
help='The notification method(s) to use when an alarm state is OK ' |
| 931 |
'as a comma separated list.') |
| 932 |
@utils.arg('undetermined_actions', |
| 933 |
metavar='<UNDETERMINED-NOTIFICATION-ID1,UNDETERMINED-NOTIFICATION-ID2,...>', |
| 934 |
help='The notification method(s) to use when an alarm state is UNDETERMINED ' |
| 935 |
'as a comma separated list.') |
| 936 |
@utils.arg('actions_enabled', metavar='<ACTIONS-ENABLED>', |
| 937 |
help='The actions-enabled boolean is one of [true,false]') |
| 938 |
@utils.arg('match_by', metavar='<MATCH_BY_DIMENSION_KEY1,MATCH_BY_DIMENSION_KEY2,...>', |
| 939 |
help='The metric dimensions to use to create unique alarms. ' |
| 940 |
'One or more dimension key names separated by a comma. ' |
| 941 |
'Key names need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 942 |
'that confuse the CLI parser.') |
| 943 |
@utils.arg('severity', metavar='<SEVERITY>', |
| 944 |
help='Severity is one of [LOW, MEDIUM, HIGH, CRITICAL].') |
| 945 |
def do_alarm_definition_update(mc, args): |
| 946 |
'''Update the alarm definition.''' |
| 947 |
fields = {} |
| 948 |
fields['alarm_id'] = args.id |
| 949 |
fields['name'] = args.name |
| 950 |
fields['description'] = args.description |
| 951 |
fields['expression'] = args.expression |
| 952 |
fields['alarm_actions'] = _arg_split_patch_update(args.alarm_actions) |
| 953 |
fields['ok_actions'] = _arg_split_patch_update(args.ok_actions) |
| 954 |
fields['undetermined_actions'] = _arg_split_patch_update(args.undetermined_actions) |
| 955 |
if args.actions_enabled not in enabled_types: |
| 956 |
errmsg = ('Invalid value, not one of [' + |
| 957 |
', '.join(enabled_types) + ']') |
| 958 |
print(errmsg) |
| 959 |
return |
| 960 |
fields['actions_enabled'] = args.actions_enabled in ['true', 'True'] |
| 961 |
fields['match_by'] = _arg_split_patch_update(args.match_by) |
| 962 |
if not _validate_severity(args.severity): |
| 963 |
return |
| 964 |
fields['severity'] = args.severity |
| 965 |
try: |
| 966 |
alarm = mc.alarm_definitions.update(**fields) |
| 967 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 968 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 969 |
else: |
| 970 |
print(jsonutils.dumps(alarm, indent=2)) |
| 971 |
|
| 972 |
|
| 973 |
@utils.arg('id', metavar='<ALARM_DEFINITION_ID>', |
| 974 |
help='The ID of the alarm definition.') |
| 975 |
@utils.arg('--name', metavar='<ALARM_DEFINITION_NAME>', |
| 976 |
help='Name of the alarm definition.') |
| 977 |
@utils.arg('--description', metavar='<DESCRIPTION>', |
| 978 |
help='Description of the alarm.') |
| 979 |
@utils.arg('--expression', metavar='<EXPRESSION>', |
| 980 |
help='The alarm expression to evaluate. Quoted.') |
| 981 |
@utils.arg('--alarm-actions', metavar='<NOTIFICATION-ID>', |
| 982 |
help='The notification method to use when an alarm state is ALARM. ' |
| 983 |
'This param may be specified multiple times.', |
| 984 |
action='append') |
| 985 |
@utils.arg('--ok-actions', metavar='<NOTIFICATION-ID>', |
| 986 |
help='The notification method to use when an alarm state is OK. ' |
| 987 |
'This param may be specified multiple times.', |
| 988 |
action='append') |
| 989 |
@utils.arg('--undetermined-actions', metavar='<NOTIFICATION-ID>', |
| 990 |
help='The notification method to use when an alarm state is ' |
| 991 |
'UNDETERMINED. This param may be specified multiple times.', |
| 992 |
action='append') |
| 993 |
@utils.arg('--actions-enabled', metavar='<ACTIONS-ENABLED>', |
| 994 |
help='The actions-enabled boolean is one of [true,false].') |
| 995 |
@utils.arg('--severity', metavar='<SEVERITY>', |
| 996 |
help='Severity is one of [LOW, MEDIUM, HIGH, CRITICAL].') |
| 997 |
def do_alarm_definition_patch(mc, args): |
| 998 |
'''Patch the alarm definition.''' |
| 999 |
fields = {} |
| 1000 |
fields['alarm_id'] = args.id |
| 1001 |
if args.name: |
| 1002 |
fields['name'] = args.name |
| 1003 |
if args.description: |
| 1004 |
fields['description'] = args.description |
| 1005 |
if args.expression: |
| 1006 |
fields['expression'] = args.expression |
| 1007 |
if args.alarm_actions: |
| 1008 |
fields['alarm_actions'] = _arg_split_patch_update(args.alarm_actions, patch=True) |
| 1009 |
if args.ok_actions: |
| 1010 |
fields['ok_actions'] = _arg_split_patch_update(args.ok_actions, patch=True) |
| 1011 |
if args.undetermined_actions: |
| 1012 |
fields['undetermined_actions'] = _arg_split_patch_update(args.undetermined_actions, |
| 1013 |
patch=True) |
| 1014 |
if args.actions_enabled: |
| 1015 |
if args.actions_enabled not in enabled_types: |
| 1016 |
errmsg = ('Invalid value, not one of [' + |
| 1017 |
', '.join(enabled_types) + ']') |
| 1018 |
print(errmsg) |
| 1019 |
return |
| 1020 |
fields['actions_enabled'] = args.actions_enabled in ['true', 'True'] |
| 1021 |
if args.severity: |
| 1022 |
if not _validate_severity(args.severity): |
| 1023 |
return |
| 1024 |
fields['severity'] = args.severity |
| 1025 |
try: |
| 1026 |
alarm = mc.alarm_definitions.patch(**fields) |
| 1027 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1028 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1029 |
else: |
| 1030 |
print(jsonutils.dumps(alarm, indent=2)) |
| 1031 |
|
| 1032 |
|
| 1033 |
@utils.arg('--alarm-definition-id', metavar='<ALARM_DEFINITION_ID>', |
| 1034 |
help='The ID of the alarm definition.') |
| 1035 |
@utils.arg('--metric-name', metavar='<METRIC_NAME>', |
| 1036 |
help='Name of the metric.') |
| 1037 |
@utils.arg('--metric-dimensions', metavar='<KEY1=VALUE1,KEY2,KEY3=VALUE2...>', |
| 1038 |
help='key value pair used to specify a metric dimension or ' |
| 1039 |
'just key to select all values of that dimension.' |
| 1040 |
'This can be specified multiple times, or once with parameters ' |
| 1041 |
'separated by a comma. ' |
| 1042 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 1043 |
'that confuse the CLI parser.', |
| 1044 |
action='append') |
| 1045 |
@utils.arg('--state', metavar='<ALARM_STATE>', |
| 1046 |
help='ALARM_STATE is one of [UNDETERMINED, OK, ALARM].') |
| 1047 |
@utils.arg('--severity', metavar='<SEVERITY>', |
| 1048 |
help='Severity is one of ["LOW", "MEDIUM", "HIGH", "CRITICAL"].') |
| 1049 |
@utils.arg('--state-updated-start-time', metavar='<UTC_STATE_UPDATED_START>', |
| 1050 |
help='Return all alarms whose state was updated on or after the time specified.') |
| 1051 |
@utils.arg('--lifecycle-state', metavar='<LIFECYCLE_STATE>', |
| 1052 |
help='The lifecycle state of the alarm.') |
| 1053 |
@utils.arg('--link', metavar='<LINK>', |
| 1054 |
help='The link to external data associated with the alarm.') |
| 1055 |
@utils.arg('--sort-by', metavar='<SORT BY FIELDS>', |
| 1056 |
help='Fields to sort by as a comma separated list. Valid values are alarm_id, ' |
| 1057 |
'alarm_definition_id, state, severity, lifecycle_state, link, ' |
| 1058 |
'state_updated_timestamp, updated_timestamp, created_timestamp. ' |
| 1059 |
'Fields may be followed by "asc" or "desc", ex "severity desc", ' |
| 1060 |
'to set the direction of sorting.') |
| 1061 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 1062 |
help='The offset used to paginate the return data.') |
| 1063 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 1064 |
help='The amount of data to be returned up to the API maximum limit.') |
| 1065 |
def do_alarm_list(mc, args): |
| 1066 |
'''List alarms for this tenant.''' |
| 1067 |
fields = {} |
| 1068 |
if args.alarm_definition_id: |
| 1069 |
fields['alarm_definition_id'] = args.alarm_definition_id |
| 1070 |
if args.metric_name: |
| 1071 |
fields['metric_name'] = args.metric_name |
| 1072 |
if args.metric_dimensions: |
| 1073 |
fields['metric_dimensions'] = utils.format_dimensions_query(args.metric_dimensions) |
| 1074 |
if args.state: |
| 1075 |
if args.state.upper() not in state_types: |
| 1076 |
errmsg = ('Invalid state, not one of [' + |
| 1077 |
', '.join(state_types) + ']') |
| 1078 |
print(errmsg) |
| 1079 |
return |
| 1080 |
fields['state'] = args.state |
| 1081 |
if args.severity: |
| 1082 |
if not _validate_severity(args.severity): |
| 1083 |
return |
| 1084 |
fields['severity'] = args.severity |
| 1085 |
if args.state_updated_start_time: |
| 1086 |
fields['state_updated_start_time'] = args.state_updated_start_time |
| 1087 |
if args.lifecycle_state: |
| 1088 |
fields['lifecycle_state'] = args.lifecycle_state |
| 1089 |
if args.link: |
| 1090 |
fields['link'] = args.link |
| 1091 |
if args.limit: |
| 1092 |
fields['limit'] = args.limit |
| 1093 |
if args.offset: |
| 1094 |
fields['offset'] = args.offset |
| 1095 |
if args.sort_by: |
| 1096 |
sort_by = args.sort_by.split(',') |
| 1097 |
for field in sort_by: |
| 1098 |
field_values = field.lower().split() |
| 1099 |
if len(field_values) > 2: |
| 1100 |
print("Invalid sort_by value {}".format(field)) |
| 1101 |
if field_values[0] not in allowed_alarm_sort_by: |
| 1102 |
print("Sort-by field name {} is not in [{}]".format(field_values[0], |
| 1103 |
allowed_alarm_sort_by)) |
| 1104 |
return |
| 1105 |
if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']: |
| 1106 |
print("Invalid value {}, must be asc or desc".format(field_values[1])) |
| 1107 |
fields['sort_by'] = args.sort_by |
| 1108 |
try: |
| 1109 |
alarm = mc.alarms.list(**fields) |
| 1110 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1111 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1112 |
else: |
| 1113 |
if args.json: |
| 1114 |
print(utils.json_formatter(alarm)) |
| 1115 |
return |
| 1116 |
cols = ['id', 'alarm_definition_id', 'alarm_definition_name', 'metric_name', |
| 1117 |
'metric_dimensions', 'severity', 'state', 'lifecycle_state', 'link', |
| 1118 |
'state_updated_timestamp', 'updated_timestamp', "created_timestamp"] |
| 1119 |
formatters = { |
| 1120 |
'id': lambda x: x['id'], |
| 1121 |
'alarm_definition_id': lambda x: x['alarm_definition']['id'], |
| 1122 |
'alarm_definition_name': lambda x: x['alarm_definition']['name'], |
| 1123 |
'metric_name': lambda x: format_metric_name(x['metrics']), |
| 1124 |
'metric_dimensions': lambda x: format_metric_dimensions(x['metrics']), |
| 1125 |
'severity': lambda x: x['alarm_definition']['severity'], |
| 1126 |
'state': lambda x: x['state'], |
| 1127 |
'lifecycle_state': lambda x: x['lifecycle_state'], |
| 1128 |
'link': lambda x: x['link'], |
| 1129 |
'state_updated_timestamp': lambda x: x['state_updated_timestamp'], |
| 1130 |
'updated_timestamp': lambda x: x['updated_timestamp'], |
| 1131 |
'created_timestamp': lambda x: x['created_timestamp'], |
| 1132 |
} |
| 1133 |
if isinstance(alarm, list): |
| 1134 |
# print the list |
| 1135 |
utils.print_list(alarm, cols, formatters=formatters) |
| 1136 |
else: |
| 1137 |
# add the dictionary to a list, so print_list works |
| 1138 |
alarm_list = list() |
| 1139 |
alarm_list.append(alarm) |
| 1140 |
utils.print_list(alarm_list, cols, formatters=formatters) |
| 1141 |
|
| 1142 |
|
| 1143 |
@utils.arg('id', metavar='<ALARM_ID>', |
| 1144 |
help='The ID of the alarm.') |
| 1145 |
def do_alarm_show(mc, args): |
| 1146 |
'''Describe the alarm.''' |
| 1147 |
fields = {} |
| 1148 |
fields['alarm_id'] = args.id |
| 1149 |
try: |
| 1150 |
alarm = mc.alarms.get(**fields) |
| 1151 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1152 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1153 |
else: |
| 1154 |
if args.json: |
| 1155 |
print(utils.json_formatter(alarm)) |
| 1156 |
return |
| 1157 |
# print out detail of a single alarm |
| 1158 |
formatters = { |
| 1159 |
'id': utils.json_formatter, |
| 1160 |
'alarm_definition': utils.json_formatter, |
| 1161 |
'metrics': utils.json_formatter, |
| 1162 |
'state': utils.json_formatter, |
| 1163 |
'links': utils.format_dictlist, |
| 1164 |
} |
| 1165 |
utils.print_dict(alarm, formatters=formatters) |
| 1166 |
|
| 1167 |
|
| 1168 |
@utils.arg('id', metavar='<ALARM_ID>', |
| 1169 |
help='The ID of the alarm.') |
| 1170 |
@utils.arg('state', metavar='<ALARM_STATE>', |
| 1171 |
help='ALARM_STATE is one of [UNDETERMINED, OK, ALARM].') |
| 1172 |
@utils.arg('lifecycle_state', metavar='<LIFECYCLE_STATE>', |
| 1173 |
help='The lifecycle state of the alarm.') |
| 1174 |
@utils.arg('link', metavar='<LINK>', |
| 1175 |
help='A link to an external resource with information about the alarm.') |
| 1176 |
def do_alarm_update(mc, args): |
| 1177 |
'''Update the alarm state.''' |
| 1178 |
fields = {} |
| 1179 |
fields['alarm_id'] = args.id |
| 1180 |
if args.state.upper() not in state_types: |
| 1181 |
errmsg = ('Invalid state, not one of [' + |
| 1182 |
', '.join(state_types) + ']') |
| 1183 |
print(errmsg) |
| 1184 |
return |
| 1185 |
fields['state'] = args.state |
| 1186 |
fields['lifecycle_state'] = args.lifecycle_state |
| 1187 |
fields['link'] = args.link |
| 1188 |
try: |
| 1189 |
alarm = mc.alarms.update(**fields) |
| 1190 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1191 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1192 |
else: |
| 1193 |
print(jsonutils.dumps(alarm, indent=2)) |
| 1194 |
|
| 1195 |
|
| 1196 |
@utils.arg('id', metavar='<ALARM_ID>', |
| 1197 |
help='The ID of the alarm.') |
| 1198 |
@utils.arg('--state', metavar='<ALARM_STATE>', |
| 1199 |
help='ALARM_STATE is one of [UNDETERMINED, OK, ALARM].') |
| 1200 |
@utils.arg('--lifecycle-state', metavar='<LIFECYCLE_STATE>', |
| 1201 |
help='The lifecycle state of the alarm.') |
| 1202 |
@utils.arg('--link', metavar='<LINK>', |
| 1203 |
help='A link to an external resource with information about the alarm.') |
| 1204 |
def do_alarm_patch(mc, args): |
| 1205 |
'''Patch the alarm state.''' |
| 1206 |
fields = {} |
| 1207 |
fields['alarm_id'] = args.id |
| 1208 |
if args.state: |
| 1209 |
if args.state.upper() not in state_types: |
| 1210 |
errmsg = ('Invalid state, not one of [' + |
| 1211 |
', '.join(state_types) + ']') |
| 1212 |
print(errmsg) |
| 1213 |
return |
| 1214 |
fields['state'] = args.state |
| 1215 |
if args.lifecycle_state: |
| 1216 |
fields['lifecycle_state'] = args.lifecycle_state |
| 1217 |
if args.link: |
| 1218 |
fields['link'] = args.link |
| 1219 |
try: |
| 1220 |
alarm = mc.alarms.patch(**fields) |
| 1221 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1222 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1223 |
else: |
| 1224 |
print(jsonutils.dumps(alarm, indent=2)) |
| 1225 |
|
| 1226 |
|
| 1227 |
@utils.arg('id', metavar='<ALARM_ID>', |
| 1228 |
help='The ID of the alarm.') |
| 1229 |
def do_alarm_delete(mc, args): |
| 1230 |
'''Delete the alarm.''' |
| 1231 |
fields = {} |
| 1232 |
fields['alarm_id'] = args.id |
| 1233 |
try: |
| 1234 |
mc.alarms.delete(**fields) |
| 1235 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1236 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1237 |
else: |
| 1238 |
print('Successfully deleted alarm') |
| 1239 |
|
| 1240 |
|
| 1241 |
def output_alarm_history(args, alarm_history): |
| 1242 |
if args.json: |
| 1243 |
print(utils.json_formatter(alarm_history)) |
| 1244 |
return |
| 1245 |
# format output |
| 1246 |
cols = ['alarm_id', 'new_state', 'old_state', 'reason', |
| 1247 |
'reason_data', 'metric_name', 'metric_dimensions', 'timestamp'] |
| 1248 |
formatters = { |
| 1249 |
'alarm_id': lambda x: x['alarm_id'], |
| 1250 |
'new_state': lambda x: x['new_state'], |
| 1251 |
'old_state': lambda x: x['old_state'], |
| 1252 |
'reason': lambda x: x['reason'], |
| 1253 |
'reason_data': lambda x: x['reason_data'], |
| 1254 |
'metric_name': lambda x: format_metric_name(x['metrics']), |
| 1255 |
'metric_dimensions': lambda x: format_metric_dimensions(x['metrics']), |
| 1256 |
'timestamp': lambda x: x['timestamp'], |
| 1257 |
} |
| 1258 |
if isinstance(alarm_history, list): |
| 1259 |
# print the list |
| 1260 |
utils.print_list(alarm_history, cols, formatters=formatters) |
| 1261 |
else: |
| 1262 |
# add the dictionary to a list, so print_list works |
| 1263 |
alarm_list = list() |
| 1264 |
alarm_list.append(alarm_history) |
| 1265 |
utils.print_list(alarm_list, cols, formatters=formatters) |
| 1266 |
|
| 1267 |
|
| 1268 |
@utils.arg('--alarm-definition-id', metavar='<ALARM_DEFINITION_ID>', |
| 1269 |
help='The ID of the alarm definition.') |
| 1270 |
@utils.arg('--metric-name', metavar='<METRIC_NAME>', |
| 1271 |
help='Name of the metric.') |
| 1272 |
@utils.arg('--metric-dimensions', metavar='<KEY1=VALUE1,KEY2,KEY3=VALUE2...>', |
| 1273 |
help='key value pair used to specify a metric dimension or ' |
| 1274 |
'just key to select all values of that dimension.' |
| 1275 |
'This can be specified multiple times, or once with parameters ' |
| 1276 |
'separated by a comma. ' |
| 1277 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 1278 |
'that confuse the CLI parser.', |
| 1279 |
action='append') |
| 1280 |
@utils.arg('--state', metavar='<ALARM_STATE>', |
| 1281 |
help='ALARM_STATE is one of [UNDETERMINED, OK, ALARM].') |
| 1282 |
@utils.arg('--severity', metavar='<SEVERITY>', |
| 1283 |
help='Severity is one of ["LOW", "MEDIUM", "HIGH", "CRITICAL"].') |
| 1284 |
@utils.arg('--state-updated-start-time', metavar='<UTC_STATE_UPDATED_START>', |
| 1285 |
help='Return all alarms whose state was updated on or after the time specified.') |
| 1286 |
@utils.arg('--lifecycle-state', metavar='<LIFECYCLE_STATE>', |
| 1287 |
help='The lifecycle state of the alarm.') |
| 1288 |
@utils.arg('--link', metavar='<LINK>', |
| 1289 |
help='The link to external data associated with the alarm.') |
| 1290 |
@utils.arg('--group-by', metavar='<GROUP_BY>', |
| 1291 |
help='Comma separated list of one or more fields to group the results by. ' |
| 1292 |
'Group by is one or more of [alarm_definition_id, name, state, link, ' |
| 1293 |
'lifecycle_state, metric_name, dimension_name, dimension_value].') |
| 1294 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 1295 |
help='The offset used to paginate the return data.') |
| 1296 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 1297 |
help='The amount of data to be returned up to the API maximum limit.') |
| 1298 |
def do_alarm_count(mc, args): |
| 1299 |
'''Count alarms.''' |
| 1300 |
fields = {} |
| 1301 |
if args.alarm_definition_id: |
| 1302 |
fields['alarm_definition_id'] = args.alarm_definition_id |
| 1303 |
if args.metric_name: |
| 1304 |
fields['metric_name'] = args.metric_name |
| 1305 |
if args.metric_dimensions: |
| 1306 |
fields['metric_dimensions'] = utils.format_dimensions_query(args.metric_dimensions) |
| 1307 |
if args.state: |
| 1308 |
if args.state.upper() not in state_types: |
| 1309 |
errmsg = ('Invalid state, not one of [' + |
| 1310 |
', '.join(state_types) + ']') |
| 1311 |
print(errmsg) |
| 1312 |
return |
| 1313 |
fields['state'] = args.state |
| 1314 |
if args.severity: |
| 1315 |
if not _validate_severity(args.severity): |
| 1316 |
return |
| 1317 |
fields['severity'] = args.severity |
| 1318 |
if args.state_updated_start_time: |
| 1319 |
fields['state_updated_start_time'] = args.state_updated_start_time |
| 1320 |
if args.lifecycle_state: |
| 1321 |
fields['lifecycle_state'] = args.lifecycle_state |
| 1322 |
if args.link: |
| 1323 |
fields['link'] = args.link |
| 1324 |
if args.group_by: |
| 1325 |
group_by = args.group_by.split(',') |
| 1326 |
if not set(group_by).issubset(set(group_by_types)): |
| 1327 |
errmsg = ('Invalid group-by, one or more values not in [' + |
| 1328 |
','.join(group_by_types) + ']') |
| 1329 |
print(errmsg) |
| 1330 |
return |
| 1331 |
fields['group_by'] = args.group_by |
| 1332 |
if args.limit: |
| 1333 |
fields['limit'] = args.limit |
| 1334 |
if args.offset: |
| 1335 |
fields['offset'] = args.offset |
| 1336 |
try: |
| 1337 |
counts = mc.alarms.count(**fields) |
| 1338 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1339 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1340 |
else: |
| 1341 |
if args.json: |
| 1342 |
print(utils.json_formatter(counts)) |
| 1343 |
return |
| 1344 |
cols = counts['columns'] |
| 1345 |
|
| 1346 |
utils.print_list(counts['counts'], [i for i in range(len(cols))], |
| 1347 |
field_labels=cols) |
| 1348 |
|
| 1349 |
|
| 1350 |
@utils.arg('id', metavar='<ALARM_ID>', |
| 1351 |
help='The ID of the alarm.') |
| 1352 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 1353 |
help='The offset used to paginate the return data.') |
| 1354 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 1355 |
help='The amount of data to be returned up to the API maximum limit.') |
| 1356 |
def do_alarm_history(mc, args): |
| 1357 |
'''Alarm state transition history.''' |
| 1358 |
fields = {} |
| 1359 |
fields['alarm_id'] = args.id |
| 1360 |
if args.limit: |
| 1361 |
fields['limit'] = args.limit |
| 1362 |
if args.offset: |
| 1363 |
fields['offset'] = args.offset |
| 1364 |
try: |
| 1365 |
alarm = mc.alarms.history(**fields) |
| 1366 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1367 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1368 |
else: |
| 1369 |
output_alarm_history(args, alarm) |
| 1370 |
|
| 1371 |
|
| 1372 |
@utils.arg('--dimensions', metavar='<KEY1=VALUE1,KEY2=VALUE2...>', |
| 1373 |
help='key value pair used to specify a metric dimension. ' |
| 1374 |
'This can be specified multiple times, or once with parameters ' |
| 1375 |
'separated by a comma. ' |
| 1376 |
'Dimensions need quoting when they contain special chars [&,(,),{,},>,<] ' |
| 1377 |
'that confuse the CLI parser.', |
| 1378 |
action='append') |
| 1379 |
@utils.arg('--starttime', metavar='<UTC_START_TIME>', |
| 1380 |
help='measurements >= UTC time. format: 2014-01-01T00:00:00Z. OR' |
| 1381 |
' format: -120 (previous 120 minutes).') |
| 1382 |
@utils.arg('--endtime', metavar='<UTC_END_TIME>', |
| 1383 |
help='measurements <= UTC time. format: 2014-01-01T00:00:00Z.') |
| 1384 |
@utils.arg('--offset', metavar='<OFFSET LOCATION>', |
| 1385 |
help='The offset used to paginate the return data.') |
| 1386 |
@utils.arg('--limit', metavar='<RETURN LIMIT>', |
| 1387 |
help='The amount of data to be returned up to the API maximum limit.') |
| 1388 |
def do_alarm_history_list(mc, args): |
| 1389 |
'''List alarms state history.''' |
| 1390 |
fields = {} |
| 1391 |
if args.dimensions: |
| 1392 |
fields['dimensions'] = utils.format_parameters(args.dimensions) |
| 1393 |
if args.starttime: |
| 1394 |
_translate_starttime(args) |
| 1395 |
fields['start_time'] = args.starttime |
| 1396 |
if args.endtime: |
| 1397 |
fields['end_time'] = args.endtime |
| 1398 |
if args.limit: |
| 1399 |
fields['limit'] = args.limit |
| 1400 |
if args.offset: |
| 1401 |
fields['offset'] = args.offset |
| 1402 |
try: |
| 1403 |
alarm = mc.alarms.history_list(**fields) |
| 1404 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1405 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1406 |
else: |
| 1407 |
output_alarm_history(args, alarm) |
| 1408 |
|
| 1409 |
|
| 1410 |
def do_notification_type_list(mc, args): |
| 1411 |
'''List notification types supported by monasca.''' |
| 1412 |
|
| 1413 |
try: |
| 1414 |
notification_types = mc.notificationtypes.list() |
| 1415 |
except (osc_exc.ClientException, k_exc.HttpError) as he: |
| 1416 |
raise osc_exc.CommandError('%s\n%s' % (he.message, he.details)) |
| 1417 |
else: |
| 1418 |
if args.json: |
| 1419 |
print(utils.json_formatter(notification_types)) |
| 1420 |
return |
| 1421 |
else: |
| 1422 |
formatters = {'types': lambda x: x["type"]} |
| 1423 |
# utils.print_list(notification_types['types'], ["types"], formatters=formatters) |
| 1424 |
utils.print_list(notification_types, ["types"], formatters=formatters) |
| 1425 |
|
| 1426 |
|
| 1427 |
def _translate_starttime(args): |
| 1428 |
if args.starttime[0] == '-': |
| 1429 |
deltaT = time.time() + (int(args.starttime) * 60) |
| 1430 |
utc = str(datetime.datetime.utcfromtimestamp(deltaT)) |
| 1431 |
utc = utc.replace(" ", "T")[:-7] + 'Z' |
| 1432 |
args.starttime = utc |
| 1433 |
|
| 1434 |
|
| 1435 |
def _arg_split_patch_update(arg, patch=False): |
| 1436 |
if patch: |
| 1437 |
arg = ','.join(arg) |
| 1438 |
if not arg or arg == "[]": |
| 1439 |
arg_split = [] |
| 1440 |
else: |
| 1441 |
arg_split = arg.split(',') |
| 1442 |
return arg_split |