Commit 15eb09f6 authored by Alexandre Dulaunoy's avatar Alexandre Dulaunoy
Browse files

Replace syslog modules to logging module (to support Windows)

This is just a quick replacement of syslog to support Windows
platform. Potential fix for #143.
parent 7fea47c9
Loading
Loading
Loading
Loading
+21 −19
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
#
# Updater script of CVE/CPE database
#
# Copyright (c) 2012-2014 	Alexandre Dulaunoy - a@foo.be
# Copyright (c) 2012-2016 	Alexandre Dulaunoy - a@foo.be
# Copyright (c) 2014-2015 	Pieter-Jan Moreels - pieterjan.moreels@gmail.com

# Imports
@@ -14,9 +14,9 @@ sys.path.append(os.path.join(runPath, ".."))

import shlex
import subprocess
import syslog
import argparse
import time
import logging

from lib.Config import Configuration
import lib.DatabaseLayer as db
@@ -34,7 +34,7 @@ posts = [{'name': "ensureindex",
          'updater': "python3 " + os.path.join(runPath, "db_mgmt_create_index.py")}]

argParser = argparse.ArgumentParser(description='Database updater for cve-search')
argParser.add_argument('-v', action='store_true', help='Logging on stdout (default is syslog)')
argParser.add_argument('-v', action='store_true', help='Logging on stdout')
argParser.add_argument('-l', action='store_true', help='Running at regular interval', default=False)
argParser.add_argument('-i', action='store_true', help='Indexing new cves entries in the fulltext indexer', default=False)
argParser.add_argument('-c', action='store_true', help='Enable CPE redis cache', default=False)
@@ -67,6 +67,8 @@ if not args.p:
    sources.extend([{'name': 'misp',
                     'updater': "python3 " + os.path.join(runPath, "db_mgmt_misp.py")}])

if not args.v:
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
if args.f and args.l:
    print ("Drop collections and running in loop should not be used.")
    argParser.print_help()
@@ -83,26 +85,26 @@ def dropcollection(collection=None):
        return False
    return db.dropCollection(collection)

def logging(message=""):
def log(message=""):
    if args.o:
        with open(Configuration.getUpdateLogFile(), "a") as log:
            log .write(message + "\n")
    if args.v:
        print (message)
    else:
        syslog.syslog(message)
        logging.info(message)

loop = True

if args.f:
    logging("Dropping metadata")
    log("Dropping metadata")
    dropcollection("info")

while (loop):
    if args.v:
        logging("==========================")
        logging(time.strftime("%a %d %B %Y %H:%M", time.gmtime()))
        logging("==========================")
        log("==========================")
        log(time.strftime("%a %d %B %Y %H:%M", time.gmtime()))
        log("==========================")
    if not args.l:
        loop = False
    newelement = 0
@@ -110,15 +112,15 @@ while (loop):
        if not Configuration.includesFeed(source['name']):
            continue
        if args.f and source['name'] is not "redis-cache-cpe":
            logging("Dropping collection: " + source['name'])
            log("Dropping collection: " + source['name'])
            dropcollection(collection=source['name'])
            logging( source['name'] + " dropped")
            log( source['name'] + " dropped")
        if source['name'] is "cpeother":
            if "cpeother" not in db.getTableNames():
                continue
        if source['name'] is not "redis-cache-cpe":
            message = 'Starting ' + source['name']
            logging(message)
            log(message)
            before = nbelement(collection=source['name'])
            if args.f and source['name'] is "cves":
                updater = "python3 " + os.path.join(runPath, "db_mgmt.py -p")
@@ -128,20 +130,20 @@ while (loop):
            after = nbelement(collection=source['name'])
            message = source['name'] + " has " + str(after) + " elements (" + str(after - before) + " update)"
            newelement = str(after - before)
            logging(message)
            log(message)
        elif (args.c is True and source['name'] is "redis-cache-cpe"):
            message = 'Starting ' + source['name']
            logging(message)
            log(message)
            subprocess.Popen((shlex.split(source['updater']))).wait()
            message = source['name'] + " updated"
            logging(message)
            log(message)
    for post in posts:
        message = 'Starting ' + post['name']
        logging(message)
        log(message)
        subprocess.Popen((shlex.split(post['updater']))).wait()
    if args.i and int(newelement) > 0:
        subprocess.Popen((shlex.split("python3 " + os.path.join(runPath, "db_fulltext.py -v -l" + newelement)))).wait()
    if args.l is not False:
        logging("Sleeping...")
        log("Sleeping...")
        time.sleep(3600)
    logging()
    log()