1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Config executor library."""
16 from importlib import import_module
17 from logging.config import dictConfig
18 from logging import getLogger
21 from .parser import Parser
22 from .serializer import Serializer
23 from .constants import Constants
28 Executor class reponsible for executing configuration.
30 def __init__(self, configuration_file):
34 :param configuration_file: Telemetry configuration file path.
35 :type configuration_file: str
37 self.parser = Parser(configuration_file)
38 self.log = self.parser.config[u"logging"]
39 self.programs = self.parser.config[u"programs"]
40 self.scheduler = self.parser.config[u"scheduler"]
44 def execute(self, hook=None):
46 Main executor function will run programs from all bundles in a loop.
54 :param hook: Process ID or socket to attach. None by default.
57 for program in self.programs:
58 serializer = Serializer()
60 package = program[u"name"]
61 name = f"telemetry.{package}"
62 package = package.replace("_", " ").title().replace(" ", "")
63 module = import_module(
67 bundle = getattr(module, package)(
69 serializer=serializer,
72 bundle.attach(duration=self.scheduler[u"duration"])
76 except (ImportError, AttributeError) as exc:
78 f"Error executing bundle {package!r}! - {exc}"
82 def execute_daemon(self, hook=None):
84 Daemon executor will execute endless loop.
86 :param hook: Process ID to attach. None by default.
90 self.execute(hook=hook)
93 class ExecutorError(Exception):
95 Creates a Executor Error Exception. This exception is supposed to handle
96 all the errors raised during executing.
98 def __init__(self, message):
100 Execute Error Excpetion init.
102 :param message: Exception error message.
106 self.message = message
107 getLogger("console_stderr").error(message)
108 sys.exit(Constants.err_telemetry_bundle)