Beispiel #1
0
 def linkify_es_by_s(self, services):
     for es in self:
         # If no host, no hope of having a service
         if not (hasattr(es, 'host_name') and hasattr(es, 'service_description')):
             continue
         es_hname, sdesc = es.host_name, es.service_description
         if '' in (es_hname.strip(), sdesc.strip()):
             continue
         for hname in strip_and_uniq(es_hname.split(',')):
             for sname in strip_and_uniq(sdesc.split(',')):
                 s = services.find_srv_by_name_and_hostname(hname, sname)
                 if s is not None:
                     #print "Linking service", s.get_name(), 'with me', es.get_name()
                     s.escalations.append(es)
Beispiel #2
0
def find_several(lst, elt, prop, key):
    print 'Find several in', lst, 'for element', elt, 'and property', prop
    value = elt.get(prop, None)
    if value is None:
        return []

    values = value.split(',')
    values = strip_and_uniq(values)
    print 'Our values are', values

    res = []
    # Now try to match what it got
    for dbi in lst:
        print 'Try to look at', dbi, 'for property', key
        v = dbi.get(key, None)
        if v is None:
            continue
        v = v.strip()
        try:
            print 'MAtch with db', v
        except:
            pass
        if v  in values:
            res.append(dbi)
    print "Return find_several::", res
    return res
Beispiel #3
0
def find_several(lst, elt, prop, key):
    print 'Find several in', lst, 'for element', elt, 'and property', prop
    value = elt.get(prop, None)
    if value is None:
        return []

    values = value.split(',')
    values = strip_and_uniq(values)
    print 'Our values are', values

    res = []
    # Now try to match what it got
    for dbi in lst:
        print 'Try to look at', dbi, 'for property', key
        v = dbi.get(key, None)
        if v is None:
            continue
        v = v.strip()
        try:
            print 'Match with db', v
        except:
            pass
        if v in values:
            res.append(dbi)
    print "Return find_several::", res
    return res
Beispiel #4
0
    def explode_contact_groups_into_contacts(self, contactgroups):
        for i in self:
            if hasattr(i, 'contact_groups'):
                # in tempaltes we need to take care, that contact_groups
                # could still have a leading '+', if we don't remove them now
                # we will not find them in the given contactgroups anymore
                cg = i.contact_groups
                if len(cg) >= 1 and cg[0] == '+':
                    cg = cg[1:]

                cgnames = cg.split(',')
                cgnames = strip_and_uniq(cgnames)
                for cgname in cgnames:
                    cg = contactgroups.find_by_name(cgname)
                    if cg is None:
                        # XXX: Why don't i receive this err when defining contact_groups in a template
                        err = "The contact group '%s' defined on the %s '%s' do not exist" % (cgname, i.__class__.my_type, i.get_name())
                        i.configuration_errors.append(err)
                        continue
                    cnames = contactgroups.get_members_by_name(cgname)
                    # We add contacts into our contacts
                    if cnames != []:
                        if hasattr(i, 'contacts'):
                            i.contacts += ',' + cnames
                        else:
                            i.contacts = cnames
Beispiel #5
0
 def linkify_with_notificationways(self, notificationways):
     for i in self:
         if not hasattr(i, 'notificationways'): continue
         new_notificationways = []
         for nw_name in strip_and_uniq(i.notificationways.split(',')):
             nw = notificationways.find_by_name(nw_name)
             if nw is not None:
                 new_notificationways.append(nw)
             else:  #TODO: What?
                 pass
         #Get the list, but first make elements uniq
         i.notificationways = list(set(new_notificationways))
Beispiel #6
0
 def linkify_with_notificationways(self, notificationways):
     for i in self:
         if not hasattr(i, 'notificationways'): continue
         new_notificationways = []
         for nw_name in strip_and_uniq(i.notificationways.split(',')):
             nw = notificationways.find_by_name(nw_name)
             if nw is not None:
                 new_notificationways.append(nw)
             else: #TODO: What?
                 pass
         #Get the list, but first make elements uniq
         i.notificationways = list(set(new_notificationways))
Beispiel #7
0
 def linkify_es_by_h(self, hosts):
     for es in self:
         # If no host, no hope of having a service
         if (not hasattr(es, 'host_name') or es.host_name.strip() == ''
                 or (hasattr(es, 'service_description') and es.service_description.strip() != '')):
             continue
         # I must be NOT a escalation on for service
         for hname in strip_and_uniq(es.host_name.split(',')):
             h = hosts.find_by_name(hname)
             if h is not None:
                 #print "Linking host", h.get_name(), 'with me', es.get_name()
                 h.escalations.append(es)
Beispiel #8
0
 def linkify_with_resultmodulations(self, resultmodulations):
     for i in self:
         if hasattr(i, 'resultmodulations'):
             resultmodulations_tab = i.resultmodulations.split(',')
             resultmodulations_tab = strip_and_uniq(resultmodulations_tab)
             new_resultmodulations = []
             for rm_name in resultmodulations_tab:
                 rm = resultmodulations.find_by_name(rm_name)
                 if rm is not None:
                     new_resultmodulations.append(rm)
                 else:  #TODO WHAT?
                     pass
             i.resultmodulations = new_resultmodulations
Beispiel #9
0
 def linkify_with_resultmodulations(self, resultmodulations):
     for i in self:
         if hasattr(i, 'resultmodulations'):
             resultmodulations_tab = i.resultmodulations.split(',')
             resultmodulations_tab = strip_and_uniq(resultmodulations_tab)
             new_resultmodulations = []
             for rm_name in resultmodulations_tab:
                 rm = resultmodulations.find_by_name(rm_name)
                 if rm is not None:
                     new_resultmodulations.append(rm)
                 else: # TODO WHAT?
                     pass
             i.resultmodulations = new_resultmodulations
Beispiel #10
0
 def linkify_with_escalations(self, escalations):
     for i in self:
         if hasattr(i, 'escalations'):
             #print i.get_name(), 'going to link escalations', i.escalations
             escalations_tab = i.escalations.split(',')
             escalations_tab = strip_and_uniq(escalations_tab)
             new_escalations = []
             for es_name in escalations_tab:
                 es = escalations.find_by_name(es_name)
                 if es is not None:
                     new_escalations.append(es)
                 else: #TODO what?
                     pass
             i.escalations = new_escalations
Beispiel #11
0
 def linkify_command_list_with_commands(self, commands, prop):
     for i in self:
         if hasattr(i, prop):
             coms = getattr(i, prop).split(',')
             coms = strip_and_uniq(coms)
             com_list = []
             for com in coms:
                 if com != '':
                     cmdCall = self.create_commandcall(i, commands, com)
                     # TODO: catch None?
                     com_list.append(cmdCall)
                 else:  # TODO: catch?
                     pass
             setattr(i, prop, com_list)
Beispiel #12
0
 def linkify_with_notificationways(self, notificationways):
     for i in self:
         if not hasattr(i, 'notificationways'):
             continue
         new_notificationways = []
         for nw_name in strip_and_uniq(i.notificationways.split(',')):
             nw = notificationways.find_by_name(nw_name)
             if nw is not None:
                 new_notificationways.append(nw)
             else:
                 err = "The 'notificationways' of the %s '%s' named '%s' is unknown!" % (i.__class__.my_type, i.get_name(), nw_name)
                 i.configuration_errors.append(err)
         # Get the list, but first make elements uniq
         i.notificationways = list(set(new_notificationways))
Beispiel #13
0
 def linkify_with_escalations(self, escalations):
     for i in self:
         if hasattr(i, 'escalations'):
             escalations_tab = i.escalations.split(',')
             escalations_tab = strip_and_uniq(escalations_tab)
             new_escalations = []
             for es_name in [e for e in escalations_tab if e != '']:
                 es = escalations.find_by_name(es_name)
                 if es is not None:
                     new_escalations.append(es)
                 else:  # Escalation not find, not good!
                     err = "the escalation '%s' defined for '%s' is unknown" % (es_name, i.get_name())
                     i.configuration_errors.append(err)
             i.escalations = new_escalations
Beispiel #14
0
 def linkify_command_list_with_commands(self, commands, prop):
     for i in self:
         if hasattr(i, prop):
             coms = getattr(i, prop).split(',')
             coms = strip_and_uniq(coms)
             com_list = []
             for com in coms:
                 if com != '':
                     cmdCall = self.create_commandcall(i, commands, com)
                     # TODO: catch None?
                     com_list.append(cmdCall)
                 else:  # TODO: catch?
                     pass
             setattr(i, prop, com_list)
Beispiel #15
0
 def linkify_with_escalations(self, escalations):
     for i in self:
         if hasattr(i, 'escalations'):
             #print i.get_name(), 'going to link escalations', i.escalations
             escalations_tab = i.escalations.split(',')
             escalations_tab = strip_and_uniq(escalations_tab)
             new_escalations = []
             for es_name in escalations_tab:
                 es = escalations.find_by_name(es_name)
                 if es is not None:
                     new_escalations.append(es)
                 else:  #TODO what?
                     pass
             i.escalations = new_escalations
Beispiel #16
0
 def linkify_with_notificationways(self, notificationways):
     for i in self:
         if not hasattr(i, 'notificationways'):
             continue
         new_notificationways = []
         for nw_name in strip_and_uniq(i.notificationways.split(',')):
             nw = notificationways.find_by_name(nw_name)
             if nw is not None:
                 new_notificationways.append(nw)
             else:
                 err = "The 'notificationways' of the %s '%s' named '%s' is unknown!" % (i.__class__.my_type, i.get_name(), nw_name)
                 i.configuration_errors.append(err)
         # Get the list, but first make elements uniq
         i.notificationways = list(set(new_notificationways))
Beispiel #17
0
 def linkify_with_escalations(self, escalations):
     for i in self:
         if hasattr(i, 'escalations'):
             escalations_tab = i.escalations.split(',')
             escalations_tab = strip_and_uniq(escalations_tab)
             new_escalations = []
             for es_name in [e for e in escalations_tab if e != '']:
                 es = escalations.find_by_name(es_name)
                 if es is not None:
                     new_escalations.append(es)
                 else:  # Escalation not find, not good!
                     err = "the escalation '%s' defined for '%s' is unknown" % (es_name, i.get_name())
                     i.configuration_errors.append(err)
             i.escalations = new_escalations
Beispiel #18
0
 def linkify_with_resultmodulations(self, resultmodulations):
     for i in self:
         if hasattr(i, 'resultmodulations'):
             resultmodulations_tab = i.resultmodulations.split(',')
             resultmodulations_tab = strip_and_uniq(resultmodulations_tab)
             new_resultmodulations = []
             for rm_name in resultmodulations_tab:
                 rm = resultmodulations.find_by_name(rm_name)
                 if rm is not None:
                     new_resultmodulations.append(rm)
                 else:
                     err = "the result modulation '%s' defined on the %s '%s' do not exist" % (rm_name, i.__class__.my_type, i.get_name())
                     i.configuration_warnings.append(err)
                     continue
             i.resultmodulations = new_resultmodulations
Beispiel #19
0
 def linkify_with_resultmodulations(self, resultmodulations):
     for i in self:
         if hasattr(i, 'resultmodulations'):
             resultmodulations_tab = i.resultmodulations.split(',')
             resultmodulations_tab = strip_and_uniq(resultmodulations_tab)
             new_resultmodulations = []
             for rm_name in resultmodulations_tab:
                 rm = resultmodulations.find_by_name(rm_name)
                 if rm is not None:
                     new_resultmodulations.append(rm)
                 else:
                     err = "the result modulation '%s' defined on the %s '%s' do not exist" % (rm_name, i.__class__.my_type, i.get_name())
                     i.configuration_warnings.append(err)
                     continue
             i.resultmodulations = new_resultmodulations
Beispiel #20
0
 def linkify_with_business_impact_modulations(self, business_impact_modulations):
     for i in self:
         if hasattr(i, 'business_impact_modulations'):
             business_impact_modulations_tab = i.business_impact_modulations.split(',')
             business_impact_modulations_tab = strip_and_uniq(business_impact_modulations_tab)
             new_business_impact_modulations = []
             for rm_name in business_impact_modulations_tab:
                 rm = business_impact_modulations.find_by_name(rm_name)
                 if rm is not None:
                     new_business_impact_modulations.append(rm)
                 else:
                     err = "the business impact modulation '%s' defined on the %s '%s' do not exist" % (rm_name, i.__class__.my_type, i.get_name())
                     i.configuration_errors.append(err)
                     continue
             i.business_impact_modulations = new_business_impact_modulations
Beispiel #21
0
 def linkify_with_business_impact_modulations(self, business_impact_modulations):
     for i in self:
         if hasattr(i, 'business_impact_modulations'):
             business_impact_modulations_tab = i.business_impact_modulations.split(',')
             business_impact_modulations_tab = strip_and_uniq(business_impact_modulations_tab)
             new_business_impact_modulations = []
             for rm_name in business_impact_modulations_tab:
                 rm = business_impact_modulations.find_by_name(rm_name)
                 if rm is not None:
                     new_business_impact_modulations.append(rm)
                 else:
                     err = "the business impact modulation '%s' defined on the %s '%s' do not exist" % (rm_name, i.__class__.my_type, i.get_name())
                     i.configuration_errors.append(err)
                     continue
             i.business_impact_modulations = new_business_impact_modulations
Beispiel #22
0
def hg_name_get_groupnames(all_res, hosts, hostgroups, res=None):
    if res is None:
        res = {}

    for tok in all_res:
        if isinstance(tok, tuple):
            hg_name_get_groupnames(tok[0], hosts, hostgroups, res)
            hg_name_get_groupnames(tok[1], hosts, hostgroups, res)
            continue
        if isinstance(tok, list):
            hg_name_get_groupnames(tok, hosts, hostgroups, res)
            continue
        
        save_tok = tok
        if tok in HostGroup_Name_Parse_Ctx.specials_hostgroup_name_chars + ( '-', ):
            if tok != '*':
                continue
            tok = HostGroup_Name_Parse_Ctx.catch_all_name
        
        if tok in res: # we already got it, good.
            continue
        
        if save_tok == '*':
            elts = get_all_host_names_set(hosts)
        else:
            # we got a group name :
            members = hostgroups.get_members_by_name(tok)
            # TODO: check why:
            # sometimes we get a list, sometimes we get a string of hosts name which are ',' separated..
            if isinstance(members, list):
                elts = members
            else:
                elts = members.split(',')
            elts = strip_and_uniq(elts)
            
            # the "host_name" members of a hostgroup can also be '*' :
            if '*' in elts:
                tok = HostGroup_Name_Parse_Ctx.catch_all_name 
                if tok in res:
                    elts = res[tok]
                else: 
                    elts = get_all_host_names_set(hosts)    
                # the original tok must still be set:
                res[save_tok] = elts 

        res[tok] = set(elts)

    return res
Beispiel #23
0
    def find_object(self, pattern):
        obj = None
        error = None
        pattern = pattern.strip()

        if pattern == "*":
            obj = [
                h.host_name
                for h in self.all_elements.items.values()
                if getattr(h, "host_name", "") != "" and not h.is_tpl()
            ]
            return obj, error

        # Ok a more classic way

        # print "GRPS", self.grps

        if self.ctx == "hostgroups":
            # Ok try to find this hostgroup
            hg = self.grps.find_by_name(pattern)
            # Maybe it's an known one?
            if not hg:
                error = "Error : cannot find the %s of the expression '%s'" % (self.ctx, pattern)
                return hg, error
            # Ok the group is found, get the elements!
            elts = hg.get_hosts().split(",")
            elts = strip_and_uniq(elts)

            # Maybe the hostgroup memebrs is '*', if so expand with all hosts
            if "*" in elts:
                elts.extend(
                    [
                        h.host_name
                        for h in self.all_elements.items.values()
                        if getattr(h, "host_name", "") != "" and not h.is_tpl()
                    ]
                )
                # And remove this strange hostname too :)
                elts.remove("*")
            return elts, error

        else:  # templates
            obj = self.grps.find_hosts_that_use_template(pattern)

        return obj, error
Beispiel #24
0
 def explode_contact_groups_into_contacts(self, contactgroups):
     for i in self:
         if hasattr(i, 'contact_groups'):
             cgnames = i.contact_groups.split(',')
             cgnames = strip_and_uniq(cgnames)
             for cgname in cgnames:
                 cg = contactgroups.find_by_name(cgname)
                 if cg is None:
                     err = "The contact group '%s' defined on the %s '%s' do not exist" % (cgname, i.__class__.my_type, i.get_name())
                     i.configuration_errors.append(err)
                     continue
                 cnames = contactgroups.get_members_by_name(cgname)
                 # We add contacts into our contacts
                 if cnames != []:
                     if hasattr(i, 'contacts'):
                         i.contacts += ',' + cnames
                     else:
                         i.contacts = cnames
Beispiel #25
0
 def linkify_with_contacts(self, contacts):
     for i in self:
         if hasattr(i, 'contacts'):
             contacts_tab = i.contacts.split(',')
             contacts_tab = strip_and_uniq(contacts_tab)
             new_contacts = []
             for c_name in contacts_tab:
                 if c_name != '':
                     c = contacts.find_by_name(c_name)
                     if c is not None:
                         new_contacts.append(c)
                     # Else: Add in the errors tab.
                     # will be raised at is_correct
                     else:
                         err = "the contact '%s' defined for '%s' is unknown" % (c_name, i.get_name())
                         i.configuration_errors.append(err)
             # Get the list, but first make elements uniq
             i.contacts = list(set(new_contacts))
Beispiel #26
0
 def linkify_with_contacts(self, contacts):
     for i in self:
         if hasattr(i, 'contacts'):
             contacts_tab = i.contacts.split(',')
             contacts_tab = strip_and_uniq(contacts_tab)
             new_contacts = []
             for c_name in contacts_tab:
                 if c_name != '':
                     c = contacts.find_by_name(c_name)
                     if c is not None:
                         new_contacts.append(c)
                     # Else: Add in the errors tab.
                     # will be raised at is_correct
                     else:
                         err = "the contact '%s' defined for '%s' is unknown" % (c_name, i.get_name())
                         i.configuration_errors.append(err)
             # Get the list, but first make elements uniq
             i.contacts = list(set(new_contacts))
Beispiel #27
0
 def explode_contact_groups_into_contacts(self, contactgroups):
     for i in self:
         if hasattr(i, 'contact_groups'):
             cgnames = i.contact_groups.split(',')
             cgnames = strip_and_uniq(cgnames)
             for cgname in cgnames:
                 cg = contactgroups.find_by_name(cgname)
                 if cg is None:
                     err = "The contact group '%s'defined on the %s '%s' do not exist" % (
                         cgname, i.__class__.my_type, i.get_name())
                     i.configuration_errors.append(err)
                     continue
                 cnames = contactgroups.get_members_by_name(cgname)
                 #We add contacts into our contacts
                 if cnames != []:
                     if hasattr(i, 'contacts'):
                         i.contacts += ',' + cnames
                     else:
                         i.contacts = cnames
    def find_object(self, pattern):
        obj = None
        error = None
        pattern = pattern.strip()

        if pattern == '*':
            obj = [
                h.host_name for h in self.all_elements.items.values()
                if getattr(h, 'host_name', '') != '' and not h.is_tpl()
            ]
            return obj, error

        # Ok a more classic way

        # print "GRPS", self.grps

        if self.ctx == 'hostgroups':
            # Ok try to find this hostgroup
            hg = self.grps.find_by_name(pattern)
            # Maybe it's an known one?
            if not hg:
                error = "Error : cannot find the %s of the expression '%s'" % (
                    self.ctx, pattern)
                return hg, error
            # Ok the group is found, get the elements!
            elts = hg.get_hosts()
            elts = strip_and_uniq(elts)

            # Maybe the hostgroup memebrs is '*', if so expand with all hosts
            if '*' in elts:
                elts.extend([
                    h.host_name for h in self.all_elements.items.values()
                    if getattr(h, 'host_name', '') != '' and not h.is_tpl()
                ])
                # And remove this strange hostname too :)
                elts.remove('*')
            return elts, error

        else:  # templates
            obj = self.grps.find_hosts_that_use_template(pattern)

        return obj, error
Beispiel #29
0
    def linkify_s_by_plug(self):
        for s in self:
            new_modules = []
            mods = s.modules.split(",")
            mods = strip_and_uniq(mods)
            for plug_name in mods:
                plug_name = plug_name.strip()

                # don't read void names
                if plug_name == "":
                    continue

                # We are the modules, we search them :)
                plug = self.find_by_name(plug_name)
                if plug is not None:
                    new_modules.append(plug)
                else:
                    logger.error("[module] unknown %s module from %s" % (plug_name, s.get_name()))
                    s.configuration_errors.append(err)
            s.modules = new_modules
Beispiel #30
0
    def linkify_s_by_plug(self):
        for s in self:
            new_modules = []
            mods = s.modules.split(',')
            mods = strip_and_uniq(mods)
            for plug_name in mods:
                plug_name = plug_name.strip()

                # don't read void names
                if plug_name == '':
                    continue

                # We are the modules, we search them :)
                plug = self.find_by_name(plug_name)
                if plug is not None:
                    new_modules.append(plug)
                else:
                    err = "Error : the module %s is unknown for %s" % (plug_name, s.get_name())
                    print "err", err
                    s.configuration_errors.append(err)
            s.modules = new_modules
Beispiel #31
0
 def linkify_command_list_with_commands(self, commands, prop):
     for i in self:
         if hasattr(i, prop):
             coms = getattr(i, prop).split(',')
             coms = strip_and_uniq(coms)
             com_list = []
             for com in coms:
                 if com != '':
                     if hasattr(i, 'poller_tag'):
                         cmdCall = CommandCall(commands, com,
                                               poller_tag=i.poller_tag)
                     elif hasattr(i, 'reactionner_tag'):
                         cmdCall = CommandCall(commands, com,
                                               reactionner_tag=i.reactionner_tag)
                     else:
                         cmdCall = CommandCall(commands, com)
                     # TODO: catch None?
                     com_list.append(cmdCall)
                 else:  # TODO: catch?
                     pass
             setattr(i, prop, com_list)
Beispiel #32
0
    def linkify_s_by_plug(self):
        for s in self:
            new_modules = []
            mods = s.modules.split(',')
            mods = strip_and_uniq(mods)
            for plug_name in mods:
                plug_name = plug_name.strip()

                # don't read void names
                if plug_name == '':
                    continue

                # We are the modules, we search them :)
                plug = self.find_by_name(plug_name)
                if plug is not None:
                    new_modules.append(plug)
                else:
                    err = "[module] unknown %s module from %s" % (plug_name, s.get_name())
                    logger.error(err)
                    s.configuration_errors.append(err)
            s.modules = new_modules
Beispiel #33
0
def find_several(lst, elt, prop, key):
    print "Find several in", lst, "for element", elt, "and property", prop
    value = elt.get(prop, None)
    if value is None:
        return []

    values = value.split(",")
    values = strip_and_uniq(values)
    print "Our values are", values

    res = []
    # Now try to match what it got
    for dbi in lst:
        print "Try to look at", dbi, "for property", key
        v = dbi.get(key, None)
        if v is None:
            continue
        v = v.strip()
        print "MAtch with db", v
        if v in values:
            res.append(dbi)
    print "Return find_several::", res
    return res
Beispiel #34
0
 def linkify_command_list_with_commands(self, commands, prop):
     for i in self:
         if hasattr(i, prop):
             coms = getattr(i, prop).split(',')
             coms = strip_and_uniq(coms)
             com_list = []
             for com in coms:
                 if com != '':
                     if hasattr(i, 'poller_tag'):
                         cmdCall = CommandCall(commands,
                                               com,
                                               poller_tag=i.poller_tag)
                     elif hasattr(i, 'reactionner_tag'):
                         cmdCall = CommandCall(
                             commands,
                             com,
                             reactionner_tag=i.reactionner_tag)
                     else:
                         cmdCall = CommandCall(commands, com)
                     #TODO: catch None?
                     com_list.append(cmdCall)
                 else:  # TODO: catch?
                     pass
             setattr(i, prop, com_list)
Beispiel #35
0
    def evaluate_hostgroup_expression(self, expr, hosts, hostgroups):
        res = []
        original_expr = expr
        #print "I'm trying to prepare the expression", expr

        #We've got problem with the "-" sign. It can be in a
        #valid name but it's a sign of difference for sets
        #so we change the - now by something, then we reverse after
        if '-' in expr:
            expr = expr.replace('-', 'MINUSSIGN')

        # ! (not) should be changed as "ALL-" (all but not...)
        if '!' in expr:
            ALLELEMENTS = self.get_all_host_names_set(hosts)
            #print "Changing ! by ALLELEMENTS- in ", expr
            expr = expr.replace('!', 'ALLELEMENTS-')

        # We change all separaton token by 10 spaces
        # (so names can still have some spaces
        # on them like Europe Servers because we wil cut byy this 10spaces after
        strip_expr = expr
        for c in ['|', '&', '(', ')', ',', '-']:
            strip_expr = strip_expr.replace(c, ' '*10)
        #print "Stripped expression:", strip_expr

        tokens = strip_expr.split(' '*10)
        # Strip and non void token
        tokens = [token.strip() for  token in tokens if token != '']
        #print "Tokens:", tokens

        #Now add in locals() dict (yes, real variables!)
        for token in tokens:
            # ALLELEMENTS is a private group for us
            if token != 'ALLELEMENTS':
                #Maybe the token was with - at the begining,
                #but we change all by "MINUSSIGN". We must change it back now
                #for the search
                if 'MINUSSIGN' in token:
                    tmp_token = token.replace('MINUSSIGN', '-')
                    members = hostgroups.get_members_by_name(tmp_token)
                else:
                    members = hostgroups.get_members_by_name(token)

                if members != []:
                    #print "Get members", members
                    elts = members.split(',')
                    elts = strip_and_uniq(elts)
                    elts = set(elts)
                    #print "Elements:", elts
                    #print "Now set in locals the token new values"
                    locals()[token.upper()] = elts
                else:
                    if 'MINUSSIGN' in token:
                        token = token.replace('MINUSSIGN', '-')
                    print self.__dict__, type(self)
                    err = "ERROR: the group %s is unknown !" % (token,)
                    print err
                    self.configuration_errors.append(err)
                    return res

            # Now changing the exprtoken value with
            # UPPER one (so less risk of problem...
            expr = expr.replace(token, token.upper())

        #print "Final expression:", expr
        try:
            evaluation = eval(expr)
        except SyntaxError:
            print "The syntax of %s is invalid" % original_expr
            return res
        except NameError:
            print "There is a unknow name in %s" % original_expr
            return res
        #print "Evaluation :", evaluation

        # In evaluation we can have multiples values because
        # of , (so it make a tuple in fact)
        # we must OR them in the result
        if ',' in expr:
            for part in evaluation:
                # Maybe we gotthe '*' member group, if so, take all
                # hosts instead
                if part == set('*'):
                    res.extend(list(self.get_all_host_names_set(hosts)))
                else:
                    #print "PART", part
                    res.extend(list(part))
        else: # no , so we do not have a tuple but a simple uniq set
            if evaluation == set('*'):
                res.extend(list(self.get_all_host_names_set(hosts)))
            else:
                res.extend(list(evaluation))
        res_string = ','.join(res)
        #print "Final resolution is", res_string
        return res_string
Beispiel #36
0
    def evaluate_hostgroup_expression(self, expr, hosts, hostgroups):
        res = []
        original_expr = expr
        #print "I'm trying to prepare the expression", expr

        #We've got problem with the "-" sign. It can be in a
        #valid name but it's a sign of difference for sets
        #so we change the - now by something, then we reverse after
        if '-' in expr:
            expr = expr.replace('-', 'MINUSSIGN')

        # ! (not) should be changed as "ALL-" (all but not...)
        if '!' in expr:
            ALLELEMENTS = self.get_all_host_names_set(hosts)
            #print "Changing ! by ALLELEMENTS- in ", expr
            expr = expr.replace('!', 'ALLELEMENTS-')

        # We change all separaton token by 10 spaces
        # (so names can still have some spaces
        # on them like Europe Servers because we wil cut byy this 10spaces after
        strip_expr = expr
        for c in ['|', '&', '(', ')', ',', '-']:
            strip_expr = strip_expr.replace(c, ' ' * 10)
        #print "Stripped expression:", strip_expr

        tokens = strip_expr.split(' ' * 10)
        # Strip and non void token
        tokens = [token.strip() for token in tokens if token != '']
        #print "Tokens:", tokens

        #Now add in locals() dict (yes, real variables!)
        for token in tokens:
            # ALLELEMENTS is a private group for us
            if token != 'ALLELEMENTS':
                #Maybe the token was with - at the begining,
                #but we change all by "MINUSSIGN". We must change it back now
                #for the search
                if 'MINUSSIGN' in token:
                    tmp_token = token.replace('MINUSSIGN', '-')
                    members = hostgroups.get_members_by_name(tmp_token)
                else:
                    members = hostgroups.get_members_by_name(token)

                if members != []:
                    #print "Get members", members
                    elts = members.split(',')
                    elts = strip_and_uniq(elts)
                    elts = set(elts)
                    #print "Elements:", elts
                    #print "Now set in locals the token new values"
                    locals()[token.upper()] = elts
                else:
                    if 'MINUSSIGN' in token:
                        token = token.replace('MINUSSIGN', '-')
                    print self.__dict__, type(self)
                    err = "ERROR: the group %s is unknown !" % (token, )
                    print err
                    self.configuration_errors.append(err)
                    return res

            # Now changing the exprtoken value with
            # UPPER one (so less risk of problem...
            expr = expr.replace(token, token.upper())

        #print "Final expression:", expr
        try:
            evaluation = eval(expr)
        except SyntaxError:
            print "The syntax of %s is invalid" % original_expr
            return res
        except NameError:
            print "There is a unknow name in %s" % original_expr
            return res
        #print "Evaluation :", evaluation

        # In evaluation we can have multiples values because
        # of , (so it make a tuple in fact)
        # we must OR them in the result
        if ',' in expr:
            for part in evaluation:
                # Maybe we gotthe '*' member group, if so, take all
                # hosts instead
                if part == set('*'):
                    res.extend(list(self.get_all_host_names_set(hosts)))
                else:
                    #print "PART", part
                    res.extend(list(part))
        else:  # no , so we do not have a tuple but a simple uniq set
            if evaluation == set('*'):
                res.extend(list(self.get_all_host_names_set(hosts)))
            else:
                res.extend(list(evaluation))
        res_string = ','.join(res)
        #print "Final resolution is", res_string
        return res_string