Skip to content

Commit

Permalink
fixup! ✨(backends) add graylog service
Browse files Browse the repository at this point in the history
  • Loading branch information
quitterie-lcs committed Oct 14, 2021
1 parent 8f76259 commit daa459d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RALPH_APP_DIR=/app/.ralph

# Graylog storage backend

# RALPH_GRAYLOG_HOST=localhost
# RALPH_GRAYLOG_HOST=graylog
# RALPH_GRAYLOG_PORT=12201

# LDP storage backend
Expand Down
31 changes: 21 additions & 10 deletions src/ralph/backends/storage/graylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,58 @@

import logging
import sys
from marshmallow import fields

from logging_gelf.formatters import GELFFormatter
from logging_gelf.handlers import GELFTCPSocketHandler
from logging_gelf.schemas import GelfSchema
from pathlib import Path
from marshmallow import fields

from ...defaults import RALPH_GRAYLOG_HOST, RALPH_GRAYLOG_PORT
from ..mixins import HistoryMixin
from .base import BaseStorage
from ...defaults import RALPH_GRAYLOG_HOST, RALPH_GRAYLOG_PORT

logger = logging.getLogger(__name__)


class EventSchema(GelfSchema):
"""Custom-made schema for GELF formatter"""

archive = fields.String()


class GraylogStorage(HistoryMixin, BaseStorage):
"""Graylog storage backend"""

# pylint: disable=too-many-arguments

name = "graylog"

def __init__(
self,
host=RALPH_GRAYLOG_HOST,
port=RALPH_GRAYLOG_PORT,
client_options: dict = None,
):

self.client_options = client_options

self.host = host
self.port = port

logger = logging.getLogger("gelf")
logger.setLevel(logging.INFO)
self.gelf_logger = logging.getLogger("gelf")
self.gelf_logger.setLevel(logging.INFO)

def write(self, name, chunk_size=None, overwrite=False):
"""Write inputs in graylog backend (one JSON event per line)"""

logger.debug("Creating archive: %s", name)

handler = GELFTCPSocketHandler(host=self.host, port=self.port)
input = sys.stdin.readlines()
for event in input:
handler.setFormatter(GELFFormatter(schema=EventSchema))
logger.addHandler(handler)
logger.info(event, extra=dict(archive=name))
handler.setFormatter(GELFFormatter(schema=EventSchema))
self.gelf_logger.addHandler(handler)

for event in sys.stdin.readlines():
self.gelf_logger.info(event, extra=self.client_options)

def list(self, details=False, new=False):
"""List files in the storage backend"""
Expand Down
3 changes: 1 addition & 2 deletions tests/backends/storage/test_graylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ def test_backends_storage_graylog_storage_instantiation():
"""Tests the GraylogStorage backend instantiation."""
# pylint: disable=protected-access

assert GraylogStorage.name == "graylog"

storage = GraylogStorage(
host=RALPH_GRAYLOG_HOST,
port=RALPH_GRAYLOG_PORT,
)

assert storage.name == "graylog"
assert storage.host == "graylog"
assert storage.port == 12201

0 comments on commit daa459d

Please sign in to comment.