Source code for

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# This file is part of Mentat system (
# Copyright (C) since 2011 CESNET, z.s.p.o (
# Use of this source is governed by the MIT license, see LICENSE file.
# -------------------------------------------------------------------------------

This module contains couple of simple helpers for working with IDEA messages.

__author__ = "Jan Mach <>"
__credits__ = "Pavel Kácha <>, Andrea Kropáčová <>"

import os

# Flask related modules.
import flask

# Custom modules.
import pyzenkit.jsonconf
import hawat.const

_DB = None

def _get_cached_values(cache_file):
    cache_dir = flask.current_app.config.get(
    return pyzenkit.jsonconf.json_load(

def _get_values(cache_file, column):
        return _get_cached_values(cache_file)
    except FileNotFoundError:
        return db_get().distinct_values(column)

[docs]def get_event_source_types(): """ Return list of all available event source types. """ return _get_values('itemset-stat-sourcetypes.json', 'source_type')
[docs]def get_event_target_types(): """ Return list of all available event target types. """ return _get_values('itemset-stat-targettypes.json', 'target_type')
[docs]def get_event_detector_types(): """ Return list of all available event detector types. """ return _get_values('itemset-stat-detectortypes.json', 'node_type')
[docs]def get_event_detectors(): """ Return list of all available event detectors. """ return _get_values('itemset-stat-detectors.json', 'node_name')
[docs]def get_event_categories(): """ Return list of all available event categories. """ return _get_values('itemset-stat-categories.json', 'category')
[docs]def get_event_severities(): """ Return list of all available event severities. """ return _get_values('itemset-stat-severities.json', 'eventseverity')
[docs]def get_event_classes(): """ Return list of all available event classes. """ return _get_values('itemset-stat-classes.json', 'eventclass')
[docs]def get_event_protocols(): """ Return list of all available event protocols. """ return _get_values('itemset-stat-protocols.json', 'protocol')
[docs]def get_event_inspection_errs(): """ Return list of all available event inspection errors. """ return _get_values('itemset-stat-inspectionerrors.json', 'inspectionerrors')
[docs]def db_settings(app): """ Return database settings from Mentat core configurations. :return: Database settings. :rtype: dict """ return app.mconfig
[docs]def get_event_enums(): # Get lists of available options for various event search form select fields. enums = {} enums.update( source_types=get_event_source_types(), target_types=get_event_target_types(), detectors=get_event_detectors(), detector_types=get_event_detector_types(), categories=get_event_categories(), severities=get_event_severities(), classes=get_event_classes(), protocols=get_event_protocols(), inspection_errs=get_event_inspection_errs() ) enums.update( host_types=sorted(list(set(enums['source_types'] + enums['target_types']))), ) return enums
[docs]def get_event_form_choices(): enums = get_event_enums() choices = {} for key, vals in enums.items(): choices[key] = list(zip(vals, vals)) return choices
[docs]def db_init(app): """ Initialize connection to event database. """ app.eventdb =
[docs]def db_get(): """ Opens a new database connection if there is none yet for the current application context. :return: Database storage handler. :rtype: flask_sqlalchemy.SQLAlchemy """ return
[docs]def db_cursor(): """ Convenience method. """ return db_get().session