Git Product home page Git Product logo

http-checks's Introduction

Sensu Bonsai Asset Go Test goreleaser

http-checks

Table of Contents

Overview

The http-checks is a collection of Sensu Checks that providing monitoring and metrics for HTTP based services.

Attribution

Portions of http-check and http-json are based on and/or derived from the HTTP check found in the NCR DevOps Platform nagiosfoundation collection of checks.

Checks

This collection contains the following checks:

  • http-check - for checking HTTP status or searching for a string in the response body
  • http-perf - for checking HTTP performance by measuring response times, provides metrics in nagios_perfdata format
  • http-json - for querying JSON output from an HTTP request
  • http-get - for fetching metrics from HTTP sources

Usage examples

http-check

Help output

HTTP Status/String Check

Usage:
  http-check [flags]
  http-check [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -u, --url string               URL to test (default "http://localhost:80/")
  -s, --search-string string     String to search for, if not provided do status check only
  -r, --redirect-ok              Allow redirects
  -T, --timeout int              Request timeout in seconds (default 15)
  -H, --header strings           Additional header(s) to send in check request
  -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
  -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
  -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
  -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
  -h, --help                     help for http-check

Use "http-check [command] --help" for more information about a command.

Example(s)

http-check --url https://sensu.io --search-string Monitoring
http-check OK: found "Monitoring" at https://sensu.io

http-check --url https://sensu.io --search-string droids
http-check CRITICAL: "droids" not found at https://sensu.io

http-check --url https://sensu.io
http-check OK: HTTP Status 200 for https://sensu.io

http-check --url https://sensu.io/notfound
http-check WARNING: HTTP Status 301 for https://sensu.io/notfound  (redirects to /oops)

http-check --url https://sensu.io/notfound --redirect-ok
http-check OK: HTTP Status 200 for https://sensu.io/oops (redirect from https://sensu.io/notfound)

http-check --url https://discourse.sensu.io/notfound
http-check CRITICAL: HTTP Status 404 for https://discourse.sensu.io/notfound

http-check --url http://localhost:8000/health --header "Origin: test.server.local" --header "RandomHeader: Header value goes here"
http-check OK: HTTP Status 200 for http://localhost:8000/health

Note(s)

  • When using --redirect-ok it affects both the string search and status checkfunctionality.
    • For a string search, if true, it searches for the string in the eventual destination.
    • For a status check, if false, receiving a redirect will return a warning status. If true, it will return an ok status.
  • Headers should be in the form of "Header-Name: Header value".

http-perf

Help output

HTTP Performance Check

Usage:
  http-perf [flags]
  http-perf [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -u, --url string               URL to test (default "http://localhost:80/")
  -T, --timeout int              Request timeout in seconds (default 15)
  -w, --warning string           Warning threshold, can be expressed as seconds or milliseconds (1s = 1000ms) (default "1s")
  -c, --critical string          Critical threshold, can be expressed as seconds or milliseconds (1s = 1000ms) (default "2s")
  -m, --output-in-ms             Provide output in milliseconds (default false, display in seconds)
  -H, --header strings           Additional header(s) to send in check request
  -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
  -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
  -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
  -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
  -h, --help                     help for http-perf

Use "http-perf [command] --help" for more information about a command.

Example(s)

http-perf --url https://sensu.io --warning 1s --critical 2s
http-perf OK: 0.243321s | dns_duration=0.016596, tls_handshake_duration=0.172235, connect_duration=0.022199, first_byte_duration=0.243267, total_request_duration=0.243321

# Let's make the warning threshold pretty improbable
http-perf --url https://sensu.io --warning 10ms --critical 1s
http-perf WARNING: 0.304795s | dns_duration=0.033904, tls_handshake_duration=0.198843, connect_duration=0.021858, first_byte_duration=0.304747, total_request_duration=0.304795

# Again, but with output in milliseconds
http-perf --url https://sensu.io --warning 10ms --critical 1s --output-in-ms
http-perf WARNING: 262ms | dns_duration=35, tls_handshake_duration=170, connect_duration=22, first_byte_duration=262, total_request_duration=262

# With cusomter header(s)
http-perf --url https://sensu.io --warning 1s --critical 2s --header "Custom-Header: Custom header value"
http-perf OK: 0.243321s | dns_duration=0.016596, tls_handshake_duration=0.172235, connect_duration=0.022199, first_byte_duration=0.243267, total_request_duration=0.243321

Note(s)

  • http-perf does not follow redirects, the page you are testing will need to be referenced explicitly.
  • Headers should be in the form of "Header-Name: Header value".

http-json

Help output

HTTP JSON Check

Usage:
  http-json [flags]
  http-json [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -u, --url string               URL to test (default "http://localhost:80/")
  -q, --query string             Query written in jq format
  -e, --expression string        Expression for comparing result of query
  -H, --header strings           Additional header(s) to send in check request
  -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
  -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
  -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
  -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
  -T, --timeout int              Request timeout in seconds (default 15)
  -h, --help                     help for http-json

Use "http-json [command] --help" for more information about a command.

Writing queries and expressions

Queries are written in jq format as implemented by the gojq library. The query is used to obtain a value in the JSON requested from the URL specified by --url. This value is then evaluated against the expression provided by --expression.

Example(s)

# Boolean example - checking Sensu cluster health
http-json --url http://backend:8080/health --query ".ClusterHealth.[0].Healthy" --expression "== true"
http-json OK:  The value true found at .ClusterHealth.[0].Healthy matched with expression "== true" and returned true

# String comparison expressions
http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .id --expression "== \"HeaFdiyIJe\""
http-json OK:  The value HeaFdiyIJe found at .id matched with expression "== \"HeaFdiyIJe\"" and returned true

http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .id --expression "== \"BadText\""
http-json CRITICAL: The value HeaFdiyIJe found at .id did not match with expression "== \"BadText\"" and returned false

# Numeric comparison expressions
http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .status --expression "== 200"
http-json OK:  The value 200 found at .status matched with expression "== 200" and returned true

http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .status --expression "< 300"
http-json OK:  The value 200 found at .status matched with expression "< 300" and returned true

http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .status --expression "> 300"
http-json CRITICAL: The value 200 found at .status did not match with expression "> 300" and returned false

# With a custom header
http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --query .status --expression "< 300" --header "Custom-Header: Custom header value"
http-json OK:  The value 200 found at .status matched with expression "< 300" and returned true

Note(s)

  • Headers should be in the form of "Header-Name: Header value".

http-get

Help output

HTTP GET Check

Usage:
  http-get [flags]
  http-get [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -H, --header strings           Additional header(s) to send in check request
  -h, --help                     help for http-get
  -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
  -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
  -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
  -T, --timeout int              Request timeout in seconds (default 15)
  -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
  -u, --url string               URL to get (default "http://localhost:80/")

Use "http-get [command] --help" for more information about a command.

Using the http-get Check

This check is intended to provide a Sensu-native way of fetching the output of a HTTP GET request against a metrics endpoint, so that those metrics can be processed by Sensu's output_metrics_threshold features.

While this functionality is overlapped by default tools like wget, curl, and Windows's Invoke-WebRequest PowerShell commandlet, this provides a cross-platform solution to fetching these HTTP-accessible metrics in a Sensu-native context.

This plugin will result in a critical exit status code if the body is empty or if the HTTP GET request fails.

Example(s)

# Prometheus node_exporter example - fetching Prometheus metrics from node_exporter
http-get --url http://localhost:9100
[... output is the metrics in Prometheus format ...]

Note(s)

  • Headers should be in the form of "Header-Name: Header value".

Configuration

Asset registration

Sensu Assets are the best way to make use of this plugin. If you're not using an asset, please consider doing so! If you're using sensuctl 5.13 with Sensu Backend 5.13 or later, you can use the following command to add the asset:

sensuctl asset add nixwiz/http-checks

If you're using an earlier version of sensuctl, you can find the asset on the Bonsai Asset Index.

Check definitions

http-check

---
type: CheckConfig
api_version: core/v2
metadata:
  name: http-check
  namespace: default
spec:
  command: http-check --url http://example.com
  subscriptions:
  - system
  runtime_assets:
  - nixwiz/http-checks

http-perf

---
type: CheckConfig
api_version: core/v2
metadata:
  name: http-perf
  namespace: default
spec:
  command: http-perf --url http://example.com
  subscriptions:
  - system
  runtime_assets:
  - nixwiz/http-checks
  output_metric_format: nagios_perfdata
  output_metric-handlers:
  - influxdb

http-json

---
type: CheckConfig
api_version: core/v2
metadata:
  name: http-json
  namespace: default
spec:
  command: http-json --url https://icanhazdadjoke.com/j/HeaFdiyIJe --path id --expression "== \"HeaFdiyIJe\""
  subscriptions:
  - system
  runtime_assets:
  - nixwiz/http-checks

http-get

---
type: CheckConfig
api_version: core/v2
metadata:
  name: prometheus-node-exporter
  namespace: default
spec:
  command: http-get --url http://localhost:9100
  subscriptions:
  - system
  runtime_assets:
  - sensu/http-checks:0.6.0
  output_metrics_format: prometheus_text
  output_metric_handlers:
  - metrics
  output_metric_tags:
  - name: entity
    value: "{{ .name }}"

Installation from source

The preferred way of installing and deploying this plugin is to use it as an Asset. If you would like to compile and install the plugin from source or contribute to it, download the latest version or create an executable script from this source.

From the local path of the http-checks repository:

go build -o bin/http-check ./cmd/http-check
go build -o bin/http-perf ./cmd/http-perf
go build -o bin/http-json ./cmd/http-json
go build -o bin/http-get ./cmd/http-get

Contributing

For more information about contributing to this plugin, see Contributing.

http-checks's People

Contributors

c-kruse avatar fancyweb avatar jspaleta avatar nixwiz avatar thoward avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

http-checks's Issues

http-check is broken, cannot handle any errors

It works fine if the port is up and running:

# ./http-check --url http://user.service.consul:80/metadata
http-check OK: HTTP Status 200 for http://user.service.consul:80/metadata

However, any error whatsoever (either timeout or connection refused) results in the Usage Help screen:

# ./http-check --url http://user.service.consul:81/metadata
Usage:
  http-check [flags]
  http-check [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -H, --header strings           Additional header(s) to send in check request
  -h, --help                     help for http-check
  -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
  -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
  -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
  -r, --redirect-ok              Allow redirects
  -s, --search-string string     String to search for, if not provided do status check only
  -T, --timeout int              Request timeout in seconds (default 15)
  -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
  -u, --url string               URL to test (default "http://localhost:80/")

Use "http-check [command] --help" for more information about a command.

Error executing http-check: error executing check: Get http://user.service.consul:81/metadata: dial tcp 10.2.95.221:81: connect: connection refused

Instead, it should produce the standard service failure message.

This plugin is broken, it cannot be used as is.

Client.Timeout results in outputting usage

We're using these checks in our Sensu cluster. Thank you!

When a site times out we get unexpected output from the check. http-check outputs "Usage" information and then an error about the timeout.

I'm hoping / expecting it can output a normal CRITICAL error message.

This is what we're seeing as the output in Sensu when the timeout occurs:

Usage:
 http-check [flags]
 http-check [command]
Available Commands:
 help        Help about any command
 version     Print the version number of this plugin
Flags:
 -H, --header strings           Additional header(s) to send in check request
 -h, --help                     help for http-check
 -i, --insecure-skip-verify     Skip TLS certificate verification (not recommended!)
 -C, --mtls-cert-file string    Certificate file for mutual TLS auth in PEM format
 -K, --mtls-key-file string     Key file for mutual TLS auth in PEM format
 -r, --redirect-ok              Allow redirects
 -s, --search-string string     String to search for, if not provided do status check only
 -T, --timeout int              Request timeout in seconds (default 15)
 -t, --trusted-ca-file string   TLS CA certificate bundle in PEM format
 -u, --url string               URL to test (default "http://localhost:80/")
Use "http-check [command] --help" for more information about a command.
Error executing http-check: error executing check: Get https://www.example.com/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)

(Domain name adjusted for privacy)

I'm not experienced enough with Go to take a crack at this myself. Hoping it's an easy fix.

Reduce redundant code

There's a lot of redundant code in regards to setting up HTTP connections, move that all to a shared package.

`http-get` with `--url` returns an error, but exits 0

When running the following check:

type: CheckConfig
api_version: core/v2
metadata:
  annotations:
    runbook: ""
    team: sre
  created_by: admin
  labels:
    sensu.io/managed_by: sensuctl
  name: sensu-backend-metrics
  namespace: monitoring
spec:
  check_hooks: null
  command: >-
    http-get --timeout 10 --redirect-ok --url https://{{ .system.hostname }}:8080/metrics
  env_vars: null
  handlers: []
  high_flap_threshold: 0
  interval: 10
  low_flap_threshold: 0
  output_metric_format: prometheus_text
  output_metric_handlers:
  - influxdb
  pipelines: []
  proxy_entity_name: ""
  publish: true
  round_robin: false
  runtime_assets:
  - sensu/http-checks
  secrets: null
  stdin: false
  subdue: null
  subscriptions:
  - sensu-backend
  timeout: 5
  ttl: 0

What's more, the check exits with a 0 rather than an error:

Event info
{
  "type": "Event",
  "api_version": "core/v2",
  "metadata": {
    "namespace": "monitoring"
  },
  "spec": {
    "check": {
      "check_hooks": null,
      "command": "http-get --timeout 10 --redirect-ok --url https://{{ .system.hostname }}:8080/metrics",
      "duration": 0.008565643,
      "env_vars": null,
      "executed": 1653677971,
      "handlers": [],
      "high_flap_threshold": 0,
      "history": [
        {
          "executed": 1653677771,
          "status": 0
        },
        {
          "executed": 1653677781,
          "status": 0
        },
        {
          "executed": 1653677791,
          "status": 0
        },
        {
          "executed": 1653677801,
          "status": 0
        },
        {
          "executed": 1653677811,
          "status": 0
        },
        {
          "executed": 1653677821,
          "status": 0
        },
        {
          "executed": 1653677831,
          "status": 0
        },
        {
          "executed": 1653677841,
          "status": 0
        },
        {
          "executed": 1653677851,
          "status": 0
        },
        {
          "executed": 1653677861,
          "status": 0
        },
        {
          "executed": 1653677871,
          "status": 0
        },
        {
          "executed": 1653677881,
          "status": 0
        },
        {
          "executed": 1653677891,
          "status": 0
        },
        {
          "executed": 1653677901,
          "status": 0
        },
        {
          "executed": 1653677911,
          "status": 0
        },
        {
          "executed": 1653677921,
          "status": 0
        },
        {
          "executed": 1653677931,
          "status": 0
        },
        {
          "executed": 1653677941,
          "status": 0
        },
        {
          "executed": 1653677951,
          "status": 0
        },
        {
          "executed": 1653677961,
          "status": 0
        },
        {
          "executed": 1653677971,
          "status": 0
        }
      ],
      "interval": 10,
      "is_silenced": false,
      "issued": 1653677971,
      "last_ok": 1653677971,
      "low_flap_threshold": 0,
      "metadata": {
        "annotations": {
          "runbook": "",
          "team": "sre"
        },
        "labels": {
          "sensu.io/managed_by": "sensuctl"
        },
        "name": "sensu-backend-metrics",
        "namespace": "monitoring"
      },
      "occurrences": 245,
      "occurrences_watermark": 245,
      "output": "Error executing http-get: unknown command \"https://sensu02.sachshaus.net:8080/metrics\" for \"http-get\"\n",
      "output_metric_format": "prometheus_text",
      "output_metric_handlers": [
        "influxdb"
      ],
      "pipelines": [],
      "processed_by": "sensu02.sachshaus.net",
      "proxy_entity_name": "",
      "publish": true,
      "round_robin": false,
      "runtime_assets": [
        "sensu/http-checks"
      ],
      "scheduler": "memory",
      "secrets": null,
      "state": "passing",
      "status": 0,
      "stdin": false,
      "subdue": null,
      "subscriptions": [
        "sensu-backend"
      ],
      "timeout": 5,
      "total_state_change": 0,
      "ttl": 0
    },
    "entity": {
      "deregister": false,
      "deregistration": {},
      "entity_class": "agent",
      "last_seen": 1653677971,
      "metadata": {
        "labels": {
          "sensu.io/managed_by": "sensu-agent"
        },
        "name": "sensu02.sachshaus.net",
        "namespace": "monitoring"
      },
      "redact": [
        "password",
        "passwd",
        "pass",
        "api_key",
        "api_token",
        "access_key",
        "secret_key",
        "private_key",
        "secret"
      ],
      "sensu_agent_version": "6.7.1",
      "subscriptions": [
        "linux",
        "system",
        "monitoring",
        "sensu-backend",
        "ssl",
        "entity:sensu02.sachshaus.net"
      ],
      "system": {
        "arch": "amd64",
        "cloud_provider": "",
        "hostname": "sensu02.sachshaus.net",
        "libc_type": "glibc",
        "network": {
          "interfaces": [
            {
              "addresses": [
                "127.0.0.1/8",
                "::1/128"
              ],
              "name": "lo"
            },
            {
              "addresses": [
                "",
                ""
              ],
              "mac": "",
              "name": ""
            },
            {
              "addresses": [
                "redacted"
              ],
              "name": "tailscale0"
            }
          ]
        },
        "os": "linux",
        "platform": "ubuntu",
        "platform_family": "debian",
        "platform_version": "20.04",
        "processes": null,
        "vm_role": "guest",
        "vm_system": "kvm"
      },
      "user": "agent"
    },
    "id": "6b67e77f-d0c0-4bb5-b376-691ab463b9dd",
    "pipelines": null,
    "sequence": 15650,
    "timestamp": 1653677971
  }
}

Steps to replicate

  1. Create a check using the example above
  2. Manually execute check (or wait)
  3. Observe the error

Environment

  • OS: Ubuntu 20.04
  • Deployment: Sensu Go .deb package
  • Sensu Go version: 6.7.1

Add support for POST w/data

All requests now are GET, add support for POST with data, possibly as an inline string or file similar to curl.

Use jq or JMESPath for http-json

The gojsonq method is quite limited in how the JSON can be parsed and evaluated. Switching to jq or JMESPath will provide much for flexibility. For e.g. the sensu cluster health example can be written in json as ClusterHealth[?Healthy==`false`].Name with expression == []

proxy-entity-per-url

For use cases where you
A) have a number of URLs to check, and for each URL you would like to monitor, you would like to generate a proxy entity
or
B) have a number of URLs to check, and for each target domain you would like to monitor, you would like to generate a proxy entity

it would be helpful to have functionality whereby the check made use of the local agent HTTP API to generate check results with proxy entities for each monitored endpoint. Presently this would require significant duplication of check configuration.

A possible input to the check could be a series of endpoint with a minimum of 1) HTTP URI to check 2) proxy entity name for endpoint, and optional params such as expected http status code, or other data to be passed to the http check during execution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.