示例#1
0
def startpolicing():
    def processserviceupdate( document ):
    #--------------------------------------------------------------------------
        """
        Handle a update of service list.
        
        Will be called by couchDB change notifier if a babbler's service list is altered
        by receiving a service update. New services are simply queued in the supervisor 
        instance and no longer existing services are removed.
        
        :param document: document which has been altered (passed by CouchDBHandler.watchdbthreading).
        """
        
        try:
            services = simplejson.loads(watchDB.read(document))["services"]
            for index in services:
                service = services[index]
                uid = "%s/%s" % (document, index)
                checker.queueservice(uid, service["proto"], service["ipv4"], service["port"], service["timeout"], 200, 180 )
        except KeyboardInterrupt:
            raise
        except:
            traceback.print_exc()
        finally:
            checker.removeobsoleteservices(document)
    #--------------------------------------------------------------------------    
    
    handler = {"HTTP":HTTPService}
    checker = Supervisor(handler)
    
    try:
        resultDB = CouchDBManager("localhost", "5984", "gossip_watchresults")
        watchDB = CouchDBManager("localhost", "5984", "gossip_watchlist")
        watchDB.watchdbthreading(processserviceupdate, lock=checker.servicelock)
    
        while True:
            
            if not checker.isqueueempty():
                wait = int(checker.getnextschedule() - time.time())
            else:
                wait = 30
        
            if wait <= 0:
                checker.checkservice()
            else:
                resultDB.write("results", checker.getresults())
                resultDB.compact()
                time.sleep(wait)
                ssldebug("%d service(s) currently watched..." % checker.getservicecount())
    finally:
        watchDB.shutdown = True
示例#2
0
 def savebabblertodb( babbler ):
 #--------------------------------------------------------------------------
     """
     Save list of babbler to a file called 'crackerbarrel.json'. Certificates
     are stored separatly in a subfolder of 'certs'.
     
     While babbler data is stored in a central file, each certificate is saved
     separately. File is locked during writing process.
     """
     db = CouchDBManager("localhost", "5984", "gossip_crackertable")
     try:
         for identifier in babbler.babblers:
             db.write(identifier, babbler.getbabbler(identifier).contact.tojson())
             db.compact()
             
             if babbler.getbabbler(identifier).x509 != None:
                 babbler.getbabbler(identifier).x509.save("%s/%s.pem" % (Babblemouth.CERTIFICATE_FOLDER, identifier), X509.FORMAT_PEM)
         
     except KeyboardInterrupt:
         raise
     except:
         print "Unable to save babblers"
         traceback.print_exc()