Hello Shinken developers,

I write a patch to add '-use' feature on discovery rules like this :

define discoveryrule {
       discoveryrule_name       RouterOS
       creation_type            host
       macvendor                routerboard.com
       openports                ^8291$
       +use                     http,router-os
       -use                      linux
}

This patch add an other feature, he do not duplicate properties in the
final stage like this :

define discoveryrule {
       discoveryrule_name       RouterOS
       creation_type            host
       macvendor                routerboard.com
       openports                ^8291$
       +use                     http,router-os,router-os
       -use                      linux
}

define host {
  use   ftp,http,dns,ssh,router-os,generic-host
  host_name   172.17.17.1
  address   172.17.17.1
}

I do not know how to offer you the patch, I give you below the mods done :

In file  /usr/local/shinken/shinken/discovery/discoverymanager.py

Add 'import string'

and change the function update_properties()

    def update_properties(self, final_phase=False):
        d = {}
        if final_phase:
            for (k,v) in self.data.iteritems():
                if k.startswith('_'):
                    d[k] = v
        else:
            d = copy.copy(self.data)

        d['host_name'] = self.name
        # Set address directive if an ip exists
        if self.data.has_key('ip'):
            d['address'] = self.data['ip']

        self.matched_rules.sort(by_order)

        for r in self.matched_rules:
            for k,v in r.writing_properties.iteritems():
                # If it's a + (add) property, append
                if k.startswith('+'):
                    lprop = k[1:]
                    # If the d do not already have this prop,
                    # create list and append it
                    if not lprop in d:
                        print 'New prop',lprop
                        d[lprop]=[]
                        for prop in string.split(v,','):
                            #check for no duplicate prop and append
                            if not prop in d[lprop]:
                                d[lprop].append(prop)
                    # oh, just append
                    else:
                        for prop in string.split(v,','):
                            #check for no duplicate prop and append
                            if not prop in d[lprop]:
                                print 'Already got', ','.join(d[lprop]),
'add', prop
                                d[lprop].append(prop)
                elif not k.startswith('-'):
                    lprop = k
                    if not lprop in d:
                        print 'New prop',lprop
                    else:
                        print 'Prop',lprop,'reset with new value'
                    d[lprop]=[]
                    for prop in string.split(v,','):
                        #check for no duplicate prop and append
                        if not prop in d[lprop]:
                            d[lprop].append(prop)

            # Now look for - (rem) property
            for k,v in r.writing_properties.iteritems():
                if k.startswith('-'):
                    lprop = k[1:]
                    if lprop in d:
                        for prop in string.split(v,','):
                            if prop in d[lprop]:
                                print 'Already got', ','.join(d[lprop]),
'rem', prop
                                d[lprop].remove(prop)

        # Change join prop list in string with a ',' separator
        for (k,v) in d.iteritems():
            if type(d[k]).__name__=='list':
               d[k]=','.join(d[k])

        self.properties = d
        print 'Update our properties', self.name, d

        # For macro-resolving, we should have our macros too
        self.customs = {}
        for (k,v) in self.properties.iteritems():
            self.customs['_'+k.upper()] = v

In file shinken/objects/discoveryrule.py

        # In my own property:
        #  -> in __dict__
        # In the properties of the 'creation_type' Class:
        #  -> in self.writing_properties
        # if not, in matches or not match (if key starts
        # with a !, it's a not rule)
        # -> in self.matches or self.not_matches
        # in writing properties if start with + (means 'add this')
        # in writing properties if start with - (means 'rem this')
        for key in params:
            # Some key are quite special
            if key in cls.properties:
                setattr(self, key, params[key])
            elif key in ['use'] or key.startswith('+') or
key.startswith('-') or key in tcls.properties or key.startswith('_'):
                self.writing_properties[key] = params[key]
            else:
                if key.startswith('!'):
                    key = key.split('!')[1]
                    self.not_matches[key] = params['!' + key]
                else:
                    self.matches[key] = params[key]


I'm not python developer (it's my first time with it) and i speak english
very bad (sorry).

I await your comments...

6jo6jo
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Shinken-devel mailing list
Shinken-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/shinken-devel

Reply via email to