# # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from cliff import command from cliff import lister from cliff import show from oslo_serialization import jsonutils from oslo_utils import strutils from aodhclient import utils ALARM_TYPES = ['threshold', 'event', 'composite', 'gnocchi_resources_threshold', 'gnocchi_aggregation_by_metrics_threshold', 'gnocchi_aggregation_by_resources_threshold'] ALARM_STATES = ['ok', 'alarm', 'insufficient data'] ALARM_SEVERITY = ['low', 'moderate', 'critical'] ALARM_OPERATORS = ['lt', 'le', 'eq', 'ne', 'ge', 'gt'] STATISTICS = ['max', 'min', 'avg', 'sum', 'count'] ALARM_LIST_COLS = ['alarm_id', 'type', 'name', 'state', 'severity', 'enabled'] class CliAlarmList(lister.Lister): """List alarms""" def get_parser(self, prog_name): parser = super(CliAlarmList, self).get_parser(prog_name) parser.add_argument('-t', '--type', required=True, choices=ALARM_TYPES, help='Type of alarm') return parser def take_action(self, parsed_args): alarms = self.app.client.alarm.list(alarm_type=parsed_args.type) return utils.list2cols(ALARM_LIST_COLS, alarms) class CliAlarmSearch(CliAlarmList): """Search alarms with specified query rules""" def get_parser(self, prog_name): parser = super(CliAlarmSearch, self).get_parser(prog_name) parser.add_argument("--query", help="Query"), return parser def take_action(self, parsed_args): type_query = '{"=": {"type": "%s"}}' % parsed_args.type if parsed_args.query: query = '{"and": [%s, %s]}' % (type_query, parsed_args.query) else: query = type_query alarms = self.app.client.alarm.search(query=query) return utils.list2cols(ALARM_LIST_COLS, alarms) def _format_alarm(alarm): if alarm.get('composite_rule'): composite_rule = jsonutils.dumps(alarm['composite_rule'], indent=2) alarm['composite_rule'] = composite_rule return alarm for alarm_type in ALARM_TYPES: if alarm.get('%s_rule' % alarm_type): alarm.update(alarm.pop('%s_rule' % alarm_type)) return alarm class CliAlarmShow(show.ShowOne): """Show an alarm""" def get_parser(self, prog_name): parser = super(CliAlarmShow, self).get_parser(prog_name) parser.add_argument("alarm_id", help="ID of an alarm") return parser def take_action(self, parsed_args): alarm = self.app.client.alarm.get(alarm_id=parsed_args.alarm_id) return self.dict2columns(_format_alarm(alarm)) class CliAlarmCreate(show.ShowOne): """Create an alarm""" create = True def get_parser(self, prog_name): parser = super(CliAlarmCreate, self).get_parser(prog_name) parser.add_argument('-t', '--type', metavar='', required=self.create, choices=ALARM_TYPES, help='Type of alarm') parser.add_argument('--name', metavar='', required=self.create, help='Name of the alarm') parser.add_argument('--project-id', metavar='', help='Project to associate with alarm ' '(configurable by admin users only)') parser.add_argument('--user-id', metavar='', help='User to associate with alarm ' '(configurable by admin users only)') parser.add_argument('--description', metavar='', help='Free text description of the alarm') parser.add_argument('--state', metavar='', choices=ALARM_STATES, help='State of the alarm, one of: ' + str(ALARM_STATES)) parser.add_argument('--severity', metavar='', choices=ALARM_SEVERITY, help='Severity of the alarm, one of: ' + str(ALARM_SEVERITY)) parser.add_argument('--enabled', type=strutils.bool_from_string, metavar='{True|False}', help=('True if alarm evaluation is enabled')) parser.add_argument('--alarm-action', dest='alarm_actions', metavar='', action='append', help=('URL to invoke when state transitions to ' 'alarm. May be used multiple times')) parser.add_argument('--ok-action', dest='ok_actions', metavar='', action='append', help=('URL to invoke when state transitions to' 'OK. May be used multiple times')) parser.add_argument('--insufficient-data-action', dest='insufficient_data_actions', metavar='', action='append', help=('URL to invoke when state transitions to ' 'insufficient data. May be used multiple ' 'times')) parser.add_argument( '--time-constraint', dest='time_constraints', metavar='