def main()

in src/kibble-scanner.py [0:0]


def main():
    pprint("Kibble Scanner v/%s starting" % VERSION)
    global CONFIG_FILE, PENDING_OBJECTS
    args = base_parser().parse_args()
    
    # Load config yaml
    if args.config:
        CONFIG_FILE = args.config
    config = yaml.load(open(CONFIG_FILE))
    pprint("Loaded YAML config from %s" % CONFIG_FILE)
    
    # Which broker type do we use here?
    broker = None
    if 'elasticsearch' in config and config['elasticsearch'].get('enabled', False):
        pprint("Using direct ElasticSearch broker model")
        broker = plugins.brokers.kibbleES.Broker(config)
    else:
        pprint("Using HTTP JSON broker model")
        broker = plugins.brokers.kibbleJSON.Broker(config)
    
    orgNo = 0
    sourceNo = 0
    for org in broker.organisations():
        if not args.org or args.org == org.id:
            pprint("Processing organisation %s" % org.id)
            orgNo += 1
            
            # Compile source list
            # If --age is passed, only append source that either
            # have never been scanned, or have been scanned more than
            # N hours ago by any scanner.
            if args.age:
                minAge = time.time() - int(args.age) * 3600
                for source in org.sources(view=args.view):
                    tooNew = False
                    if 'steps' in source:
                        for key, step in source['steps'].items():
                            if 'time' in step and step['time'] >= minAge:
                                tooNew = True
                                break
                    if not tooNew:
                        if not args.source or (args.source == source['sourceID']):
                            PENDING_OBJECTS.append(source)
            else:
                PENDING_OBJECTS = []
                for source in org.sources(view=args.view):
                    if not args.source or (args.source == source['sourceID']):
                        PENDING_OBJECTS.append(source)
                sourceNo += len(PENDING_OBJECTS)
            
            # Start up some threads equal to number of cores on the box,
            # but no more than 4. We don't want an IOWait nightmare.
            threads = []
            core_count = min((4, int( multiprocessing.cpu_count() )))
            for i in range(0, core_count):
                sThread = scanThread(broker, org, i+1, args.type, args.exclude)
                sThread.start()
                threads.append(sThread)
            
            # Wait for them all to finish.
            for t in threads:
                t.join()
        
    pprint("All done scanning for now, found %i organisations and %i sources to process." % (orgNo, sourceNo))