This commit is contained in:
bel
2021-09-12 22:16:11 -06:00
commit ceeb6f0385
129 changed files with 9221 additions and 0 deletions

View File

View File

@@ -0,0 +1,84 @@
# Contians the arg parser options.
import argparse
import sys
def get_builder_args():
"""
Gets the arguments passed in to the aom_builder main call
:return: parser object
"""
parser = argparse.ArgumentParser(description="Generates a valid yaml file for alerting on metrics. "
"If you are familiar with the yaml structure for an alert"
"you don't have to use this builder, it's just convenient")
parser.add_argument('-q', '--query', help="The Kariosdb query string to use")
parser.add_argument('-i', '--interval', type=int, default=60, help="The interval that the check will run. "
"This value is in seconds")
parser.add_argument('-t', '--threshold', '--upperthreshold', help="The upper threshold is the value that when reached will cause an alert "
"depending on the threshold logic. "
"Use in conjunction with lower threshold to define a normal band.")
parser.add_argument('-b', '--lowerthreshold', help="The lower threshold is the value that when reached will cause an alert "
"depending on the threshold logic"
"Use in conjunction with upper threshold to define a normal band.")
parser.add_argument('-m', '--measure', choices=['gt', 'lt', 'eq'], help="The measure to use to compare the "
"threshold to the values of the alerts")
parser.add_argument('-a', '--alert_config', help='A valid Yaml representation of your alerting block')
parser.add_argument('-l', '--log_level', type=int, default=0, help="The log level for the aom_builder run. "
"[0=Error, 1=Info, 2=Debug]")
parser.add_argument('-p', '--port', type=int, default=8080, help="The port to run the webapp on")
return args_to_dict(parser)
def get_tester_service_args():
"""
Gets arguments passed into aom_tester.py
Returns: parser object
"""
parser = argparse.ArgumentParser(description="Parameters to start the alerting on metrics dummy tester service")
parser.add_argument('-l', '--log_level', type=int, default=0, help="The log level for the aom_service app"
"[0=Error, 1=Info, 2=Debug]")
parser.add_argument('-a', '--alert_configs', default=None,
help="If provided will override the folder location read from the config with the value passed "
"in. Is helpful for testing and troubleshooting alerts")
parser.add_argument('--hostname', help="If provided, will override the actual hostname check with this value")
parser.add_argument('-p', '--port', type=int, default=8080, help="The port to run the webapp on")
return args_to_dict(parser)
def get_service_args():
"""
Gets arguments passed into aom_service.py
Returns: parser object
"""
parser = argparse.ArgumentParser(description="Parameters to start the alerting on metrics service")
parser.add_argument('-l', '--log_level', type=int, default=0, help="The log level for the aom_service app"
"[0=Error, 1=Info, 2=Debug]")
parser.add_argument('-a', '--alert_configs', default=None,
help="If provided will override the folder location read from the config with the value passed "
"in. Is helpful for testing and troubleshooting alerts")
parser.add_argument('-o', '--override', action='store_true', help="Overrides the check leader election value")
parser.add_argument('--hostname', help="If provided, will override the actual hostname check with this value")
parser.add_argument('-p', '--port', type=int, default=8080, help="The port to run the webapp on")
return args_to_dict(parser)
def args_to_dict(parsed_args):
"""
Converts the argument parser object to a dict
Args:
parsed_args: Arg parser object
Returns:
Dictionary of arguments
"""
try:
arg_list = parsed_args.parse_args()
# RETURN A DICT OF ARGUMENTS
arg_dict = dict()
for val in vars(arg_list):
arg_dict[val] = getattr(arg_list, val)
return arg_dict
except argparse.ArgumentError:
parsed_args.print_help()
sys.exit(1)

View File

@@ -0,0 +1,22 @@
# config.py
import logging
import glob
import yaml
logger = logging.getLogger(__name__)
def glob_the_configs(config_path):
"""
Args:
config_path (string): relative path to the configs
Returns:
List of configs
"""
alert_list = []
for config_file in glob.glob(config_path + "/*.yaml"):
logger.debug("Found {} config".format(config_file))
# LOAD CONFIG
alert_list.append(yaml.load(open(config_file, 'rb').read()))
logger.info("Loaded {} configs".format(len(alert_list)))
return alert_list

View File

@@ -0,0 +1,118 @@
# logger.py
import logging
import logging.handlers
import os
logging.getLogger('requests').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.ERROR)
logging.getLogger('werkzeug').setLevel(logging.ERROR)
class SingleLevelFilter(logging.Filter):
def __init__(self, passlevel, reject):
"""
initilizer(constructor) of the singlelevelfilter
@param passlevel (int) - the int value of the level of the log
@param reject (bool) - if true will return if the record level is not equal to the passlevel
@return SingleLevelFilter object
@note Sets some object parameters
"""
self.passlevel = passlevel
self.reject = reject
def filter(self, record):
"""
Returns True/False depending on parameters
@param record (Log int) - the record that the filter belongs to
@return bool - True/False depending on what self.reject is set to and what record.levelno and self.passlevel are set to
@note This causes either only logging of the exact same level to get logged, or only logging other than the same level to get logged
"""
if self.reject:
return (record.levelno != self.passlevel)
else:
return (record.levelno == self.passlevel)
class AlertLogging(logging.Logger):
"""
Class Object to handle the logging of the alert on metrics service
starts at Error level and can flip on (and add) an additional log file and
Debug logger as needed.
"""
def __init__(self, name):
"""
Inits the formaters and logger
"""
self.name = name
self.debug_formatter = logging.Formatter(
"%(asctime)s - [%(levelname)s] - [%(module)s:%(lineno)d] - %(message)s", "%m-%d %H:%M:%S")
self.standard_formatter = logging.Formatter("%(asctime)s - [%(levelname)s] - %(message)s",
"%m-%d %H:%M:%S")
logging.getLogger()
logging.Logger.__init__(self, name, logging.DEBUG)
logging.setLoggerClass(AlertLogging)
def start(self):
"""
Returns:
"""
info_handler = logging.StreamHandler()
info_handler.setLevel(logging.INFO)
info_handler.setFormatter(self.standard_formatter)
self.addHandler(info_handler)
return self
def start_log_file(self, file_path, mode='a'):
"""
Creates a separate log file handler
Args:
file_path: path to the log file
mode: the type of mode to open the file handler with
Returns:
"""
self.log_path = file_path
work_folder = os.path.dirname(file_path)
if len(work_folder) > 0 and not os.path.exists(work_folder):
os.makedirs(work_folder)
self.log_handler = logging.FileHandler(file_path, mode)
self.log_handler.setLevel(logging.DEBUG)
self.log_handler.setFormatter(self.debug_formatter)
self.addHandler(self.log_handler)
def stop_log_file(self):
"""
Closes Log file and sets the handler to None
Returns:
"""
self.log_handler.close()
self.removeHandler(self.log_handler)
self.log_handler = None
def start_debug(self):
"""
Returns:
"""
self.debug_handler = logging.StreamHandler()
self.debug_handler.setLevel(logging.DEBUG)
self.debug_handler.addFilter(SingleLevelFilter(logging.DEBUG, False))
self.debug_handler.setFormatter(self.debug_formatter)
self.addHandler(self.debug_handler)
def stop_debug(self):
"""
stop the debugger
Returns:
"""
self.removeHandler(self.debug_handler)
self.debug_handler = None