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

Naparuba naparuba at gmail.com
Tue Feb 28 22:14:41 UTC 2012


The following commit has been merged in the debian/master branch:
commit d785208ef5fd60e842c3a22fd3d1e395c036a819
Author: Naparuba <naparuba at gmail.com>
Date:   Wed Jan 18 14:16:54 2012 +0100

    Add : plug the form for the discovery of new hosts with nmap and the workers.

diff --git a/bin/shinken-discovery b/bin/shinken-discovery
index 8a344d3..192ef83 100755
--- a/bin/shinken-discovery
+++ b/bin/shinken-discovery
@@ -128,6 +128,8 @@ output_dir = opts.output_dir
 overwrite = opts.overwrite
 dbmod = opts.dbmod
 
+
+
 # Get the Manager for all of the discovery thing
 d = DiscoveryManager(cfg_input, macros, overwrite, runners, output_dir=output_dir, dbmod=dbmod)
 
diff --git a/etc/shinken-specific.cfg b/etc/shinken-specific.cfg
index b0b4a98..72f8902 100755
--- a/etc/shinken-specific.cfg
+++ b/etc/shinken-specific.cfg
@@ -500,7 +500,7 @@ define module{
        module_type       nsca_server
        host              *
        port              5667
-       encryption_method 0
+       encryption_method 1
        password          helloworld
 }
 
@@ -612,6 +612,7 @@ define arbiter{
        spare		0
 #      uncomment the line below if you want to use the GLPI and NSCA modules
 #      modules		CommandFile,GLPIImport, NSCA, VMWare_auto_linking, TSCA
+       modules		NSCA
 
 #      Uncomment these lines in a HA architecture so the master
 #      and slaves know how long they may wait for each other
diff --git a/libexec/nmap_discovery_runner.py b/libexec/nmap_discovery_runner.py
index 6048dce..2588e20 100755
--- a/libexec/nmap_discovery_runner.py
+++ b/libexec/nmap_discovery_runner.py
@@ -44,7 +44,8 @@ parser.add_option('-t', '--targets', dest="targets",
 parser.add_option('-v', '--verbose', dest="verbose", action='store_true',
                   help="Verbose output.")
 parser.add_option('-s', '--simulate', dest="simulate", 
-                  help="Silulate a launch by reading an nmap XML output instead of launching a new one.")
+                  help="Simulate a launch by reading an nmap XML output instead of launching a new one.")
+
 
 targets = []
 opts, args = parser.parse_args()
@@ -65,7 +66,6 @@ if not opts.verbose:
 else:
     verbose = True
 
-
 if args:
     targets.extend(args)
 
diff --git a/libexec/nsca_client.py b/libexec/nsca_client.py
index 76e551d..ba0a103 100644
--- a/libexec/nsca_client.py
+++ b/libexec/nsca_client.py
@@ -180,5 +180,5 @@ class NSCA_client():
 
 
 
-nsca = NSCA_client('localhost', 5667, 1, 'toto')
+nsca = NSCA_client('localhost', 5667, 1, 'helloworld')
 nsca.main()
diff --git a/shinken/daemons/skonfdaemon.py b/shinken/daemons/skonfdaemon.py
index b934c7b..c0ced78 100644
--- a/shinken/daemons/skonfdaemon.py
+++ b/shinken/daemons/skonfdaemon.py
@@ -45,6 +45,8 @@ from shinken.brok import Brok
 from shinken.external_command import ExternalCommand
 from shinken.util import safe_print
 from shinken.skonfuiworker import SkonfUIWorker
+from shinken.message import Message
+
 
 # Now the bottle HTTP part :)
 from shinken.webui.bottle import Bottle, run, static_file, view, route, request, response
@@ -173,6 +175,7 @@ class Skonf(Daemon):
         self.workers = {}   # dict of active workers
 
 
+
     # Use for adding things like broks
     def add(self, b):
         if isinstance(b, Brok):
@@ -446,6 +449,22 @@ class Skonf(Daemon):
                 self.returns_queue = Queue()
             else:
                 self.returns_queue = self.manager.Queue()
+
+            # create the input queue of all workers
+            try:
+               if is_android:
+                  self.workers_queue = Queue()
+               else:
+                  self.workers_queue = self.manager.Queue()
+            # If we got no /dev/shm on linux, we can got problem here. 
+            # Must raise with a good message
+            except OSError, exp:
+               # We look for the "Function not implemented" under Linux
+               if exp.errno == 38 and os.name == 'posix':
+                  logger.log("ERROR : get an exception (%s). If you are under Linux, please check that your /dev/shm directory exists." % (str(exp)))
+                  raise
+
+
                 
             # For multiprocess things, we should not have
             # sockettimeouts. will be set explicitly in Pyro calls
@@ -847,23 +866,9 @@ class Skonf(Daemon):
 
     # Create and launch a new worker, and put it into self.workers
     def create_and_launch_worker(self):
-        # ceate the input queue of this worker
-        try:
-           if is_android:
-              q = Queue()
-           else:
-              q = self.manager.Queue()
-        # If we got no /dev/shm on linux, we can got problem here. 
-        # Must raise with a good message
-        except OSError, exp:
-            # We look for the "Function not implemented" under Linux
-            if exp.errno == 38 and os.name == 'posix':
-                logger.log("ERROR : get an exception (%s). If you are under Linux, please check that your /dev/shm directory exists." % (str(exp)))
-            raise
-            
-
-        w = SkonfUIWorker(1, q, self.returns_queue, 1, mortal=False, max_plugins_output_length = 1, target=None)
+        w = SkonfUIWorker(1, self.workers_queue, self.returns_queue, 1, mortal=False, max_plugins_output_length = 1, target=None)
         w.module_name = 'skonfuiworker'
+        w.add_database_data('localhost')
 
         # save this worker
         self.workers[w.id] = w
@@ -887,3 +892,9 @@ class Skonf(Daemon):
     # TODO : code this!
     def check_auth(self, login, password):
        return True
+
+    # We are asking to a worker .. to work :)
+    def ask_new_scan(self, id):
+       msg = Message(id=0, type='ScanAsk', data={'scan_id' : id})
+       print "Creating a Message for ScanAsk", msg
+       self.workers_queue.put(msg)
diff --git a/shinken/discovery/discoverymanager.py b/shinken/discovery/discoverymanager.py
index dccfec2..23233ff 100644
--- a/shinken/discovery/discoverymanager.py
+++ b/shinken/discovery/discoverymanager.py
@@ -196,7 +196,13 @@ class DiscoveryManager:
 
 
     def launch_runners(self):
-        for r in self.allowed_runners():
+        allowed_runners = self.allowed_runners()
+
+        if len(allowed_runners) == 0:
+            print "ERROR : there is no matching runners selected!"
+            return
+
+        for r in allowed_runners:
             print "I'm launching", r.get_name(), "with a %d seconds timeout" % self.conf.runners_timeout
             r.launch(timeout=self.conf.runners_timeout)
 
@@ -227,6 +233,8 @@ class DiscoveryManager:
             print "Got Raw disco data", self.raw_disco_data
         else:
             print "Got no data!"
+            for r in self.allowed_runners():
+                print "DBG", r.current_launch
 
 
     # Write all configuration we've got
diff --git a/shinken/skonfuiworker.py b/shinken/skonfuiworker.py
index e16b381..01cf93a 100644
--- a/shinken/skonfuiworker.py
+++ b/shinken/skonfuiworker.py
@@ -4,6 +4,7 @@
 #     Gerhard Lausser, Gerhard.Lausser at consol.de
 #     Gregory Starck, g.starck at gmail.com
 #     Hartmut Goebel, h.goebel at goebel-consult.de
+#     Andreas Karfusehr, andreas at karfusehr.de
 # 
 # This file is part of Shinken.
 # 
@@ -42,6 +43,12 @@ else:
    from Queue import Queue
    from threading import Thread as Process
 
+# Mongodb lib
+try:
+    from pymongo.connection import Connection
+except ImportError:
+    Connection = None
+
 import time
 import sys
 import signal
@@ -59,13 +66,78 @@ class SkonfUIWorker(Worker):
     _timeout = None
     _c = None
 
+    def add_database_data(self, server):
+       self.database_server = server
+
 
-    def get_new_scan(self):
-       print "I ask for a scan"
-       time.sleep(1)
+    def connect_database(self):
+       con = Connection('localhost')
+       self.db = con.shinken
 
-    def launch_new_scan(self):
+
+    def get_scan_data(self):
+       print "I ask for a scan with the id", self.scan_asked
+       scan_id = self.scan_asked.get('scan_id')
+       # I search the scan entry in the asked_scans table
+       cur = self.db.scans.find({'_id' : scan_id})
+       if cur.count() == 0:
+          print "No scan found with id", scan_id
+          return
+       self.scan = cur[0]
+       
+
+    def launch_scan(self):
        print "I try to launch scan", self.scan
+       scan_id = self.scan.get('_id')
+       nmap = self.scan.get('use_nmap')
+       vmware = self.scan.get('use_vmware')
+       names = self.scan.get('names')
+       state = self.scan.get('state')
+       print "IN SCAN WORKER:",nmap, vmware, names, state
+       
+       # Updating the scan entry state
+       self.db.scans.update({'_id' : scan_id}, { '$set': { 'state' : 'preparing' }})
+       
+       from shinken.discovery.discoverymanager import DiscoveryManager
+       
+       elts = names.splitlines()
+       targets = ' '.join(elts)
+       print "Launching Nmap with targets", targets
+       macros = [('NMAPTARGETS', targets)]
+       overwrite = False
+       runners = ['nmap']
+       output_dir = None
+       dbmod = 'Mongodb'
+
+       
+
+       d = DiscoveryManager('/home/shinken/shinken/etc/discovery.cfg', 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()
+
+       # Set the scan as launched state
+       self.db.scans.update( {'_id' : scan_id}, { '$set': { 'state' : 'launched' }})
+
+       # 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()
+
+       # Set the scan as done :)
+       self.db.scans.update({'_id' : scan_id}, { '$set': { 'state' : 'done' }})
+
+
+
+       
     
 
     # id = id of the worker
@@ -79,10 +151,14 @@ class SkonfUIWorker(Worker):
         if not is_android:
            signal.signal(signal.SIGTERM, signal.SIG_DFL)
         timeout = 1.0
+        self.scan_asked = None
         self.scan = None
         self.returns_queue = returns_queue
         self.s = s
         self.t_each_loop = time.time()
+
+        self.connect_database()
+
         while True:
             begin = time.time()
             msg = None
@@ -91,15 +167,25 @@ class SkonfUIWorker(Worker):
             # If we are dying (big problem!) we do not
             # take new jobs, we just finished the current one
             if not self.i_am_dying:
-                self.get_new_scan()
-                self.launch_new_scan()
+               try:
+                #print "I", self.id, "wait for a message"
+                  msg = self.s.get(block=False)
+                  print "I", self.id, "I've got a message!", msg                  
+                  if msg is not None and msg.get_type() == 'ScanAsk':
+                     self.scan_asked = msg.get_data()
+                     self.get_scan_data()
+                     self.launch_scan()
+               except Empty , exp:
+                  print "UI worker go to sleep", self.id
+                  time.sleep(1)
 
             # Now get order from master
             try:
-                cmsg = c.get(block=False)
-                if cmsg.get_type() == 'Die':
-                   print "[%d]Dad say we are dying..." % self.id
-                   break
+               cmsg = c.get(block=False)
+               if cmsg.get_type() == 'Die':
+                  print "[%d]Dad say we are dying..." % self.id
+                  break
+               
             except :
                 pass
 
diff --git a/shinken/webui/plugins_skonf/newhosts/newhosts.py b/shinken/webui/plugins_skonf/newhosts/newhosts.py
index e8e69ed..02e1bad 100644
--- a/shinken/webui/plugins_skonf/newhosts/newhosts.py
+++ b/shinken/webui/plugins_skonf/newhosts/newhosts.py
@@ -21,11 +21,16 @@
 #You should have received a copy of the GNU Affero General Public License
 #along with Shinken.  If not, see <http://www.gnu.org/licenses/>.
 
+import time
+import random
 
 from shinken.webui.bottle import redirect
 from shinken.util import to_bool
 
 
+# Ask for a random init
+random.seed(time.time())
+
 
 ### Will be populated by the UI with it's own value
 app = None
@@ -59,6 +64,15 @@ def get_launch():
     print 'nmap?', use_nmap
     print 'vmware?', use_vmware
 
+    # We are putting a ask ask in the database
+    i = random.randint(1, 65535)
+    scan_ask = {'_id' : i, 'names' : names, 'use_nmap' : use_nmap, 'use_vmware' : use_vmware, 'state' : 'pending', 'creation' : int(time.time())}
+    print "Saving", scan_ask, "in", app.db.scans
+    r = app.db.scans.save(scan_ask)
+    # We just want the id as string, not the object
+    print "We create the scan", i
+    app.ask_new_scan(i)
+
     return {'app' : app}
 
 
@@ -67,11 +81,19 @@ def get_scans():
     print "Got scans"
     return {'app' : app}
 
+
 def get_results():
     print "Looking for hosts in pending aprouval"
     cur = app.db.discovered_hosts.find({})
     pending_hosts = [h for h in cur]
-    return {'app' : app, 'pending_hosts' : pending_hosts}
+
+    print "And in progress scans"
+    cur = app.db.scans.find({})
+    scans = [s for s in cur]
+    for s in scans:
+        print "SCAN", s
+
+    return {'app' : app, 'pending_hosts' : pending_hosts, 'scans' : scans}
 
 
 # This is the dict teh webui will try to "load".
diff --git a/shinken/webui/plugins_skonf/newhosts/views/newhosts_results.tpl b/shinken/webui/plugins_skonf/newhosts/views/newhosts_results.tpl
index e89c623..fd0a517 100644
--- a/shinken/webui/plugins_skonf/newhosts/views/newhosts_results.tpl
+++ b/shinken/webui/plugins_skonf/newhosts/views/newhosts_results.tpl
@@ -2,7 +2,13 @@
 %rebase layout_skonf globals()
 <div> <h1> Discover your new hosts </h1> </div>
 
-Here are the results :
+<p>Here are the scans :</p>
+%for s in scans:
+     <br/>{{s}}
+%end
+
+<p>Here are the results :</p>
 
 %for h in pending_hosts:
-     <br/>{{h}}
\ No newline at end of file
+     <br/>{{h}}
+%end
\ No newline at end of file

-- 
UNNAMED PROJECT



More information about the Pkg-nagios-changes mailing list