142 lines
5.2 KiB
Plaintext
142 lines
5.2 KiB
Plaintext
#!<%= @virtual_env %>/bin/python
|
|
|
|
import json
|
|
from keystoneclient.v3 import client as keystone_client
|
|
from monascaclient import client as monasca_client
|
|
import monascaclient.exc as exc
|
|
import os
|
|
|
|
|
|
def get_monasca_client():
|
|
kwargs = {
|
|
'username': os.environ['OS_USERNAME'],
|
|
'password': os.environ['OS_PASSWORD'],
|
|
'auth_url': os.environ['OS_AUTH_URL'],
|
|
'project_name': os.environ['OS_PROJECT_NAME'],
|
|
}
|
|
|
|
_ksclient = keystone_client.Client(**kwargs)
|
|
|
|
kwargs = {
|
|
'token': _ksclient.auth_token,
|
|
}
|
|
|
|
return monasca_client.Client(
|
|
'2_0',
|
|
os.environ['MONASCA_API_URL'],
|
|
**kwargs)
|
|
|
|
|
|
def get_current_def_names(client):
|
|
names = []
|
|
current_definitions = client.alarm_definitions.list()
|
|
for definition in current_definitions:
|
|
names.append(definition['name'])
|
|
return names
|
|
|
|
|
|
def get_current_notif_names(client):
|
|
names = []
|
|
current_notifications = client.notifications.list()
|
|
for notification in current_notifications:
|
|
names.append(notification['name'])
|
|
return names
|
|
|
|
def build_alarm_def(current_def, new_id, method, actions):
|
|
new_def = {}
|
|
new_def['alarm_id'] = current_def['id']
|
|
new_def['name'] = current_def['name']
|
|
new_def['description'] = current_def['description']
|
|
new_def['match_by'] = current_def['match_by']
|
|
new_def['severity'] = current_def['severity']
|
|
new_def['expression'] = current_def['expression']
|
|
new_def['actions_enabled'] = current_def['actions_enabled']
|
|
new_def['alarm_actions'] = current_def['alarm_actions']
|
|
new_def['ok_actions'] = current_def['ok_actions']
|
|
new_def['undetermined_actions'] = current_def['undetermined_actions']
|
|
|
|
if 'ALARM' in actions and new_id not in new_def['alarm_actions']:
|
|
print("Adding ALARM action for method '%s' to '%s'" % (method, new_def['name']))
|
|
new_def['alarm_actions'].append(new_id)
|
|
if 'OK' in actions and new_id not in new_def['ok_actions']:
|
|
print("Adding OK action for method '%s' to '%s'" % (method, new_def['name']))
|
|
new_def['ok_actions'].append(new_id)
|
|
if 'UNDETERMINED' in actions and new_id not in new_def['undetermined_actions']:
|
|
print("Adding UNDETERMINED action for method '%s' to '%s'" % (method, new_def['name']))
|
|
new_def['undetermined_actions'].append(new_id)
|
|
|
|
return new_def
|
|
|
|
|
|
def assign_notification_method(client, assignment):
|
|
|
|
def_name = assignment['alarm_definition_name']
|
|
methods = assignment['notification_methods']
|
|
actions = assignment['actions']
|
|
|
|
current_definitions = client.alarm_definitions.list()
|
|
current_notifications = client.notifications.list()
|
|
for definition in current_definitions:
|
|
if def_name == definition['name']:
|
|
for method in methods:
|
|
for notification in current_notifications:
|
|
if method == notification['name']:
|
|
new_id = notification['id']
|
|
if new_id in definition['alarm_actions']:
|
|
print("Skipping assignment of notification method '%s' to '%s', already there." % (method, def_name))
|
|
continue
|
|
new_def = build_alarm_def(definition,
|
|
notification['id'],
|
|
method,
|
|
actions)
|
|
client.alarm_definitions.update(**new_def)
|
|
print("Assigned notification method '%s' to '%s'" % (method, def_name))
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
client = get_monasca_client()
|
|
def_names = get_current_def_names(client)
|
|
notif_names = get_current_notif_names(client)
|
|
alarm_definitions = json.load(open('<%= @alarm_definition_config %>'))
|
|
notification_methods = json.load(open('<%= @notification_config %>'))
|
|
notification_assignments = json.load(open('<%= @notification_assignments %>'))
|
|
|
|
#
|
|
# Create all the alarm definitions
|
|
#
|
|
for alarm_definition in alarm_definitions:
|
|
name = alarm_definition['name']
|
|
if name in def_names:
|
|
print("Skipping alarm definition '%s', already defined." % name)
|
|
continue
|
|
client.alarm_definitions.create(**alarm_definition)
|
|
print("Added alarm definition '%s'" % name)
|
|
|
|
#
|
|
# Create all the notification methods
|
|
#
|
|
for notification in notification_methods:
|
|
name = notification['name']
|
|
if name in notif_names:
|
|
print("Skipping notification '%s', already defined." % name)
|
|
continue
|
|
client.notifications.create(**notification)
|
|
print("Added notification method '%s'" % name)
|
|
|
|
#
|
|
# Now assign the notification method(s) to
|
|
# alarm definitions defined in assignment file.
|
|
#
|
|
for assignment in notification_assignments:
|
|
assign_notification_method(client, assignment)
|
|
|
|
except exc.HTTPException as he:
|
|
raise exc.CommandError('HTTPException code=%s message=%s' %
|
|
(he.code, he.message))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|