/web-monitor

Back-end microservice watches your website and notifies of failures

Primary LanguageTypeScriptMIT LicenseMIT

web-monitor

Latest version CI/CD status Test coverage Licence

Docker-based microservice monitors configured metrics and notifies administrators when they fail.

  • metrics:
    • SSL validity (revocation & pubkey pinning not checked)
    • response code
    • response time
    • response content match (Regex)
    • HTML element availability via CSS selector
  • notifications:
    • email

Note: response body metrics only check the content without evaluating it, therefore they do not work with Single Page Applications.

Deployment

Requirements:

  • Docker

Steps:

  • Configure docker-compose.yml (see below)
  • Edit config/config.yml (see below)
  • Run docker-compose up

docker-compose.yml

version: "3"

services:
  web-monitor:
    image: marius321967/web-monitor
    environment:
      - LOG_LEVEL=info
    volumes:
      - ./config:/app/config
      - ./logs:/app/logs
    restart: unless-stopped

config.yml

web-monitor requires you to set up:

  • endpoints to monitor (monitors)
  • people to notify (notify)
  • credentials for notification services (email_notifier)
monitors:
  <monitor-id>:
    label: human readable name
    type: ssl_validity|response_code|response_time|content_match|element_match
    interval: <integer> seconds|minutes|hours|days|weeks
    request: https://example.com

    # string value will initiate a GET request. For more configurable requests, provide an object:
    request:
      url: https://example.com/form_submit
      method: GET|POST|DELETE|HEAD|OPTIONS|PUT|PATCH
      # (auth_header is optional)
      auth_header: Basic Zm9vOmJhcg==

    # some monitors require additional parameters
    type: response_code
    expected_code: 200

    # response_time monitor will send a notification if response takes longer than threshold
    type: response_time
    threshold: 20 seconds

    type: content_match
    pattern: mailto:john@example.com
    # pattern supports regex if surrounded by forward slashes. Flags after slashes are not supported.
    # see Node's support for RegEx:
    # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility
    pattern: /mailto:\w+@example.com/

    type: element_match
    # pattern supports CSS-style selectors provided by cheerio (https://cheerio.js.org/)
    pattern: body .contact-form

# notification recipients are listed as such
notify:
  <recipient-id>:
    # (web-monitor currently only notifies by email)
    email: admin@example.com
  <recipient-id>:
    email: johndoe@example.com

# finally, the mail server is set up
email_notifier:
  host: sandbox.smtp.mailtrap.io
  port: 465
  secure: false
  auth:
    user: foo
    pass: bar

Also see config/config.example.yml

Environment variables

  • LOG_LEVEL=error/warn/info/http/verbose/debug

Logging

web-monitor.log will be created in the logs directory - it will contain the same logs as in stdout. No cleanup or file splitting is done by the service.

Development

Spin up a dev environment with Docker:

export COMPOSE_FILE=docker-compose.dev.yml

docker-compose up -d dev

Then edit from the host machine or by connecting to the dev container from VS Code via Remote Containers.

Testing

Unit test: yarn test:unit

Integration test: yarn test:integration

Infrastructure test:

export COMPOSE_FILE=docker-compose.dev.yml

docker-compose up \
  --abort-on-container-exit \
  --exit-code-from=infra-test \
  infra-test