mtakaki / cachet-url-monitor

URL monitor plugin for cachethq.io
MIT License
124 stars 48 forks source link
cachet-url-monitor cachethq docker monitoring

Status

CircleCI Coverage Status Codacy Badge Docker Pulls Docker stars License Latest release pre-commit

cachet-url-monitor

Python plugin for cachet that monitors an URL, verifying it's response status and latency. The frequency the URL is tested is configurable, along with the assertion applied to the request response.

This project is available at PyPI: https://pypi.python.org/pypi/cachet-url-monitor

Configuration

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
  - name: Insecure-site
    url: https://www.Insecure-site-internal.com
    method: GET
    header:
      SOME-HEADER: SOME-VALUE
    insecure: true
    timeout: 1 # seconds
    expectation:
      - type: HTTP_STATUS
        status_range: 200-205
    allowed_fails: 0
    component_id: 2
    action:
      - CREATE_INCIDENT
    public_incidents: true
    frequency: 5
cachet:
  api_url: http://status.cachethq.io/api/v1
  token:
    - type: ENVIRONMENT_VARIABLE
      value: CACHET_TOKEN
    - type: AWS_SECRETS_MANAGER
      secret_name: cachethq
      secret_key: token
      region: us-west-2
    - type: TOKEN
      value: my_token
webhooks:
  - url: "https://push.example.com/message?token=<apptoken>"
    params:
      title: "{title}"
      message: "{message}"
      priority: 5
messages:
  incident_outage: "{name} is unavailable"
  incident_operational: "{name} is operational"
  incident_performance: "{name} has degraded performance"

Each expectation has their own default incident status. It can be overridden by setting the incident property to any of the following values:

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

Following parameters are available in webhook interpolation

Parameter Description
{title} Event title, includes endpoint name and short status
{message} Event message, same as sent to Cachet

AWS Secrets Manager

This tools can integrate with AWS Secrets Manager, where the token is fetched directly from the service. In order to get this functionality working, you will need to setup the AWS credentials into the container. The easiest way would be setting the environment variables:

$ docker run --rm -it -e AWS_ACCESS_KEY_ID=xyz -e AWS_SECRET_ACCESS_KEY=aaa -v "$PWD"/my_config.yml:/usr/src/app/config/config.yml:ro mtakaki/cachet-url-monitor

Setting up

The application should be installed using virtualenv, through the following command:

$ git clone https://github.com/mtakaki/cachet-url-monitor.git
$ cd cachet-url-monitor
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python3 setup.py install

To start the agent:

$ 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 is already checked in and it's ready to be used.

You have two choices, checking this repo out and building the docker image or it can be pulled directly from dockerhub. You will need to create your own custom config .yml file and run (it will pull latest):

$ docker pull mtakaki/cachet-url-monitor
$ docker run --rm -it -v "$PWD":/usr/src/app/config/ mtakaki/cachet-url-monitor

If you're going to use a file with a name other than config.yml, you will need to map the local file, like this:

$ docker run --rm -it -v "$PWD"/my_config.yml:/usr/src/app/config/config.yml:ro mtakaki/cachet-url-monitor

Docker compose

Docker compose has been removed from this repo as it had a dependency on PostgreSQL and it slightly complicated how it works. This has been kindly handled on: https://github.com/boonisz/cachet-url-monitor-dc It facilitates spawning CachetHQ with its dependencies and cachet-url-monitor alongside to it.

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:

$ 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):

$ 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:

Caveats

Because we can't predict what expectations will be needed, it will default to these behavior:

Troubleshooting

SSLERROR

If it's throwing the following exception:

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 setting 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.

Development

If you want to contribute to this project, feel free to fork this repo and post PRs with any improvements or bug fixes. This is highly appreciated, as it's been hard to deal with numerous requests coming my end.

This repo is setup with pre-commit hooks and it should ensure code style is consistent . The steps to start development on this repo is the same as the setup aforementioned above:

$ git clone https://github.com/mtakaki/cachet-url-monitor.git
$ cd cachet-url-monitor
$ pre-commit install
$ virtualenv venv
$ source venv/bin/activate
$ tox