1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 ep_file = '/etc/ceilometer/event_pipeline.yaml'
14 ep_file_bak = '/etc/ceilometer/event_pipeline.yaml.bak'
15 event_notifier_topic = 'notifier://?topic=alarm.all'
18 def set_notifier_topic():
19 config_modified = False
21 if not os.path.isfile(ep_file):
22 raise Exception("File doesn't exist: %s." % ep_file)
24 with open(ep_file, 'r') as file:
25 config = yaml.safe_load(file)
27 sinks = config['sinks']
29 if sink['name'] == 'event_sink':
30 publishers = sink['publishers']
31 if event_notifier_topic not in publishers:
32 print('Add event notifier in ceilometer')
33 publishers.append(event_notifier_topic)
34 config_modified = True
36 print('NOTE: event notifier is configured'
37 'in ceilometer as we needed')
40 shutil.copyfile(ep_file, ep_file_bak)
41 with open(ep_file, 'w+') as file:
42 file.write(yaml.safe_dump(config))
45 def set_maintenance_event_definitions():
46 ed_file = '/etc/ceilometer/event_definitions.yaml'
47 ed_file_bak = '/etc/ceilometer/event_definitions.bak'
49 if not os.path.isfile(ed_file):
50 raise Exception("File doesn't exist: %s." % ed_file)
52 with open(ed_file, 'r') as file:
53 config = yaml.safe_load(file)
55 et_list = [et['event_type'] for et in config]
57 if 'maintenance.scheduled' in et_list:
58 add_mscheduled = False
59 print('NOTE: maintenance.scheduled allready configured')
61 print('NOTE: add maintenance.scheduled to event_definitions.yaml')
64 'event_type': 'maintenance.scheduled',
66 'allowed_actions': {'fields': 'payload.allowed_actions'},
67 'instance_ids': {'fields': 'payload.instance_ids'},
68 'reply_url': {'fields': 'payload.reply_url'},
69 'actions_at': {'fields': 'payload.actions_at',
71 'state': {'fields': 'payload.state'},
72 'session_id': {'fields': 'payload.session_id'},
73 'project_id': {'fields': 'payload.project_id'},
74 'metadata': {'fields': 'payload.metadata'}
77 config.append(mscheduled)
79 if 'maintenance.host' in et_list:
81 print('NOTE: maintenance.host allready configured')
83 print('NOTE: add maintenance.host to event_definitions.yaml')
86 'event_type': 'maintenance.host',
88 'host': {'fields': 'payload.host'},
89 'project_id': {'fields': 'payload.project_id'},
90 'state': {'fields': 'payload.state'},
91 'session_id': {'fields': 'payload.session_id'}
96 if add_mscheduled or add_mhost:
97 shutil.copyfile(ed_file, ed_file_bak)
98 with open(ed_file, 'w+') as file:
99 file.write(yaml.safe_dump(config))
102 def set_cpu_allocation_ratio():
103 nova_file = '/etc/nova/nova.conf'
104 nova_file_bak = '/etc/nova/nova.bak'
106 if not os.path.isfile(nova_file):
107 raise Exception("File doesn't exist: %s." % nova_file)
108 # TODO (tojuvone): Unfortunately ConfigParser did not produce working conf
109 fcheck = open(nova_file)
110 found_list = ([ca for ca in fcheck.readlines() if "cpu_allocation_ratio"
113 if found_list and len(found_list):
116 for car in found_list:
117 if car.startswith('#'):
119 if car.startswith('cpu_allocation_ratio'):
121 if "1.0" not in car.split('=')[1]:
123 if not found or change:
124 # need to add or change
125 shutil.copyfile(nova_file, nova_file_bak)
126 fin = open(nova_file_bak)
127 fout = open(nova_file, "wt")
129 if change and line.startswith("cpu_allocation_ratio"):
130 line = "cpu_allocation_ratio=1.0"
131 if not found and line.startswith("[DEFAULT]"):
132 line += "cpu_allocation_ratio=1.0\n"
138 set_maintenance_event_definitions()
139 set_cpu_allocation_ratio()