Example #1
0
def minimal_logger(namespace, extra_fields=None, debug=False):
    """Make and return a minimal console logger.

    NOTE: this does apparently *not* work with logbook as I first thought, and
    log handlers will *not* take care of output. If something is to be
    logged to a file in a module, the logger has to be implemented for
    that particular purpose.

    The current function is copied from cement.core.backend.

    :param namespace: namspace of logger
    """
    config = cf.load_config()
    log = logbook.Logger(namespace, level=logbook.INFO)
    s_h = logbook.StreamHandler(sys.stdout, level=logbook.INFO, bubble=True)
    log.handlers.append(s_h)
    try:
        host = config.get('log', 'redis_host')
        port = config.getint('log', 'redis_port')
        key = config.get('log', 'redis_key')
        password = config.get('log', 'redis_password')
        if not extra_fields:
            extra_fields = {"program": "pm", "command": namespace}
        r_h = RedisHandler(host=host,
                           port=port,
                           key=key,
                           password=password,
                           extra_fields=extra_fields,
                           level=logbook.INFO,
                           bubble=True)
        log.handlers.append(r_h)
    except:
        log.debug('Not loading RedisHandler')
        pass

    # FIX ME: really don't want to hard check sys.argv like this but
    # can't figure any better way get logging started (only for debug)
    # before the app logging is setup. Besides, this will fail for
    # tests since sys.argv will consist of the test call arguments.
    if '--debug' in sys.argv or debug:
        try:
            #If there was any problem loading the RedisHandler, at this point
            #the variable r_h will not exist
            r_h.level = logbook.DEBUG
        except UnboundLocalError:
            pass
        s_h.level = logbook.DEBUG
        log.level = logbook.DEBUG

    return log
Example #2
0
def minimal_logger(namespace, extra_fields=None, debug=False):
    """Make and return a minimal console logger.

    NOTE: this does apparently *not* work with logbook as I first thought, and
    log handlers will *not* take care of output. If something is to be
    logged to a file in a module, the logger has to be implemented for
    that particular purpose.

    The current function is copied from cement.core.backend.

    :param namespace: namspace of logger
    """
    config = cf.load_config()
    log = logbook.Logger(namespace, level=logbook.INFO)
    s_h = logbook.StreamHandler(sys.stdout, level = logbook.INFO, bubble=True)
    log.handlers.append(s_h)
    try:
        host = config.get('log', 'redis_host')
        port = config.getint('log', 'redis_port')
        key = config.get('log', 'redis_key')
        password = config.get('log', 'redis_password')
        if not extra_fields:
            extra_fields = {"program": "pm",
                            "command": namespace}
        r_h = RedisHandler(host=host, port=port, key=key, password=password,
                extra_fields=extra_fields, level=logbook.INFO, bubble=True)
        log.handlers.append(r_h)
    except:
        log.debug('Not loading RedisHandler')
        pass

    # FIX ME: really don't want to hard check sys.argv like this but
    # can't figure any better way get logging started (only for debug)
    # before the app logging is setup. Besides, this will fail for
    # tests since sys.argv will consist of the test call arguments.
    if '--debug' in sys.argv or debug:
        try:
            #If there was any problem loading the RedisHandler, at this point
            #the variable r_h will not exist
            r_h.level = logbook.DEBUG
        except UnboundLocalError:
            pass
        s_h.level = logbook.DEBUG
        log.level = logbook.DEBUG

    return log
Example #3
0
def main():
    dirsizes = {"time": datetime.datetime.now().isoformat(),
                "unit": "bytes",
                "errors": []}

    parser = argparse.ArgumentParser(description="Compute directory size(s) and report them to a CouchDB database")

    parser.add_argument('--dir', dest='root', action='append',
                        help="the directory to calculate dirsizes from")

    parser.add_argument("--server", dest='server', action='store', default="localhost:5984",
                        help="CouchDB instance to connect to, defaults to localhost:5984")

    parser.add_argument("--db", dest='db', action='store', default="tests",
                        help="CouchDB database name, defaults to 'tests'")

    parser.add_argument("--dry-run", dest='dry_run', action='store_true', default=False,
                        help="Do not submit the resulting hash to CouchDB")

    args = parser.parse_args()

    #Import DB credentials from pm.conf
    c = config.load_config()
    try:
        user = c.get('db', 'user')
        password = c.get('db', 'password')
        credentials = (user, password)
    except:
        raise KeyError('Please specify DB credentials in your pm.conf file')


    for r in args.root:  # multiple --dir args provided
        if os.path.exists(r) and os.path.isdir(r):
            for d in os.listdir(r):
                path = os.path.join(r, d)
                try:
                    dirsizes[path] = int(get_dirsizes(path))
                except subprocess.CalledProcessError as pe:
                    dirsizes['errors'].append(pe.output)
        else:
            dirsizes = parse_dirsizes(r, dirsizes)

    if args.dry_run:
        print(dirsizes)
    else:
        send_db(args.server, args.db, credentials, dirsizes)
Example #4
0
def main():
    dirsizes = {
        "time": datetime.datetime.now().isoformat(),
        "unit": "bytes",
        "errors": []
    }

    parser = argparse.ArgumentParser(
        description=
        "Compute directory size(s) and report them to a CouchDB database")

    parser.add_argument('--dir',
                        dest='root',
                        action='append',
                        help="the directory to calculate dirsizes from")

    parser.add_argument(
        "--server",
        dest='server',
        action='store',
        default="localhost:5984",
        help="CouchDB instance to connect to, defaults to localhost:5984")

    parser.add_argument("--db",
                        dest='db',
                        action='store',
                        default="tests",
                        help="CouchDB database name, defaults to 'tests'")

    parser.add_argument("--dry-run",
                        dest='dry_run',
                        action='store_true',
                        default=False,
                        help="Do not submit the resulting hash to CouchDB")

    args = parser.parse_args()

    #Import DB credentials from pm.conf
    c = config.load_config()
    try:
        user = c.get('db', 'user')
        password = c.get('db', 'password')
        credentials = (user, password)
    except:
        raise KeyError('Please specify DB credentials in your pm.conf file')

    for r in args.root:  # multiple --dir args provided
        if os.path.exists(r) and os.path.isdir(r):
            for d in os.listdir(r):
                path = os.path.join(r, d)
                try:
                    dirsizes[path] = int(get_dirsizes(path))
                except subprocess.CalledProcessError as pe:
                    dirsizes['errors'].append(pe.output)
        else:
            dirsizes = parse_dirsizes(r, dirsizes)

    if args.dry_run:
        print(dirsizes)
    else:
        send_db(args.server, args.db, credentials, dirsizes)