mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Merge pull request #29 from mtakaki/mtakaki_action_enums
Converting actions to enums
This commit is contained in:
@@ -33,7 +33,9 @@ cachet:
|
|||||||
token: my_token
|
token: my_token
|
||||||
component_id: 1
|
component_id: 1
|
||||||
metric_id: 1
|
metric_id: 1
|
||||||
update_status: true
|
action:
|
||||||
|
- CREATE_INCIDENT
|
||||||
|
- UPDATE_STATUS
|
||||||
frequency: 30
|
frequency: 30
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -50,7 +52,9 @@ frequency: 30
|
|||||||
- **token**, the API token.
|
- **token**, the API token.
|
||||||
- **component_id**, the id of the component we're monitoring. This will be used to update the status of the component.
|
- **component_id**, the id of the component we're monitoring. This will be used to update the status of the component.
|
||||||
- **metric_id**, this will be used to store the latency of the API. If this is not set, it will be ignored.
|
- **metric_id**, this will be used to store the latency of the API. If this is not set, it will be ignored.
|
||||||
- **update_status**, boolean to decide if the component status should be changed in case of a check failure.
|
- **action**, the action to be done when one of the expectations fails. This is optional and if left blank, nothing will be done to the component.
|
||||||
|
- **CREATE_INCIDENT**, we will create an incident when the expectation fails.
|
||||||
|
- **UPDATE_STATUS**, updates the component status
|
||||||
- **frequency**, how often we'll send a request to the given URL. The unit is in seconds.
|
- **frequency**, how often we'll send a request to the given URL. The unit is in seconds.
|
||||||
|
|
||||||
## Setting up
|
## Setting up
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import re
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import status as st
|
|
||||||
from yaml import dump
|
from yaml import dump
|
||||||
from yaml import load
|
from yaml import load
|
||||||
|
|
||||||
|
import status as st
|
||||||
|
|
||||||
# This is the mandatory fields that must be in the configuration file in this
|
# This is the mandatory fields that must be in the configuration file in this
|
||||||
# same exact structure.
|
# same exact structure.
|
||||||
configuration_mandatory_fields = {
|
configuration_mandatory_fields = {
|
||||||
@@ -100,6 +101,15 @@ class Configuration(object):
|
|||||||
"""
|
"""
|
||||||
return 'update_status' in self.data['cachet'] and self.data['cachet']['update_status']
|
return 'update_status' in self.data['cachet'] and self.data['cachet']['update_status']
|
||||||
|
|
||||||
|
def get_action(self):
|
||||||
|
"""Retrieves the action list from the configuration. If it's empty, returns an empty list.
|
||||||
|
:return: The list of actions, which can be an empty list.
|
||||||
|
"""
|
||||||
|
if self.data['cachet'].get('action') is None:
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
return self.data['cachet']['action']
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
"""Validates the configuration by verifying the mandatory fields are
|
"""Validates the configuration by verifying the mandatory fields are
|
||||||
present and in the correct format. If the validation fails, a
|
present and in the correct format. If the validation fails, a
|
||||||
|
|||||||
@@ -1,18 +1,23 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from configuration import Configuration
|
|
||||||
import logging
|
import logging
|
||||||
import schedule
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import schedule
|
||||||
|
|
||||||
|
from configuration import Configuration
|
||||||
|
|
||||||
|
|
||||||
class Agent(object):
|
class Agent(object):
|
||||||
"""Monitor agent that will be constantly verifying if the URL is healthy
|
"""Monitor agent that will be constantly verifying if the URL is healthy
|
||||||
and updating the component.
|
and updating the component.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, configuration):
|
def __init__(self, configuration, decorators=None):
|
||||||
self.configuration = configuration
|
self.configuration = configuration
|
||||||
|
if decorators is None:
|
||||||
|
decorators = []
|
||||||
|
self.decorators = decorators
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
"""Will verify the API status and push the status and metrics to the
|
"""Will verify the API status and push the status and metrics to the
|
||||||
@@ -21,43 +26,48 @@ class Agent(object):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
self.configuration.push_metrics()
|
self.configuration.push_metrics()
|
||||||
|
|
||||||
|
for decorator in self.decorators:
|
||||||
|
decorator.execute(self.configuration)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Sets up the schedule based on the configuration file."""
|
"""Sets up the schedule based on the configuration file."""
|
||||||
schedule.every(self.configuration.data['frequency']).seconds.do(self.execute)
|
schedule.every(self.configuration.data['frequency']).seconds.do(self.execute)
|
||||||
|
|
||||||
|
|
||||||
class UpdateStatusAgent(Agent):
|
class Decorator(object):
|
||||||
def __init__(self, configuration):
|
def execute(self, configuration):
|
||||||
super(UpdateStatusAgent, self).__init__(configuration)
|
pass
|
||||||
|
|
||||||
def execute(self):
|
|
||||||
super(UpdateStatusAgent, self).execute()
|
|
||||||
self.configuration.push_status()
|
|
||||||
|
|
||||||
|
|
||||||
class CreateIncidentAgent(Agent):
|
class UpdateStatusDecorator(Decorator):
|
||||||
def __init__(self, configuration):
|
def execute(self, configuration):
|
||||||
super(CreateIncidentAgent, self).__init__(configuration)
|
configuration.push_status()
|
||||||
|
|
||||||
def execute(self):
|
|
||||||
super(CreateIncidentAgent, self).execute()
|
class CreateIncidentDecorator(Decorator):
|
||||||
self.configuration.push_incident()
|
def execute(self, configuration):
|
||||||
|
configuration.push_incident()
|
||||||
|
|
||||||
|
|
||||||
class Scheduler(object):
|
class Scheduler(object):
|
||||||
def __init__(self, config_file):
|
def __init__(self, config_file):
|
||||||
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
|
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
|
||||||
self.configuration = Configuration(config_file)
|
self.configuration = Configuration(config_file)
|
||||||
|
self.agent = self.get_agent()
|
||||||
if self.configuration.is_create_incident():
|
|
||||||
self.agent = CreateIncidentAgent(self.configuration)
|
|
||||||
elif self.configuration.is_update_status():
|
|
||||||
self.agent = UpdateStatusAgent(self.configuration)
|
|
||||||
else:
|
|
||||||
self.agent = Agent(self.configuration)
|
|
||||||
|
|
||||||
self.stop = False
|
self.stop = False
|
||||||
|
|
||||||
|
def get_agent(self):
|
||||||
|
action_names = {
|
||||||
|
'CREATE_INCIDENT': CreateIncidentDecorator,
|
||||||
|
'UPDATE_STATUS': UpdateStatusDecorator,
|
||||||
|
}
|
||||||
|
actions = []
|
||||||
|
for action in self.configuration.get_action():
|
||||||
|
self.logger.info('Registering action %s' % (action))
|
||||||
|
actions.append(action_names[action]())
|
||||||
|
return Agent(self.configuration, decorators=actions)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.agent.start()
|
self.agent.start()
|
||||||
self.logger.info('Starting monitor agent...')
|
self.logger.info('Starting monitor agent...')
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ cachet:
|
|||||||
token: my_token
|
token: my_token
|
||||||
component_id: 1
|
component_id: 1
|
||||||
#metric_id: 1
|
#metric_id: 1
|
||||||
create_incident: true
|
action:
|
||||||
update_status: true
|
- CREATE_INCIDENT
|
||||||
|
- UPDATE_STATUS
|
||||||
frequency: 30
|
frequency: 30
|
||||||
|
|||||||
Reference in New Issue
Block a user