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