And the documentation wiki. I hope that you can help me to correct my very
bad english.

== Rem something after add ==
Sometimes you need to simply remove a property that conflicts with a new
one. For example, some routers are derived from linux system but does not
work with the linux template. That's why you can say a property should be
"remove" by using the character "-" before it.

For exemple we want to add the "router-os" template but not the "linux"
template on the host and do not remove previously inserted values.

<code>
define discoveryrule {
       discoveryrule_name       Ftp
       creation_type            host
       openports                ^21$
       +use                     ftp
  }

define discoveryrule {
       discoveryrule_name       Http
       creation_type            host
       openports                ^21$
       +use                     http
  }

define discoveryrule {
       discoveryrule_name       Linux
       creation_type            host
       os                       linux
       +use                     linux
}

define discoveryrule {
       discoveryrule_name       RouterOS
       creation_type            host
       macvendor                routerboard
       +use                     router-os
       -use                     linux
}
</code>

If both ports are open, os detected is linux and the macvendor is
routerboard it will create an host with:
<code>
define host {
    host_name   myrouter
    use         ftp,http,router-os
}
</code>

2012/10/26 Joel Ramat <jra...@gmail.com>

> I open an issue and past my diff in, but i'm not sure it's the good way :/
>
> My diff below
>
> diff --git a/shinken/discovery/discoverymanager.py
> b/shinken/discovery/discoverymanager.py
> index 1396d3d..9ea9884 100755
> --- a/shinken/discovery/discoverymanager.py
> +++ b/shinken/discovery/discoverymanager.py
> @@ -32,6 +32,7 @@ import re
>  import time
>  import copy
>  import random
> +import string
>  # Always initialize random...
>  random.seed(time.time())
>  try:
> @@ -112,22 +113,52 @@ class DiscoveredHost(object):
>              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, add with a ,
> +                # If it's a + (add) property, append
>                  if k.startswith('+'):
> -                    prop = k[1:]
> +                    kprop = k[1:]
>                      # If the d do not already have this prop,
> -                    # just push it
> -                    if not prop in d:
> -                        d[prop] = v
> -                    # oh, must add with a , so
> +                    # create list
> +                    if not kprop in d:
> +                        print 'New prop',kprop
> +                        d[kprop]=[]
> +
> +                elif not k.startswith('-'):
> +                    kprop = k
> +                    if not kprop in d:
> +                        print 'New prop',kprop
>                      else:
> -                        print 'Already got', d[prop], 'add', v
> -                        d[prop] = d[prop] + ',' + v
> -                else:
> -                    d[k] = v
> +                        print 'Prop',kprop,'reset with new value'
> +                    d[kprop]=[]
> +
> +                for prop in string.split(v,','):
> +                    prop=prop.strip()
> +                    #checks that prop does not already exist and adds
> +                    if not prop in d[kprop]:
> +                    if not prop in d[kprop]:
> +                        if len(d[kprop]) > 0:
> +                            print 'Already got', ','.join(d[kprop]),
> 'add', prop
> +                        else:
> +                            print 'Add',prop
> +                        d[kprop].append(prop)
> +
> +            # Now look for - (rem) property
> +            for k,v in r.writing_properties.iteritems():
> +                if k.startswith('-'):
> +                    kprop = k[1:]
> +                    if kprop in d:
> +                        for prop in string.split(v,','):
> +                            prop = prop.strip()
> +                            if prop in d[kprop]:
> +                                print 'Already got', ','.join(d[kprop]),
> 'rem', prop
> +                                d[kprop].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
>
> diff --git a/shinken/objects/discoveryrule.py
> b/shinken/objects/discoveryrule.py
> index 45624ab..26c63ee 100755
> --- a/shinken/objects/discoveryrule.py
> +++ b/shinken/objects/discoveryrule.py
> @@ -88,11 +88,12 @@ class Discoveryrule(MatchingItem):
>          # 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 'del 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 in
> tcls.properties or key.startswith('_'):
> +            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('!'):
>
>
> 2012/10/25 Francois Mikus <fmi...@acktomic.com>
>
>>  Hello,
>>
>> Thank you for your development time.
>>
>> You can submit patches in two ways:
>>
>> Method 1: Changes once in a while
>> 1 - Install git
>> 2 - git clone https://github.com/naparuba/shinken.git
>> 3 - copy your changes into the clone
>> 4 - git -a -m "Add: Super changes"
>> 5 - git diff
>> 6 - Open an issue and paste your diff (or put a link to the git diff
>> files in paste bin)
>>
>> Method 2: Many changes in the pipeline
>>
>> 1 - Create a github account
>> 2 - Fork shinken
>> 3 - Install git on a linux workstation/server
>> 4 - git clone your github forked shinken repository using HTTPS
>> 5 - Configure a remote for (naparuba/shinken) and fetch any changes
>> 6 - Merge any changes from naparuba/shinken and then commit/push them
>> back to your github fork
>> 7 - You are now up to date
>> 8 - Switch to a branch
>> 9 - Do your changes
>> 10 - Run the test suite (and add new tests...)
>> 11 - commit and push back to your github
>> 12 - In naparuba/shinken do a pull request against your branch
>> 13 - Rule the world!
>>
>> I would suggest you use Method 1. And create an issue with a git diff.
>>
>> xkilian
>>
>>
>>
>> On 12-10-24 8:18 PM, Joel Ramat wrote:
>>
>> 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 
>> listShinken-devel@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/shinken-devel
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> 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
>>
>>
>
------------------------------------------------------------------------------
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