Esempio n. 1
0
 def getLogger(self):
     '''
     метод возвращает объект logger
     
     :return: logging.Logger
     '''
     if self._logger == None :
         
         config        = self._getConfig()['logger']
         udpConfig     = config['graylog']['UDP']
         sysLogConfig  = config['syslog']
         fileLogConfig = config['filelog']
         formater      = logging.Formatter(config['format'])
         
         logger = logging.getLogger()
         logger.setLevel(config['level'])
         
         if udpConfig['enable']:
             gelfHandler = GelfUdpHandler(
                 host = udpConfig['host'],
                 port = udpConfig['port']
             )
             
             gelfHandler.setFormatter(formater)
             logger.addHandler(gelfHandler)
         
         if fileLogConfig['enable']:
             fileHandler = WatchedFileHandler(
                 fileLogConfig['path'],
                 mode = 'a+'
             )
             
             fileHandler.setFormatter(formater)
             logger.addHandler(fileHandler)
         
         if sysLogConfig['enable']:
             monologHandler = MonologHandler(
                 SysLogHandler(
                     address = (sysLogConfig['host'], sysLogConfig['port'])
                 )
             )
             
             monologHandler.setFormatter(formater)
             logger.addHandler(monologHandler)
         
         self._logger = logger
         
     return self._logger
Esempio n. 2
0
def build_log(log_config):
    logging = log.getLogger(log_config['name'])
    level = get_level(log_config['level'])
    formatter = log.Formatter(log_config['format'])
    logging.setLevel(level)
    if not logging.handlers:
        if log_config['file']['enable']:
            __file_handler = TimedRotatingFileHandler(
                log_config['file']['path'], when="D")
            __file_handler.setFormatter(formatter)
            logging.addHandler(__file_handler)
        if log_config['console']:
            __console_handler = log.StreamHandler(stream=sys.stdout, )
            __console_handler.setFormatter(formatter)
            logging.addHandler(__console_handler)
        if log_config['syslog']['enable']:
            __syslog_handler = SysLogHandler(
                address=(log_config['syslog']['ip'],
                         log_config['syslog']['port']),
                facility=log_config['syslog']['facility'])
            __syslog_handler.setFormatter(formatter)
            logging.addHandler(__syslog_handler)
        if log_config['gelf']['enable']:
            __gelf_handler = GelfUdpHandler(host=log_config['gelf']['ip'],
                                            port=log_config['gelf']['port'],
                                            _app_name=log_config['name'],
                                            debug=log_config['gelf']['debug'],
                                            _facility='GELF',
                                            _tag=log_config['gelf']['tag'])
            logging.addHandler(__gelf_handler)

        return logging
    return logging
Esempio n. 3
0
 def init_logger(self):
     logging.basicConfig(level=logging.INFO)
     self.logger = logging.getLogger()
     handler = GelfUdpHandler(host=self.config_json['graylog_ip'],
                              port=self.config_json['graylog_port'],
                              _app="auditwatch")
     self.logger.addHandler(handler)
Esempio n. 4
0
def main():
    # Set up logger
    logging.basicConfig(level=logging.INFO)
    gelf_handler = GelfUdpHandler(host=env.get('GRAYLOG_HOST', '127.0.0.1'),
                                  port=int(env.get('GRAYLOG_PORT', '12201')),
                                  include_extra_fields=True)
    logger = logging.getLogger()
    logger.addHandler(gelf_handler)

    # Default record info
    defaults = {
        'name': 'supervisor_gelf',
        'level': logging.getLevelName(logging.INFO),
        'levelno': logging.INFO,
        'pathname': osp.realpath(__file__),
        'msg': '',
        'args': None,
        'exc_info': None
    }

    # Log each event
    for headers, event in events(sys.stdin, sys.stdout):
        record = merged(
            defaults, {
                'msg':
                "Supervisor event: {eventname}".format(
                    **merged(headers, event['headers'])),
                'data':
                event['data'],
                'eventname':
                headers['eventname']
            }, event['headers'])

        logger.handle(logging.makeLogRecord(record))
Esempio n. 5
0
def get_gelf_handler():
    gelf_config = frappe.local.conf.gelf_config
    if not gelf_config:
        return

    gelf_gelf_host = gelf_config.get('host', '127.0.0.1')
    gelf_gelf_port = gelf_config.get('port', 32000)
    return GelfUdpHandler(host=gelf_gelf_host,
                          port=gelf_gelf_port,
                          include_extra_fields=True)
Esempio n. 6
0
def configure_graylog(app):
    """Set up Graylog"""

    additional_fields = {
        "app": APP_NAME,
        "facility": PRODUCT_NAME,
        "environment": app.config['ENVIRONMENT']
    }

    app.logger.setLevel(logging.INFO)
    gelf_upd_handler = GelfUdpHandler(host=app.config["GL_SERVER"],
                                      port=app.config["GL_PORT"],
                                      include_extra_fields=True,
                                      compress=False,
                                      chunk_size=1300,
                                      **additional_fields)

    gelf_upd_handler.debug = True
    gelf_upd_handler.setLevel(logging.INFO)
    app.logger.addFilter(GrayLogContextFilter())
    app.logger.addHandler(gelf_upd_handler)
Esempio n. 7
0
def configure_graylog(authenticator):
    """Set up Graylog"""

    additional_fields = {
        "app": "wotnot-data-access",
        "facility": "wotnot",
        "environment": authenticator.config['ENVIRONMENT']}

    authenticator.logger = logging.getLogger('WotNot_logger')
    authenticator.logger.setLevel(logging.INFO)

    gelf_upd_handler = GelfUdpHandler(host=authenticator.config["GL_SERVER"],
                                      port=authenticator.config["GL_PORT"],
                                      include_extra_fields=True,
                                      compress=False,
                                      chunk_size=1300,
                                      **additional_fields)

    gelf_upd_handler.debug = True
    gelf_upd_handler.setLevel(logging.INFO)

    authenticator.logger.addHandler(gelf_upd_handler)
Esempio n. 8
0
    def set_gelf_logger(log_level: int = logging.INFO,
                        transport_layer='TCP',
                        stdout=False,
                        include_extra_fields=True,
                        **gelf_kwargs):  # noqa: E301
        """
        Sets gelf console logger. Handler for console output is not included by default,
        for testing in non-gelf environments use stdout=True.

        Args:
            log_level: logging level, default: 'logging.INFO'
            transport_layer: 'TCP' or 'UDP', default:'UDP
            stdout: if set to True, Stout handler is also included
            include_extra_fields:
                Include extra GELF fields in the log messages.
                e.g. logging.warning('Some warning',
                                     extra={"additional_info": "Extra info to be displayed in the detail"}

        Returns: Logger object
        """
        # remove existing handlers
        for h in logging.getLogger().handlers:
            logging.getLogger().removeHandler(h)
        if stdout:
            CommonInterface.set_default_logger(log_level)

        # gelf handler setup
        gelf_kwargs['include_extra_fields'] = include_extra_fields

        host = os.getenv('KBC_LOGGER_ADDR', 'localhost')
        port = os.getenv('KBC_LOGGER_PORT', 12201)
        if transport_layer == 'TCP':
            gelf = GelfTcpHandler(host=host, port=port, **gelf_kwargs)
        elif transport_layer == 'UDP':
            gelf = GelfUdpHandler(host=host, port=port, **gelf_kwargs)
        else:
            raise ValueError(
                F'Unsupported gelf transport layer: {transport_layer}. Choose TCP or UDP'
            )

        logging.getLogger().setLevel(log_level)
        logging.getLogger().addHandler(gelf)

        logger = logging.getLogger()
        return logger
Esempio n. 9
0
 def __get_graylog_handler(self, config):
     handler = None
     host = config["host"]
     port = config["port"]
     if config["type"].lower() == "udp":
         from pygelf import GelfUdpHandler
         handler = GelfUdpHandler(host=host,
                                  port=port,
                                  include_extra_fields=True)
     elif config["type"].lower() == "tcp":
         from pygelf import GelfTcpHandler
         handler = GelfTcpHandler(host=host,
                                  port=port,
                                  include_extra_fields=True)
     elif config["type"].lower() == "http":
         from pygelf import GelfHttpHandler
         handler = GelfHttpHandler(host=host,
                                   port=port,
                                   include_extra_fields=True)
     return handler
Esempio n. 10
0
def _logger_gelf(logger: logging.Logger, url: str) -> logging.Handler:
    def _split_path_fields(path):
        def by_len(x):
            if len(x) == 2:
                return x
            elif len(x) == 1:
                return [x[0], "1"]
            else:
                return [None, None]

        return {
            k: v
            for k, v in (by_len(x) for x in (x.split('=', 1)
                                             for x in path.split('/')
                                             if len(x))) if k
        }

    o: ParseResult = urlparse(url)

    kwds_a = _split_path_fields(o.path)

    kwds_b = {}

    if o.query:
        kwds_b = {
            k: v[-1] if v else '1'
            for k, v in parse_qs(o.query, keep_blank_values=True).items()
        }

    kwds = {**kwds_a, **kwds_b}

    ch = GelfUdpHandler(host=o.hostname,
                        port=o.port,
                        chunk_size=1350,
                        debug=True,
                        static_fields=kwds)

    logger.addHandler(ch)

    return ch
 def __init__(self,
              stream,
              host='localhost',
              port=12201,
              protocol='udp',
              _filters=None,
              environment=None,
              json_only=False):
     signal.signal(signal.SIGINT, self.stop)
     signal.signal(signal.SIGTERM, self.stop)
     self.buffer = deque()
     self.environment = str(environment)
     if _filters is not None:
         self.filters = _filters.split(',')
     else:
         self.filters = None
     self.json_only = bool(json_only)
     self.protocol = str(protocol)
     self.stream = stream
     self.handleropts = {
         'host': str(host),
         'port': int(port),
         'protocol': str(protocol)
     }
     if str(protocol) == 'udp':
         self.gelfhandler = GelfUdpHandler(host=str(host),
                                           port=int(port),
                                           include_extra_fields=True,
                                           compress=True)
     elif str(protocol) == 'tcp':
         self.gelfhandler = GelfTcpHandler(host=str(host),
                                           port=int(port),
                                           include_extra_fields=True)
     elif str(protocol) == 'tls':
         self.gelfhandler = GelfTlsHandler(host=str(host),
                                           port=int(port),
                                           include_extra_fields=True)
Esempio n. 12
0
mongo_client = pymongo.MongoClient(
    'mongodb://%s:%s@%s:27017/admin' %
    (MONGO_ROOT_USERNAME, MONGO_ROOT_PASSWORD, MONGO_HOST))

tweets_collection = mongo_client.twitterDB.tweets
users_collection = mongo_client.twitterDB.users

app = Flask(__name__)
metrics = PrometheusMetrics(app)

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
logger.addFilter(ContextFilter())
logger.addHandler(
    GelfUdpHandler(host='graylog', port=12201, include_extra_fields=True))


@app.route("/what/users/most/followers")
def what_users_with_most_followers():
    logging.info('Starting to get what user have most followers...')

    # Purposefully generate a delay in response
    time.sleep(random.random() * 0.6)

    # Get users in descending order of the amount of followers
    users = users_collection.find() \
                .sort([('user_followers_count', pymongo.DESCENDING)]) \
                .limit(5)

    users = [{
Esempio n. 13
0
import socket
import pytest
import mock
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfHttpHandler, GelfTlsHandler
from tests.helper import logger, get_unique_message, log_warning, log_exception

SYSLOG_LEVEL_ERROR = 3
SYSLOG_LEVEL_WARNING = 4


@pytest.fixture(params=[
    GelfTcpHandler(host='127.0.0.1', port=12201),
    GelfUdpHandler(host='127.0.0.1', port=12202),
    GelfUdpHandler(host='127.0.0.1', port=12202, compress=False),
    GelfHttpHandler(host='127.0.0.1', port=12203),
    GelfHttpHandler(host='127.0.0.1', port=12203, compress=False),
    GelfTlsHandler(host='127.0.0.1', port=12204),
    # GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem'),
])
def handler(request):
    return request.param


def test_simple_message(logger):
    message = get_unique_message()
    graylog_response = log_warning(logger, message)
    assert graylog_response['message'] == message
    assert graylog_response['level'] == SYSLOG_LEVEL_WARNING
    assert 'full_message' not in graylog_response
    assert 'file' not in graylog_response
    assert 'module' not in graylog_response
Esempio n. 14
0
    JSON_MESSAGE['channel'] = slackChannel
    JSON_MESSAGE['icon_url'] = slackIcon
    JSON_MESSAGE['username'] = slackUsername


if graylogEnabled or localLog:
        import logging
        logger = logging.getLogger()
        logging.basicConfig(level=logging.INFO)
        


if graylogEnabled:

    from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler
    logger.addHandler(GelfUdpHandler(host=graylogServer, port=graylogPort))
    """
    Fun note about pygelf - the function it uses to get the system's hostname, `socket.getfqdn()` typically just returns the first domain in /etc/hosts
    So I suggest editing /etc/hosts and adding your system's name in there. I have a pull request open for a fix
    """

if localLog:
    localHandler = logging.FileHandler(localLogFile)
    formatter = logging.Formatter('%(asctime)s -- %(levelname)s -- %(message)s')
    localHandler.setFormatter(formatter)
    logger.addHandler(localHandler)

session = boto3.Session(profile_name=AWS_PROFILE)
s3 = session.client('s3')

for VM_EXPORT in os.listdir(BACKUP_PATH):
     sys.exit('Unable to read config file')
 config = cfg_p[cfg_p.sections()[0]]
 token = config.get('token')
 limit = config.get('limit', None)
 timespan = int(config.get('timespan', 3600))  # in seconds
 end_ts = time.time() * 1000
 start_ts = end_ts - (timespan * 1000)
 _dg_debug = config.getboolean('debug', False)
 # Setup GELF logger
 h = None
 gelf_host = config.get('host', '127.0.0.1')
 gelf_port = int(config.get('port', 11201))
 gelf_protocol = config.get('protocol', 'udp')  # (tcp|udp|tls)
 gelf_source = config.get('source_label', 'Dropbox-audit')
 if gelf_protocol == 'udp':
     h = GelfUdpHandler(host=gelf_host, port=gelf_port,
                        source=gelf_source)
 elif gelf_protocol == 'tcp':
     h = GelfTcpHandler(host=gelf_host, port=gelf_port,
                        source=gelf_source)
 elif gelf_protocol == 'tls':
     tls_cafile = config.get('tls_cafile',
                             '/etc/ssl/certs/ca-certificates.crt')
     h = GelfTlsHandler(host=gelf_host, port=gelf_port,
                        source=gelf_source, validate=True,
                        ca_certs=tls_cafile)
 else:
     sys.exit('Unknown protocol')
 logger = logging.getLogger()
 logger.addHandler(h)
 # Import audit entries
 n = dropbox_to_graylog(token, start_ts, end_ts, limit=limit, handler=h)
Esempio n. 16
0
import pytest
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfHttpHandler, GelfTlsHandler
from tests.helper import logger, get_unique_message, log_warning


@pytest.fixture(params=[
    GelfTcpHandler(host='127.0.0.1', port=12201, debug=True),
    GelfUdpHandler(host='127.0.0.1', port=12202, debug=True),
    GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, debug=True),
    GelfHttpHandler(host='127.0.0.1', port=12203, debug=True),
    GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, debug=True),
    GelfTlsHandler(host='127.0.0.1', port=12204, debug=True),
    GelfTlsHandler(host='127.0.0.1',
                   port=12204,
                   debug=True,
                   validate=True,
                   ca_certs='tests/config/cert.pem'),
])
def handler(request):
    return request.param


def test_debug_mode(logger):
    message = get_unique_message()
    graylog_response = log_warning(logger, message)
    assert graylog_response['message'] == message
    assert graylog_response['file'] == 'helper.py'
    assert graylog_response['module'] == 'helper'
    assert graylog_response['func'] == 'log_warning'
    assert graylog_response['logger_name'] == 'test'
    assert 'line' in graylog_response
Esempio n. 17
0
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler
import logging
import json
import pytest
import mock


@pytest.yield_fixture
def send(extra_fields_handler):
    with mock.patch.object(extra_fields_handler, 'send') as mock_send:
        yield mock_send


@pytest.fixture(params=[
    GelfTcpHandler(host='127.0.0.1', port=12000, include_extra_fields=True),
    GelfUdpHandler(host='127.0.0.1', port=12000, compress=False, include_extra_fields=True),
    GelfTlsHandler(host='127.0.0.1', port=12000, include_extra_fields=True)
])
def extra_fields_handler(request):
    return request.param


class ContextFilter(logging.Filter):
    def filter(self, record):
        record.ozzy = 'diary of a madman'
        record.van_halen = 1984
        record.id = 123
        return True


@pytest.yield_fixture
Esempio n. 18
0
     sys.exit('Unable to read config file')
 config = cfg_p[cfg_p.sections()[0]]
 token = config.get('token')
 limit = config.get('limit', None)
 timespan = int(config.get('timespan', 3600))  # in seconds
 end_ts = time.time() * 1000
 start_ts = end_ts - (timespan * 1000)
 _dg_debug = config.getboolean('debug', False)
 # Setup GELF logger
 h = None
 gelf_host = config.get('host', '127.0.0.1')
 gelf_port = int(config.get('port', 11201))
 gelf_protocol = config.get('protocol', 'udp')  # (tcp|udp|tls)
 gelf_source = config.get('source_label', 'Dropbox-audit')
 if gelf_protocol == 'udp':
     h = GelfUdpHandler(host=gelf_host, port=gelf_port, source=gelf_source)
 elif gelf_protocol == 'tcp':
     h = GelfTcpHandler(host=gelf_host, port=gelf_port, source=gelf_source)
 elif gelf_protocol == 'tls':
     tls_cafile = config.get('tls_cafile',
                             '/etc/ssl/certs/ca-certificates.crt')
     h = GelfTlsHandler(host=gelf_host,
                        port=gelf_port,
                        source=gelf_source,
                        validate=True,
                        ca_certs=tls_cafile)
 else:
     sys.exit('Unknown protocol')
 logger = logging.getLogger()
 logger.addHandler(h)
 # Import audit entries
Esempio n. 19
0
    if os.path.isfile(lastErrorFile):
        with open(lastErrorFile, 'r') as f:
            last_error = json.loads(f.read())
except json.decoder.JSONDecodeError:
    print("Couldn't parse last-error.json, assuming empty...", file=sys.stderr)
with open(lastErrorFile, 'w') as f:
    f.write(json.dumps(data[0]))

new_errors = []
for row in data:
    if row == last_error: break
    new_errors.insert(0, row)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
handler = GelfUdpHandler(sys.argv[2],
                         port=int(sys.argv[3]),
                         include_extra_fields=True)
handler.domain = sys.argv[4]
logger.addHandler(handler)

for reportedTime, originalPriority, code, message in new_errors:
    priority = priorityMapping[
        originalPriority] if originalPriority in priorityMapping else logging.WARNING
    logger.log(priority,
               message,
               extra={
                   'originalPriority': originalPriority,
                   'code': code,
                   'reportedTime': reportedTime,
               })
import json
import pytest
import mock
import socket

ADDITIONAL_FIELDS = {
    '_ozzy': 'diary of a madman',
    '_van_halen': 1984,
    '_id': '123'
}


@pytest.fixture(params=[
    GelfTcpHandler(host='127.0.0.1', port=12000, **ADDITIONAL_FIELDS),
    GelfUdpHandler(host='127.0.0.1',
                   port=12000,
                   compress=False,
                   **ADDITIONAL_FIELDS),
    GelfTlsHandler(host='127.0.0.1', port=12000, **ADDITIONAL_FIELDS)
])
def handler(request):
    return request.param


@pytest.yield_fixture
def send(handler):
    with mock.patch.object(handler, 'send') as mock_send:
        yield mock_send


@pytest.yield_fixture
def logger(handler):
Esempio n. 21
0
class ContextFilter(logging.Filter):
    def filter(self, record):
        record.version = VERSION
        record.k8s_app = APP_NAME
        record.k8s_namespace = ENVIRONMENT
        record.k8s_node = PARENT_HOST
        record.k8s_pod_name = POD_NAME
        return True


gelflogger.addFilter(ContextFilter())

gelflogger.addHandler(
    GelfUdpHandler(host=LOG_PARAM["ip"],
                   port=LOG_PARAM["port"],
                   include_extra_fields=True))
gelflogger.info('Starting Hazliconnect {}'.format(VERSION))

## End Logging Setup ##

gitlab = oauth.remote_app(
    'gitlab',
    base_url='{}/api/v3/'.format(OAUTH_PARAM["url"]),
    request_token_url=None,
    access_token_url='{}/oauth/token'.format(OAUTH_PARAM["url"]),
    authorize_url='{}/oauth/authorize'.format(OAUTH_PARAM["url"]),
    access_token_method='POST',
    consumer_key='{}'.format(OAUTH_PARAM["key"]),
    consumer_secret='{}'.format(OAUTH_PARAM["secret"]))
Esempio n. 22
0
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler
import logging
import json
import pytest
import mock


def log_and_decode(_logger, _send, text):
    _logger.warning(text)
    message = _send.call_args[0][0].replace(b'\x00', b'').decode('utf-8')
    return json.loads(message)


@pytest.mark.parametrize('handler,version', [
    (GelfTcpHandler(host='127.0.0.1', port=12000, version='10.7'), '10.7'),
    (GelfUdpHandler(
        host='127.0.0.1', port=12000, compress=False, version='11.5'), '11.5'),
    (GelfTlsHandler(host='127.0.0.1', port=12000, version='12.3'), '12.3')
])
def test_custom_version(handler, version):
    logger = logging.getLogger('test')
    logger.addHandler(handler)
    with mock.patch.object(handler, 'send') as send:
        message = log_and_decode(logger, send, 'custom version')
        assert message['version'] == version
    logger.removeHandler(handler)
Esempio n. 23
0
def setup_logging(root_log_level: Optional[str] = None,
                  module_log_levels: Optional[Mapping[str, Union[str,
                                                                 int]]] = None,
                  add_console_handler: bool = True,
                  console_handler_format: Optional[str] = None):
    """
    Setup log handlers for each application's namespace

    :param root_log_level: The log level all loggers use by default. (Default: `"WARNING"`)
    :param module_log_levels: A mapping of module names to their desired log levels.
    :param add_console_handler: If `True`, enable logging to stdout. (Default: `True`).
    :param console_handler_format: Specifies the format of stdout logs. (Default: `DEFAULT_LOG_FORMAT`).
    """

    if root_log_level is None:
        root_log_level = "WARNING"

    root_logger = logging.getLogger()
    root_logger.setLevel(root_log_level)

    if module_log_levels:
        for module_name, log_level in module_log_levels.items():
            logging.getLogger(module_name).setLevel(log_level)

    if add_console_handler:
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(
            logging.Formatter(
                fmt=console_handler_format or DEFAULT_LOG_FORMAT))
        root_logger.addHandler(console_handler)

    # Add GELF handler if GELF is enabled
    if "GRAYLOG_HOST" in os.environ:
        common_opts = dict(host=os.environ["GRAYLOG_HOST"],
                           debug=bool(os.environ.get("GRAYLOG_DEBUG", True)),
                           include_extra_fields=True,
                           _app_name=os.environ["GRAYLOG_APP_NAME"],
                           _env=os.environ["GRAYLOG_ENV"])
        handler_type = os.environ.get("GRAYLOG_HANDLER_TYPE", "tls").lower()
        if handler_type == "udp":
            gelf_handler = GelfUdpHandler(**common_opts,
                                          port=int(
                                              os.environ.get(
                                                  "GRAYLOG_UDP_PORT", 12201)),
                                          chunk_size=int(
                                              os.environ.get(
                                                  "GRAYLOG_UDP_CHUNK_SIZE",
                                                  1300)),
                                          compress=bool(
                                              os.environ.get(
                                                  "GRAYLOG_UDP_COMPRESS",
                                                  True)))
        elif handler_type == "tls":
            gelf_handler = GelfTlsHandler(**common_opts,
                                          port=int(
                                              os.environ.get(
                                                  "GRAYLOG_TLS_PORT", 12201)),
                                          timeout=float(
                                              os.environ.get(
                                                  "GRAYLOG_TLS_TIMEOUT_SECS",
                                                  0.3)))
        else:
            raise ValueError(
                "Graylog handler type '{}' not recognized. Valid types are 'udp' and 'tls'."
                .format(handler_type))

        root_logger.addHandler(gelf_handler)

    # Add datadog handler if log to datadog is enabled
    if "DATADOG_HOST" in os.environ:
        opts = dict(host=os.environ["DATADOG_HOST"],
                    port=int(os.environ.get("DATADOG_UDP_PORT", 10518)))

        datadog_handler = DataDogUdpHandler(**opts)
        root_logger.addHandler(datadog_handler)
Esempio n. 24
0
import configparser
import json

# configuration file
configfile = 'nfr.cfg'
config = configparser.ConfigParser()
config.read(configfile)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
if ("general" in config) and ("gelf_logging_host"
                              in config["general"]) and ("gelf_logging_port"
                                                         in config["general"]):
    logger.addHandler(
        GelfUdpHandler(host=config["general"]["gelf_logging_host"],
                       port=int(config["general"]["gelf_logging_port"]),
                       _app_name="nfr_rss"))
    print("Logging GELF to", config["general"]["gelf_logging_host"])
else:
    print("NOTICE: Centralized logging is not configured!")

pointerfile = 'pointer.dat'

# retrieve pointer to last id received
last_id = 0
try:
    with open(pointerfile, 'r') as f:
        # account for an empty or corrupt pointerfile
        try:
            last_id = int(f.read())
        except:
Esempio n. 25
0
#!/usr/bin/env python36
import requests, json, logging
from datetime import datetime, timedelta
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler
from sys import exit

NOW = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
FIFTEEN_MINUTES_AGO = datetime.now() - timedelta(minutes=15)
FIFTEEN_MINUTES_AGO = FIFTEEN_MINUTES_AGO.strftime('%Y-%m-%d %H:%M:%S')

GRAYLOG_SERVER = "graylog.you.com"
GRAYLOG_PORT = 11589
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
logger.addHandler(GelfUdpHandler(host=GRAYLOG_SERVER, port=GRAYLOG_PORT))
"""
Get API_KEY from https://lastpass.com/company/#!/settings/enterprise-api
"""
API_KEY = ""
API_URL = "https://lastpass.com/enterpriseapi.php"

REQUEST = {}
# Get cid from https://lastpass.com/company/#!/settings/enterprise-api
REQUEST['cid'] = ''
REQUEST['provhash'] = API_KEY
REQUEST['cmd'] = "reporting"
REQUEST['data'] = {}
REQUEST['data']['from'] = NOW
REQUEST['data']['to'] = FIFTEEN_MINUTES_AGO

r = requests.post(API_URL, data=json.dumps(REQUEST))
Esempio n. 26
0
# Example logging with https://github.com/keeprocking/pygelf
# First, `pip install pygelf`

from pygelf import GelfUdpHandler
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.addHandler(GelfUdpHandler(host='127.0.0.1', port=12201))

logger.info('Hello, from Python!')
Esempio n. 27
0
        for device in devices:
                if device.find('/28') > -1:
                        try:
                                dev = {'MessageType':'ProbeTemperatureMessage'}
                                dev['ProbeAddress'] = device
                                dev['MessageDate'] = datetime.datetime.now().isoformat()
                                dev['TemperatureValue'] = owproxy.read(device + 'temperature')
                                logger.info(datetime.datetime.now().isoformat() + ":Temp:" + dev['TemperatureValue'] + "@" + dev['ProbeAddress'])
                                channel.basic_publish(exchange="InboundMessages",routing_key="device.temperature",body=json.dumps(dev))
                        except :
                            logger.debug("Oops!  That was no valid number.  Try again...")
                else:
                    logger.debug("Device type not recogniuzed :" + device)
        logger.info("Ending listing")
        connection.close()

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(level=logging.INFO)
logger.addHandler(GelfUdpHandler(host='graylog', _app_name='temperature', port=12201))

logger.info("Starting temperature ")
time.sleep(15)

logger.debug("First sleep finished")
refresh()
scheduler = BlockingScheduler()
scheduler.add_job(refresh, 'interval', seconds=cfg["Timeout"])
scheduler.start()

Esempio n. 28
0
import pytest
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfHttpHandler, GelfTlsHandler
from tests.helper import logger, get_unique_message, log_warning


STATIC_FIELDS = {
    '_ozzy': 'diary of a madman',
    '_van_halen': 1984,
    '_id': 42
}


@pytest.fixture(params=[
    GelfTcpHandler(host='127.0.0.1', port=12201, **STATIC_FIELDS),
    GelfUdpHandler(host='127.0.0.1', port=12202, **STATIC_FIELDS),
    GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, **STATIC_FIELDS),
    GelfHttpHandler(host='127.0.0.1', port=12203, **STATIC_FIELDS),
    GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, **STATIC_FIELDS),
    GelfTlsHandler(host='127.0.0.1', port=12204, **STATIC_FIELDS),
    # GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem', **STATIC_FIELDS),
    GelfTcpHandler(host='127.0.0.1', port=12201, static_fields=STATIC_FIELDS, _ozzy='billie jean'),
    GelfUdpHandler(host='127.0.0.1', port=12202, static_fields=STATIC_FIELDS, _ozzy='billie jean'),
    GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'),
    GelfHttpHandler(host='127.0.0.1', port=12203, static_fields=STATIC_FIELDS, _ozzy='billie jean'),
    GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'),
    GelfTlsHandler(host='127.0.0.1', port=12204, static_fields=STATIC_FIELDS),
    # GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem', static_fields=STATIC_FIELDS, _ozzy='billie jean'),
])
def handler(request):
    return request.param
Esempio n. 29
0
import json
import logging
from botocore.vendored import requests
from pygelf import GelfUdpHandler
"""
Set your envs in lambda envinroment:
- GOOGLE_WEBHOOK_URL
- GRAYLOG_URL
- GRAYLOG_PORT
- GRAYLOG_TAG
"""

logging.getLogger().addHandler(
    GelfUdpHandler(host=os.environ['GRAYLOG_URL'],
                   port=os.environ['GRAYLOG_PORT'],
                   include_extra_fields=True,
                   _facility='_facility',
                   tag=os.environ['GRAYLOG_TAG']))
google_chat_url = os.environ['GOOGLE_WEBHOOK_URL']


def lambda_handler(event, context):
    # print("Received event: " + json.dumps(event, indent=2))

    try:
        text = get_text(get_message_from_event(event))
        send_to_chat(text, google_chat_url)
        logging.warning(f'message body: {event}')
        return {'statusCode': 200, 'body': str(text)}

    except Exception as e: