mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Compare commits
94 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c7fff8d92 | ||
|
|
5679bdaa52 | ||
|
|
0a55b1f513 | ||
|
|
bd610671fa | ||
|
|
46955addf1 | ||
|
|
df2d094dc6 | ||
|
|
79a9a7f8d6 | ||
|
|
b712eda001 | ||
|
|
e36a4e5429 | ||
|
|
eeec3a2220 | ||
|
|
5be0217c00 | ||
|
|
bcafbd64f7 | ||
|
|
a13a42d51c | ||
|
|
9a73063a6f | ||
|
|
b3e4000cc1 | ||
|
|
e132f8660b | ||
|
|
9e5d42f8b8 | ||
|
|
cb6f405e82 | ||
|
|
a296cfc1aa | ||
|
|
9569818bb0 | ||
|
|
3af53ce9b6 | ||
|
|
eae51967c4 | ||
|
|
f1e69bf39b | ||
|
|
393aaa0b30 | ||
|
|
950ef86f33 | ||
|
|
fbc87b7846 | ||
|
|
10e0141454 | ||
|
|
ea4b8ccd4e | ||
|
|
200b255e6b | ||
|
|
300ac29a24 | ||
|
|
609cc57fd9 | ||
|
|
36f4ef6cee | ||
|
|
8fc0f7c77f | ||
|
|
54cfda3177 | ||
|
|
a9035362b9 | ||
|
|
2c354b0960 | ||
|
|
5b6c34741a | ||
|
|
29eb92790f | ||
|
|
1f1bab6398 | ||
|
|
79bb5c5c3a | ||
|
|
57fc100d49 | ||
|
|
5cfef6392e | ||
|
|
68a5609abc | ||
|
|
028488b503 | ||
|
|
4c336ec714 | ||
|
|
2adb5ca095 | ||
|
|
d3c14e6491 | ||
|
|
1899e95642 | ||
|
|
726f5377b1 | ||
|
|
9e66736f48 | ||
|
|
5e5e74938b | ||
|
|
350d125d26 | ||
|
|
0b1e83eae1 | ||
|
|
8ce89e452f | ||
|
|
284ef97168 | ||
|
|
22d032308f | ||
|
|
da7568300e | ||
|
|
fd0cca2060 | ||
|
|
ffa141d114 | ||
|
|
ab9957761c | ||
|
|
6c1b95961e | ||
|
|
c36e42ea11 | ||
|
|
19e6811900 | ||
|
|
cf43e568b1 | ||
|
|
f7381aa2bc | ||
|
|
cfd0ddcb2b | ||
|
|
a055e0b76d | ||
|
|
0ed87469ce | ||
|
|
bdd74a89c7 | ||
|
|
cb5137c526 | ||
|
|
3830063ba4 | ||
|
|
e4bd02c44f | ||
|
|
e8d4b88c79 | ||
|
|
9f3e2b6eff | ||
|
|
740f726b48 | ||
|
|
a147adda35 | ||
|
|
c59126fd0f | ||
|
|
9ccdc7e5c3 | ||
|
|
96346926cd | ||
|
|
194a07c403 | ||
|
|
fe0325dc9e | ||
|
|
23326fd828 | ||
|
|
969a2b1580 | ||
|
|
b018f9e675 | ||
|
|
d63420ac01 | ||
|
|
a3a91edadc | ||
|
|
2c01d8eb30 | ||
|
|
a83abfd1d3 | ||
|
|
ca358eab2b | ||
|
|
0f53ff8678 | ||
|
|
9c8c89c1dd | ||
|
|
9051f2d9b3 | ||
|
|
15dc800c9b | ||
|
|
8fce82b721 |
55
.circleci/config.yml
Normal file
55
.circleci/config.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
# 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
|
||||
pip3 install coveralls
|
||||
|
||||
- save_cache:
|
||||
paths:
|
||||
- ./venv
|
||||
key: v1-dependencies-{{ checksum "dev_requirements.txt" }}
|
||||
|
||||
- run:
|
||||
name: run tests
|
||||
command: |
|
||||
. bin/activate
|
||||
python -m pytest 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
.codacy.yml
Normal file
5
.codacy.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
engines:
|
||||
pylint:
|
||||
enabled: true
|
||||
python_version: 3
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -10,3 +10,8 @@ share/
|
||||
*.egg-info
|
||||
MANIFEST
|
||||
dist/
|
||||
.idea
|
||||
.pytest_cache/
|
||||
pip-selfcheck.json
|
||||
.eggs
|
||||
test-reports/
|
||||
|
||||
14
Dockerfile
14
Dockerfile
@@ -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"]
|
||||
|
||||
171
README.md
171
README.md
@@ -1,7 +1,11 @@
|
||||
# Status
|
||||

|
||||
[](https://circleci.com/gh/mtakaki/cachet-url-monitor/tree/master)
|
||||
[](https://coveralls.io/github/mtakaki/cachet-url-monitor?branch=master)
|
||||
[](https://www.codacy.com/app/mitsuotakaki/cachet-url-monitor?utm_source=github.com&utm_medium=referral&utm_content=mtakaki/cachet-url-monitor&utm_campaign=Badge_Grade)
|
||||
[](https://hub.docker.com/r/mtakaki/cachet-url-monitor/)
|
||||
[](https://hub.docker.com/r/mtakaki/cachet-url-monitor/)
|
||||

|
||||
[](https://pypi.python.org/pypi/cachet-url-monitor)
|
||||
|
||||
cachet-url-monitor
|
||||
========================
|
||||
@@ -12,72 +16,159 @@ This project is available at PyPI: [https://pypi.python.org/pypi/cachet-url-moni
|
||||
## Configuration
|
||||
|
||||
```yaml
|
||||
endpoint:
|
||||
url: http://www.google.com
|
||||
method: GET
|
||||
timeout: 0.010 # seconds
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status: 200
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: ".*<body>.*"
|
||||
endpoints:
|
||||
- name: Google
|
||||
url: http://www.google.com
|
||||
method: GET
|
||||
header:
|
||||
SOME-HEADER: SOME-VALUE
|
||||
timeout: 1 # seconds
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 200-205
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: ".*<body>.*"
|
||||
allowed_fails: 0
|
||||
component_id: 1
|
||||
metric_id: 1
|
||||
action:
|
||||
- UPDATE_STATUS
|
||||
public_incidents: true
|
||||
latency_unit: ms
|
||||
frequency: 5
|
||||
- name: Amazon
|
||||
url: http://www.amazon.com
|
||||
method: GET
|
||||
header:
|
||||
SOME-HEADER: SOME-VALUE
|
||||
timeout: 1 # seconds
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 200-205
|
||||
incident: MAJOR
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: ".*<body>.*"
|
||||
threshold: 10
|
||||
allowed_fails: 0
|
||||
component_id: 2
|
||||
action:
|
||||
- CREATE_INCIDENT
|
||||
public_incidents: true
|
||||
latency_unit: ms
|
||||
frequency: 5
|
||||
cachet:
|
||||
api_url: http://status.cachethq.io/api/v1/
|
||||
token: my_token
|
||||
component_id: 1
|
||||
metric_id: 1
|
||||
frequency: 30
|
||||
api_url: http://status.cachethq.io/api/v1
|
||||
token: mytoken
|
||||
```
|
||||
|
||||
- **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.
|
||||
- **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.
|
||||
- **endpoints**, the configuration about the URL/Urls that will be monitored.
|
||||
- **name**, The name of the component. This is now mandatory (since 0.6.0) so we can distinguish the logs for each URL being monitored.
|
||||
- **url**, the URL that is going to be monitored. *mandatory*
|
||||
- **method**, the HTTP method that will be used by the monitor. *mandatory*
|
||||
- **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. *mandatory*
|
||||
- **expectation**, the list of expectations set for the URL. *mandatory*
|
||||
- **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.
|
||||
- **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.
|
||||
- **allowed_fails**, create incident/update component status only after specified amount of failed connection trials.
|
||||
- **component_id**, the id of the component we're monitoring. This will be used to update the status of the component. *mandatory*
|
||||
- **metric_id**, this will be used to store the latency of the API. If this is not set, it will be ignored.
|
||||
- **frequency**, how often we'll send a request to the given URL. The unit is in seconds.
|
||||
- **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.
|
||||
- **PUSH_METRICS**, uploads response latency metrics.
|
||||
- **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.
|
||||
- **cachet**, this is the settings for our cachet server.
|
||||
- **api_url**, the cachet API endpoint. *mandatory*
|
||||
- **token**, the API token. *mandatory*
|
||||
|
||||
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
|
||||
```bash
|
||||
$ 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
|
||||
```bash
|
||||
$ 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
|
||||
```bash
|
||||
$ 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:
|
||||
|
||||
```bash
|
||||
$ docker run --rm -it -v "$PWD"/my_config.yml:/usr/src/app/config/config.yml:ro mtakaki/cachet-url-monitor
|
||||
```
|
||||
$ 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
|
||||
|
||||
## Generating configuration from existing CachetHQ instance (since 0.6.2)
|
||||
|
||||
In order to expedite the creation of your configuration file, you can use the client to automatically scrape the CachetHQ instance and spit out a YAML file. It can be used like this:
|
||||
```bash
|
||||
$ python cachet_url_monitor/client.py http://localhost/api/v1 my-token test.yml
|
||||
```
|
||||
Or from docker (you will end up with a `test.yml` in your `$PWD/tmp` folder):
|
||||
```bash
|
||||
$ docker run --rm -it -v $PWD/tmp:/home/tmp/ mtakaki/cachet-url-monitor python3.7 ./cachet_url_monitor/client.py http://localhost/api/v1 my-token /home/tmp/test.yml
|
||||
```
|
||||
The arguments are:
|
||||
- **URL**, the CachetHQ API URL, so that means appending `/api/v1` to your hostname.
|
||||
- **token**, the token that has access to your CachetHQ instance.
|
||||
- **filename**, the file where it should write the configuration.
|
||||
|
||||
### Caveats
|
||||
Because we can't predict what expectations will be needed, it will default to these behavior:
|
||||
- Verify a [200-300[ HTTP status range.
|
||||
- If status fail, make the incident major and public.
|
||||
- Frequency of 30 seconds.
|
||||
- `GET` request.
|
||||
- Timeout of 1s.
|
||||
- We'll read the `link` field from the components and use it as the URL.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### SSLERROR
|
||||
If it's throwing the following exception:
|
||||
```python
|
||||
raise SSLError(e, request=request)
|
||||
requests.exceptions.SSLError: HTTPSConnectionPool(host='redacted', port=443): Max retries exceeded with url: /api/v1/components/19 (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:579)'),))
|
||||
```
|
||||
|
||||
It can be resolved by seting the CA bundle environment variable `REQUESTS_CA_BUNDLE` pointing at your certificate file. It can either be set in your python environment, before running this tool, or in your docker container.
|
||||
|
||||
144
cachet_url_monitor/client.py
Normal file
144
cachet_url_monitor/client.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python
|
||||
from typing import Dict
|
||||
from typing import Optional
|
||||
|
||||
import click
|
||||
import requests
|
||||
from yaml import dump
|
||||
from cachet_url_monitor import latency_unit, status, exceptions
|
||||
|
||||
|
||||
def normalize_url(url: str) -> str:
|
||||
"""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
|
||||
|
||||
|
||||
def save_config(config_map, filename: str):
|
||||
with open(filename, 'w') as file:
|
||||
dump(config_map, file)
|
||||
|
||||
|
||||
class CachetClient(object):
|
||||
"""Utility class to interact with CahetHQ server."""
|
||||
url: str
|
||||
token: str
|
||||
headers: Dict[str, str]
|
||||
|
||||
def __init__(self, url: str, token: str):
|
||||
self.url = normalize_url(url)
|
||||
self.token = token
|
||||
self.headers = {'X-Cachet-Token': token}
|
||||
|
||||
def get_components(self):
|
||||
"""Retrieves all components registered in cachet-hq"""
|
||||
return requests.get(f"{self.url}/components", headers=self.headers).json()['data']
|
||||
|
||||
def get_metrics(self):
|
||||
"""Retrieves all metrics registered in cachet-hq"""
|
||||
return requests.get(f"{self.url}/metrics", headers=self.headers).json()['data']
|
||||
|
||||
def generate_config(self):
|
||||
components = self.get_components()
|
||||
generated_endpoints = [
|
||||
{
|
||||
'name': component['name'],
|
||||
'url': component['link'],
|
||||
'method': 'GET',
|
||||
'timeout': 1,
|
||||
'expectation': [
|
||||
{
|
||||
'type': 'HTTP_STATUS',
|
||||
'status_range': '200-300',
|
||||
'incident': 'MAJOR'
|
||||
}
|
||||
],
|
||||
'allowed_fails': 0,
|
||||
'frequency': 30,
|
||||
'component_id': component['id'],
|
||||
'action': [
|
||||
'CREATE_INCIDENT',
|
||||
'UPDATE_STATUS',
|
||||
],
|
||||
'public_incidents': True,
|
||||
} for component in components if component['enabled']
|
||||
]
|
||||
generated_config = {
|
||||
'cachet': {
|
||||
'api_url': self.url,
|
||||
'token': self.token,
|
||||
},
|
||||
'endpoints': generated_endpoints
|
||||
}
|
||||
return generated_config
|
||||
|
||||
def get_default_metric_value(self, metric_id):
|
||||
"""Returns default value for configured metric."""
|
||||
get_metric_request = requests.get(f"{self.url}/metrics/{metric_id}", headers=self.headers)
|
||||
|
||||
if get_metric_request.ok:
|
||||
return get_metric_request.json()['data']['default_value']
|
||||
else:
|
||||
raise exceptions.MetricNonexistentError(metric_id)
|
||||
|
||||
def get_component_status(self, component_id: int) -> Optional[status.ComponentStatus]:
|
||||
"""Retrieves the current status of the given component. 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'{self.url}/components/{component_id}', headers=self.headers)
|
||||
|
||||
if get_status_request.ok:
|
||||
# The component exists.
|
||||
return status.ComponentStatus(int(get_status_request.json()['data']['status']))
|
||||
else:
|
||||
raise exceptions.ComponentNonexistentError(component_id)
|
||||
|
||||
def push_status(self, component_id: int, component_status: status.ComponentStatus):
|
||||
"""Pushes the status of the component to the cachet server.
|
||||
"""
|
||||
params = {'id': component_id, 'status': component_status.value}
|
||||
return requests.put(f"{self.url}/components/{component_id}", params=params, headers=self.headers)
|
||||
|
||||
def push_metrics(self, metric_id: int, latency_time_unit: str, elapsed_time_in_seconds: int, timestamp: int):
|
||||
"""Pushes the total amount of seconds the request took to get a response from the URL.
|
||||
"""
|
||||
value = latency_unit.convert_to_unit(latency_time_unit, elapsed_time_in_seconds)
|
||||
params = {'id': metric_id, 'value': value, 'timestamp': timestamp}
|
||||
return requests.post(f"{self.url}/metrics/{metric_id}/points", params=params, headers=self.headers)
|
||||
|
||||
def push_incident(self, status_value: status.ComponentStatus, is_public_incident: bool, component_id: int,
|
||||
previous_incident_id=None, message=None):
|
||||
"""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 previous_incident_id and status_value == status.ComponentStatus.OPERATIONAL:
|
||||
# If the incident already exists, it means it was unhealthy but now it's healthy again.
|
||||
params = {'status': status.IncidentStatus.FIXED.value, 'visible': is_public_incident,
|
||||
'component_id': component_id, 'component_status': status_value.value, 'notify': True}
|
||||
|
||||
return requests.put(f'{self.url}/incidents/{previous_incident_id}', params=params, headers=self.headers)
|
||||
elif not previous_incident_id and status_value != status.ComponentStatus.OPERATIONAL:
|
||||
# This is the first time the incident is being created.
|
||||
params = {'name': 'URL unavailable', 'message': message,
|
||||
'status': status.IncidentStatus.INVESTIGATING.value,
|
||||
'visible': is_public_incident, 'component_id': component_id, 'component_status': status_value.value,
|
||||
'notify': True}
|
||||
return requests.post(f'{self.url}/incidents', params=params, headers=self.headers)
|
||||
|
||||
@click.group()
|
||||
def run_client():
|
||||
pass
|
||||
|
||||
@click.command()
|
||||
@click.argument('url')
|
||||
@click.argument('token')
|
||||
@click.argument('output')
|
||||
def run_client(url, token, output):
|
||||
client = CachetClient(url, token)
|
||||
config = client.generate_config()
|
||||
save_config(config, output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_client()
|
||||
@@ -1,71 +1,133 @@
|
||||
#!/usr/bin/env python
|
||||
import abc
|
||||
import copy
|
||||
import logging
|
||||
import re
|
||||
import requests
|
||||
import time
|
||||
from yaml import load
|
||||
from typing import Dict
|
||||
|
||||
import requests
|
||||
from yaml import dump
|
||||
|
||||
import cachet_url_monitor.status as st
|
||||
from cachet_url_monitor.client import CachetClient, normalize_url
|
||||
from cachet_url_monitor.exceptions import ConfigurationValidationError
|
||||
from cachet_url_monitor.expectation import Expectation
|
||||
from cachet_url_monitor.status import ComponentStatus
|
||||
|
||||
# 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': []}
|
||||
|
||||
|
||||
class ConfigurationValidationError(Exception):
|
||||
"""Exception raised when there's a validation error."""
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return repr(self.value)
|
||||
configuration_mandatory_fields = ['url', 'method', 'timeout', 'expectation', 'component_id', 'frequency']
|
||||
|
||||
|
||||
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'))
|
||||
endpoint_index: int
|
||||
endpoint: str
|
||||
client: CachetClient
|
||||
token: str
|
||||
current_fails: int
|
||||
trigger_update: bool
|
||||
headers: Dict[str, str]
|
||||
|
||||
endpoint_method: str
|
||||
endpoint_url: str
|
||||
endpoint_timeout: int
|
||||
endpoint_header: Dict[str, str]
|
||||
|
||||
allowed_fails: int
|
||||
component_id: int
|
||||
metric_id: int
|
||||
default_metric_value: int
|
||||
latency_unit: str
|
||||
|
||||
status: ComponentStatus
|
||||
previous_status: ComponentStatus
|
||||
message: str
|
||||
|
||||
def __init__(self, config, endpoint_index: int, client: CachetClient, token: str):
|
||||
self.endpoint_index = endpoint_index
|
||||
self.data = config
|
||||
self.endpoint = self.data['endpoints'][endpoint_index]
|
||||
self.client = client
|
||||
self.token = token
|
||||
|
||||
self.current_fails = 0
|
||||
self.trigger_update = True
|
||||
|
||||
if 'name' not in self.endpoint:
|
||||
# We have to make this mandatory, otherwise the logs are confusing when there are multiple URLs.
|
||||
raise ConfigurationValidationError('name')
|
||||
|
||||
self.logger = logging.getLogger(f'cachet_url_monitor.configuration.Configuration.{self.endpoint["name"]}')
|
||||
|
||||
# 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': self.token}
|
||||
|
||||
self.endpoint_method = self.endpoint['method']
|
||||
self.endpoint_url = normalize_url(self.endpoint['url'])
|
||||
self.endpoint_timeout = self.endpoint.get('timeout') or 1
|
||||
self.endpoint_header = self.endpoint.get('header') or None
|
||||
self.allowed_fails = self.endpoint.get('allowed_fails') or 0
|
||||
|
||||
self.component_id = self.endpoint['component_id']
|
||||
self.metric_id = self.endpoint.get('metric_id')
|
||||
|
||||
if self.metric_id is not None:
|
||||
self.default_metric_value = self.client.get_default_metric_value(self.metric_id)
|
||||
|
||||
# The latency_unit configuration is not mandatory and we fallback to seconds, by default.
|
||||
self.latency_unit = 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 = self.client.get_component_status(self.component_id)
|
||||
self.previous_status = self.status
|
||||
self.logger.info(f'Component current status: {self.status}')
|
||||
|
||||
# Get remaining settings
|
||||
self.public_incidents = int(self.endpoint['public_incidents'])
|
||||
|
||||
self.logger.info('Monitoring URL: %s %s' % (self.endpoint_method, self.endpoint_url))
|
||||
self.expectations = [Expectation.create(expectation) for expectation in self.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_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.endpoint.get('action') is None:
|
||||
return []
|
||||
else:
|
||||
return self.endpoint['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():
|
||||
if key not in self.data:
|
||||
for key in configuration_mandatory_fields:
|
||||
if key not in self.endpoint:
|
||||
configuration_errors.append(key)
|
||||
|
||||
for sub_key in sub_entries:
|
||||
if sub_key not in self.data[key]:
|
||||
configuration_errors.append('%s.%s' % (key, sub_key))
|
||||
|
||||
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)):
|
||||
if 'expectation' in self.endpoint:
|
||||
if (not isinstance(self.endpoint['expectation'], list) or
|
||||
(isinstance(self.endpoint['expectation'], list) and
|
||||
len(self.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(
|
||||
'Endpoint [%s] failed validation. Missing keys: %s' % (self.endpoint,
|
||||
', '.join(configuration_errors)))
|
||||
|
||||
def evaluate(self):
|
||||
"""Sends the request to the URL set in the configuration and executes
|
||||
@@ -73,142 +135,136 @@ 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.ComponentStatus.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.ComponentStatus.PARTIAL_OUTAGE
|
||||
return
|
||||
except requests.Timeout:
|
||||
self.logger.warning('Request timed out')
|
||||
self.status = 3
|
||||
except (requests.Timeout, requests.ConnectTimeout):
|
||||
self.message = 'Request timed out'
|
||||
self.logger.warning(self.message)
|
||||
self.status = st.ComponentStatus.PERFORMANCE_ISSUES
|
||||
return
|
||||
|
||||
# We initially assume the API is healthy.
|
||||
self.status = 1
|
||||
self.status = st.ComponentStatus.OPERATIONAL
|
||||
self.message = ''
|
||||
for expectation in self.expectations:
|
||||
status = expectation.get_status(self.request)
|
||||
status: ComponentStatus = expectation.get_status(self.request)
|
||||
|
||||
# The greater the status is, the worse the state of the API is.
|
||||
if status > self.status:
|
||||
if status.value > self.status.value:
|
||||
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']
|
||||
temporary_data['endpoints'] = temporary_data['endpoints'][self.endpoint_index]
|
||||
|
||||
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 != st.ComponentStatus.OPERATIONAL:
|
||||
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:
|
||||
# We don't want to keep spamming if there's no change in status.
|
||||
self.logger.info(f'No changes to component status.')
|
||||
self.trigger_update = False
|
||||
return
|
||||
|
||||
self.previous_status = self.status
|
||||
|
||||
if not self.trigger_update:
|
||||
return
|
||||
|
||||
api_component_status = self.client.get_component_status(self.component_id)
|
||||
|
||||
if self.status == api_component_status:
|
||||
return
|
||||
|
||||
component_request = self.client.push_status(self.component_id, self.status)
|
||||
if component_request.ok:
|
||||
# Successful update
|
||||
self.logger.info('Component update: status [%d]' % (self.status,))
|
||||
self.logger.info(f'Component update: status [{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))
|
||||
self.logger.warning(f'Component update failed with HTTP status: {component_request.status_code}. API'
|
||||
f' status: {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.
|
||||
metrics_request = self.client.push_metrics(self.metric_id, self.latency_unit,
|
||||
self.request.elapsed.total_seconds(), self.current_timestamp)
|
||||
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' % (self.request.elapsed.total_seconds(), 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}]')
|
||||
|
||||
|
||||
class Expectaction(object):
|
||||
"""Base class for URL result expectations. Any new excpectation 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.
|
||||
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.
|
||||
"""
|
||||
expectations = {
|
||||
'HTTP_STATUS': HttpStatus,
|
||||
'LATENCY': Latency,
|
||||
'REGEX': Regex
|
||||
}
|
||||
return expectations.get(configuration['type'])(configuration)
|
||||
if not self.trigger_update:
|
||||
return
|
||||
if hasattr(self, 'incident_id') and self.status == st.ComponentStatus.OPERATIONAL:
|
||||
incident_request = self.client.push_incident(self.status, self.public_incidents, self.component_id,
|
||||
previous_incident_id=self.incident_id)
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_status(self, response):
|
||||
"""Returns the status of the API, following cachet's component status
|
||||
documentation: https://docs.cachethq.io/docs/component-statuses
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_message(self, response):
|
||||
"""Gets the error message."""
|
||||
|
||||
|
||||
class HttpStatus(Expectaction):
|
||||
def __init__(self, configuration):
|
||||
self.status = configuration['status']
|
||||
|
||||
def get_status(self, response):
|
||||
if response.status_code == self.status:
|
||||
return 1
|
||||
else:
|
||||
return 3
|
||||
|
||||
def get_message(self, response):
|
||||
return 'Unexpected HTTP status (%s)' % (response.status_code,)
|
||||
|
||||
def __str__(self):
|
||||
return repr('HTTP status: %s' % (self.status,))
|
||||
|
||||
|
||||
class Latency(Expectaction):
|
||||
def __init__(self, configuration):
|
||||
self.threshold = configuration['threshold']
|
||||
|
||||
def get_status(self, response):
|
||||
if response.elapsed.total_seconds() <= self.threshold:
|
||||
return 1
|
||||
else:
|
||||
return 2
|
||||
|
||||
def get_message(self, response):
|
||||
return 'Latency above threshold: %.4f' % (response.elapsed.total_seconds(),)
|
||||
|
||||
def __str__(self):
|
||||
return repr('Latency threshold: %.4f' % (self.threshold,))
|
||||
|
||||
|
||||
class Regex(Expectaction):
|
||||
def __init__(self, configuration):
|
||||
self.regex_string = configuration['regex']
|
||||
self.regex = re.compile(configuration['regex'])
|
||||
|
||||
def get_status(self, response):
|
||||
if self.regex.match(response.text):
|
||||
return 1
|
||||
else:
|
||||
return 3
|
||||
|
||||
def get_message(self, response):
|
||||
return 'Regex did not match anything in the body'
|
||||
|
||||
def __str__(self):
|
||||
return repr('Regex: %s' % (self.regex_string,))
|
||||
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.ComponentStatus.OPERATIONAL:
|
||||
incident_request = self.client.push_incident(self.status, self.public_incidents, self.component_id,
|
||||
message=self.message)
|
||||
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}"')
|
||||
|
||||
29
cachet_url_monitor/exceptions.py
Normal file
29
cachet_url_monitor/exceptions.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python
|
||||
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.')
|
||||
|
||||
|
||||
class ConfigurationValidationError(Exception):
|
||||
"""Exception raised when there's a validation error."""
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return repr(self.value)
|
||||
127
cachet_url_monitor/expectation.py
Normal file
127
cachet_url_monitor/expectation.py
Normal file
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env python
|
||||
import abc
|
||||
import re
|
||||
|
||||
import cachet_url_monitor.status as st
|
||||
from cachet_url_monitor.exceptions import ConfigurationValidationError
|
||||
from cachet_url_monitor.status import ComponentStatus
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
if configuration['type'] not in expectations:
|
||||
raise ConfigurationValidationError(f"Invalid type: {configuration['type']}")
|
||||
|
||||
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) -> ComponentStatus:
|
||||
"""Returns the status of the API, following cachet's component status
|
||||
documentation: https://docs.cachethq.io/docs/component-statuses
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_message(self, response) -> str:
|
||||
"""Gets the error message."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_default_incident(self):
|
||||
"""Returns the default status when this incident happens."""
|
||||
|
||||
def parse_incident_status(self, configuration) -> ComponentStatus:
|
||||
return st.INCIDENT_MAP.get(configuration.get('incident', None), self.get_default_incident())
|
||||
|
||||
|
||||
class HttpStatus(Expectation):
|
||||
def __init__(self, configuration):
|
||||
self.status_range = HttpStatus.parse_range(configuration['status_range'])
|
||||
super(HttpStatus, self).__init__(configuration)
|
||||
|
||||
@staticmethod
|
||||
def parse_range(range_string):
|
||||
if isinstance(range_string, int):
|
||||
# This happens when there's no range and no dash character, it will be parsed as int already.
|
||||
return range_string, range_string + 1
|
||||
|
||||
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) -> ComponentStatus:
|
||||
if self.status_range[0] <= response.status_code < self.status_range[1]:
|
||||
return st.ComponentStatus.OPERATIONAL
|
||||
else:
|
||||
return self.incident_status
|
||||
|
||||
def get_default_incident(self):
|
||||
return st.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def get_message(self, response):
|
||||
return f'Unexpected HTTP status ({response.status_code})'
|
||||
|
||||
def __str__(self):
|
||||
return repr(f'HTTP status range: [{self.status_range[0]}, {self.status_range[1]}[')
|
||||
|
||||
|
||||
class Latency(Expectation):
|
||||
def __init__(self, configuration):
|
||||
self.threshold = configuration['threshold']
|
||||
super(Latency, self).__init__(configuration)
|
||||
|
||||
def get_status(self, response) -> ComponentStatus:
|
||||
if response.elapsed.total_seconds() <= self.threshold:
|
||||
return st.ComponentStatus.OPERATIONAL
|
||||
else:
|
||||
return self.incident_status
|
||||
|
||||
def get_default_incident(self):
|
||||
return st.ComponentStatus.PERFORMANCE_ISSUES
|
||||
|
||||
def get_message(self, response):
|
||||
return 'Latency above threshold: %.4f seconds' % (response.elapsed.total_seconds(),)
|
||||
|
||||
def __str__(self):
|
||||
return repr('Latency threshold: %.4f seconds' % (self.threshold,))
|
||||
|
||||
|
||||
class Regex(Expectation):
|
||||
def __init__(self, configuration):
|
||||
self.regex_string = configuration['regex']
|
||||
self.regex = re.compile(configuration['regex'], re.UNICODE + re.DOTALL)
|
||||
super(Regex, self).__init__(configuration)
|
||||
|
||||
def get_status(self, response) -> ComponentStatus:
|
||||
if self.regex.match(response.text):
|
||||
return st.ComponentStatus.OPERATIONAL
|
||||
else:
|
||||
return self.incident_status
|
||||
|
||||
def get_default_incident(self):
|
||||
return st.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def get_message(self, response):
|
||||
return 'Regex did not match anything in the body'
|
||||
|
||||
def __str__(self):
|
||||
return repr(f'Regex: {self.regex_string}')
|
||||
17
cachet_url_monitor/latency_unit.py
Normal file
17
cachet_url_monitor/latency_unit.py
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
from typing import Dict
|
||||
|
||||
seconds_per_unit: Dict[str, float] = {"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: str, value: float):
|
||||
"""
|
||||
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]
|
||||
@@ -1,36 +1,79 @@
|
||||
#!/usr/bin/env python
|
||||
from configuration import Configuration
|
||||
import logging
|
||||
import schedule
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import os
|
||||
|
||||
import schedule
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
from cachet_url_monitor.client import CachetClient
|
||||
from cachet_url_monitor.configuration import Configuration
|
||||
|
||||
cachet_mandatory_fields = ['api_url', 'token']
|
||||
|
||||
|
||||
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)
|
||||
schedule.every(self.configuration.endpoint['frequency']).seconds.do(self.execute)
|
||||
|
||||
|
||||
class Decorator(object):
|
||||
"""Defines the actions a user can configure to be executed when there's an incident."""
|
||||
|
||||
def execute(self, configuration):
|
||||
pass
|
||||
|
||||
|
||||
class UpdateStatusDecorator(Decorator):
|
||||
"""Updates the component status when an incident happens."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_status()
|
||||
|
||||
|
||||
class CreateIncidentDecorator(Decorator):
|
||||
"""Creates an incident entry on cachet when an incident happens."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_incident()
|
||||
|
||||
|
||||
class PushMetricsDecorator(Decorator):
|
||||
"""Updates the URL latency metric."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_metrics()
|
||||
|
||||
|
||||
class Scheduler(object):
|
||||
def __init__(self, config_file):
|
||||
def __init__(self, configuration, agent):
|
||||
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
|
||||
self.configuration = Configuration(config_file)
|
||||
self.agent = Agent(self.configuration)
|
||||
self.configuration = configuration
|
||||
self.agent = agent
|
||||
self.stop = False
|
||||
|
||||
def start(self):
|
||||
@@ -38,7 +81,46 @@ class Scheduler(object):
|
||||
self.logger.info('Starting monitor agent...')
|
||||
while not self.stop:
|
||||
schedule.run_pending()
|
||||
time.sleep(self.configuration.data['frequency'])
|
||||
time.sleep(self.configuration.endpoint['frequency'])
|
||||
|
||||
|
||||
class NewThread(threading.Thread):
|
||||
def __init__(self, scheduler):
|
||||
threading.Thread.__init__(self)
|
||||
self.scheduler = scheduler
|
||||
|
||||
def run(self):
|
||||
self.scheduler.start()
|
||||
|
||||
|
||||
def build_agent(configuration, logger):
|
||||
action_names = {
|
||||
'CREATE_INCIDENT': CreateIncidentDecorator,
|
||||
'UPDATE_STATUS': UpdateStatusDecorator,
|
||||
'PUSH_METRICS': PushMetricsDecorator,
|
||||
}
|
||||
actions = []
|
||||
for action in configuration.get_action():
|
||||
logger.info(f'Registering action {action}')
|
||||
actions.append(action_names[action]())
|
||||
return Agent(configuration, decorators=actions)
|
||||
|
||||
|
||||
def validate_config():
|
||||
if 'endpoints' not in config_data.keys():
|
||||
fatal_error('Endpoints is a mandatory field')
|
||||
|
||||
if config_data['endpoints'] is None:
|
||||
fatal_error('Endpoints array can not be empty')
|
||||
|
||||
for key in cachet_mandatory_fields:
|
||||
if key not in config_data['cachet']:
|
||||
fatal_error('Missing cachet mandatory fields')
|
||||
|
||||
|
||||
def fatal_error(message):
|
||||
logging.getLogger('cachet_url_monitor.scheduler').fatal("%s", message)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -48,8 +130,20 @@ 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])
|
||||
scheduler.start()
|
||||
try:
|
||||
config_data = load(open(sys.argv[1], 'r'), SafeLoader)
|
||||
except FileNotFoundError:
|
||||
logging.getLogger('cachet_url_monitor.scheduler').fatal(f'File not found: {sys.argv[1]}')
|
||||
sys.exit(1)
|
||||
|
||||
validate_config()
|
||||
|
||||
for endpoint_index in range(len(config_data['endpoints'])):
|
||||
token = os.environ.get('CACHET_TOKEN') or config_data['cachet']['token']
|
||||
api_url = os.environ.get('CACHET_API_URL') or config_data['cachet']['api_url']
|
||||
configuration = Configuration(config_data, endpoint_index, CachetClient(api_url, token), token)
|
||||
NewThread(Scheduler(configuration,
|
||||
build_agent(configuration, logging.getLogger('cachet_url_monitor.scheduler')))).start()
|
||||
|
||||
33
cachet_url_monitor/status.py
Normal file
33
cachet_url_monitor/status.py
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/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.
|
||||
"""
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ComponentStatus(Enum):
|
||||
UNKNOWN = 0
|
||||
OPERATIONAL = 1
|
||||
PERFORMANCE_ISSUES = 2
|
||||
PARTIAL_OUTAGE = 3
|
||||
MAJOR_OUTAGE = 4
|
||||
|
||||
|
||||
INCIDENT_PARTIAL = 'PARTIAL'
|
||||
INCIDENT_MAJOR = 'MAJOR'
|
||||
INCIDENT_PERFORMANCE = 'PERFORMANCE'
|
||||
|
||||
INCIDENT_MAP = {
|
||||
INCIDENT_PARTIAL: ComponentStatus.PARTIAL_OUTAGE,
|
||||
INCIDENT_MAJOR: ComponentStatus.MAJOR_OUTAGE,
|
||||
INCIDENT_PERFORMANCE: ComponentStatus.PERFORMANCE_ISSUES,
|
||||
}
|
||||
|
||||
|
||||
class IncidentStatus(Enum):
|
||||
SCHEDULED = 0
|
||||
INVESTIGATING = 1
|
||||
IDENTIFIED = 2
|
||||
WATCHING = 3
|
||||
FIXED = 4
|
||||
40
config.yml
40
config.yml
@@ -1,17 +1,27 @@
|
||||
endpoint:
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
timeout: 0.01
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status: 200
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: '.*<body>.*'
|
||||
endpoints:
|
||||
- name: swagger
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
header:
|
||||
SOME-HEADER: SOME-VALUE
|
||||
timeout: 0.01
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 200-300
|
||||
incident: MAJOR
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: '.*(<body).*'
|
||||
allowed_fails: 0
|
||||
frequency: 30
|
||||
component_id: 1
|
||||
metric_id: 1
|
||||
action:
|
||||
- CREATE_INCIDENT
|
||||
- UPDATE_STATUS
|
||||
public_incidents: true
|
||||
latency_unit: ms
|
||||
cachet:
|
||||
api_url: https://demo.cachethq.io/api/v1
|
||||
token: my_token
|
||||
component_id: 1
|
||||
#metric_id: 1
|
||||
frequency: 30
|
||||
token: my_token
|
||||
@@ -1,8 +1,10 @@
|
||||
PyYAML==3.11
|
||||
ipython==4.2.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
|
||||
codacy-coverage==1.3.11
|
||||
coverage==5.0.3
|
||||
coveralls==1.10.0
|
||||
ipython==7.11.1
|
||||
mock==3.0.5
|
||||
pudb==2019.2
|
||||
pytest==5.3.5
|
||||
pytest-cov==2.8.1
|
||||
requests-mock==1.7.0
|
||||
twine==3.1.1
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
version: '2'
|
||||
services:
|
||||
monitor:
|
||||
build: .
|
||||
volumes:
|
||||
- .:/usr/src/app/
|
||||
@@ -1,3 +1,4 @@
|
||||
PyYAML==3.11
|
||||
requests==2.9.1
|
||||
schedule==0.3.2
|
||||
PyYAML==5.3
|
||||
requests==2.22.0
|
||||
schedule==0.6.0
|
||||
Click==7.0
|
||||
|
||||
9
setup.cfg
Normal file
9
setup.cfg
Normal file
@@ -0,0 +1,9 @@
|
||||
[metadata]
|
||||
description-file = README.md
|
||||
|
||||
[aliases]
|
||||
test=pytest
|
||||
|
||||
[tool:pytest]
|
||||
addopts = --verbose
|
||||
python_files = tests/*.py
|
||||
14
setup.py
14
setup.py
@@ -1,17 +1,21 @@
|
||||
#!/usr/bin/env python
|
||||
from distutils.core import setup
|
||||
#from distutils.core import setup
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(name='cachet-url-monitor',
|
||||
version='0.2',
|
||||
version='0.6.7',
|
||||
description='Cachet URL monitor plugin',
|
||||
author='Mitsuo Takaki',
|
||||
author_email='mitsuotakaki@gmail.com',
|
||||
url='https://github.com/mtakaki/cachet-url-monitor',
|
||||
packages=['cachet_url_monitor'],
|
||||
packages=find_packages(),
|
||||
license='MIT',
|
||||
requires=[
|
||||
'requests',
|
||||
'yaml',
|
||||
'schedule',
|
||||
]
|
||||
)
|
||||
'Click',
|
||||
],
|
||||
setup_requires=["pytest-runner"],
|
||||
tests_require=["pytest", "requests-mock"]
|
||||
)
|
||||
|
||||
26
tests/configs/config.yml
Normal file
26
tests/configs/config.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
endpoints:
|
||||
- name: foo
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
header:
|
||||
SOME-HEADER: SOME-VALUE
|
||||
timeout: 0.01
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 200-300
|
||||
incident: MAJOR
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: '.*(<body).*'
|
||||
allowed_fails: 0
|
||||
component_id: 1
|
||||
action:
|
||||
- CREATE_INCIDENT
|
||||
- UPDATE_STATUS
|
||||
public_incidents: true
|
||||
latency_unit: ms
|
||||
frequency: 30
|
||||
cachet:
|
||||
api_url: https://demo.cachethq.io/api/v1
|
||||
token: my_token
|
||||
22
tests/configs/config_invalid_type.yml
Normal file
22
tests/configs/config_invalid_type.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
endpoints:
|
||||
- name: foo
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
header:
|
||||
SOME-HEADER: SOME-VALUE
|
||||
timeout: 0.01
|
||||
expectation:
|
||||
- type: HTTP
|
||||
status_range: 200-300
|
||||
incident: MAJOR
|
||||
allowed_fails: 0
|
||||
component_id: 1
|
||||
action:
|
||||
- CREATE_INCIDENT
|
||||
- UPDATE_STATUS
|
||||
public_incidents: true
|
||||
latency_unit: ms
|
||||
frequency: 30
|
||||
cachet:
|
||||
api_url: https://demo.cachethq.io/api/v1
|
||||
token: my_token
|
||||
28
tests/configs/config_multiple_urls.yml
Normal file
28
tests/configs/config_multiple_urls.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
endpoints:
|
||||
- name: foo
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 200-300
|
||||
allowed_fails: 0
|
||||
component_id: 1
|
||||
latency_unit: ms
|
||||
frequency: 30
|
||||
timeout: 1
|
||||
public_incidents: true
|
||||
- name: bar
|
||||
url: http://localhost:8080/bar
|
||||
method: POST
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status_range: 500
|
||||
allowed_fails: 0
|
||||
component_id: 2
|
||||
latency_unit: ms
|
||||
frequency: 30
|
||||
timeout: 1
|
||||
public_incidents: true
|
||||
cachet:
|
||||
api_url: https://demo.cachethq.io/api/v1
|
||||
token: my_token
|
||||
155
tests/test_client.py
Normal file
155
tests/test_client.py
Normal file
@@ -0,0 +1,155 @@
|
||||
#!/usr/bin/env python
|
||||
import unittest
|
||||
from typing import Dict, List
|
||||
|
||||
import requests_mock
|
||||
|
||||
from cachet_url_monitor.client import CachetClient
|
||||
from cachet_url_monitor.exceptions import MetricNonexistentError
|
||||
from cachet_url_monitor.status import ComponentStatus
|
||||
|
||||
TOKEN: str = 'token_123'
|
||||
CACHET_URL: str = 'http://foo.localhost'
|
||||
JSON: Dict[str, List[Dict[str, int]]] = {'data': [{'id': 1}]}
|
||||
|
||||
|
||||
class ClientTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.client = CachetClient('foo.localhost', TOKEN)
|
||||
|
||||
def test_init(self):
|
||||
self.assertEqual(self.client.headers, {'X-Cachet-Token': TOKEN}, 'Header was not set correctly')
|
||||
self.assertEqual(self.client.url, CACHET_URL, 'Cachet API URL was set incorrectly')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_components(self, m):
|
||||
m.get(f'{CACHET_URL}/components', json=JSON, headers={'X-Cachet-Token': TOKEN})
|
||||
components = self.client.get_components()
|
||||
|
||||
self.assertEqual(components, [{'id': 1}],
|
||||
'Getting components list is incorrect.')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_metrics(self, m):
|
||||
m.get(f'{CACHET_URL}/metrics', json=JSON)
|
||||
metrics = self.client.get_metrics()
|
||||
|
||||
self.assertEqual(metrics, [{'id': 1}],
|
||||
'Getting metrics list is incorrect.')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_generate_config(self, m):
|
||||
def components():
|
||||
return {
|
||||
'data': [
|
||||
{
|
||||
'id': '1',
|
||||
'name': 'apache',
|
||||
'link': 'http://abc.def',
|
||||
'enabled': True
|
||||
},
|
||||
{
|
||||
'id': '2',
|
||||
'name': 'haproxy',
|
||||
'link': 'http://ghi.jkl',
|
||||
'enabled': False
|
||||
},
|
||||
{
|
||||
'id': '3',
|
||||
'name': 'nginx',
|
||||
'link': 'http://mno.pqr',
|
||||
'enabled': True
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
m.get(f'{CACHET_URL}/components', json=components(), headers={'X-Cachet-Token': TOKEN})
|
||||
config = self.client.generate_config()
|
||||
|
||||
self.assertEqual(config, {
|
||||
'cachet': {
|
||||
'api_url': CACHET_URL,
|
||||
'token': TOKEN
|
||||
},
|
||||
'endpoints': [
|
||||
{
|
||||
'name': 'apache',
|
||||
'url': 'http://abc.def',
|
||||
'method': 'GET',
|
||||
'timeout': 1,
|
||||
'expectation': [
|
||||
{
|
||||
'type': 'HTTP_STATUS',
|
||||
'status_range': '200-300',
|
||||
'incident': 'MAJOR'
|
||||
}
|
||||
],
|
||||
'allowed_fails': 0,
|
||||
'frequency': 30,
|
||||
'component_id': '1',
|
||||
'action': [
|
||||
'CREATE_INCIDENT',
|
||||
'UPDATE_STATUS',
|
||||
],
|
||||
'public_incidents': True,
|
||||
},
|
||||
{
|
||||
'name': 'nginx',
|
||||
'url': 'http://mno.pqr',
|
||||
'method': 'GET',
|
||||
'timeout': 1,
|
||||
'expectation': [
|
||||
{
|
||||
'type': 'HTTP_STATUS',
|
||||
'status_range': '200-300',
|
||||
'incident': 'MAJOR'
|
||||
}
|
||||
],
|
||||
'allowed_fails': 0,
|
||||
'frequency': 30,
|
||||
'component_id': '3',
|
||||
'action': [
|
||||
'CREATE_INCIDENT',
|
||||
'UPDATE_STATUS',
|
||||
],
|
||||
'public_incidents': True,
|
||||
}
|
||||
]
|
||||
}, 'Generated config is incorrect.')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_default_metric_value(self, m):
|
||||
m.get(f'{CACHET_URL}/metrics/123', json={'data': {'default_value': 0.456}}, headers={'X-Cachet-Token': TOKEN})
|
||||
default_metric_value = self.client.get_default_metric_value(123)
|
||||
|
||||
self.assertEqual(default_metric_value, 0.456,
|
||||
'Getting default metric value is incorrect.')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_default_metric_value_invalid_id(self, m):
|
||||
m.get(f'{CACHET_URL}/metrics/123', headers={'X-Cachet-Token': TOKEN}, status_code=400)
|
||||
with self.assertRaises(MetricNonexistentError):
|
||||
self.client.get_default_metric_value(123)
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_component_status(self, m):
|
||||
def json():
|
||||
return {
|
||||
'data': {
|
||||
'status': ComponentStatus.OPERATIONAL.value
|
||||
}
|
||||
}
|
||||
|
||||
m.get(f'{CACHET_URL}/components/123', json=json(), headers={'X-Cachet-Token': TOKEN})
|
||||
status = self.client.get_component_status(123)
|
||||
|
||||
self.assertEqual(status, ComponentStatus.OPERATIONAL,
|
||||
'Getting component status value is incorrect.')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_push_status(self, m):
|
||||
m.put(f'{CACHET_URL}/components/123?id=123&status={ComponentStatus.PARTIAL_OUTAGE.value}',
|
||||
headers={'X-Cachet-Token': TOKEN})
|
||||
response = self.client.push_status(123, ComponentStatus.PARTIAL_OUTAGE)
|
||||
|
||||
self.assertTrue(response.ok, 'Pushing status value is failed.')
|
||||
@@ -1,129 +1,184 @@
|
||||
#!/usr/bin/env python
|
||||
import mock
|
||||
import unittest
|
||||
import sys
|
||||
from requests import ConnectionError,HTTPError,Timeout
|
||||
sys.modules['requests'] = mock.Mock()
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
import requests
|
||||
import requests_mock
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
import cachet_url_monitor.exceptions
|
||||
import cachet_url_monitor.status
|
||||
|
||||
sys.modules['logging'] = mock.Mock()
|
||||
from cachet_url_monitor.configuration import Configuration
|
||||
import os
|
||||
|
||||
class ConfigurationTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
def getLogger(name):
|
||||
self.mock_logger = mock.Mock()
|
||||
return self.mock_logger
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
|
||||
self.configuration = Configuration('config.yml')
|
||||
sys.modules['requests'].Timeout = Timeout
|
||||
sys.modules['requests'].ConnectionError = ConnectionError
|
||||
sys.modules['requests'].HTTPError = HTTPError
|
||||
@pytest.fixture()
|
||||
def mock_client():
|
||||
client = mock.Mock()
|
||||
client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
yield client
|
||||
|
||||
def test_init(self):
|
||||
assert len(self.configuration.data) == 3
|
||||
assert len(self.configuration.expectations) == 3
|
||||
|
||||
def test_evaluate(self):
|
||||
def total_seconds():
|
||||
return 0.1
|
||||
def request(method, url, timeout=None):
|
||||
response = mock.Mock()
|
||||
response.status_code = 200
|
||||
response.elapsed = mock.Mock()
|
||||
response.elapsed.total_seconds = total_seconds
|
||||
response.text = '<body>'
|
||||
return response
|
||||
@pytest.fixture()
|
||||
def config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == 1
|
||||
@pytest.fixture()
|
||||
def multiple_urls_config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
def test_evaluate_with_failure(self):
|
||||
def total_seconds():
|
||||
return 0.1
|
||||
def request(method, url, timeout=None):
|
||||
response = mock.Mock()
|
||||
# We are expecting a 200 response, so this will fail the expectation.
|
||||
response.status_code = 400
|
||||
response.elapsed = mock.Mock()
|
||||
response.elapsed.total_seconds = total_seconds
|
||||
response.text = '<body>'
|
||||
return response
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
@pytest.fixture()
|
||||
def invalid_config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config_invalid_type.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
assert self.configuration.status == 3
|
||||
|
||||
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
|
||||
@pytest.fixture()
|
||||
def mock_logger():
|
||||
mock_logger = mock.Mock()
|
||||
|
||||
raise Timeout()
|
||||
def getLogger(name):
|
||||
return mock_logger
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
yield mock_logger
|
||||
|
||||
assert self.configuration.status == 3
|
||||
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
|
||||
@pytest.fixture()
|
||||
def configuration(config_file, mock_client, mock_logger):
|
||||
yield Configuration(config_file, 0, mock_client, 'token2')
|
||||
|
||||
raise ConnectionError()
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
@pytest.fixture()
|
||||
def multiple_urls_configuration(multiple_urls_config_file, mock_client, mock_logger):
|
||||
yield [Configuration(multiple_urls_config_file, index, mock_client, 'token2') for index in
|
||||
range(len(multiple_urls_config_file['endpoints']))]
|
||||
|
||||
assert self.configuration.status == 3
|
||||
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 test_init(configuration):
|
||||
assert len(configuration.data) == 2, 'Number of root elements in config.yml is incorrect'
|
||||
assert len(configuration.expectations) == 3, 'Number of expectations read from file is incorrect'
|
||||
assert configuration.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'
|
||||
assert configuration.endpoint_header == {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect'
|
||||
|
||||
raise HTTPError()
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
def test_init_unknown_status(config_file, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.UNKNOWN
|
||||
configuration = Configuration(config_file, 0, mock_client, 'token2')
|
||||
|
||||
assert self.configuration.status == 3
|
||||
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||
'response'))
|
||||
assert configuration.previous_status == cachet_url_monitor.status.ComponentStatus.UNKNOWN
|
||||
|
||||
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'}
|
||||
|
||||
response = mock.Mock()
|
||||
response.status_code = 200
|
||||
return response
|
||||
def test_evaluate(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>')
|
||||
configuration.evaluate()
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.configuration.status = 1
|
||||
self.configuration.push_status()
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.OPERATIONAL, 'Component status set incorrectly'
|
||||
|
||||
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'}
|
||||
|
||||
response = mock.Mock()
|
||||
response.status_code = 300
|
||||
return response
|
||||
def test_evaluate_without_header(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>')
|
||||
configuration.evaluate()
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.configuration.status = 1
|
||||
self.configuration.push_status()
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.OPERATIONAL, 'Component status set incorrectly'
|
||||
|
||||
|
||||
def test_evaluate_with_failure(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>', status_code=400)
|
||||
configuration.evaluate()
|
||||
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.MAJOR_OUTAGE, 'Component status set incorrectly or custom incident status is incorrectly parsed'
|
||||
|
||||
|
||||
def test_evaluate_with_timeout(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.Timeout)
|
||||
configuration.evaluate()
|
||||
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PERFORMANCE_ISSUES, 'Component status set incorrectly'
|
||||
mock_logger.warning.assert_called_with('Request timed out')
|
||||
|
||||
|
||||
def test_evaluate_with_connection_error(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.ConnectionError)
|
||||
configuration.evaluate()
|
||||
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE, 'Component status set incorrectly'
|
||||
mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
|
||||
|
||||
|
||||
def test_evaluate_with_http_error(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.HTTPError)
|
||||
configuration.evaluate()
|
||||
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE, 'Component status set incorrectly'
|
||||
mock_logger.exception.assert_called_with('Unexpected HTTP response')
|
||||
|
||||
|
||||
def test_push_status(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
push_status_response = mock.Mock()
|
||||
mock_client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = True
|
||||
configuration.previous_status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
|
||||
configuration.push_status()
|
||||
|
||||
mock_client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.OPERATIONAL)
|
||||
|
||||
|
||||
def test_push_status_with_new_failure(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
push_status_response = mock.Mock()
|
||||
mock_client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = False
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
configuration.push_status()
|
||||
|
||||
mock_client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE)
|
||||
|
||||
|
||||
def test_push_status_same_status(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
|
||||
configuration.push_status()
|
||||
|
||||
mock_client.push_status.assert_not_called()
|
||||
|
||||
|
||||
def test_init_multiple_urls(multiple_urls_configuration):
|
||||
expected_method = ['GET', 'POST']
|
||||
expected_url = ['http://localhost:8080/swagger', 'http://localhost:8080/bar']
|
||||
|
||||
assert len(multiple_urls_configuration) == 2
|
||||
for index in range(len(multiple_urls_configuration)):
|
||||
config = multiple_urls_configuration[index]
|
||||
assert len(config.data) == 2, 'Number of root elements in config.yml is incorrect'
|
||||
assert len(config.expectations) == 1, 'Number of expectations read from file is incorrect'
|
||||
assert config.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'
|
||||
|
||||
assert expected_method[index] == config.endpoint_method
|
||||
assert expected_url[index] == config.endpoint_url
|
||||
|
||||
|
||||
def test_init_invalid_configuration(invalid_config_file, mock_client):
|
||||
with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError):
|
||||
Configuration(invalid_config_file, 0, mock_client, 'token2')
|
||||
|
||||
@@ -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.expectation import HttpStatus, Regex, Latency
|
||||
from cachet_url_monitor.status import ComponentStatus
|
||||
|
||||
|
||||
class LatencyTest(unittest.TestCase):
|
||||
@@ -16,60 +19,86 @@ 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
|
||||
elapsed.total_seconds = total_seconds
|
||||
|
||||
assert self.expectation.get_status(request) == 1
|
||||
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
def total_seconds():
|
||||
return 2
|
||||
|
||||
request = mock.Mock()
|
||||
elapsed = mock.Mock()
|
||||
request.elapsed = elapsed
|
||||
elapsed.total_seconds = total_seconds
|
||||
|
||||
assert self.expectation.get_status(request) == 2
|
||||
assert self.expectation.get_status(request) == ComponentStatus.PERFORMANCE_ISSUES
|
||||
|
||||
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()
|
||||
request.status_code = 200
|
||||
|
||||
assert self.expectation.get_status(request) == 1
|
||||
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
|
||||
|
||||
def test_get_status_healthy_boundary(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 299
|
||||
|
||||
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 400
|
||||
|
||||
assert self.expectation.get_status(request) == 3
|
||||
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def test_get_status_unhealthy_boundary(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 300
|
||||
|
||||
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def test_get_message(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 400
|
||||
|
||||
assert self.expectation.get_message(request) == ('Unexpected HTTP '
|
||||
'status (400)')
|
||||
'status (400)')
|
||||
|
||||
|
||||
class RegexTest(unittest.TestCase):
|
||||
@@ -77,23 +106,23 @@ 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
|
||||
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
request = mock.Mock()
|
||||
request.text = 'We will not find it here'
|
||||
|
||||
assert self.expectation.get_status(request) == 3
|
||||
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def test_get_message(self):
|
||||
request = mock.Mock()
|
||||
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')
|
||||
|
||||
19
tests/test_latency_unit.py
Normal file
19
tests/test_latency_unit.py
Normal 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
|
||||
@@ -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,11 +22,11 @@ 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
|
||||
self.configuration.data = {'frequency': 5}
|
||||
self.configuration.endpoint = {'frequency': 5}
|
||||
|
||||
self.agent.start()
|
||||
|
||||
@@ -33,16 +34,54 @@ class AgentTest(unittest.TestCase):
|
||||
|
||||
|
||||
class SchedulerTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_configuration = sys.modules[('cachet_url_monitor.configuration'
|
||||
'.Configuration')]
|
||||
self.scheduler = Scheduler('config.yml')
|
||||
@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.agent = mock.MagicMock()
|
||||
|
||||
self.scheduler = Scheduler(
|
||||
{
|
||||
'endpoints': [
|
||||
{
|
||||
'name': 'foo',
|
||||
'url': 'http://localhost:8080/swagger',
|
||||
'method': 'GET',
|
||||
'expectation': [
|
||||
{
|
||||
'type': 'HTTP_STATUS',
|
||||
'status_range': '200 - 300',
|
||||
'incident': 'MAJOR',
|
||||
}
|
||||
],
|
||||
'allowed_fails': 0,
|
||||
'component_id': 1,
|
||||
'action': ['CREATE_INCIDENT', 'UPDATE_STATUS'],
|
||||
'public_incidents': True,
|
||||
'latency_unit': 'ms',
|
||||
'frequency': 30
|
||||
}
|
||||
],
|
||||
'cachet': {
|
||||
'api_url': 'https: // demo.cachethq.io / api / v1',
|
||||
'token': 'my_token'
|
||||
}
|
||||
}, self.agent)
|
||||
|
||||
def test_init(self):
|
||||
assert self.scheduler.stop == False
|
||||
self.assertFalse(self.scheduler.stop)
|
||||
|
||||
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()
|
||||
|
||||
self.agent.start.assert_called()
|
||||
|
||||
Reference in New Issue
Block a user