blob: 3aa48659387c41dbadc6bca9ac200b2631c126b2 [file] [log] [blame]
rdudyalab086cf32016-08-11 00:07:45 -04001import yaml
2import random
3import string
4import logging
5import fnmatch
6
7def main():
8 orig_pipeline_conf = "/etc/ceilometer/pipeline.yaml"
9 with open (orig_pipeline_conf, 'r') as fap:
10 data = fap.read()
11 pipeline_cfg = yaml.safe_load(data)
12 return pipeline_cfg
13
14def build_meter_list():
15 ''' function to exiting meter list from pipeline.yaml'''
16 orig_pipeline_conf = "/etc/ceilometer/pipeline.yaml"
17 with open (orig_pipeline_conf, 'r') as fap:
18 data = fap.read()
19 pipeline_cfg = yaml.safe_load(data)
20 source_cfg = pipeline_cfg['sources']
21 meter_list=[]
22 for i in source_cfg:
23 meter_list.append(i['meters'])
24
25 return meter_list
26
27def get_sink_name_from_publisher(publisher,pipeline_cfg):
28 sink_cfg = pipeline_cfg['sinks']
29 ''' Iterating over the list of publishers to get sink name'''
30 try :
31 for sinks in sink_cfg:
32 pub_list = sinks.get('publishers')
33 try :
34 k = pub_list.index(publisher)
35 return sinks.get('name')
36 except Exception as e:
37 #print ("Got Exception",e.__str__())
38 continue
39 except Exception as e:
40 return None
41
42def get_source_name_from_meter(meter,pipeline_cfg):
43 source_cfg = pipeline_cfg['sources']
44 ''' Iternating over the list of meters to get source name'''
45 try :
46 for sources in source_cfg:
47 meter_list = sources.get('meters')
48 try :
49 k = meter_list.index(meter)
50 return sources.get('name')
51 except Exception as e:
52 #print ("Got Exception",e.__str__())
53 continue
54 except Exception as e:
55 return None
56
57def get_source_name_from_with_meter_patter_match(meter,pipeline_cfg):
58 ''' Iternating over the list of meters for wildcard match to get source name'''
59 source_cfg = pipeline_cfg['sources']
60 try :
61 for sources in source_cfg:
62 meter_list = sources.get('meters')
63 for k in meter_list:
64 if k[0] == "*":
65 logging.warning("Ignoring wild card meter(*) case ")
66 continue
67 if fnmatch.fnmatch(k,meter):
68 logging.debug("substring match")
69 return (sources.get('name'),"superset",k)
70 if fnmatch.fnmatch(meter,k):
71 logging.debug("input is super match")
72 return (sources.get('name'),"subset",k)
73 except Exception as e:
74 return None,None,None
75
76 return None,None,None
77
78def get_source_name_from_sink_name(sink_name,pipeline_cfg):
79 ''' iterating over list of sources to get sink name'''
80 source_cfg = pipeline_cfg['sources']
81 try :
82 for sources in source_cfg:
83 sink_list = sources.get("sinks")
84 try :
85 k = sink_list.index(sink_name)
86 #sources.get("meters").append("m2")
87 return sources.get("name")
88 except Exception as e:
89 continue
90 except Exception as e:
91 return None
92
93def get_sink_name_from_source_name(source_name,pipeline_cfg):
94 ''' iterating over list of sinks to get sink name'''
95 source_cfg = pipeline_cfg['sources']
96 try :
97 for sources in source_cfg:
98 try :
99 if sources.get("name") == source_name:
100 return sources.get("sinks")
101 except Exception as e:
102 continue
103 except Exception as e:
104 return None
105
106def add_meter_to_source(meter_name,source_name,pipeline_cfg):
107 ''' iterating over the list of sources to add meter to the matching source'''
108 source_cfg = pipeline_cfg['sources']
109 try :
110 for sources in source_cfg:
111 try :
112 if sources.get("name") == source_name:
113 sources.get("meters").append(meter_name)
114 return True
115 except Exception as e:
116 continue
117 except Exception as e:
118 return False
119
120def get_meter_list_from_source(source_name,pipeline_cfg):
121 ''' iterating over the list of sources to get meters under the given source'''
122 source_cfg = pipeline_cfg['sources']
123 try :
124 for sources in source_cfg:
125 try :
126 if sources.get("name") == source_name:
127 return sources.get("meters")
128 except Exception as e:
129 continue
130 except Exception as e:
131 return None
132
133def get_publisher_list_from_sink(sink_name,pipeline_cfg):
134 sink_cfg = pipeline_cfg['sinks']
135 ''' Iterating over the list of sinks to build publishers list '''
136 publisher_list = []
137 try :
138 for sinks in sink_cfg:
139 try :
140 for j in sink_name:
141 if j == sinks.get("name"):
142 publisher_list.append(sinks.get("publishers"))
143 return publisher_list
144 except Exception as e:
145 #print ("Got Exception",e.__str__())
146 continue
147 except Exception as e:
148 return None
149
150def get_publisher_list_from_sinkname(sink_name,pipeline_cfg):
151 sink_cfg = pipeline_cfg['sinks']
152 ''' Iterating over the list of sinks to build publishers list '''
153 try :
154 for sinks in sink_cfg:
155 pub_list = sinks.get('publishers')
156 try :
157 if sink_name == sinks.get("name"):
158 return pub_list
159 except Exception as e:
160 #print ("Got Exception",e.__str__())
161 continue
162 except Exception as e:
163 return None
164
165
166def delete_meter_from_source(meter_name,source_name,pipeline_cfg) :
167 ''' function to delete meter for the given source '''
168 source_cfg = pipeline_cfg['sources']
169 try :
170 for sources in source_cfg:
171 try :
172 if sources.get("name") == source_name:
173 meter_list = sources.get('meters')
174 try :
175 meter_index = meter_list.index(meter_name)
176 logging.debug("meter name is present at index:%s",meter_index)
177 if len(meter_list) == 1 and meter_index == 0:
178 logging.debug("Only one meter exists removing entire source entry")
179 source_cfg.remove(sources)
180 else :
181 meter_list.pop(meter_index)
182 return True
183 except Exception as e:
184 continue
185 except Exception as e:
186 continue
187 except Exception as e:
188 return False
189
190def delete_publisher_from_sink(publisher,sink_name,pipeline_cfg):
191 sink_cfg = pipeline_cfg['sinks']
192 ''' Iterating over the list of publishers '''
193 try :
194 for sinks in sink_cfg:
195 pub_list = sinks.get('publishers')
196 #print pub_list
197 try :
198 if sink_name == sinks.get("name"):
199 k = pub_list.index(publisher)
200 pub_list.pop(k)
201 #print k
202 return True
203 except Exception as e:
204 #print ("Got Exception",e.__str__())
205 continue
206 except Exception as e:
207 return None
208
209def delete_sink_from_pipeline(sink_name,pipeline_cfg):
210 sink_cfg = pipeline_cfg['sinks']
211 try :
212 for sinks in sink_cfg:
213 if sink_name == sinks.get("name"):
214 sink_cfg.remove(sinks)
215 return True
216 except Exception as e:
217 return False
218
219def add_publisher_to_sink(publisher_name,sink_name,pipeline_cfg):
220 sink_cfg = pipeline_cfg['sinks']
221 try :
222 for sinks in sink_cfg:
223 if sink_name == sinks.get("name"):
224 sinks.get('publishers').append(publisher_name)
225 return True
226 except Exception as e:
227 return None
228
229def get_source_info(meter):
230 name = ''.join(random.choice(string.ascii_lowercase) for _ in range(9))
231 sink_name = name + "_sink"
232 meter_name = name + "_source"
233 source_info = {'interval': 6,'meters': [meter],'name': meter_name,'sinks':[sink_name]}
234 logging.debug("* new source_info :%s",source_info)
235 return (source_info,sink_name)
236
237def get_sink_info(meter,sink_name,target):
238 sink_info = {'publishers':['notifier://',target],'transformers':None ,'name': sink_name}
239 logging.debug("* new source_info :%s",sink_info)
240 return sink_info
241
242def delete_conf_from_pipe_line_cfg(meter,publisher,pipeline_cfg):
243 #import pdb;pdb.set_trace()
244
245 sink_name = get_sink_name_from_publisher(publisher,pipeline_cfg)
246 source_name = get_source_name_from_meter(meter,pipeline_cfg)
247
248 if sink_name is None or source_name is None:
249 logging.error("Either sink or source name Exists in the pipeline.yaml")
250 return False
251
252 meter_list = get_meter_list_from_source(source_name,pipeline_cfg)
253
254 temp_meter_list = []
255
256 for j in meter_list:
257 temp_meter_list.append(j)
258
259 pub_list = get_publisher_list_from_sinkname(sink_name,pipeline_cfg)
260 if len(pub_list) > 2 and len(temp_meter_list) == 1:
261 if delete_publisher_from_sink(publisher,sink_name,pipeline_cfg):
262 return True
263 else:
264 return False
265
266 if delete_meter_from_source(meter,source_name,pipeline_cfg) :
267 if len(temp_meter_list) == 1:
268 if delete_publisher_from_sink(publisher,sink_name,pipeline_cfg) :
269 if get_source_name_from_sink_name(sink_name,pipeline_cfg) is None:
270 delete_sink_from_pipeline(sink_name,pipeline_cfg)
271 return True
272 else :
273 return False
274 return True
275 return False
276
277
278def update_sink_aggrgation(meter,publisher,source_name,matching_meter,meter_match,pipeline_cfg):
279 ''' Build new source and sink '''
280 new_source_info,new_sink_name = get_source_info(meter)
281 new_sink_info = get_sink_info(meter,new_sink_name,publisher)
282
283 meter_list = get_meter_list_from_source(source_name,pipeline_cfg)
284 sink_name = get_sink_name_from_source_name(source_name,pipeline_cfg)
285 publisher_list = get_publisher_list_from_sink(sink_name,pipeline_cfg)
286 for i in publisher_list:
287 for j in i:
288 #print j
289 if j not in new_sink_info.get("publishers") :
290 new_sink_info.get("publishers").append(j)
291 #print new_sink_info
292
293 cfg_source = pipeline_cfg['sources']
294 cfg_sink = pipeline_cfg['sinks']
295 if meter_match == "superset" :
296 new_source_info.get("meters").append("!"+ matching_meter)
297 elif meter_match == "subset" :
298 ''' here need to get list of meters with sub-string match '''
299 add_meter_to_source("!"+meter,source_name,pipeline_cfg)
300 add_publisher_to_sink(publisher,sink_name,pipeline_cfg)
301
302 logging.debug("----------- Before Updating Meter Info ------------------")
303 logging.debug("%s",pipeline_cfg)
304
305 ''' Updating source and sink info '''
306 cfg_source.append(new_source_info)
307 cfg_sink.append(new_sink_info)
308 logging.debug("----------- After Updating Meter Info --------------------")
309 logging.debug("%s",pipeline_cfg)
310
311def update_conf_to_pipe_line_cfg(meter,publisher,pipeline_cfg):
312 #import pdb;pdb.set_trace()
313 sink_name = get_sink_name_from_publisher(publisher,pipeline_cfg)
314 source_name = get_source_name_from_meter(meter,pipeline_cfg)
315 if sink_name is None :
316 logging.debug("No Sink exists with the given Publisher")
317 if source_name is None:
318 ''' Commenting the code related t owild card '''
319 '''
320 pattern_source_name,pattern,matching_meter = get_source_name_from_with_meter_patter_match(meter,pipeline_cfg)
321 if pattern_source_name is not None:
322 if pattern == "superset" :
323 #add_meter_to_source("!"+meter,pattern_source_name,pipeline_cfg)
324 update_sink_aggrgation(meter,publisher,pattern_source_name,matching_meter,"superset",pipeline_cfg)
325 #print pipeline_cfg
326 return True
327 if pattern == "subset" :
328 update_sink_aggrgation(meter,publisher,pattern_source_name,matching_meter,"subset",pipeline_cfg)
329 return True
330 '''
331 source_info,sink_name = get_source_info(meter)
332 sink_info = get_sink_info(meter,sink_name,publisher)
333
334 cfg_source = pipeline_cfg['sources']
335 cfg_sink = pipeline_cfg['sinks']
336
337 logging.debug("----------- Before Updating Meter Info ------------------")
338 logging.debug("%s",pipeline_cfg)
339
340 ''' Updating source and sink info '''
341 cfg_source.append(source_info)
342 cfg_sink.append(sink_info)
343 logging.debug("----------- After Updating Meter Info --------------------")
344 logging.debug("%s",pipeline_cfg)
345 return True
346 else :
347 logging.debug("Meter already exists in the conf file under source name:%s ",source_name)
348 meter_list = get_meter_list_from_source(source_name,pipeline_cfg)
349 publisher_list=[]
350 if len(meter_list) > 1:
351 sink_name = get_sink_name_from_source_name(source_name,pipeline_cfg)
352 '''
353 if type(sink_name) is list :
354 for sinkname in sink_name:
355 publisher_list.append(get_publisher_list_from_sink(sinkname,pipeline_cfg))
356 else :
357 publisher_list.append(get_publisher_list_from_sink(sink_name,pipeline_cfg))
358 '''
359 publisher_list = get_publisher_list_from_sink(sink_name,pipeline_cfg)
360 new_source_info,new_sink_name = get_source_info(meter)
361 new_sink_info = get_sink_info(meter,new_sink_name,publisher)
362 for i in publisher_list:
363 for j in i:
364 #print j
365 if j not in new_sink_info.get("publishers") :
366 new_sink_info.get("publishers").append(j)
367 cfg_source = pipeline_cfg['sources']
368 cfg_sink = pipeline_cfg['sinks']
369
370 logging.debug("----------- Before Updating Meter Info ------------------")
371 logging.debug("%s",pipeline_cfg)
372
373 ''' Updating source and sink info '''
374 cfg_source.append(new_source_info)
375 cfg_sink.append(new_sink_info)
376 logging.debug("----------- After Updating Meter Info --------------------")
377 logging.debug("%s",pipeline_cfg)
378 delete_meter_from_source(meter,source_name,pipeline_cfg)
379 logging.debug("%s",pipeline_cfg)
380 return True
381 else :
382 logging.debug ("Source already exists for this meter add publisher to it .....:%s",source_name)
383 sink_name_list = get_sink_name_from_source_name(source_name,pipeline_cfg)
384 for sink_name in sink_name_list :
385 add_publisher_to_sink(publisher,sink_name,pipeline_cfg)
386 return True
387 #print pipeline_cfg
388 else :
389 logging.debug ("Publisher already exists under sink:%s",sink_name)
390 if get_source_name_from_meter(meter,pipeline_cfg) is not None:
391 logging.debug("Both meter and publisher already exists in the conf file")
392 logging.debug( "Update request is not sucessful")
393 return False
394 else :
395 source_name = get_source_name_from_sink_name(sink_name,pipeline_cfg)
396 logging.debug ("Need to add meter to already existing source which \
397 has this publisher under one of its sink")
398 #print source_name
399 if add_meter_to_source(meter,source_name,pipeline_cfg):
400 logging.debug("Meter added sucessfully")
401 return True
402
403
404