Skip to content

Instantly share code, notes, and snippets.

@jspaleta
Last active April 19, 2019 22:43
Show Gist options
  • Save jspaleta/24a5514b9b10c48adc8ca817eedfebdf to your computer and use it in GitHub Desktop.
Save jspaleta/24a5514b9b10c48adc8ca817eedfebdf to your computer and use it in GitHub Desktop.
Quick Start for Sensu Ruby Plugin Checks in Sensu Go Agent containers using Sensu Assets

Quick Start

Setup Sensu Containers

Start Sensu Backend Container

docker run -v /tmp/sensu:/var/lib/sensu -d --name sensu-backend -p 2380:2380 -p 3000:3000 -p 8080:8080 -p 8081:8081 sensu/sensu:latest sensu-backend start

Note 1: this will mount your hosts /tmp/sensu directory into the container's /var/lib/sensu. Replace the host directory as needed for your environment. I use /tmp/sensu here to make it easy to clean up and get back to an unconfigured state.

Note 2: The sensu-backend service exposes all necessary ports to the local host so that you can interact with it as if it were running as a local service instead of a container.

Start Sensu Agent Container

docker run -v /tmp/sensu:/var/lib/sensu -d --name sensu-agent --link=sensu-backend sensu/sensu:latest sensu-agent start --backend-url ws://sensu-backend:8081 --subscriptions dev --cache-dir /var/lib/sensu

Note: this will mount your hosts '/tmp/sensu' directory into the container's /var/lib/sensu and use it as the cache location in which to store downloaded assets. You will be able to look into the host's /tmp/sensu/ and see new asset directories added as you include additional assets in your check configurations.

Configure sensuctl on docker host

sensuctl configure

Use the default user/password as and Sensu Backend URL: http://localhost:8080

Ensure Agent is connected to backend

sensuctl entity list

should have 1 entity corresponding to the sensu-agent container

Build a CPU Check Using assets

Download Asset Definition

Go to Bonsai: https://bonsai.sensu.io/assets/sensu-plugins/sensu-plugins-cpu-checks Download the latest asset definition, choose platform: alpine and arch: amd64

Install asset definition

sensuctl create -f ~/Downloads/sensu-plugins-sensu-plugins-cpu-checks-4.0.0-alpine-amd64.yml

You may need to adjust the file path depending on where your browser is configured to save Downloaded files and the version of the asset you selected.

Create Check Definition Using Asset

type: CheckConfig
api_version: core/v2
metadata:
  name: check-cpu-alpine
  namespace: default
spec:
  check_hooks: null
  command: check-cpu.rb
  env_vars: null
  handlers: []
  high_flap_threshold: 0
  interval: 60
  low_flap_threshold: 0
  output_metric_format: ""
  output_metric_handlers: []
  proxy_entity_name: ""
  publish: true
  round_robin: false
  runtime_assets:
  - sensu-plugins-cpu-checks
  stdin: false
  subdue: null
  subscriptions:
  - dev
  timeout: 20
  ttl: 0

Review event list

Wait a minute for the check to execute

sensuctl event list

check event should be in a critical state should error output because we have not installed the ruby runtime environment yet.

Install ruby runtime asset

Go to Bonsai: https://bonsai.sensu.io/assets/sensu/sensu-ruby-runtime Download the latest version of asset definition, choose platform: alpine and arch: amd64

Install asset definition

sensuctl create -f ~/Downloads/sensu-sensu-ruby-runtime-0.0.5-alpine-amd64.yml

Add runtime asset to check config

sensuctl edit check check-cpu-alpine

Final check defiition should look like this:

type: CheckConfig
api_version: core/v2
metadata:
  name: check-cpu-alpine
  namespace: default
spec:
  check_hooks: null
  command: check-cpu.rb
  env_vars: null
  handlers: []
  high_flap_threshold: 0
  interval: 60
  low_flap_threshold: 0
  output_metric_format: ""
  output_metric_handlers: []
  proxy_entity_name: ""
  publish: true
  round_robin: false
  runtime_assets:
  - sensu-ruby-runtime
  - sensu-plugins-cpu-checks
  stdin: false
  subdue: null
  subscriptions:
  - dev
  timeout: 20
  ttl: 0

Review check event

Wait a minute and review the event list again. Check should now be operating correctly.

Shutdown and Cleanup Containers

docker stop sensu-agent
docker rm sensu-agent
docker stop sensu-backend
docker rm sensu-backend

You can also choose to clean out the /tmp/sensu directory. if you re-run this process again without cleaning this directory out, sensu containers will re-use resources configured in prior run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment