Beispiel #1
0
def init_logging(config):
    """
    Initialize logging based on configuration

    Either the entire config, or the logging subsection must be passed in. If no
    logging `path` parameter is found a :py:class:`~logging.StreamHandler` will
    be initialized instead. If `path` is found, a :py:class:`~logging.handlers.RotatingFileHandler`
    will be initialized, with a default rotation of 10mb, and `backupCount` of 1.

    Args:
        config (dict): Configuration.
    """
    global LOG_MODE, LOG_MAXSIZE, LOG_PATH, LOG_FILENAME, LOG_FILEMODE, LOG_ENCODING

    hdlr = None
    logging = config.get('logging', config)

    LOG_MODE = logging.get('mode', 'ERROR')
    LOG_MAXSIZE = logging.get('maxsize', '10M')
    LOG_PATH = logging.get('path', None)
    LOG_FILEMODE = logging.get('filemode', 'a+').lower()
    LOG_ENCODING = logging.get('encoding', None)

    if LOG_PATH:
        hdlr = RotatingFileHandler(LOG_PATH,
                                   mode=LOG_FILEMODE,
                                   maxBytes=mem_cast(LOG_MAXSIZE, 'B'),
                                   backupCount=1,
                                   encoding=LOG_ENCODING)
    if not hdlr:
        hdlr = StreamHandler()

    umsg.init(mode=LOG_MODE)
    umsg.add_handler(hdlr)
    def test_formatter(self, capsys):
        prefix = 'fmt'
        msg = 'stream it'
        umsg.add_handler(logging.StreamHandler)
        umsg.log(msg, prefix=prefix)
        date = datetime.date.today().strftime('%Y-%m-%d')

        expected = '{} ([\\d]{{2}}:?){{3}} - INFO - <{}> {}'.format(
            date, prefix, msg)
        captured = capsys.readouterr()

        assert (re.match(expected, captured.err))
Beispiel #3
0
    def test_init_handlers(self):
        # logger is already setup for this module
        logger = umsg.get_attr('logger')

        # add instance
        umsg.add_handler(logging.StreamHandler())

        # add class
        umsg.add_handler(logging.handlers.SocketHandler, **{
            'host': '127.0.0.1',
            'port': 99
        })

        assert_that(logger.handlers[0]).is_instance_of(logging.NullHandler)
        assert_that(logger.handlers[1]).is_instance_of(logging.StreamHandler)
        assert_that(logger.handlers[2]).is_instance_of(
            logging.handlers.SocketHandler)
def main(config_file):
    args = read_config_file(config_file)

    requests.packages.urllib3.disable_warnings(
        requests.packages.urllib3.exceptions.InsecureRequestWarning)
    umsg.init(level=args.get('LOG_LEVEL', 'info'))
    log_file = os.path.join(args.get('LOG_DIR', ''), args.get('LOG_FILE', ''))

    if log_file:
        handler = logging.handlers.RotatingFileHandler(log_file,
                                                       mode='a',
                                                       maxBytes=10 * 1024 *
                                                       1024,
                                                       backupCount=1,
                                                       encoding=None,
                                                       delay=0)
        umsg.add_handler(handler)

    else:
        umsg.add_handler(logging.StreamHandler())

    umsg.log('Starting script')
    csv_data = get_csv_data(filename=args['INPUT_CSV_NAME'],
                            csv_location=args['CSV_LOCATION'],
                            entity_headers=args.get('ENTITY_FIELD_MAP'),
                            match_ip=args.get('MATCH_IP', False))
    apps = parse_csv_into_apps(csv_data, args.get('APP_PREFIX', ''))

    if args.get('IGNORE_TURBO_VERSION'):
        spec = vc.VersionSpec(versions=[], required=False)
        vmt_conn = vc.Connection(os.environ['TURBO_ADDRESS'],
                                 os.environ['TURBO_USERNAME'],
                                 os.environ['TURBO_PASSWORD'],
                                 req_versions=spec)

    else:
        vmt_conn = vc.Connection(os.environ['TURBO_ADDRESS'],
                                 os.environ['TURBO_USERNAME'],
                                 os.environ['TURBO_PASSWORD'])
    turbo_vms = get_turbo_vms(vmt_conn, start=0, end=500, step=500)
    apps = match_apps_to_turbo_vms(apps, turbo_vms,
                                   args.get('MATCH_IP', False))
    make_apps_thru_atm(vmt_conn, apps)

    umsg.log('Finished script')
def main(config_file):
    args = read_config_file(config_file)
    host_name = '0.0.0.0'
    port_number = 8081
    umsg.init(level=args.get('LOG_LEVEL', 'INFO'))
    log_file = os.path.join(args.get('LOG_DIR'), args.get('LOG_FILE'))

    if log_file:
        handler = logging.handlers.RotatingFileHandler(log_file,
                                                       mode='a',
                                                       maxBytes=10 * 1024 *
                                                       1024,
                                                       backupCount=1,
                                                       encoding=None,
                                                       delay=0)
        umsg.add_handler(handler)

    else:
        umsg.add_handler(logging.StreamHandler())

    topology_handler = MakeHandlerClassFromArgs(args['INPUT_CSV_NAME'],
                                                args['CSV_LOCATION'],
                                                args.get('ENTITY_FIELD_MAP'),
                                                args.get('METRIC_FIELD_MAP'),
                                                args.get('APP_PREFIX'))

    httpd = HTTPServer((host_name, port_number), topology_handler)

    umsg.log(f'Server Starts - {host_name}:{port_number}')
    try:
        httpd.serve_forever()

    except KeyboardInterrupt:
        pass

    httpd.server_close()
    umsg.log(f'Server Stops - {host_name}:{port_number}')
Beispiel #6
0
## ----------------------------------------------------
##  python __main__ execution
## ----------------------------------------------------
if __name__ == '__main__':
    try:
        required('TR_ACTION_TYPES', ACTION_TYPES)
        required('TR_AUTH', AUTH)
        required('TR_GROUPS', GROUPS)
        required('TR_TAGS', EMAIL_TAGS)

        os.chdir(WORKING_DIR)
        socket.setdefaulttimeout(30)

        vmt_handler = init_log_handler(LOG_FORMATTER, LOG_MODE)
        umsg.init(mode=LOG_MODE, msg_prefix=MSG_PREFIX)
        umsg.add_handler(vmt_handler)
        LOGGER = umsg.get_attr('logger')
        LOGGER.setLevel(LOG_MODE)

        if DISABLE_SSL_WARNINGS:
            _msg('Disabling SSL warnings', level='debug')
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        if HTTP_DEBUG:
            _msg('HTTP Debuging output enabled', level='debug')
            from http.client import HTTPConnection
            HTTPConnection.debuglevel = 1
            requests_log = logging.getLogger('requests.packages.urllib3')
            requests_log.setLevel(logging.DEBUG)
            requests_log.propagate = True