[Pkg-nagios-changes] [SCM] UNNAMED PROJECT branch, debian/master, updated. 810edbdd3feedbfe37f4a65bee50b57b2f60fa2a

Naparuba naparuba at gmail.com
Tue Feb 28 22:13:33 UTC 2012


The following commit has been merged in the debian/master branch:
commit 65266b721e377b5ab16d4f1d65b1cb252a069680
Author: Naparuba <naparuba at gmail.com>
Date:   Fri Jan 13 15:20:14 2012 +0100

    Add : mongodb insert capabilities for the discovery.

diff --git a/bin/shinken-discovery b/bin/shinken-discovery
index 5c4cef9..8a344d3 100755
--- a/bin/shinken-discovery
+++ b/bin/shinken-discovery
@@ -41,7 +41,7 @@ except ImportError:
 from shinken.discovery.discoverymanager import DiscoveryManager
 
 
-VERSION = '0.1'
+VERSION = '0.8.5+'
 if os.name != 'nt': 
     DEFAULT_CFG = '/etc/shinken/discovery.cfg'
 else:
@@ -61,6 +61,8 @@ parser.add_option('-r', '--runners', dest="runners",
                   help="List of runners you allow to run, (like nmap,vsphere)")
 parser.add_option('-m', '--macros', dest="macros",
                   help="List of macros (like NMAPTARGETS=192.168.0.0/24). Should be the last argument")
+parser.add_option('--db', dest="dbmod",
+                  help="Optionnal : Name of the database module to use.")
 
 
 
@@ -71,21 +73,26 @@ macros=[]
 
 if not opts.cfg_input:
     if not os.path.exists(DEFAULT_CFG):
-        parser.error("Requires a discovery configuration file, discovery.cfg (option -c/--cfg-input")
+        parser.error("Requires a discovery configuration file, discovery.cfg (option -c/--cfg-input)")
     else : # take the default file
         opts.cfg_input = DEFAULT_CFG
-if not opts.output_dir:
-    parser.error("Requires one output directory (option -o/--dir-output")
+
+if not opts.output_dir and not opts.dbmod:
+    parser.error("Requires one output directory (option -o/--dir-output) or database output (option --db)")
+
 if not opts.overwrite:
     overwrite = False
 else:
     overwrite = opts.overwrite
+
 if not opts.runners:
     runners = ['*']
 else:
     runners = opts.runners.split(',')
+
 if opts.macros:
     raw_macros.append(opts.macros)
+
 if args:
     raw_macros.extend(args)
 
@@ -116,25 +123,26 @@ print "Got macros", macros
 #                self.parents.append(h.get_name())
         
 
-        
-
-        
-
-
 cfg_input = opts.cfg_input
 output_dir = opts.output_dir
 overwrite = opts.overwrite
-d = DiscoveryManager(cfg_input, output_dir, macros, overwrite, runners)
+dbmod = opts.dbmod
 
-d.launch_runners()
+# Get the Manager for all of the discovery thing
+d = DiscoveryManager(cfg_input, macros, overwrite, runners, output_dir=output_dir, dbmod=dbmod)
 
+# #Ok, let start the plugins that will give us the data
+d.launch_runners()
 d.wait_for_runners_ends()
 
+# We get the results, now we can reap the data
 d.get_runners_outputs()
 
+# and parse them
 d.read_disco_buf()
 
 # Now look for rules
 d.match_rules()
 
+# Ok, we know what to create, now do it!
 d.write_config()
diff --git a/etc/discovery.cfg b/etc/discovery.cfg
index 35850e3..47d47f2 100644
--- a/etc/discovery.cfg
+++ b/etc/discovery.cfg
@@ -12,6 +12,9 @@ cfg_dir=packs
 cfg_file=discovery_rules.cfg
 cfg_file=discovery_runs.cfg
 
+# Load modules, for possible bdd connection
+cfg_file=shinken-specific.cfg
+
 # Load all commands that will be used
 cfg_file=commands.cfg
 
diff --git a/etc/shinken-specific.cfg b/etc/shinken-specific.cfg
index 15a81b2..b0b4a98 100755
--- a/etc/shinken-specific.cfg
+++ b/etc/shinken-specific.cfg
@@ -484,6 +484,15 @@ define module{
 }
 
 
+# Read objects in a mongodb database
+define module{
+       module_name      Mongodb
+       module_type      mongodb
+       server		localhost
+       database		shinken
+}
+
+
 #You know NSCA? You can send check results to Shinken
 #using send_nsca command
 define module{
diff --git a/shinken/discovery/discoverymanager.py b/shinken/discovery/discoverymanager.py
index c418b0c..e7b746d 100644
--- a/shinken/discovery/discoverymanager.py
+++ b/shinken/discovery/discoverymanager.py
@@ -26,18 +26,25 @@ import os
 import re
 import time
 
+try:
+    from pymongo.connection import Connection
+except ImportError:
+    Connection = None
+
 from shinken.log import logger
 from shinken.objects import *
 from shinken.macroresolver import MacroResolver
 
 
 class DiscoveryManager:
-    def __init__(self, path, output_dir, macros, overwrite, runners):
+    def __init__(self, path, macros, overwrite, runners, output_dir=None, dbmod='', db_direct_insert=False):
         # i am arbiter-like
         self.log = logger
         self.overwrite = overwrite
         self.runners = runners
         self.output_dir = output_dir
+        self.dbmod = dbmod
+        self.db_direct_insert = db_direct_insert
         self.log.load_obj(self)
         self.config_files = [path]
         self.conf = Config()
@@ -78,10 +85,35 @@ class DiscoveryManager:
         # Hash = name, and in it rules that apply
         self.disco_matches = {}
 
+        self.init_database()
+
 
     def add(self, obj):
         pass
 
+    # We try to init the database connection
+    def init_database(self):
+        self.dbconnection = None
+        self.db = None
+
+        if self.dbmod == '':
+            return
+
+        for mod in self.conf.modules:
+            if getattr(mod, 'module_name', '') == self.dbmod:
+                if Connection is None:
+                    print "ERROR : cannot use Mongodb database : please install the pymongo librairy"
+                    break
+                # Now try to connect
+                try:
+                    server = mod.server
+                    database = mod.database
+                    self.dbconnection = Connection(server)
+                    self.db = getattr(self.dbconnection, 'database')
+                    print "Connection to Mongodb:%s:%s is OK" % (server, database)
+                except Exception, exp:
+                    logger.log('Error in database init : %s' % exp)
+
     # Look if the name is a IPV4 address or not
     def is_ipv4_addr(self, name):
         p = r"^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$"
@@ -108,26 +140,25 @@ class DiscoveryManager:
             if self.conf.strip_idname_fqdn:
                 if not self.is_ipv4_addr(name):
                     name = name.split('.', 1)[0]
-
+            
             data = '::'.join(elts[1:])
-
-            #print "Name", name
-            #print "data", data
+            
             # Register the name
             if not name in self.disco_data:
-                self.disco_data[name] = []
+                self.disco_data[name] = {}
+
             # Now get key,values
             if not '=' in data:
-                #print "Bad discovery data"
                 continue
+
             elts = data.split('=')
             if len(elts) <= 1:
-                #print "Bad discovery data"
                 continue
+
             key = elts[0].strip()
             value = elts[1].strip()
             print "-->", name, key, value
-            self.disco_data[name].append((key, value))
+            self.disco_data[name][key] = value
 
 
     def match_rules(self):
@@ -235,10 +266,17 @@ class DiscoveryManager:
                         d[prop] = d[prop] + ',' + v
                 else:
                     d[k] = v
-            print "current D", d
-        print "Will generate", d
-        self.write_host_config_to_file(host, d)
-
+        print "Will generate an host", d
+        
+        # Maybe we do not got a directory output, but
+        # a bdd one.
+        if self.output_dir:
+            self.write_host_config_to_file(host, d)
+
+        # Maybe we want a database insert
+        if self.db:
+            self.write_host_config_to_db(host, d)
+            
         
     # Will wrote all properties/values of d for the host
     # in the file
@@ -291,7 +329,11 @@ class DiscoveryManager:
             for r in rules:
                 d.update(r.writing_properties)
             print "Generating", desc, d
-            self.write_service_config_to_file(host, desc, d)
+
+            # Maybe we do not got a directory output, but
+            # a bdd one.
+            if self.output_dir:
+                self.write_service_config_to_file(host, desc, d)
 
 
     # Will wrote all properties/values of d for the host
@@ -331,3 +373,24 @@ class DiscoveryManager:
         return '\n'.join(tab)
         
 
+
+    # Will wrote all properties/values of d for the host
+    # in the database
+    def write_host_config_to_db(self, host, d):
+        table = None
+        # Maybe we directly insert/enable the hosts,
+        # or in the SkonfUI we want to go with an intermediate
+        # table to select/enable only some
+        if self.db_direct_insert:
+            table = self.db.hosts
+        else:
+            table = self.db.discovered_hosts
+        cur = table.find({'host_name': host})
+        exists = cur.count() > 0
+        if exists and not self.overwrite:
+            print "The host '%s' already exists in the database table %s" % (host, table)
+            return
+
+        print "Saving in database", d
+        table.save(d)
+        print "saved"
diff --git a/shinken/objects/discoveryrule.py b/shinken/objects/discoveryrule.py
index 3ebf7bd..abe8ff4 100644
--- a/shinken/objects/discoveryrule.py
+++ b/shinken/objects/discoveryrule.py
@@ -165,7 +165,7 @@ class Discoveryrule(Item):
         for m in self.matches:
             #print "Compare to", m
             match_one = False
-            for (k, v) in datas:
+            for (k, v) in datas.iteritems():
                 # We found at least one of our match key
                 if m == k:
                     if self.is_matching(k, v):
@@ -183,7 +183,7 @@ class Discoveryrule(Item):
         for m in self.not_matches:
             #print "Compare to NOT", m
             match_one = False
-            for (k, v) in datas:
+            for (k, v) in datas.iteritems():
                 #print "K,V", k,v
                 # We found at least one of our match key
                 if m == k:

-- 
UNNAMED PROJECT



More information about the Pkg-nagios-changes mailing list