80 Commits
0.2 ... 0.5

Author SHA1 Message Date
Mitsuo Takaki
b3e4000cc1 Fixing the version number 2019-10-25 08:12:10 -07:00
Mitsuo Takaki
e132f8660b #72 - Adding the ability to control incident status 2019-10-25 08:10:12 -07:00
Mitsuo Takaki
9e5d42f8b8 Fixing broken build, due to updates to the libraries 2019-10-24 21:31:04 -07:00
Mitsuo Takaki
cb6f405e82 Updating the instructions on how to run it 2019-10-24 07:35:51 -07:00
mazhead
a296cfc1aa Dont update if same (#64)
* Return status as integer

During the first start the if statement on line 237 does not get evaluated correctly and will not "return"
Therefore by any restart the first evaluation triggers a update even if the original status is same.

* Prevent unecessery push

By a case where allowed_fails > 0 and one failure is received the url monitor pushes a unnecessary update as the component state was not changed, resulting in a notification of "component is operational", but in fact the component was still operation before the update.

This creates another api call (get) towards the cachet, but it is a better trade-off to prevent unnecessary notifications for end-users.
2019-04-04 08:44:34 -07:00
mazhead
9569818bb0 Return status as integer (#63)
During the first start the if statement on line 237 does not get evaluated correctly and will not "return"
Therefore by any restart the first evaluation triggers a update even if the original status is same.
2019-04-04 08:39:25 -07:00
mazhead
3af53ce9b6 Add client header option to requests (#61)
* Add header option

* Add header option

* Add unit test for headers

* Add client header

* Remove environ for HEADER

environ does not support dictionaries, so HEADER can't be passed.
2019-03-31 01:18:48 -07:00
Mitsuo Takaki
eae51967c4 Fixing the broken test 2019-02-24 22:01:54 -08:00
Patrick Fruh
f1e69bf39b Fixed metric latency unit and logging (#49)
* move latency_unit into cachet dictionary

* log actual metric latency_unit

* also update config in readme

* also update config description in readme
2019-02-24 21:59:02 -08:00
Mitsuo Takaki
393aaa0b30 Adding coverage to development dependencies 2019-02-23 21:52:05 -08:00
Mitsuo Takaki
950ef86f33 Trying to integrate codacy and coveralls back into the build, now using circleci 2019-02-23 21:46:16 -08:00
Mitsuo Takaki
fbc87b7846 Updating the build badge 2019-02-22 02:19:09 -08:00
mtakaki
10e0141454 Upgrading to python3 (#60)
* Migrating to python 3.7.2, but docker image is not working properly.

Need to continue investigating why it's not properly running.

* Trying to fix the build and fixing logging in the scheduler initialization.

* Trying to fix the build

* Collecting test results

* Fixing the dockerfile

* Updating the development dependencies
2019-02-22 02:17:13 -08:00
Mitsuo Takaki
ea4b8ccd4e Trying to fix build 2019-02-03 23:34:54 -08:00
Mitsuo Takaki
200b255e6b Trying to fix build 2019-02-03 23:31:38 -08:00
Mitsuo Takaki
300ac29a24 Trying to fix build 2019-02-03 23:27:09 -08:00
Mitsuo Takaki
609cc57fd9 Trying to fix circleci build 2019-02-03 23:21:30 -08:00
Mitsuo Takaki
36f4ef6cee Trying to fix circleci build 2019-02-03 23:19:20 -08:00
Mitsuo Takaki
8fc0f7c77f Trying to fix circleci build 2019-02-03 23:12:41 -08:00
Mitsuo Takaki
54cfda3177 Upgrading dependencies to fix vulnerabilities 2019-01-19 13:55:05 -08:00
Mitsuo Takaki
a9035362b9 Upgrading dependencies to fix vulnerabilities 2019-01-19 13:38:57 -08:00
mtakaki
2c354b0960 Merge pull request #36 from jacekszubert/dont_push_status
Update component status only when it has changed
2018-09-07 00:32:42 -07:00
Mitsuo Takaki
5b6c34741a Fixing circleci configuration 2018-06-27 23:00:05 -07:00
Mitsuo Takaki
29eb92790f Setting up circle-ci, as codeship is currently not supporting builds from forks 2018-06-11 23:31:13 -07:00
mtakaki
1f1bab6398 Merge branch 'master' into dont_push_status 2018-03-20 00:20:33 -07:00
mtakaki
79bb5c5c3a Merge pull request #33 from jacekszubert/allowed_fails
Create incident/update component status only after specified amount
2018-03-19 23:41:49 -07:00
mtakaki
57fc100d49 Merge branch 'master' into allowed_fails 2018-03-19 23:38:52 -07:00
Mitsuo Takaki
5cfef6392e Adding setup.cfg for pypi 2018-03-18 22:47:44 -07:00
Mitsuo Takaki
68a5609abc Bumping up the version to 0.4 2018-03-18 22:36:58 -07:00
mtakaki
028488b503 Merge pull request #44 from mtakaki/mtakaki_manual_testing
Running some manual tests and updating Dockerfile
2018-03-18 22:21:43 -07:00
Mitsuo Takaki
4c336ec714 Running some manual tests and updating Dockerfile 2018-03-18 22:17:29 -07:00
mtakaki
2adb5ca095 Merge pull request #43 from mtakaki/mtakaki_38_latency_ms
#38 - Adding support to milliseconds and different units for latency …
2018-03-18 17:54:18 -07:00
Mitsuo Takaki
d3c14e6491 Addressing codacy issues 2018-03-18 17:44:01 -07:00
Mitsuo Takaki
1899e95642 #38 - Adding support to milliseconds and different units for latency and changing http status to a range, instead of a single value. 2018-03-18 16:58:02 -07:00
mtakaki
726f5377b1 Merge pull request #42 from mtakaki/mtakaki_fix_build
Fixing the build and upgrading pytest and pytest-cov to latest
2018-03-18 14:45:51 -07:00
Mitsuo Takaki
9e66736f48 Fixing the build and upgrading pytest and pytest-cov to latest 2018-03-18 14:43:41 -07:00
mtakaki
5e5e74938b Merge branch 'master' into dont_push_status 2017-05-30 22:28:37 -07:00
mtakaki
350d125d26 Merge branch 'master' into allowed_fails 2017-03-12 11:57:32 -07:00
Jacek Szubert
0b1e83eae1 Create incident/update component status only after specified amount of failed connection trials 2017-03-12 19:51:25 +11:00
mtakaki
8ce89e452f Merge pull request #34 from jacekszubert/default_metric
Push default metric value for failed connection trials
2017-03-11 18:56:08 -08:00
mtakaki
284ef97168 Merge pull request #35 from jacekszubert/better_url_handling
Better url scheme handling
2017-03-11 16:54:37 -08:00
Jacek Szubert
22d032308f Better url scheme handling 2017-03-11 18:58:11 +11:00
Jacek Szubert
da7568300e Update component status only when it has changed 2017-03-09 18:25:45 +11:00
Jacek Szubert
fd0cca2060 Push default metric value for failed connection trials 2017-03-09 13:35:05 +11:00
mtakaki
ffa141d114 Merge pull request #32 from jacekszubert/default_schema
Use http as a default schema
2017-03-08 00:43:23 -08:00
Jacek Szubert
ab9957761c Use http as a default schema 2017-03-08 16:55:55 +11:00
mtakaki
6c1b95961e Merge pull request #31 from jacekszubert/remove_unused_functs
Remove unused functions
2017-03-06 01:14:02 -08:00
mtakaki
c36e42ea11 Merge branch 'master' into remove_unused_functs 2017-03-06 01:11:04 -08:00
mtakaki
19e6811900 Merge pull request #30 from jacekszubert/private_incidents
Added configuration for incident visibility
2017-03-06 01:09:53 -08:00
Jacek Szubert
cf43e568b1 Remove unused functions 2017-03-06 15:17:49 +11:00
Jacek Szubert
f7381aa2bc Added configuration for incident visibility 2017-03-03 16:26:58 +11:00
Mitsuo Takaki
cfd0ddcb2b Removing specific docker tag from the docker example
Signed-off-by: Mitsuo Takaki <mitsuotakaki@gmail.com>
2017-02-11 14:27:44 -08:00
Mitsuo Takaki
a055e0b76d Reverting change to config.yml to fix breaking tests. 2017-02-11 14:13:48 -08:00
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
18 changed files with 670 additions and 171 deletions

56
.circleci/config.yml Normal file
View File

@@ -0,0 +1,56 @@
# Python CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-python/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
- image: circleci/python:3.7.2
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "dev_requirements.txt" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run:
name: install dependencies
command: |
sudo pip3 install virtualenv
virtualenv .
source bin/activate
pip3 install -r dev_requirements.txt
pip3 install -r requirements.txt
python3 setup.py install
pip3 install coveralls
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "dev_requirements.txt" }}
- run:
name: run tests
command: |
. bin/activate
py.test tests --junitxml=test-reports/junit.xml --cov=cachet_url_monitor
coveralls
coverage xml
python-codacy-coverage -r coverage.xml
- store_test_results:
path: test-reports
- store_artifacts:
path: test-reports
destination: test-reports

5
.gitignore vendored
View File

@@ -10,3 +10,8 @@ share/
*.egg-info
MANIFEST
dist/
.idea
.pytest_cache/
pip-selfcheck.json
.eggs
test-reports/

View File

@@ -1,7 +1,17 @@
FROM python:2-onbuild
FROM python:3.7.2-alpine
MAINTAINER Mitsuo Takaki <mitsuotakaki@gmail.com>
WORKDIR /usr/src/app
RUN python3.7 -m pip install --upgrade pip
COPY requirements.txt ./
RUN pip3 install --no-cache-dir -r requirements.txt
COPY cachet_url_monitor/*.py /usr/src/app/cachet_url_monitor/
COPY setup.py /usr/src/app/
RUN python3.7 setup.py install
COPY config.yml /usr/src/app/config/
VOLUME /usr/src/app/config/
ENTRYPOINT ["python", "cachet_url_monitor/scheduler.py", "config/config.yml"]
CMD ["python3.7", "./cachet_url_monitor/scheduler.py", "config/config.yml"]

View File

@@ -1,7 +1,11 @@
# Status
![Build Status](https://codeship.com/projects/5a246b70-f088-0133-9388-2640b49afa9e/status?branch=master)
[![CircleCI](https://circleci.com/gh/mtakaki/cachet-url-monitor/tree/master.svg?style=svg)](https://circleci.com/gh/mtakaki/cachet-url-monitor/tree/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)
[![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
========================
@@ -15,69 +19,98 @@ This project is available at PyPI: [https://pypi.python.org/pypi/cachet-url-moni
endpoint:
url: http://www.google.com
method: GET
timeout: 0.010 # seconds
header:
SOME-HEADER: SOME-VALUE
timeout: 1 # seconds
expectation:
- type: HTTP_STATUS
status: 200
status_range: 200-300
incident: MAJOR
- type: LATENCY
threshold: 1
- type: REGEX
regex: ".*<body>.*"
allowed_fails: 0
cachet:
api_url: http://status.cachethq.io/api/v1/
api_url: http://status.cachethq.io/api/v1
token: my_token
component_id: 1
metric_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
public_incidents: true
latency_unit: ms
frequency: 30
```
- **endpoint**, the configuration about the URL that will be monitored.
- **url**, the URL that is going to be monitored.
- **method**, the HTTP method that will be used by the monitor.
- **header**, client header passed to the request. Remove if you do not want to pass a header.
- **timeout**, how long we'll wait to consider the request failed. The unit of it is seconds.
- **expectation**, the list of expectations set for the URL.
- **HTTP_STATUS**, we will verify if the response status code matches what we expect.
- **HTTP_STATUS**, we will verify if the response status code falls into the expected range. Please keep in mind the range is inclusive on the first number and exclusive on the second number. If just one value is specified, it will default to only the given value, for example `200` will be converted to `200-201`.
- **LATENCY**, we measure how long the request took to get a response and fail if it's above the threshold. The unit is in seconds.
- **REGEX**, we verify if the response body matches the given regex.
- **allowed_fails**, create incident/update component status only after specified amount of failed connection trials.
- **cachet**, this is the settings for our cachet server.
- **api_url**, the cachet API endpoint.
- **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.
- **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
- **public_incidents**, boolean to decide if created incidents should be visible to everyone or only to logged in users. Important only if `CREATE_INCIDENT` or `UPDATE_STATUS` are set.
- **latency_unit**, the latency unit used when reporting the metrics. It will automatically convert to the specified unit. It's not mandatory and it will default to **seconds**. Available units: `ms`, `s`, `m`, `h`.
- **frequency**, how often we'll send a request to the given URL. The unit is in seconds.
Each `expectation` has their own default incident status. It can be overridden by setting the `incident` property to any of the following values:
- `PARTIAL`
- `MAJOR`
- `PERFORMANCE`
By choosing any of the aforementioned statuses, it will let you control the kind of incident it should be considered. These are the default incident status for each `expectation` type:
| Expectation | Incident status |
| ----------- | --------------- |
| HTTP_STATUS | PARTIAL |
| LATENCY | PERFORMANCE |
| REGEX | PARTIAL |
## Setting up
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
$ cd cachet-url-monitor
$ source bin/activate
$ pip install -r requirements.txt
$ python3 setup.py install
```
To start the agent:
```
$ python cachet_url_monitor/scheduler.py config.yml
$ python3 cachet_url_monitor/scheduler.py config.yml
```
## Docker
You can run the agent in docker, so you won't need to worry about installing python, virtualenv, or any other dependency into your OS. The `Dockerfile` and `docker-compose.yml` files are already checked in and it's ready to be used.
You can run the agent in docker, so you won't need to worry about installing python, virtualenv, or any other dependency into your OS. The `Dockerfile` is already checked in and it's ready to be used.
To start the agent in a container using docker compose:
You have two choices, checking this repo out and building the docker image or it can be pulled 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-compose build
$ docker-compose up
$ docker pull mtakaki/cachet-url-monitor
$ docker run --rm -it -v "$PWD":/usr/src/app/config/ mtakaki/cachet-url-monitor
```
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`):
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 pull mtakaki/cachet-url-monitor:0.1
$ docker run --rm -it -v my_config/:/usr/src/app/config/ mtakaki/cachet-url-monitor:0.1
$ docker run --rm -it -v "$PWD"/my_config.yml:/usr/src/app/config/config.yml:ro mtakaki/cachet-url-monitor
```

View File

@@ -1,22 +1,30 @@
#!/usr/bin/env python
import abc
import copy
import logging
import os
import re
import requests
import time
from yaml import load
import requests
from yaml import dump
from yaml import load
from yaml import FullLoader
import cachet_url_monitor.latency_unit as latency_unit
import cachet_url_monitor.status as st
# This is the mandatory fields that must be in the configuration file in this
# same exact structure.
configuration_mandatory_fields = {
'endpoint': ['url', 'method', 'timeout', 'expectation'],
'cachet': ['api_url', 'token', 'component_id'],
'frequency': []}
'endpoint': ['url', 'method', 'timeout', 'expectation'],
'cachet': ['api_url', 'token', 'component_id'],
'frequency': []}
class ConfigurationValidationError(Exception):
"""Exception raised when there's a validation error."""
def __init__(self, value):
self.value = value
@@ -24,30 +32,123 @@ class ConfigurationValidationError(Exception):
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(f'Component with id [{self.component_id}] does not exist.')
class MetricNonexistentError(Exception):
"""Exception raised when the component does not exist."""
def __init__(self, metric_id):
self.metric_id = metric_id
def __str__(self):
return repr(f'Metric with id [{self.metric_id}] does not exist.')
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(f'{endpoint_url}/components/{component_id}', headers=headers)
if get_status_request.ok:
# The component exists.
return int(get_status_request.json()['data']['status'])
else:
raise ComponentNonexistentError(component_id)
def normalize_url(url):
"""If passed url doesn't include schema return it with default one - http."""
if not url.lower().startswith('http'):
return f'http://{url}'
return url
class Configuration(object):
"""Represents a configuration file, but it also includes the functionality
of assessing the API and pushing the results to cachet.
"""
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.config_file = config_file
self.data = load(file(self.config_file, 'r'))
self.data = load(open(self.config_file, 'r'), Loader=FullLoader)
self.current_fails = 0
self.trigger_update = True
# 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.logger.info('Monitoring URL: %s %s' %
(self.data['endpoint']['method'], self.data['endpoint']['url']))
self.expectations = [Expectaction.create(expectation) for expectation
in self.data['endpoint']['expectation']]
# We store the main information from the configuration file, so we don't keep reading from the data dictionary.
self.headers = {'X-Cachet-Token': os.environ.get('CACHET_TOKEN') or self.data['cachet']['token']}
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_url = normalize_url(self.endpoint_url)
self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1
self.endpoint_header = self.data['endpoint'].get('header') or None
self.allowed_fails = os.environ.get('ALLOWED_FAILS') or self.data['endpoint'].get('allowed_fails') or 0
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')
if self.metric_id is not None:
self.default_metric_value = self.get_default_metric_value(self.metric_id)
# The latency_unit configuration is not mandatory and we fallback to seconds, by default.
self.latency_unit = os.environ.get('LATENCY_UNIT') or self.data['cachet'].get('latency_unit') or 's'
# 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.previous_status = self.status
# Get remaining settings
self.public_incidents = int(
os.environ.get('CACHET_PUBLIC_INCIDENTS') or self.data['cachet']['public_incidents'])
self.logger.info('Monitoring URL: %s %s' % (self.endpoint_method, self.endpoint_url))
self.expectations = [Expectation.create(expectation) for expectation in self.data['endpoint']['expectation']]
for expectation in self.expectations:
self.logger.info('Registered expectation: %s' % (expectation,))
self.headers = {'X-Cachet-Token': self.data['cachet']['token']}
def get_default_metric_value(self, metric_id):
"""Returns default value for configured metric."""
get_metric_request = requests.get('%s/metrics/%s' % (self.api_url, metric_id), headers=self.headers)
if get_metric_request.ok:
return get_metric_request.json()['data']['default_value']
else:
raise MetricNonexistentError(metric_id)
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):
"""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 = []
for key, sub_entries in configuration_mandatory_fields.iteritems():
for key, sub_entries in configuration_mandatory_fields.items():
if key not in self.data:
configuration_errors.append(key)
@@ -58,14 +159,13 @@ class Configuration(object):
if ('endpoint' in self.data and 'expectation' in
self.data['endpoint']):
if (not isinstance(self.data['endpoint']['expectation'], list) or
(isinstance(self.data['endpoint']['expectation'], list) and
len(self.data['endpoint']['expectation']) == 0)):
(isinstance(self.data['endpoint']['expectation'], list) and
len(self.data['endpoint']['expectation']) == 0)):
configuration_errors.append('endpoint.expectation')
if len(configuration_errors) > 0:
raise ConfigurationValidationError(('Config file [%s] failed '
'validation. Missing keys: %s') % (self.config_file,
', '.join(configuration_errors)))
raise ConfigurationValidationError(
f"Config file [{self.config_file}] failed validation. Missing keys: {', '.join(configuration_errors)}")
def evaluate(self):
"""Sends the request to the URL set in the configuration and executes
@@ -73,84 +173,169 @@ class Configuration(object):
according to the expectation results.
"""
try:
self.request = requests.request(self.data['endpoint']['method'],
self.data['endpoint']['url'],
timeout=self.data['endpoint']['timeout'])
if self.endpoint_header is not None:
self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout,
headers=self.endpoint_header)
else:
self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout)
self.current_timestamp = int(time.time())
except requests.ConnectionError:
self.logger.warning('The URL is unreachable: %s %s' %
(self.data['endpoint']['method'],
self.data['endpoint']['url']))
self.status = 3
self.message = 'The URL is unreachable: %s %s' % (self.endpoint_method, self.endpoint_url)
self.logger.warning(self.message)
self.status = st.COMPONENT_STATUS_PARTIAL_OUTAGE
return
except requests.HTTPError:
self.logger.exception('Unexpected HTTP response')
self.status = 3
self.message = 'Unexpected HTTP response'
self.logger.exception(self.message)
self.status = st.COMPONENT_STATUS_PARTIAL_OUTAGE
return
except requests.Timeout:
self.logger.warning('Request timed out')
self.status = 3
self.message = 'Request timed out'
self.logger.warning(self.message)
self.status = st.COMPONENT_STATUS_PERFORMANCE_ISSUES
return
# We initially assume the API is healthy.
self.status = 1
self.status = st.COMPONENT_STATUS_OPERATIONAL
self.message = ''
for expectation in self.expectations:
status = expectation.get_status(self.request)
# The greater the status is, the worse the state of the API is.
if status > self.status:
self.status = status
self.message = expectation.get_message(self.request)
self.logger.info(self.message)
def print_out(self):
self.logger.info(f'Current configuration:\n{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 if_trigger_update(self):
"""
Checks if update should be triggered - trigger it for all operational states
and only for non-operational ones above the configured threshold (allowed_fails).
"""
if self.status != 1:
self.current_fails = self.current_fails + 1
self.logger.warning(f'Failure #{self.current_fails} with threshold set to {self.allowed_fails}')
if self.current_fails <= self.allowed_fails:
self.trigger_update = False
return
self.current_fails = 0
self.trigger_update = True
def push_status(self):
params = {'id': self.data['cachet']['component_id'], 'status':
self.status}
component_request = requests.put('%s/components/%d' %
(self.data['cachet']['api_url'],
self.data['cachet']['component_id']),
params=params, headers=self.headers)
"""Pushes the status of the component to the cachet server. It will update the component
status based on the previous call to evaluate().
"""
if self.previous_status == self.status:
return
self.previous_status = self.status
if not self.trigger_update:
return
self.api_component_status = get_current_status(self.api_url, self.component_id, self.headers)
if self.status == self.api_component_status:
return
params = {'id': self.component_id, 'status': self.status}
component_request = requests.put('%s/components/%d' % (self.api_url, self.component_id), params=params,
headers=self.headers)
if component_request.ok:
# Successful update
self.logger.info('Component update: status [%d]' % (self.status,))
else:
# Failed to update the API status
self.logger.warning('Component update failed with status [%d]: API'
' status: [%d]' % (component_request.status_code, self.status))
' status: [%d]' % (component_request.status_code, self.status))
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.
In case of failed connection trial pushes the default metric value.
"""
if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'):
params = {'id': self.data['cachet']['metric_id'], 'value':
self.request.elapsed.total_seconds(), 'timestamp':
self.current_timestamp}
metrics_request = requests.post('%s/metrics/%d/points' %
(self.data['cachet']['api_url'],
self.data['cachet']['metric_id']), params=params,
headers=self.headers)
# We convert the elapsed time from the request, in seconds, to the configured unit.
value = self.default_metric_value if self.status != 1 else latency_unit.convert_to_unit(self.latency_unit,
self.request.elapsed.total_seconds())
params = {'id': self.metric_id, 'value': value,
'timestamp': self.current_timestamp}
metrics_request = requests.post('%s/metrics/%d/points' % (self.api_url, self.metric_id), params=params,
headers=self.headers)
if metrics_request.ok:
# Successful metrics upload
self.logger.info('Metric uploaded: %.6f seconds' %
(self.request.elapsed.total_seconds(),))
self.logger.info('Metric uploaded: %.6f %s' % (value, self.latency_unit))
else:
self.logger.warning('Metric upload failed with status [%d]' %
(metrics_request.status_code,))
self.logger.warning(f'Metric upload failed with status [{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 not self.trigger_update:
return
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': self.public_incidents, 'component_id': self.component_id,
'component_status': self.status,
'notify': True}
incident_request = requests.put(f'{self.api_url}/incidents/{self.incident_id}', params=params,
headers=self.headers)
if incident_request.ok:
# Successful metrics upload
self.logger.info(
f'Incident updated, API healthy again: component status [{self.status}], message: "{self.message}"')
del self.incident_id
else:
self.logger.warning(
f'Incident update failed with status [{incident_request.status_code}], message: "{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': self.public_incidents,
'component_id': self.component_id, 'component_status': self.status, 'notify': True}
incident_request = requests.post(f'{self.api_url}/incidents', params=params, headers=self.headers)
if incident_request.ok:
# Successful incident upload.
self.incident_id = incident_request.json()['data']['id']
self.logger.info(
f'Incident uploaded, API unhealthy: component status [{self.status}], message: "{self.message}"')
else:
self.logger.warning(
f'Incident upload failed with status [{incident_request.status_code}], message: "{self.message}"')
class Expectaction(object):
"""Base class for URL result expectations. Any new excpectation should extend
class Expectation(object):
"""Base class for URL result expectations. Any new expectation should extend
this class and the name added to create() method.
"""
@staticmethod
def create(configuration):
"""Creates a list of expectations based on the configuration types
list.
"""
# If a need expectation is created, this is where we need to add it.
expectations = {
'HTTP_STATUS': HttpStatus,
'LATENCY': Latency,
'REGEX': Regex
}
'HTTP_STATUS': HttpStatus,
'LATENCY': Latency,
'REGEX': Regex
}
return expectations.get(configuration['type'])(configuration)
def __init__(self, configuration):
self.incident_status = self.parse_incident_status(configuration)
@abc.abstractmethod
def get_status(self, response):
"""Returns the status of the API, following cachet's component status
@@ -161,54 +346,83 @@ class Expectaction(object):
def get_message(self, response):
"""Gets the error message."""
@abc.abstractmethod
def get_default_incident(self):
"""Returns the default status when this incident happens."""
class HttpStatus(Expectaction):
def parse_incident_status(self, configuration):
return st.INCIDENT_MAP.get(configuration.get('incident', None), self.get_default_incident())
class HttpStatus(Expectation):
def __init__(self, configuration):
self.status = configuration['status']
self.status_range = HttpStatus.parse_range(configuration['status_range'])
super(HttpStatus, self).__init__(configuration)
@staticmethod
def parse_range(range_string):
statuses = range_string.split("-")
if len(statuses) == 1:
# When there was no range given, we should treat the first number as a single status check.
return int(statuses[0]), int(statuses[0]) + 1
else:
# We shouldn't look into more than one value, as this is a range value.
return int(statuses[0]), int(statuses[1])
def get_status(self, response):
if response.status_code == self.status:
return 1
if self.status_range[0] <= response.status_code < self.status_range[1]:
return st.COMPONENT_STATUS_OPERATIONAL
else:
return 3
return self.incident_status
def get_default_incident(self):
return st.COMPONENT_STATUS_PARTIAL_OUTAGE
def get_message(self, response):
return 'Unexpected HTTP status (%s)' % (response.status_code,)
return f'Unexpected HTTP status ({response.status_code})'
def __str__(self):
return repr('HTTP status: %s' % (self.status,))
return repr(f'HTTP status range: {self.status_range}')
class Latency(Expectaction):
class Latency(Expectation):
def __init__(self, configuration):
self.threshold = configuration['threshold']
super(Latency, self).__init__(configuration)
def get_status(self, response):
if response.elapsed.total_seconds() <= self.threshold:
return 1
return st.COMPONENT_STATUS_OPERATIONAL
else:
return 2
return self.incident_status
def get_default_incident(self):
return st.COMPONENT_STATUS_PERFORMANCE_ISSUES
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):
return repr('Latency threshold: %.4f' % (self.threshold,))
return repr('Latency threshold: %.4f seconds' % (self.threshold,))
class Regex(Expectaction):
class Regex(Expectation):
def __init__(self, configuration):
self.regex_string = configuration['regex']
self.regex = re.compile(configuration['regex'])
self.regex = re.compile(configuration['regex'], re.UNICODE + re.DOTALL)
super(Regex, self).__init__(configuration)
def get_status(self, response):
if self.regex.match(response.text):
return 1
return st.COMPONENT_STATUS_OPERATIONAL
else:
return 3
return self.incident_status
def get_default_incident(self):
return st.COMPONENT_STATUS_PARTIAL_OUTAGE
def get_message(self, response):
return 'Regex did not match anything in the body'
def __str__(self):
return repr('Regex: %s' % (self.regex_string,))
return repr(f'Regex: {self.regex_string}')

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env python
seconds_per_unit = {"ms": 1000, "milliseconds": 1000, "s": 1, "seconds": 1, "m": float(1) / 60,
"minutes": float(1) / 60, "h": float(1) / 3600, "hours": float(1) / 3600}
def convert_to_unit(time_unit, value):
"""
Will convert the given value from seconds to the given time_unit.
:param time_unit: The time unit to which the value will be converted to, from seconds.
This is a string parameter. The unit must be in the short form.
:param value: The given value that will be converted. This value must be in seconds.
:return: The converted value.
"""
return value * seconds_per_unit[time_unit]

View File

@@ -1,38 +1,74 @@
#!/usr/bin/env python
from configuration import Configuration
import logging
import schedule
import sys
import time
import schedule
from cachet_url_monitor.configuration import Configuration
class Agent(object):
"""Monitor agent that will be constantly verifying if the URL is healthy
and updating the component.
"""
def __init__(self, configuration):
def __init__(self, configuration, decorators=None):
self.configuration = configuration
if decorators is None:
decorators = []
self.decorators = decorators
def execute(self):
"""Will verify the API status and push the status and metrics to the
cachet server.
"""
self.configuration.evaluate()
self.configuration.push_status()
self.configuration.push_metrics()
self.configuration.if_trigger_update()
for decorator in self.decorators:
decorator.execute(self.configuration)
def start(self):
"""Sets up the schedule based on the configuration file."""
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):
def __init__(self, config_file):
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
self.configuration = Configuration(config_file)
self.agent = Agent(self.configuration)
self.agent = self.get_agent()
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):
self.agent.start()
self.logger.info('Starting monitor agent...')
@@ -48,7 +84,7 @@ if __name__ == "__main__":
handler.addFilter(logging.Filter('cachet_url_monitor'))
if len(sys.argv) <= 1:
logging.fatal('Missing configuration file argument')
logging.getLogger('cachet_url_monitor.scheduler').fatal('Missing configuration file argument')
sys.exit(1)
scheduler = Scheduler(sys.argv[1])

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python
"""
This file defines all the different status different values.
These are all constants and are coupled to cachet's API configuration.
"""
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]
INCIDENT_PARTIAL = 'PARTIAL'
INCIDENT_MAJOR = 'MAJOR'
INCIDENT_PERFORMANCE = 'PERFORMANCE'
INCIDENT_MAP = {
INCIDENT_PARTIAL: COMPONENT_STATUS_PARTIAL_OUTAGE,
INCIDENT_MAJOR: COMPONENT_STATUS_MAJOR_OUTAGE,
INCIDENT_PERFORMANCE: COMPONENT_STATUS_PERFORMANCE_ISSUES,
}

View File

@@ -1,17 +1,26 @@
endpoint:
url: http://localhost:8080/swagger
method: GET
header:
SOME-HEADER: SOME-VALUE
timeout: 0.01
expectation:
- type: HTTP_STATUS
status: 200
status_range: 200-300
incident: MAJOR
- type: LATENCY
threshold: 1
- type: REGEX
regex: '.*<body>.*'
regex: '.*(<body).*'
allowed_fails: 0
cachet:
api_url: https://demo.cachethq.io/api/v1
token: my_token
component_id: 1
#metric_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
public_incidents: true
latency_unit: ms
frequency: 30

View File

@@ -1,8 +1,7 @@
PyYAML==3.11
ipython==4.2.0
codacy-coverage==1.3.11
ipython==7.8.0
mock==2.0.0
pudb==2016.1
pytest==2.9.1
pytest-cov==2.2.1
requests==2.9.1
schedule==0.3.2
pytest==5.2.2
pytest-cov==2.8.1
coverage==4.5.2

View File

@@ -1,6 +0,0 @@
version: '2'
services:
monitor:
build: .
volumes:
- .:/usr/src/app/

View File

@@ -1,3 +1,3 @@
PyYAML==3.11
requests==2.9.1
schedule==0.3.2
PyYAML==5.1.2
requests==2.22.0
schedule==0.6.0

9
setup.cfg Normal file
View File

@@ -0,0 +1,9 @@
[metadata]
description-file = README.md
[aliases]
test=pytest
[tool:pytest]
addopts = --verbose
python_files = tests/*.py

View File

@@ -1,8 +1,9 @@
#!/usr/bin/env python
from distutils.core import setup
#from distutils.core import setup
from setuptools import setup
setup(name='cachet-url-monitor',
version='0.2',
version='0.5',
description='Cachet URL monitor plugin',
author='Mitsuo Takaki',
author_email='mitsuotakaki@gmail.com',
@@ -13,5 +14,7 @@ setup(name='cachet-url-monitor',
'requests',
'yaml',
'schedule',
]
)
],
setup_requires=["pytest-runner"],
tests_require=["pytest"]
)

View File

@@ -1,32 +1,54 @@
#!/usr/bin/env python
import mock
import unittest
import sys
from requests import ConnectionError,HTTPError,Timeout
import unittest
import mock
from requests import ConnectionError, HTTPError, Timeout
import cachet_url_monitor.status
sys.modules['requests'] = mock.Mock()
sys.modules['logging'] = mock.Mock()
from cachet_url_monitor.configuration import Configuration
import os
class ConfigurationTest(unittest.TestCase):
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
def setUp(self):
def getLogger(name):
self.mock_logger = mock.Mock()
return self.mock_logger
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, 'default_value': 0.5}}
return get_return
sys.modules['requests'].get = get
self.configuration = Configuration('config.yml')
sys.modules['requests'].Timeout = Timeout
sys.modules['requests'].ConnectionError = ConnectionError
sys.modules['requests'].HTTPError = HTTPError
def test_init(self):
assert len(self.configuration.data) == 3
assert len(self.configuration.expectations) == 3
self.assertEqual(len(self.configuration.data), 3, 'Number of root elements in config.yml is incorrect')
self.assertEqual(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.assertEqual(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
'Cachet API URL was set incorrectly')
self.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect')
def test_evaluate(self):
def total_seconds():
return 0.1
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
@@ -37,12 +59,32 @@ class ConfigurationTest(unittest.TestCase):
sys.modules['requests'].request = request
self.configuration.evaluate()
assert self.configuration.status == 1
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_without_header(self):
def total_seconds():
return 0.1
def request(method, url, headers=None, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
response.elapsed.total_seconds = total_seconds
response.text = '<body>'
return response
sys.modules['requests'].request = request
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_with_failure(self):
def total_seconds():
return 0.1
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
response = mock.Mock()
# We are expecting a 200 response, so this will fail the expectation.
response.status_code = 400
@@ -54,76 +96,80 @@ class ConfigurationTest(unittest.TestCase):
sys.modules['requests'].request = request
self.configuration.evaluate()
assert self.configuration.status == 3
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_MAJOR_OUTAGE,
'Component status set incorrectly or custom incident status is incorrectly parsed')
def test_evaluate_with_timeout(self):
def request(method, url, timeout=None):
assert method == 'GET'
assert url == 'http://localhost:8080/swagger'
assert timeout == 0.010
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise Timeout()
sys.modules['requests'].request = request
self.configuration.evaluate()
assert self.configuration.status == 3
self.assertEqual(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')
def test_evaluate_with_connection_error(self):
def request(method, url, timeout=None):
assert method == 'GET'
assert url == 'http://localhost:8080/swagger'
assert timeout == 0.010
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise ConnectionError()
sys.modules['requests'].request = request
self.configuration.evaluate()
assert self.configuration.status == 3
self.mock_logger.warning.assert_called_with(('The URL is '
'unreachable: GET http://localhost:8080/swagger'))
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
'Component status set incorrectly')
self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
def test_evaluate_with_http_error(self):
def request(method, url, timeout=None):
assert method == 'GET'
assert url == 'http://localhost:8080/swagger'
assert timeout == 0.010
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise HTTPError()
sys.modules['requests'].request = request
self.configuration.evaluate()
assert self.configuration.status == 3
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
'response'))
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
'Component status set incorrectly')
self.mock_logger.exception.assert_called_with('Unexpected HTTP response')
def test_push_status(self):
def put(url, params=None, headers=None):
assert url == 'https://demo.cachethq.io/api/v1/components/1'
assert params == {'id': 1, 'status': 1}
assert headers == {'X-Cachet-Token': 'my_token'}
self.assertEqual(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock()
response.status_code = 200
return response
sys.modules['requests'].put = put
self.configuration.status = 1
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status()
def test_push_status_with_failure(self):
def put(url, params=None, headers=None):
assert url == 'https://demo.cachethq.io/api/v1/components/1'
assert params == {'id': 1, 'status': 1}
assert headers == {'X-Cachet-Token': 'my_token'}
self.assertEqual(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock()
response.status_code = 300
response.status_code = 400
return response
sys.modules['requests'].put = put
self.configuration.status = 1
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status()

View File

@@ -1,9 +1,12 @@
#!/usr/bin/env python
import mock
import re
import unittest
from cachet_url_monitor.configuration import Expectaction,Latency
from cachet_url_monitor.configuration import HttpStatus,Regex
import mock
import pytest
from cachet_url_monitor.configuration import HttpStatus, Regex
from cachet_url_monitor.configuration import Latency
class LatencyTest(unittest.TestCase):
@@ -16,6 +19,7 @@ class LatencyTest(unittest.TestCase):
def test_get_status_healthy(self):
def total_seconds():
return 0.1
request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
@@ -26,6 +30,7 @@ class LatencyTest(unittest.TestCase):
def test_get_status_unhealthy(self):
def total_seconds():
return 2
request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
@@ -36,21 +41,33 @@ class LatencyTest(unittest.TestCase):
def test_get_message(self):
def total_seconds():
return 0.1
request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
elapsed.total_seconds = total_seconds
assert self.expectation.get_message(request) == ('Latency above '
'threshold: 0.1000')
'threshold: 0.1000 seconds')
class HttpStatusTest(unittest.TestCase):
def setUp(self):
self.expectation = HttpStatus({'type': 'HTTP_STATUS', 'status': 200})
self.expectation = HttpStatus({'type': 'HTTP_STATUS', 'status_range': "200-300"})
def test_init(self):
assert self.expectation.status == 200
assert self.expectation.status_range == (200, 300)
def test_init_with_one_status(self):
"""With only one value, we still expect a valid tuple"""
self.expectation = HttpStatus({'type': 'HTTP_STATUS', 'status_range': "200"})
assert self.expectation.status_range == (200, 201)
def test_init_with_invalid_number(self):
"""Invalid values should just fail with a ValueError, as we can't convert it to int."""
with pytest.raises(ValueError):
self.expectation = HttpStatus({'type': 'HTTP_STATUS', 'status_range': "foo"})
def test_get_status_healthy(self):
request = mock.Mock()
@@ -69,7 +86,7 @@ class HttpStatusTest(unittest.TestCase):
request.status_code = 400
assert self.expectation.get_message(request) == ('Unexpected HTTP '
'status (400)')
'status (400)')
class RegexTest(unittest.TestCase):
@@ -77,11 +94,11 @@ class RegexTest(unittest.TestCase):
self.expectation = Regex({'type': 'REGEX', 'regex': '.*(find stuff).*'})
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):
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
@@ -96,4 +113,4 @@ class RegexTest(unittest.TestCase):
request.text = 'We will not find it here'
assert self.expectation.get_message(request) == ('Regex did not match '
'anything in the body')
'anything in the body')

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env python
from cachet_url_monitor.latency_unit import convert_to_unit
def test_convert_to_unit_ms():
assert convert_to_unit("ms", 1) == 1000
def test_convert_to_unit_s():
assert convert_to_unit("s", 20) == 20
def test_convert_to_unit_m():
assert convert_to_unit("m", 3) == float(3) / 60
def test_convert_to_unit_h():
assert convert_to_unit("h", 7200) == 2

View File

@@ -1,10 +1,11 @@
#!/usr/bin/env python
import mock
import unittest
import sys
import unittest
import 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
class AgentTest(unittest.TestCase):
@@ -21,7 +22,7 @@ class AgentTest(unittest.TestCase):
self.agent.execute()
evaluate.assert_called_once()
push_status.assert_called_once()
push_status.assert_not_called()
def test_start(self):
every = sys.modules['schedule'].every
@@ -33,16 +34,24 @@ class AgentTest(unittest.TestCase):
class SchedulerTest(unittest.TestCase):
def setUp(self):
self.mock_configuration = sys.modules[('cachet_url_monitor.configuration'
'.Configuration')]
@mock.patch('requests.get')
def setUp(self, mock_requests):
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')
def test_init(self):
assert self.scheduler.stop == False
def test_start(self):
#TODO(mtakaki|2016-05-01): We need a better way of testing this method.
# TODO(mtakaki|2016-05-01): We need a better way of testing this method.
# Leaving it as a placeholder.
self.scheduler.stop = True
self.scheduler.start()