27 Commits
0.2 ... 0.3

Author SHA1 Message Date
Mitsuo Takaki
0ed87469ce Bumping the version to release 0.3 2017-02-11 12:26:22 -08:00
mtakaki
bdd74a89c7 Merge pull request #29 from mtakaki/mtakaki_action_enums
Converting actions to enums
2017-02-11 01:28:33 -08:00
Mitsuo Takaki
cb5137c526 Fixing default list parameter 2017-02-11 01:15:09 -08:00
Mitsuo Takaki
3830063ba4 Renaming Agent classes to decorators to comply with the design pattern 2017-02-11 01:09:12 -08:00
Mitsuo Takaki
e4bd02c44f Fixing sample configuration with the actions as a list. 2017-02-11 01:04:42 -08:00
Mitsuo Takaki
e8d4b88c79 Changing the actions to enum list so it's more flexible. 2017-02-11 01:02:27 -08:00
mtakaki
9f3e2b6eff Merge pull request #28 from jacekszubert/feat/configurable_status_updates
Make update status optional
2017-02-10 17:32:31 -08:00
Jacek Szubert
740f726b48 Make update status optional 2017-02-10 14:50:07 +11:00
mtakaki
a147adda35 Merge pull request #25 from mtakaki/mtakaki_expose_configuration
Exposing the configuration to avoid the current parsing problems. #20
2017-01-20 01:05:31 -08:00
Mitsuo Takaki
c59126fd0f Exposing the configuration to avoid the current parsing problems. #20 2017-01-20 00:58:55 -08:00
mtakaki
9ccdc7e5c3 Merge pull request #19 from gardner/master
Updating README to use https url instead of git url. Removing trailin…
2016-10-27 21:38:32 -07:00
Gardner Bickford
96346926cd Updating README to use https url instead of git url. Removing trailing slash from documentation to address issue #18 2016-10-27 11:00:28 -05:00
mtakaki
194a07c403 Merge pull request #14 from waffle-iron/master
waffle.io Badge
2016-06-29 22:41:23 -07:00
Making GitHub Delicious
fe0325dc9e add waffle.io badge 2016-06-29 23:38:27 -06:00
mtakaki
23326fd828 Merge pull request #13 from rahulg963/patch-2
Correction in Import statement of configuration.py
2016-06-24 21:30:17 -07:00
RAHUL GOEL
969a2b1580 Correction in Import statement of configuration.py
In current version of code status file was being imported as cachet_url_monitor.status while configuration.py and status.py being kept in same folder.
Hence replaced imported statement as import status as st.
2016-06-24 22:16:24 +05:30
mtakaki
b018f9e675 Merge pull request #8 from mtakaki/mtakaki_1_allow_overriding_through_env_var
Adding support to overriding some of the configuration through environment variables.
2016-05-22 11:55:51 -07:00
Mitsuo Takaki
d63420ac01 Documenting better the code and small tweaks to the unit tests. 2016-05-20 02:23:39 -07:00
Mitsuo Takaki
a3a91edadc Adding support to overriding some of the configuration through environment variables. 2016-05-19 23:41:20 -07:00
mtakaki
2c01d8eb30 Merge pull request #6 from mtakaki/mtakaki_3_create_incident
Initial attempt at creating incidents when an URL becomes unhealthy #3
2016-05-19 08:43:32 -07:00
Mitsuo Takaki
a83abfd1d3 Fixing failing test. Using @mock.path decorator, instead of overriding the sys.modules. 2016-05-19 08:26:18 -07:00
Mitsuo Takaki
ca358eab2b Verifying component status when we initialize to set the initial state. Tests are failing, but checking it in nonetheless. 2016-05-18 09:34:58 -07:00
Mitsuo Takaki
0f53ff8678 Initial attempt at creating incidents when an URL becomes unhealthy. Missing to actually call it from the scheduler. #3 2016-05-16 01:31:53 -07:00
Mitsuo Takaki
9c8c89c1dd Improving docker documentation to be simpler than overriding the command. 2016-05-13 17:49:15 -07:00
Mitsuo Takaki
9051f2d9b3 Updating the badges to link to docker and adding pypi badge for latest release 2016-05-13 01:19:54 -07:00
Mitsuo Takaki
15dc800c9b Reporting codacy test coverage. 2016-05-10 01:54:20 -07:00
Mitsuo Takaki
8fce82b721 Fixing Dockerfile to be more flexible (using CMD instead of ENTRYPOINT) and adding docker badges. 2016-05-04 20:11:30 -07:00
12 changed files with 347 additions and 122 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ share/
*.egg-info *.egg-info
MANIFEST MANIFEST
dist/ dist/
.idea

View File

@@ -4,4 +4,4 @@ MAINTAINER Mitsuo Takaki <mitsuotakaki@gmail.com>
COPY config.yml /usr/src/app/config/ COPY config.yml /usr/src/app/config/
VOLUME /usr/src/app/config/ VOLUME /usr/src/app/config/
ENTRYPOINT ["python", "cachet_url_monitor/scheduler.py", "config/config.yml"] CMD ["python", "cachet_url_monitor/scheduler.py", "config/config.yml"]

View File

@@ -1,7 +1,12 @@
[![Stories in Ready](https://badge.waffle.io/mtakaki/cachet-url-monitor.png?label=ready&title=Ready)](https://waffle.io/mtakaki/cachet-url-monitor)
# Status # Status
![Build Status](https://codeship.com/projects/5a246b70-f088-0133-9388-2640b49afa9e/status?branch=master) ![Build Status](https://codeship.com/projects/5a246b70-f088-0133-9388-2640b49afa9e/status?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/mtakaki/cachet-url-monitor/badge.svg?branch=master)](https://coveralls.io/github/mtakaki/cachet-url-monitor?branch=master) [![Coverage Status](https://coveralls.io/repos/github/mtakaki/cachet-url-monitor/badge.svg?branch=master)](https://coveralls.io/github/mtakaki/cachet-url-monitor?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/7ef4123130ef4140b8ea7b94d460ba64)](https://www.codacy.com/app/mitsuotakaki/cachet-url-monitor?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=mtakaki/cachet-url-monitor&amp;utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/7ef4123130ef4140b8ea7b94d460ba64)](https://www.codacy.com/app/mitsuotakaki/cachet-url-monitor?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=mtakaki/cachet-url-monitor&amp;utm_campaign=Badge_Grade)
[![Docker pulls](https://img.shields.io/docker/pulls/mtakaki/cachet-url-monitor.svg)](https://hub.docker.com/r/mtakaki/cachet-url-monitor/)
[![Docker stars](https://img.shields.io/docker/stars/mtakaki/cachet-url-monitor.svg)](https://hub.docker.com/r/mtakaki/cachet-url-monitor/)
![License](https://img.shields.io/github/license/mtakaki/cachet-url-monitor.svg)
[![Latest release](https://img.shields.io/pypi/v/cachet-url-monitor.svg)](https://pypi.python.org/pypi/cachet-url-monitor)
cachet-url-monitor cachet-url-monitor
======================== ========================
@@ -15,7 +20,7 @@ This project is available at PyPI: [https://pypi.python.org/pypi/cachet-url-moni
endpoint: endpoint:
url: http://www.google.com url: http://www.google.com
method: GET method: GET
timeout: 0.010 # seconds timeout: 1 # seconds
expectation: expectation:
- type: HTTP_STATUS - type: HTTP_STATUS
status: 200 status: 200
@@ -24,10 +29,13 @@ endpoint:
- type: REGEX - type: REGEX
regex: ".*<body>.*" regex: ".*<body>.*"
cachet: cachet:
api_url: http://status.cachethq.io/api/v1/ api_url: http://status.cachethq.io/api/v1
token: my_token token: my_token
component_id: 1 component_id: 1
metric_id: 1 metric_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
frequency: 30 frequency: 30
``` ```
@@ -44,6 +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.
- **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
@@ -51,7 +62,7 @@ frequency: 30
The application should be installed using **virtualenv**, through the following command: The application should be installed using **virtualenv**, through the following command:
``` ```
$ git clone git@github.com:mtakaki/cachet-url-monitor.git $ git clone https://github.com/mtakaki/cachet-url-monitor.git
$ virtualenv cachet-url-monitor $ virtualenv cachet-url-monitor
$ cd cachet-url-monitor $ cd cachet-url-monitor
$ source bin/activate $ source bin/activate
@@ -75,9 +86,15 @@ $ docker-compose build
$ docker-compose up $ docker-compose up
``` ```
Or pulling directly from [dockerhub](https://hub.docker.com/r/mtakaki/cachet-url-monitor/). You will need to create your own custom `config.yml` file and put it in a folder (`my_config`): Or pulling directly from [dockerhub](https://hub.docker.com/r/mtakaki/cachet-url-monitor/). You will need to create your own custom `config.yml` file and run (it will pull latest):
``` ```
$ docker pull mtakaki/cachet-url-monitor:0.1 $ docker pull mtakaki/cachet-url-monitor
$ docker run --rm -it -v my_config/:/usr/src/app/config/ mtakaki/cachet-url-monitor:0.1 $ docker run --rm -it -v "$PWD":/usr/src/app/config/ mtakaki/cachet-url-monitor
```
If you're going to use a file with a name other than `config.yml`, you will need to map the local file, like this:
```
$ docker run --rm -it -v "$PWD"/my_config.yml:/usr/src/app/config/config.yml:ro mtakaki/cachet-url-monitor:0.2
``` ```

View File

@@ -1,11 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
import abc import abc
import copy
import logging import logging
import os
import re import re
import requests
import time import time
import requests
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.
@@ -17,6 +22,7 @@ configuration_mandatory_fields = {
class ConfigurationValidationError(Exception): class ConfigurationValidationError(Exception):
"""Exception raised when there's a validation error.""" """Exception raised when there's a validation error."""
def __init__(self, value): def __init__(self, value):
self.value = value self.value = value
@@ -24,28 +30,91 @@ class ConfigurationValidationError(Exception):
return repr(self.value) return repr(self.value)
class ComponentNonexistentError(Exception):
"""Exception raised when the component does not exist."""
def __init__(self, component_id):
self.component_id = component_id
def __str__(self):
return repr('Component with id [%d] does not exist.' % (self.component_id,))
def get_current_status(endpoint_url, component_id, headers):
"""Retrieves the current status of the component that is being monitored. It will fail if the component does
not exist or doesn't respond with the expected data.
:return component status.
"""
get_status_request = requests.get('%s/components/%s' % (endpoint_url, component_id), headers=headers)
if get_status_request.ok:
# The component exists.
return get_status_request.json()['data']['status']
else:
raise ComponentNonexistentError(component_id)
class Configuration(object): class Configuration(object):
"""Represents a configuration file, but it also includes the functionality """Represents a configuration file, but it also includes the functionality
of assessing the API and pushing the results to cachet. of assessing the API and pushing the results to cachet.
""" """
def __init__(self, config_file): def __init__(self, config_file):
#TODO(mtakaki#1|2016-04-28): Accept overriding settings using environment
# variables so we have a more docker-friendly approach.
self.logger = logging.getLogger('cachet_url_monitor.configuration.Configuration') self.logger = logging.getLogger('cachet_url_monitor.configuration.Configuration')
self.config_file = config_file self.config_file = config_file
self.data = load(file(self.config_file, 'r')) self.data = load(file(self.config_file, 'r'))
# Exposing the configuration to confirm it's parsed as expected.
self.print_out()
# We need to validate the configuration is correct and then validate the component actually exists.
self.validate() self.validate()
self.logger.info('Monitoring URL: %s %s' % # We store the main information from the configuration file, so we don't keep reading from the data dictionary.
(self.data['endpoint']['method'], self.data['endpoint']['url'])) self.headers = {'X-Cachet-Token': os.environ.get('CACHET_TOKEN') or self.data['cachet']['token']}
self.expectations = [Expectaction.create(expectation) for expectation
in self.data['endpoint']['expectation']] self.endpoint_method = os.environ.get('ENDPOINT_METHOD') or self.data['endpoint']['method']
self.endpoint_url = os.environ.get('ENDPOINT_URL') or self.data['endpoint']['url']
self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1
self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url']
self.component_id = os.environ.get('CACHET_COMPONENT_ID') or self.data['cachet']['component_id']
self.metric_id = os.environ.get('CACHET_METRIC_ID') or self.data['cachet'].get('metric_id')
# We need the current status so we monitor the status changes. This is necessary for creating incidents.
self.status = get_current_status(self.api_url, self.component_id, self.headers)
self.logger.info('Monitoring URL: %s %s' % (self.endpoint_method, self.endpoint_url))
self.expectations = [Expectaction.create(expectation) for expectation in self.data['endpoint']['expectation']]
for expectation in self.expectations: for expectation in self.expectations:
self.logger.info('Registered expectation: %s' % (expectation,)) self.logger.info('Registered expectation: %s' % (expectation,))
self.headers = {'X-Cachet-Token': self.data['cachet']['token']}
def is_create_incident(self):
"""Will verify if the configuration is set to create incidents or not.
:return True if the configuration is set to create incidents or False it otherwise.
"""
return 'create_incident' in self.data['cachet'] and self.data['cachet']['create_incident']
def is_update_status(self):
"""Will verify if the configuration is set to update status or not.
:return True if the configuration is set to update status or False it otherwise.
"""
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
present and in the correct format. If the validation fails, a
ConfigurationValidationError is raised. Otherwise nothing will happen.
"""
configuration_errors = [] configuration_errors = []
for key, sub_entries in configuration_mandatory_fields.iteritems(): for key, sub_entries in configuration_mandatory_fields.iteritems():
if key not in self.data: if key not in self.data:
@@ -63,8 +132,8 @@ class Configuration(object):
configuration_errors.append('endpoint.expectation') configuration_errors.append('endpoint.expectation')
if len(configuration_errors) > 0: if len(configuration_errors) > 0:
raise ConfigurationValidationError(('Config file [%s] failed ' raise ConfigurationValidationError(
'validation. Missing keys: %s') % (self.config_file, 'Config file [%s] failed validation. Missing keys: %s' % (self.config_file,
', '.join(configuration_errors))) ', '.join(configuration_errors)))
def evaluate(self): def evaluate(self):
@@ -73,41 +142,52 @@ class Configuration(object):
according to the expectation results. according to the expectation results.
""" """
try: try:
self.request = requests.request(self.data['endpoint']['method'], self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout)
self.data['endpoint']['url'],
timeout=self.data['endpoint']['timeout'])
self.current_timestamp = int(time.time()) self.current_timestamp = int(time.time())
except requests.ConnectionError: except requests.ConnectionError:
self.logger.warning('The URL is unreachable: %s %s' % self.message = 'The URL is unreachable: %s %s' % (self.endpoint_method, self.endpoint_url)
(self.data['endpoint']['method'], self.logger.warning(self.message)
self.data['endpoint']['url'])) self.status = st.COMPONENT_STATUS_PARTIAL_OUTAGE
self.status = 3
return return
except requests.HTTPError: except requests.HTTPError:
self.logger.exception('Unexpected HTTP response') self.message = 'Unexpected HTTP response'
self.status = 3 self.logger.exception(self.message)
self.status = st.COMPONENT_STATUS_PARTIAL_OUTAGE
return return
except requests.Timeout: except requests.Timeout:
self.logger.warning('Request timed out') self.message = 'Request timed out'
self.status = 3 self.logger.warning(self.message)
self.status = st.COMPONENT_STATUS_PERFORMANCE_ISSUES
return return
# We initially assume the API is healthy. # We initially assume the API is healthy.
self.status = 1 self.status = st.COMPONENT_STATUS_OPERATIONAL
self.message = ''
for expectation in self.expectations: for expectation in self.expectations:
status = expectation.get_status(self.request) status = expectation.get_status(self.request)
# The greater the status is, the worse the state of the API is. # The greater the status is, the worse the state of the API is.
if status > self.status: if status > self.status:
self.status = status self.status = status
self.message = expectation.get_message(self.request)
self.logger.info(self.message)
def print_out(self):
self.logger.info('Current configuration:\n%s' % (self.__repr__()))
def __repr__(self):
temporary_data = copy.deepcopy(self.data)
# Removing the token so we don't leak it in the logs.
del temporary_data['cachet']['token']
return dump(temporary_data, default_flow_style=False)
def push_status(self): def push_status(self):
params = {'id': self.data['cachet']['component_id'], 'status': """Pushes the status of the component to the cachet server. It will update the component
self.status} status based on the previous call to evaluate().
component_request = requests.put('%s/components/%d' % """
(self.data['cachet']['api_url'], params = {'id': self.component_id, 'status': self.status}
self.data['cachet']['component_id']), component_request = requests.put('%s/components/%d' % (self.api_url, self.component_id), params=params,
params=params, headers=self.headers) headers=self.headers)
if component_request.ok: if component_request.ok:
# Successful update # Successful update
self.logger.info('Component update: status [%d]' % (self.status,)) self.logger.info('Component update: status [%d]' % (self.status,))
@@ -117,13 +197,13 @@ class Configuration(object):
' status: [%d]' % (component_request.status_code, self.status)) ' status: [%d]' % (component_request.status_code, self.status))
def push_metrics(self): def push_metrics(self):
"""Pushes the total amount of seconds the request took to get a response from the URL.
It only will send a request if the metric id was set in the configuration.
"""
if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'): if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'):
params = {'id': self.data['cachet']['metric_id'], 'value': params = {'id': self.metric_id, 'value': self.request.elapsed.total_seconds(),
self.request.elapsed.total_seconds(), 'timestamp': 'timestamp': self.current_timestamp}
self.current_timestamp} metrics_request = requests.post('%s/metrics/%d/points' % (self.api_url, self.metric_id), params=params,
metrics_request = requests.post('%s/metrics/%d/points' %
(self.data['cachet']['api_url'],
self.data['cachet']['metric_id']), params=params,
headers=self.headers) headers=self.headers)
if metrics_request.ok: if metrics_request.ok:
@@ -134,11 +214,48 @@ class Configuration(object):
self.logger.warning('Metric upload failed with status [%d]' % self.logger.warning('Metric upload failed with status [%d]' %
(metrics_request.status_code,)) (metrics_request.status_code,))
def push_incident(self):
"""If the component status has changed, we create a new incident (if this is the first time it becomes unstable)
or updates the existing incident once it becomes healthy again.
"""
if hasattr(self, 'incident_id') and self.status == st.COMPONENT_STATUS_OPERATIONAL:
# If the incident already exists, it means it was unhealthy but now it's healthy again.
params = {'status': 4, 'visible': 1, 'component_id': self.component_id, 'component_status': self.status,
'notify': True}
incident_request = requests.put('%s/incidents/%d' % (self.api_url, self.incident_id), params=params,
headers=self.headers)
if incident_request.ok:
# Successful metrics upload
self.logger.info(
'Incident updated, API healthy again: component status [%d], message: "%s"' % (
self.status, self.message))
del self.incident_id
else:
self.logger.warning('Incident update failed with status [%d], message: "%s"' % (
incident_request.status_code, self.message))
elif not hasattr(self, 'incident_id') and self.status != st.COMPONENT_STATUS_OPERATIONAL:
# This is the first time the incident is being created.
params = {'name': 'URL unavailable', 'message': self.message, 'status': 1, 'visible': 1,
'component_id': self.component_id, 'component_status': self.status, 'notify': True}
incident_request = requests.post('%s/incidents' % (self.api_url,), params=params, headers=self.headers)
if incident_request.ok:
# Successful incident upload.
self.incident_id = incident_request.json()['data']['id']
self.logger.info(
'Incident uploaded, API unhealthy: component status [%d], message: "%s"' % (
self.status, self.message))
else:
self.logger.warning(
'Incident upload failed with status [%d], message: "%s"' % (
incident_request.status_code, self.message))
class Expectaction(object): class Expectaction(object):
"""Base class for URL result expectations. Any new excpectation should extend """Base class for URL result expectations. Any new excpectation should extend
this class and the name added to create() method. this class and the name added to create() method.
""" """
@staticmethod @staticmethod
def create(configuration): def create(configuration):
"""Creates a list of expectations based on the configuration types """Creates a list of expectations based on the configuration types
@@ -168,9 +285,9 @@ class HttpStatus(Expectaction):
def get_status(self, response): def get_status(self, response):
if response.status_code == self.status: if response.status_code == self.status:
return 1 return st.COMPONENT_STATUS_OPERATIONAL
else: else:
return 3 return st.COMPONENT_STATUS_PARTIAL_OUTAGE
def get_message(self, response): def get_message(self, response):
return 'Unexpected HTTP status (%s)' % (response.status_code,) return 'Unexpected HTTP status (%s)' % (response.status_code,)
@@ -185,27 +302,27 @@ class Latency(Expectaction):
def get_status(self, response): def get_status(self, response):
if response.elapsed.total_seconds() <= self.threshold: if response.elapsed.total_seconds() <= self.threshold:
return 1 return st.COMPONENT_STATUS_OPERATIONAL
else: else:
return 2 return st.COMPONENT_STATUS_PERFORMANCE_ISSUES
def get_message(self, response): def get_message(self, response):
return 'Latency above threshold: %.4f' % (response.elapsed.total_seconds(),) return 'Latency above threshold: %.4f seconds' % (response.elapsed.total_seconds(),)
def __str__(self): def __str__(self):
return repr('Latency threshold: %.4f' % (self.threshold,)) return repr('Latency threshold: %.4f seconds' % (self.threshold,))
class Regex(Expectaction): class Regex(Expectaction):
def __init__(self, configuration): def __init__(self, configuration):
self.regex_string = configuration['regex'] self.regex_string = configuration['regex']
self.regex = re.compile(configuration['regex']) self.regex = re.compile(configuration['regex'], re.UNICODE + re.DOTALL)
def get_status(self, response): def get_status(self, response):
if self.regex.match(response.text): if self.regex.match(response.text):
return 1 return st.COMPONENT_STATUS_OPERATIONAL
else: else:
return 3 return st.COMPONENT_STATUS_PARTIAL_OUTAGE
def get_message(self, response): def get_message(self, response):
return 'Regex did not match anything in the body' return 'Regex did not match anything in the body'

View File

@@ -1,38 +1,73 @@
#!/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
cachet server. cachet server.
""" """
self.configuration.evaluate() self.configuration.evaluate()
self.configuration.push_status()
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 Decorator(object):
def execute(self, configuration):
pass
class UpdateStatusDecorator(Decorator):
def execute(self, configuration):
configuration.push_status()
class CreateIncidentDecorator(Decorator):
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 = Agent(self.configuration) self.agent = self.get_agent()
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...')

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env python
COMPONENT_STATUS_OPERATIONAL = 1
COMPONENT_STATUS_PERFORMANCE_ISSUES = 2
COMPONENT_STATUS_PARTIAL_OUTAGE = 3
COMPONENT_STATUS_MAJOR_OUTAGE = 4
COMPONENT_STATUSES = [COMPONENT_STATUS_OPERATIONAL,
COMPONENT_STATUS_PERFORMANCE_ISSUES, COMPONENT_STATUS_PARTIAL_OUTAGE,
COMPONENT_STATUS_MAJOR_OUTAGE]

View File

@@ -1,17 +1,20 @@
endpoint: endpoint:
url: http://localhost:8080/swagger url: http://localhost:8080/swagger
method: GET method: GET
timeout: 0.01 timeout: 1
expectation: expectation:
- type: HTTP_STATUS - type: HTTP_STATUS
status: 200 status: 200
- type: LATENCY - type: LATENCY
threshold: 1 threshold: 1
- type: REGEX - type: REGEX
regex: '.*<body>.*' regex: '.*(<body).*'
cachet: cachet:
api_url: https://demo.cachethq.io/api/v1 api_url: https://demo.cachethq.io/api/v1
token: my_token token: my_token
component_id: 1 component_id: 1
#metric_id: 1 #metric_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
frequency: 30 frequency: 30

View File

@@ -1,4 +1,5 @@
PyYAML==3.11 PyYAML==3.11
codacy-coverage==1.2.18
ipython==4.2.0 ipython==4.2.0
mock==2.0.0 mock==2.0.0
pudb==2016.1 pudb==2016.1

View File

@@ -2,7 +2,7 @@
from distutils.core import setup from distutils.core import setup
setup(name='cachet-url-monitor', setup(name='cachet-url-monitor',
version='0.2', version='0.3',
description='Cachet URL monitor plugin', description='Cachet URL monitor plugin',
author='Mitsuo Takaki', author='Mitsuo Takaki',
author_email='mitsuotakaki@gmail.com', author_email='mitsuotakaki@gmail.com',

View File

@@ -1,31 +1,53 @@
#!/usr/bin/env python #!/usr/bin/env python
import mock
import unittest
import sys import sys
import unittest
import cachet_url_monitor.status
import mock
from requests import ConnectionError, HTTPError, Timeout from requests import ConnectionError, HTTPError, Timeout
sys.modules['requests'] = mock.Mock() sys.modules['requests'] = mock.Mock()
sys.modules['logging'] = mock.Mock() sys.modules['logging'] = mock.Mock()
from cachet_url_monitor.configuration import Configuration from cachet_url_monitor.configuration import Configuration
from test.test_support import EnvironmentVarGuard
class ConfigurationTest(unittest.TestCase): class ConfigurationTest(unittest.TestCase):
def setUp(self): def setUp(self):
def getLogger(name): def getLogger(name):
self.mock_logger = mock.Mock() self.mock_logger = mock.Mock()
return self.mock_logger return self.mock_logger
sys.modules['logging'].getLogger = getLogger sys.modules['logging'].getLogger = getLogger
def get(url, headers):
get_return = mock.Mock()
get_return.ok = True
get_return.json = mock.Mock()
get_return.json.return_value = {'data': {'status': 1}}
return get_return
sys.modules['requests'].get = get
self.env = EnvironmentVarGuard()
self.env.set('CACHET_TOKEN', 'token2')
self.configuration = Configuration('config.yml') self.configuration = Configuration('config.yml')
sys.modules['requests'].Timeout = Timeout sys.modules['requests'].Timeout = Timeout
sys.modules['requests'].ConnectionError = ConnectionError sys.modules['requests'].ConnectionError = ConnectionError
sys.modules['requests'].HTTPError = HTTPError sys.modules['requests'].HTTPError = HTTPError
def test_init(self): def test_init(self):
assert len(self.configuration.data) == 3 self.assertEqual(len(self.configuration.data), 3, 'Configuration data size is incorrect')
assert len(self.configuration.expectations) == 3 self.assertEquals(len(self.configuration.expectations), 3, 'Number of expectations read from file is incorrect')
self.assertDictEqual(self.configuration.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
self.assertEquals(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
'Cachet API URL was set incorrectly')
def test_evaluate(self): def test_evaluate(self):
def total_seconds(): def total_seconds():
return 0.1 return 0.1
def request(method, url, timeout=None): def request(method, url, timeout=None):
response = mock.Mock() response = mock.Mock()
response.status_code = 200 response.status_code = 200
@@ -37,11 +59,13 @@ class ConfigurationTest(unittest.TestCase):
sys.modules['requests'].request = request sys.modules['requests'].request = request
self.configuration.evaluate() self.configuration.evaluate()
assert self.configuration.status == 1 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_with_failure(self): def test_evaluate_with_failure(self):
def total_seconds(): def total_seconds():
return 0.1 return 0.1
def request(method, url, timeout=None): def request(method, url, timeout=None):
response = mock.Mock() response = mock.Mock()
# We are expecting a 200 response, so this will fail the expectation. # We are expecting a 200 response, so this will fail the expectation.
@@ -54,76 +78,80 @@ class ConfigurationTest(unittest.TestCase):
sys.modules['requests'].request = request sys.modules['requests'].request = request
self.configuration.evaluate() self.configuration.evaluate()
assert self.configuration.status == 3 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
'Component status set incorrectly')
def test_evaluate_with_timeout(self): def test_evaluate_with_timeout(self):
def request(method, url, timeout=None): def request(method, url, timeout=None):
assert method == 'GET' self.assertEquals(method, 'GET', 'Incorrect HTTP method')
assert url == 'http://localhost:8080/swagger' self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
assert timeout == 0.010 self.assertEquals(timeout, 0.010)
raise Timeout() raise Timeout()
sys.modules['requests'].request = request sys.modules['requests'].request = request
self.configuration.evaluate() self.configuration.evaluate()
assert self.configuration.status == 3 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES,
'Component status set incorrectly')
self.mock_logger.warning.assert_called_with('Request timed out') self.mock_logger.warning.assert_called_with('Request timed out')
def test_evaluate_with_connection_error(self): def test_evaluate_with_connection_error(self):
def request(method, url, timeout=None): def request(method, url, timeout=None):
assert method == 'GET' self.assertEquals(method, 'GET', 'Incorrect HTTP method')
assert url == 'http://localhost:8080/swagger' self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
assert timeout == 0.010 self.assertEquals(timeout, 0.010)
raise ConnectionError() raise ConnectionError()
sys.modules['requests'].request = request sys.modules['requests'].request = request
self.configuration.evaluate() self.configuration.evaluate()
assert self.configuration.status == 3 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
self.mock_logger.warning.assert_called_with(('The URL is ' 'Component status set incorrectly')
'unreachable: GET http://localhost:8080/swagger')) self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
def test_evaluate_with_http_error(self): def test_evaluate_with_http_error(self):
def request(method, url, timeout=None): def request(method, url, timeout=None):
assert method == 'GET' self.assertEquals(method, 'GET', 'Incorrect HTTP method')
assert url == 'http://localhost:8080/swagger' self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
assert timeout == 0.010 self.assertEquals(timeout, 0.010)
raise HTTPError() raise HTTPError()
sys.modules['requests'].request = request sys.modules['requests'].request = request
self.configuration.evaluate() self.configuration.evaluate()
assert self.configuration.status == 3 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
self.mock_logger.exception.assert_called_with(('Unexpected HTTP ' 'Component status set incorrectly')
'response')) self.mock_logger.exception.assert_called_with('Unexpected HTTP response')
def test_push_status(self): def test_push_status(self):
def put(url, params=None, headers=None): def put(url, params=None, headers=None):
assert url == 'https://demo.cachethq.io/api/v1/components/1' self.assertEquals(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
assert params == {'id': 1, 'status': 1} self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
assert headers == {'X-Cachet-Token': 'my_token'} self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock() response = mock.Mock()
response.status_code = 200 response.status_code = 200
return response return response
sys.modules['requests'].put = put sys.modules['requests'].put = put
self.configuration.status = 1 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status() self.configuration.push_status()
def test_push_status_with_failure(self): def test_push_status_with_failure(self):
def put(url, params=None, headers=None): def put(url, params=None, headers=None):
assert url == 'https://demo.cachethq.io/api/v1/components/1' self.assertEquals(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
assert params == {'id': 1, 'status': 1} self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
assert headers == {'X-Cachet-Token': 'my_token'} self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock() response = mock.Mock()
response.status_code = 300 response.status_code = 400
return response return response
sys.modules['requests'].put = put sys.modules['requests'].put = put
self.configuration.status = 1 self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status() self.configuration.push_status()

View File

@@ -1,9 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import unittest
import mock import mock
import re import re
import unittest
from cachet_url_monitor.configuration import Expectaction,Latency
from cachet_url_monitor.configuration import HttpStatus, Regex from cachet_url_monitor.configuration import HttpStatus, Regex
from cachet_url_monitor.configuration import Latency
class LatencyTest(unittest.TestCase): class LatencyTest(unittest.TestCase):
@@ -16,6 +17,7 @@ class LatencyTest(unittest.TestCase):
def test_get_status_healthy(self): def test_get_status_healthy(self):
def total_seconds(): def total_seconds():
return 0.1 return 0.1
request = mock.Mock() request = mock.Mock()
elapsed = mock.Mock() elapsed = mock.Mock()
request.elapsed = elapsed request.elapsed = elapsed
@@ -26,6 +28,7 @@ class LatencyTest(unittest.TestCase):
def test_get_status_unhealthy(self): def test_get_status_unhealthy(self):
def total_seconds(): def total_seconds():
return 2 return 2
request = mock.Mock() request = mock.Mock()
elapsed = mock.Mock() elapsed = mock.Mock()
request.elapsed = elapsed request.elapsed = elapsed
@@ -36,13 +39,14 @@ class LatencyTest(unittest.TestCase):
def test_get_message(self): def test_get_message(self):
def total_seconds(): def total_seconds():
return 0.1 return 0.1
request = mock.Mock() request = mock.Mock()
elapsed = mock.Mock() elapsed = mock.Mock()
request.elapsed = elapsed request.elapsed = elapsed
elapsed.total_seconds = total_seconds elapsed.total_seconds = total_seconds
assert self.expectation.get_message(request) == ('Latency above ' assert self.expectation.get_message(request) == ('Latency above '
'threshold: 0.1000') 'threshold: 0.1000 seconds')
class HttpStatusTest(unittest.TestCase): class HttpStatusTest(unittest.TestCase):
@@ -77,11 +81,11 @@ class RegexTest(unittest.TestCase):
self.expectation = Regex({'type': 'REGEX', 'regex': '.*(find stuff).*'}) self.expectation = Regex({'type': 'REGEX', 'regex': '.*(find stuff).*'})
def test_init(self): def test_init(self):
assert self.expectation.regex == re.compile('.*(find stuff).*') assert self.expectation.regex == re.compile('.*(find stuff).*', re.UNICODE + re.DOTALL)
def test_get_status_healthy(self): def test_get_status_healthy(self):
request = mock.Mock() request = mock.Mock()
request.text = 'We could find stuff in this body.' request.text = 'We could find stuff\n in this body.'
assert self.expectation.get_status(request) == 1 assert self.expectation.get_status(request) == 1

View File

@@ -1,9 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import mock
import unittest
import sys import sys
import unittest
import mock
sys.modules['schedule'] = mock.Mock() sys.modules['schedule'] = mock.Mock()
sys.modules['cachet_url_monitor.configuration.Configuration'] = mock.Mock()
from cachet_url_monitor.scheduler import Agent, Scheduler from cachet_url_monitor.scheduler import Agent, Scheduler
@@ -21,7 +22,7 @@ class AgentTest(unittest.TestCase):
self.agent.execute() self.agent.execute()
evaluate.assert_called_once() evaluate.assert_called_once()
push_status.assert_called_once() push_status.assert_not_called()
def test_start(self): def test_start(self):
every = sys.modules['schedule'].every every = sys.modules['schedule'].every
@@ -33,9 +34,17 @@ class AgentTest(unittest.TestCase):
class SchedulerTest(unittest.TestCase): class SchedulerTest(unittest.TestCase):
def setUp(self): @mock.patch('requests.get')
self.mock_configuration = sys.modules[('cachet_url_monitor.configuration' def setUp(self, mock_requests):
'.Configuration')] def get(url, headers):
get_return = mock.Mock()
get_return.ok = True
get_return.json = mock.Mock()
get_return.json.return_value = {'data': {'status': 1}}
return get_return
mock_requests.get = get
self.scheduler = Scheduler('config.yml') self.scheduler = Scheduler('config.yml')
def test_init(self): def test_init(self):