Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple index sets in Graylog #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions graylog_archiver/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
from elasticsearch import Elasticsearch
from graylog_archiver.graylog_archiver import GraylogArchiver
from graylog_archiver.config import Config
from .graylog_archiver import GraylogArchiver
from .config import Config


def parse():
Expand Down
6 changes: 3 additions & 3 deletions graylog_archiver/graylog_archiver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import shutil
from graylog_archiver import logs
from graylog_archiver.graylog_elasticsearch import GraylogElasticsearch
from graylog_archiver.utils import compress_directory
from . import logs
from .graylog_elasticsearch import GraylogElasticsearch
from .utils import compress_directory


class GraylogArchiver:
Expand Down
17 changes: 14 additions & 3 deletions graylog_archiver/graylog_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import time
import re
import shutil
from itertools import groupby, chain

from graylog_archiver import utils
from . import utils

INDEX_NAME_PATTERN = re.compile(r"(\S+)_\d+")


def extract_number(index):
Expand All @@ -15,6 +18,13 @@ def sort_indices(indices):
return sorted(indices, key=extract_number, reverse=True)


def group_and_sort_indices(indices):
index_sets = [(re.match(INDEX_NAME_PATTERN, x).group(1), x) for x in indices if re.match(INDEX_NAME_PATTERN, x)]
return dict(
(x[0], sort_indices([y[1] for y in x[1]])) for x in groupby(sorted(index_sets), lambda x: x[0])
)


class GraylogElasticsearch:
def __init__(self, es, max_indices, backup_dir):
self.es = es
Expand All @@ -25,8 +35,9 @@ def indices(self):
return list(self.es.indices.get_mapping().keys())

def indices_to_archive(self):
indices_sorted = sort_indices(self.indices())
return indices_sorted[self.max_indices:] # keeps the max indices
indices_sorted = group_and_sort_indices(self.indices())
to_be_archieved = [idx[self.max_indices:] for idx in indices_sorted.values()]
return list(chain(*to_be_archieved)) # keeps the max indices from each index set

def create_backup_repository(self, repository, location):
return self.es.snapshot.create_repository(
Expand Down
1 change: 0 additions & 1 deletion graylog_archiver/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import uuid
from shutil import make_archive
import os


def random_string():
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

from setuptools import setup, find_packages

Expand Down
13 changes: 12 additions & 1 deletion test/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from graylog_archiver.graylog_elasticsearch import sort_indices
from graylog_archiver.graylog_elasticsearch import sort_indices, group_and_sort_indices


class TestStringMethods(unittest.TestCase):
Expand All @@ -9,6 +9,17 @@ def test_sort_from_oldest(self):
indices_sorted = sort_indices(indices)
self.assertEqual(['graylog_36', 'graylog_9'], indices_sorted)

def test_group_and_sort_indexsets(self):
indices = [
'graylog_9',
'graylog_internal_3',
'graylog_internal_5',
'graylog_36',
'graylog_internal_6'
]
indices_grouped = group_and_sort_indices(indices)
self.assertEqual(['graylog_36', 'graylog_9'], indices_grouped['graylog'])
self.assertEqual(['graylog_internal_6', 'graylog_internal_5', 'graylog_internal_3'], indices_grouped['graylog_internal'])

if __name__ == '__main__':
unittest.main()