Skip to content
Snippets Groups Projects
Commit b036337b authored by Rajmund Hruška's avatar Rajmund Hruška
Browse files

Feature: Create skeleton class for detectors management. (Redmine issue: #7577)

parent 390a3d46
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license.
#-------------------------------------------------------------------------------
"""
This Mentat module is a script providing functions for detectors management
for Mentat system database. Still work in progress, use with caution.
To view built-in help please execute the application with ``--help`` command line
option::
mentat-detmngr.py --help
To view local documentation please use ``pydoc3``::
pydoc3 mentat.module.detmngr
This script is implemented using the :py:mod:`pyzenkit.zenscript` framework and
so it provides all of its core features. See the documentation for more in-depth
details.
License
^^^^^^^
Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
Use of this source is governed by the MIT license.
"""
__author__ = "Rajmund Hruška <rajmund.hruska@cesnet.cz>"
__credits__ = "Jan Mach <jan.mach@cesnet.cz>, Pavel Kácha <pavel.kacha@cesnet.cz>, \
Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
#
# Custom libraries.
#
from mentat.module.detmngr import MentatDetmngrScript
#
# Execute the script.
#
if __name__ == "__main__":
MentatDetmngrScript().run()
#-------------------------------------------------------------------------------
#
# CONFIGURATION FILE FOR MENTAT-DETMNGR.PY MODULE
#
#-------------------------------------------------------------------------------
{
#---------------------------------------------------------------------------
# Custom script configurations
#---------------------------------------------------------------------------
# Path to detectors file containing data.
# default: None
# type: string
#
#"detectors_file": null,
# Source of detectors file.
# default: warden
# type: string
#
#"source": "detectors-file",
#---------------------------------------------------------------------------
# Common script configurations
#---------------------------------------------------------------------------
#"regular": false,
#"shell": false,
#"command": "generate",
"interval": "6_hourly",
#"adjust_thresholds": false,
#"time_high": null,
#---------------------------------------------------------------------------
# Common application configurations
#---------------------------------------------------------------------------
#"quiet": false,
#"verbosity": 0,
#"log_file": "/var/mentat/log/mentat-detmngr.py.log",
#"log_level": "info",
#"runlog_dir": "/var/mentat/run/mentat-detmngr.py",
#"runlog_dump": false,
#"runlog_log": false,
#"pstate_file": "/var/mentat/run/mentat-detmngr.py.pstate",
#"pstate_dump": false,
#"pstate_log": false,
#"action": null,
#"user": "mentat",
#"group": "mentat",
# This is a dummy last configuration so that the user does not have to fix
# the commas in the whole configuration file after each change.
"_dummy_": "_dummy_"
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
#-------------------------------------------------------------------------------
"""
This Mentat module is a script providing functions for detectors management
for Mentat system database.
This script is implemented using the :py:mod:`pyzenkit.zenscript` framework and
so it provides all of its core features. See the documentation for more in-depth
details.
.. note::
Still work in progress, use with caution.
Usage examples
--------------
.. code-block:: shell
# Display help message and exit.
mentat-detmngr.py --help
# Run in debug mode (enable output of debugging information to terminal).
mentat-detmngr.py --debug
# Run with increased logging level.
mentat-detmngr.py --log-level debug
Available script commands
-------------------------
``status`` (*default*)
Detect and display the state of internal whois database contents according
to the data in given reference whois file.
``update``
Attempt to update the state of internal whois database contents according
to the data in given reference whois file.
Custom configuration
--------------------
Custom command line options
^^^^^^^^^^^^^^^^^^^^^^^^^^^
``--detectors-file file-path``
Path to reference detectors file containing data.
*Type:* ``string``, *default:* ``None``
``--source``
Origin of the whois file.
*Type:* ``string``, *default:* ``detectors-file``
"""
__author__ = "Rajmund Hruška <rajmund.hruska@cesnet.cz>"
__credits__ = "Jan Mach <jan.mach@cesnet.cz>, Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import re
import json
import collections
#
# Custom libraries.
#
import pyzenkit.jsonconf
import mentat.script.fetcher
import mentat.const
import mentat.datatype.internal
from mentat.datatype.sqldb import GroupModel, NetworkModel, SettingsReportingModel, \
networkmodel_from_typeddict
DETECTORS_FILE_GENERIC = 'detectors-file'
DETECTORS_FILE_WARDEN = 'warden'
class MentatDetmngrScript(mentat.script.fetcher.FetcherScript):
"""
Implementation of Mentat module (script) providing functions for detectors
management for Mentat database.
"""
#
# Class constants.
#
# List of configuration keys.
CONFIG_DETECTORS_FILE = 'detectors_file'
CONFIG_DETECTORS_SOURCE = 'source'
def __init__(self):
"""
Initialize detmngr script object. This method overrides the base
implementation in :py:func:`pyzenkit.zenscript.ZenScript.__init__` and
it aims to even more simplify the script object creation by providing
configuration values for parent contructor.
"""
self.eventservice = None
self.sqlservice = None
super().__init__(
description = 'mentat-detmngr.py - Detector management script for Mentat database',
)
def _init_argparser(self, **kwargs):
"""
Initialize script command line argument parser. This method overrides the
base implementation in :py:func:`pyzenkit.zenscript.ZenScript._init_argparser`
and it must return valid :py:class:`argparse.ArgumentParser` object. It
appends additional command line options custom for this script object.
This method is called from the main constructor in :py:func:`pyzenkit.baseapp.BaseApp.__init__`
as a part of the **__init__** stage of application`s life cycle.
:param kwargs: Various additional parameters passed down from object constructor.
:return: Valid argument parser object.
:rtype: argparse.ArgumentParser
"""
argparser = super()._init_argparser(**kwargs)
#
# Create and populate options group for custom script arguments.
#
arggroup_script = argparser.add_argument_group('custom script arguments')
arggroup_script.add_argument(
'--detectors-file',
type = str,
default = None,
help = 'path to reference detectors file containing data'
)
arggroup_script.add_argument(
'--source',
type = str,
default = DETECTORS_FILE_GENERIC,
help = 'origin of the detectors file'
)
return argparser
def _init_config(self, cfgs, **kwargs):
"""
Initialize default script configurations. This method overrides the base
implementation in :py:func:`pyzenkit.zenscript.ZenScript._init_config`
and it appends additional configurations via ``cfgs`` parameter.
This method is called from the main constructor in :py:func:`pyzenkit.baseapp.BaseApp.__init__`
as a part of the **__init__** stage of application`s life cycle.
:param list cfgs: Additional set of configurations.
:param kwargs: Various additional parameters passed down from constructor.
:return: Default configuration structure.
:rtype: dict
"""
cfgs = (
(self.CONFIG_DETECTORS_FILE, None),
(self.CONFIG_DETECTORS_SOURCE, DETECTORS_FILE_GENERIC)
) + cfgs
return super()._init_config(cfgs, **kwargs)
#---------------------------------------------------------------------------
def get_default_command(self):
"""
Return the name of the default script command. This command will be executed
in case it is not explicitly selected either by command line option, or
by configuration file directive.
:return: Name of the default command.
:rtype: str
"""
return 'status'
def cbk_command_status(self):
"""
Implementation of the **status** command (*default*).
Detect and display the status of detectors collection.
"""
result = self._process_detectors(True)
return result
def cbk_command_update(self):
"""
Implementation of the **update** command.
Attempt to update the state of internal detectors database contents according
to the data in given reference detectors file.
"""
result = self._process_detectors(False)
return result
#---------------------------------------------------------------------------
def _process_detectors(self, status_only):
"""
The actual worker method for processing detectors records.
:param bool status_only: Do not actually perform any database operations, just report status.
:return: Structure containing information about changes.
:rtype: dict
"""
raise NotImplementedError
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment