|
|
|
@ -16,15 +16,14 @@ from prometheus_client import start_http_server
|
|
|
|
|
|
|
|
|
|
DEFAULT_METRICS_PORT = 8080
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.ERROR,
|
|
|
|
|
format='%(asctime)s %(levelname)s %(name)s %(message)s'
|
|
|
|
|
level=logging.ERROR, format="%(asctime)s %(levelname)s %(name)s %(message)s"
|
|
|
|
|
)
|
|
|
|
|
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_yaml(path):
|
|
|
|
|
"""Loads config from a YAML file with env interpolation"""
|
|
|
|
|
with open(path, 'r') as yaml:
|
|
|
|
|
with open(path, "r") as yaml:
|
|
|
|
|
contents = yaml.read()
|
|
|
|
|
return yamlenv.load(contents)
|
|
|
|
|
|
|
|
|
@ -35,44 +34,40 @@ def validate_monitor_settings(settings):
|
|
|
|
|
Note: Cannot yet validate the Alerts exist from within this class.
|
|
|
|
|
That will be done by Minitor later
|
|
|
|
|
"""
|
|
|
|
|
name = settings.get('name')
|
|
|
|
|
name = settings.get("name")
|
|
|
|
|
if not name:
|
|
|
|
|
raise InvalidMonitorException('Invalid name for monitor')
|
|
|
|
|
if not settings.get('command'):
|
|
|
|
|
raise InvalidMonitorException(
|
|
|
|
|
'Invalid command for monitor {}'.format(name)
|
|
|
|
|
)
|
|
|
|
|
raise InvalidMonitorException("Invalid name for monitor")
|
|
|
|
|
if not settings.get("command"):
|
|
|
|
|
raise InvalidMonitorException("Invalid command for monitor {}".format(name))
|
|
|
|
|
|
|
|
|
|
type_assertions = (
|
|
|
|
|
('check_interval', int),
|
|
|
|
|
('alert_after', int),
|
|
|
|
|
('alert_every', int),
|
|
|
|
|
("check_interval", int),
|
|
|
|
|
("alert_after", int),
|
|
|
|
|
("alert_every", int),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for key, val_type in type_assertions:
|
|
|
|
|
val = settings.get(key)
|
|
|
|
|
if not isinstance(val, val_type):
|
|
|
|
|
raise InvalidMonitorException(
|
|
|
|
|
'Invalid type on {}: {}. Expected {} and found {}'.format(
|
|
|
|
|
"Invalid type on {}: {}. Expected {} and found {}".format(
|
|
|
|
|
name, key, val_type.__name__, type(val).__name__
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
non_zero = (
|
|
|
|
|
'check_interval',
|
|
|
|
|
'alert_after',
|
|
|
|
|
"check_interval",
|
|
|
|
|
"alert_after",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for key in non_zero:
|
|
|
|
|
if settings.get(key) == 0:
|
|
|
|
|
raise InvalidMonitorException(
|
|
|
|
|
'Invalid value for {}: {}. Value cannot be 0'.format(
|
|
|
|
|
name, key
|
|
|
|
|
)
|
|
|
|
|
"Invalid value for {}: {}. Value cannot be 0".format(name, key)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def maybe_decode(bstr, encoding='utf-8'):
|
|
|
|
|
def maybe_decode(bstr, encoding="utf-8"):
|
|
|
|
|
try:
|
|
|
|
|
return bstr.decode(encoding)
|
|
|
|
|
except TypeError:
|
|
|
|
@ -82,14 +77,14 @@ def maybe_decode(bstr, encoding='utf-8'):
|
|
|
|
|
def call_output(*popenargs, **kwargs):
|
|
|
|
|
"""Similar to check_output, but instead returns output and exception"""
|
|
|
|
|
# So we can capture complete output, redirect sderr to stdout
|
|
|
|
|
kwargs.setdefault('stderr', subprocess.STDOUT)
|
|
|
|
|
kwargs.setdefault("stderr", subprocess.STDOUT)
|
|
|
|
|
output, ex = None, None
|
|
|
|
|
try:
|
|
|
|
|
output = check_output(*popenargs, **kwargs)
|
|
|
|
|
except CalledProcessError as e:
|
|
|
|
|
output, ex = e.output, e
|
|
|
|
|
|
|
|
|
|
output = output.rstrip(b'\n')
|
|
|
|
|
output = output.rstrip(b"\n")
|
|
|
|
|
return output, ex
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -113,23 +108,23 @@ class Monitor(object):
|
|
|
|
|
def __init__(self, config, counter=None, logger=None):
|
|
|
|
|
"""Accepts a dictionary of configuration items to override defaults"""
|
|
|
|
|
settings = {
|
|
|
|
|
'alerts': ['log'],
|
|
|
|
|
'check_interval': 30,
|
|
|
|
|
'alert_after': 4,
|
|
|
|
|
'alert_every': -1,
|
|
|
|
|
"alerts": ["log"],
|
|
|
|
|
"check_interval": 30,
|
|
|
|
|
"alert_after": 4,
|
|
|
|
|
"alert_every": -1,
|
|
|
|
|
}
|
|
|
|
|
settings.update(config)
|
|
|
|
|
validate_monitor_settings(settings)
|
|
|
|
|
|
|
|
|
|
self.name = settings['name']
|
|
|
|
|
self.command = settings['command']
|
|
|
|
|
self.alert_down = settings.get('alert_down', [])
|
|
|
|
|
self.name = settings["name"]
|
|
|
|
|
self.command = settings["command"]
|
|
|
|
|
self.alert_down = settings.get("alert_down", [])
|
|
|
|
|
if not self.alert_down:
|
|
|
|
|
self.alert_down = settings.get('alerts', [])
|
|
|
|
|
self.alert_up = settings.get('alert_up', [])
|
|
|
|
|
self.check_interval = settings.get('check_interval')
|
|
|
|
|
self.alert_after = settings.get('alert_after')
|
|
|
|
|
self.alert_every = settings.get('alert_every')
|
|
|
|
|
self.alert_down = settings.get("alerts", [])
|
|
|
|
|
self.alert_up = settings.get("alert_up", [])
|
|
|
|
|
self.check_interval = settings.get("check_interval")
|
|
|
|
|
self.alert_after = settings.get("alert_after")
|
|
|
|
|
self.alert_every = settings.get("alert_every")
|
|
|
|
|
|
|
|
|
|
self.alert_count = 0
|
|
|
|
|
self.last_check = None
|
|
|
|
@ -140,18 +135,18 @@ class Monitor(object):
|
|
|
|
|
self._counter = counter
|
|
|
|
|
if logger is None:
|
|
|
|
|
self._logger = logging.getLogger(
|
|
|
|
|
'{}({})'.format(self.__class__.__name__, self.name)
|
|
|
|
|
"{}({})".format(self.__class__.__name__, self.name)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
self._logger = logger.getChild(
|
|
|
|
|
'{}({})'.format(self.__class__.__name__, self.name)
|
|
|
|
|
"{}({})".format(self.__class__.__name__, self.name)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _count_check(self, is_success=True, is_alert=False):
|
|
|
|
|
if self._counter is not None:
|
|
|
|
|
self._counter.labels(
|
|
|
|
|
monitor=self.name,
|
|
|
|
|
status=('success' if is_success else 'failure'),
|
|
|
|
|
status=("success" if is_success else "failure"),
|
|
|
|
|
is_alert=is_alert,
|
|
|
|
|
).inc()
|
|
|
|
|
|
|
|
|
@ -199,7 +194,7 @@ class Monitor(object):
|
|
|
|
|
back_up = None
|
|
|
|
|
if not self.is_up():
|
|
|
|
|
back_up = MinitorAlert(
|
|
|
|
|
'{} check is up again!'.format(self.name),
|
|
|
|
|
"{} check is up again!".format(self.name),
|
|
|
|
|
self,
|
|
|
|
|
)
|
|
|
|
|
self.total_failure_count = 0
|
|
|
|
@ -215,7 +210,7 @@ class Monitor(object):
|
|
|
|
|
if self.total_failure_count < self.alert_after:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
failure_count = (self.total_failure_count - self.alert_after)
|
|
|
|
|
failure_count = self.total_failure_count - self.alert_after
|
|
|
|
|
if self.alert_every > 0:
|
|
|
|
|
# Otherwise, we should check against our alert_every
|
|
|
|
|
should_alert = (failure_count % self.alert_every) == 0
|
|
|
|
@ -223,15 +218,15 @@ class Monitor(object):
|
|
|
|
|
# Only alert on the first failure
|
|
|
|
|
should_alert = failure_count == 1
|
|
|
|
|
else:
|
|
|
|
|
should_alert = (failure_count >= (2 ** self.alert_count) - 1)
|
|
|
|
|
should_alert = failure_count >= (2**self.alert_count) - 1
|
|
|
|
|
|
|
|
|
|
if should_alert:
|
|
|
|
|
self.alert_count += 1
|
|
|
|
|
raise MinitorAlert(
|
|
|
|
|
'{} check has failed {} times'.format(
|
|
|
|
|
"{} check has failed {} times".format(
|
|
|
|
|
self.name, self.total_failure_count
|
|
|
|
|
),
|
|
|
|
|
self
|
|
|
|
|
self,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def is_up(self):
|
|
|
|
@ -243,18 +238,18 @@ class Alert(object):
|
|
|
|
|
def __init__(self, name, config, counter=None, logger=None):
|
|
|
|
|
"""An alert must be named and have a config dict"""
|
|
|
|
|
self.name = name
|
|
|
|
|
self.command = config.get('command')
|
|
|
|
|
self.command = config.get("command")
|
|
|
|
|
if not self.command:
|
|
|
|
|
raise InvalidAlertException('Invalid alert {}'.format(self.name))
|
|
|
|
|
raise InvalidAlertException("Invalid alert {}".format(self.name))
|
|
|
|
|
|
|
|
|
|
self._counter = counter
|
|
|
|
|
if logger is None:
|
|
|
|
|
self._logger = logging.getLogger(
|
|
|
|
|
'{}({})'.format(self.__class__.__name__, self.name)
|
|
|
|
|
"{}({})".format(self.__class__.__name__, self.name)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
self._logger = logger.getChild(
|
|
|
|
|
'{}({})'.format(self.__class__.__name__, self.name)
|
|
|
|
|
"{}({})".format(self.__class__.__name__, self.name)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _count_alert(self, monitor):
|
|
|
|
@ -277,7 +272,7 @@ class Alert(object):
|
|
|
|
|
def _format_datetime(self, dt):
|
|
|
|
|
"""Formats a datetime for an alert"""
|
|
|
|
|
if dt is None:
|
|
|
|
|
return 'Never'
|
|
|
|
|
return "Never"
|
|
|
|
|
return dt.isoformat()
|
|
|
|
|
|
|
|
|
|
def alert(self, message, monitor):
|
|
|
|
@ -313,64 +308,72 @@ class Minitor(object):
|
|
|
|
|
|
|
|
|
|
def _parse_args(self, args=None):
|
|
|
|
|
"""Parses command line arguments and returns them"""
|
|
|
|
|
parser = ArgumentParser(description='Minimal monitoring')
|
|
|
|
|
parser = ArgumentParser(description="Minimal monitoring")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--config', '-c',
|
|
|
|
|
dest='config_path',
|
|
|
|
|
default='config.yml',
|
|
|
|
|
help='Path to the config YAML file to use',
|
|
|
|
|
"--config",
|
|
|
|
|
"-c",
|
|
|
|
|
dest="config_path",
|
|
|
|
|
default="config.yml",
|
|
|
|
|
help="Path to the config YAML file to use",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--metrics', '-m',
|
|
|
|
|
dest='metrics',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='Start webserver with metrics',
|
|
|
|
|
"--metrics",
|
|
|
|
|
"-m",
|
|
|
|
|
dest="metrics",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Start webserver with metrics",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--metrics-port', '-p',
|
|
|
|
|
dest='metrics_port',
|
|
|
|
|
"--metrics-port",
|
|
|
|
|
"-p",
|
|
|
|
|
dest="metrics_port",
|
|
|
|
|
type=int,
|
|
|
|
|
default=DEFAULT_METRICS_PORT,
|
|
|
|
|
help='Port to use when serving metrics',
|
|
|
|
|
help="Port to use when serving metrics",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--verbose', '-v',
|
|
|
|
|
action='count',
|
|
|
|
|
help=('Adjust log verbosity by increasing arg count. Default log',
|
|
|
|
|
'level is ERROR. Level increases with each `v`'),
|
|
|
|
|
"--verbose",
|
|
|
|
|
"-v",
|
|
|
|
|
action="count",
|
|
|
|
|
help=(
|
|
|
|
|
"Adjust log verbosity by increasing arg count. Default log",
|
|
|
|
|
"level is ERROR. Level increases with each `v`",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
return parser.parse_args(args)
|
|
|
|
|
|
|
|
|
|
def _setup(self, config_path):
|
|
|
|
|
"""Load all setup from YAML file at provided path"""
|
|
|
|
|
config = read_yaml(config_path)
|
|
|
|
|
self.check_interval = config.get('check_interval', 30)
|
|
|
|
|
self.check_interval = config.get("check_interval", 30)
|
|
|
|
|
self.monitors = [
|
|
|
|
|
Monitor(
|
|
|
|
|
mon,
|
|
|
|
|
counter=self._monitor_counter,
|
|
|
|
|
logger=self._logger,
|
|
|
|
|
)
|
|
|
|
|
for mon in config.get('monitors', [])
|
|
|
|
|
for mon in config.get("monitors", [])
|
|
|
|
|
]
|
|
|
|
|
# Add default alert for logging
|
|
|
|
|
self.alerts = {
|
|
|
|
|
'log': Alert(
|
|
|
|
|
'log',
|
|
|
|
|
{'command': ['echo', '{alert_message}!']},
|
|
|
|
|
"log": Alert(
|
|
|
|
|
"log",
|
|
|
|
|
{"command": ["echo", "{alert_message}!"]},
|
|
|
|
|
counter=self._alert_counter,
|
|
|
|
|
logger=self._logger,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
self.alerts.update({
|
|
|
|
|
alert_name: Alert(
|
|
|
|
|
alert_name,
|
|
|
|
|
alert,
|
|
|
|
|
counter=self._alert_counter,
|
|
|
|
|
logger=self._logger,
|
|
|
|
|
)
|
|
|
|
|
for alert_name, alert in config.get('alerts', {}).items()
|
|
|
|
|
})
|
|
|
|
|
self.alerts.update(
|
|
|
|
|
{
|
|
|
|
|
alert_name: Alert(
|
|
|
|
|
alert_name,
|
|
|
|
|
alert,
|
|
|
|
|
counter=self._alert_counter,
|
|
|
|
|
logger=self._logger,
|
|
|
|
|
)
|
|
|
|
|
for alert_name, alert in config.get("alerts", {}).items()
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _validate_monitors(self):
|
|
|
|
|
"""Validates monitors are valid against other config values"""
|
|
|
|
@ -378,7 +381,7 @@ class Minitor(object):
|
|
|
|
|
# Validate that the interval is valid
|
|
|
|
|
if monitor.check_interval < self.check_interval:
|
|
|
|
|
raise InvalidMonitorException(
|
|
|
|
|
'Monitor {} check interval is lower global value {}'.format(
|
|
|
|
|
"Monitor {} check interval is lower global value {}".format(
|
|
|
|
|
monitor.name, self.check_interval
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@ -386,26 +389,26 @@ class Minitor(object):
|
|
|
|
|
for alert in chain(monitor.alert_down, monitor.alert_up):
|
|
|
|
|
if alert not in self.alerts:
|
|
|
|
|
raise InvalidMonitorException(
|
|
|
|
|
'Monitor {} contains an unknown alert: {}'.format(
|
|
|
|
|
"Monitor {} contains an unknown alert: {}".format(
|
|
|
|
|
monitor.name, alert
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _init_metrics(self):
|
|
|
|
|
self._alert_counter = Counter(
|
|
|
|
|
'minitor_alert_total',
|
|
|
|
|
'Number of Minitor alerts',
|
|
|
|
|
['alert', 'monitor'],
|
|
|
|
|
"minitor_alert_total",
|
|
|
|
|
"Number of Minitor alerts",
|
|
|
|
|
["alert", "monitor"],
|
|
|
|
|
)
|
|
|
|
|
self._monitor_counter = Counter(
|
|
|
|
|
'minitor_check_total',
|
|
|
|
|
'Number of Minitor checks',
|
|
|
|
|
['monitor', 'status', 'is_alert'],
|
|
|
|
|
"minitor_check_total",
|
|
|
|
|
"Number of Minitor checks",
|
|
|
|
|
["monitor", "status", "is_alert"],
|
|
|
|
|
)
|
|
|
|
|
self._monitor_status_gauge = Gauge(
|
|
|
|
|
'minitor_monitor_up_count',
|
|
|
|
|
'Currently responsive monitors',
|
|
|
|
|
['monitor'],
|
|
|
|
|
"minitor_monitor_up_count",
|
|
|
|
|
"Currently responsive monitors",
|
|
|
|
|
["monitor"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _loop(self):
|
|
|
|
@ -420,9 +423,7 @@ class Minitor(object):
|
|
|
|
|
result = monitor.check()
|
|
|
|
|
if result is not None:
|
|
|
|
|
self._logger.info(
|
|
|
|
|
'%s: %s',
|
|
|
|
|
monitor.name,
|
|
|
|
|
'SUCCESS' if result else 'FAILURE'
|
|
|
|
|
"%s: %s", monitor.name, "SUCCESS" if result else "FAILURE"
|
|
|
|
|
)
|
|
|
|
|
except MinitorAlert as minitor_alert:
|
|
|
|
|
self._logger.warning(minitor_alert)
|
|
|
|
@ -475,5 +476,5 @@ def main(args=None):
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
sys.exit(main())
|
|
|
|
|