Skip to content

Commands: Monitoring & logging

Prometheus, Alertmanager, Grafana and Elasticsearch commands.

Index
  • #prometheus
  • #grafana
  • #elasticsearch
  • #monitoring
Updated

Also:observability · promql · alertmanager · elk · kibana · es · alerts

15 items

Prometheus6

Prometheus: Check which targets are down

Safe

Every scrape target currently failing, as job + instance.

curl -s "<prometheus-url>/api/v1/query?query=up==0" | jq -r '.data.result[].metric | "\(.job) \(.instance)"'
What each part does
  1. curl -sHTTP request, quiet
  2. /api/v1/query?query=up==0instant query: targets whose last scrape failed
  3. | jq -rparse the JSON, print raw strings
  4. '.data.result[].metric | "\(.job) \(.instance)"'for each result, print its job and instance

Also known as: target down · up == 0 · scrape failed

Prometheus: Validate config and rule files

Safe

Checks prometheus.yml and every rule file it references. Run it in CI before deploying.

promtool check config <config-file>
What each part does
  1. promtool check configvalidate prometheus.yml and the rule files it references
  2. <config-file>path to the config; use promtool check rules <file> for a rule file alone

Also known as: promtool · rule syntax

Prometheus: Run a PromQL query from the CLI

Safe

Evaluate an instant query without opening the UI.

promtool query instant <prometheus-url> '<query>'
What each part does
  1. promtool query instantevaluate a PromQL query at the current time
  2. <prometheus-url>the Prometheus server
  3. '<query>'the PromQL expression, in single quotes so the shell leaves it alone
Example
$ promtool query instant http://localhost:9090 'sum by (pod) (rate(container_cpu_usage_seconds_total[5m]))'

Also known as: promql

Prometheus: Reload config without restarting

Safe

Hot-reload config and rules.

curl -X POST <prometheus-url>/-/reload
What each part does
  1. curl -X POSTthe reload endpoint needs POST
  2. <prometheus-url>/-/reloadre-read config and rule files; only works with --web.enable-lifecycle

My note Needs --web.enable-lifecycle. With the Operator, edit the CRDs instead.

Also known as: reload prometheus

Prometheus: List firing alerts

Safe

Names of all alerts that are firing right now.

curl -s <prometheus-url>/api/v1/alerts | jq -r '.data.alerts[] | select(.state=="firing") | .labels.alertname'
What each part does
  1. curl -s <prometheus-url>/api/v1/alertsall active alerts (pending and firing)
  2. | jq -rparse the JSON, print raw strings
  3. '.data.alerts[] | select(.state=="firing") | .labels.alertname'keep firing alerts and print their names

Also known as: firing alerts · alerts api

Prometheus: Silence an alert with amtool

Caution

Mute an alert during maintenance. amtool silence query lists active silences.

amtool silence add alertname=<alert> --duration=<duration> --comment="<reason>" --alertmanager.url=<alertmanager-url>
What each part does
  1. amtool silence addcreate a silence in Alertmanager
  2. alertname=<alert>matcher: which alerts to mute (add more label=value matchers to narrow)
  3. --duration=<duration>how long, e.g. 2h
  4. --comment="<reason>"why: shown to everyone in the UI
  5. --alertmanager.url=<alertmanager-url>the Alertmanager to talk to
Example
$ amtool silence add alertname=KubeNodeNotReady --duration=2h --comment="node drain" --alertmanager.url=http://localhost:9093

Also known as: alertmanager · silence · maintenance window

Grafana3

Grafana: Check health

Safe

Returns version and database status. Handy for load balancer checks.

curl -s <grafana-url>/api/health
What each part does
  1. curl -sHTTP request, quiet
  2. <grafana-url>/api/healthunauthenticated health endpoint: database ok + version

Also known as: grafana down

Grafana: Reset the admin password

Caution

Run on the Grafana host/container when the admin login is lost.

grafana cli admin reset-admin-password <new-password>
What each part does
  1. grafana cliGrafana's command line (grafana-cli on older versions); run on the server
  2. admin reset-admin-passwordset a new password for the admin user directly in the database
  3. <new-password>the new password

Also known as: forgot admin password · grafana-cli

Grafana: Export a dashboard as JSON

Safe

Back up or version a dashboard. The uid is in the dashboard URL.

curl -s -H "Authorization: Bearer $GRAFANA_TOKEN" <grafana-url>/api/dashboards/uid/<dashboard-uid> | jq '.dashboard' > <dashboard-uid>.json
What each part does
  1. curl -sHTTP request, quiet
  2. -H "Authorization: Bearer $GRAFANA_TOKEN"authenticate with a service account token from an env var
  3. <grafana-url>/api/dashboards/uid/<dashboard-uid>the dashboard (UID is in the dashboard URL)
  4. | jq '.dashboard'keep the dashboard model, drop the meta block
  5. > <dashboard-uid>.jsonsave it to a file
  • #grafana
  • #backup

Also known as: dashboard json · dashboards as code

Elasticsearch6

Elasticsearch: Check cluster health

Safe

Status green / yellow / red, node count and unassigned shards.

curl -s "<es-url>/_cluster/health?pretty"
What each part does
  1. curl -sHTTP request, no progress bar
  2. <es-url>cluster address, e.g. http://localhost:9200 (add -u user:pass if secured)
  3. /_cluster/healthcluster status: green / yellow (replicas unassigned) / red (primaries missing), shard counts
  4. ?prettyindent the JSON

Also known as: cluster red · cluster yellow · _cluster/health

Elasticsearch: Explain unassigned shards

Safe

Why the first unassigned shard can't be allocated (disk watermark, node left…).

curl -s "<es-url>/_cluster/allocation/explain?pretty"
What each part does
  1. curl -sHTTP request, quiet
  2. /_cluster/allocation/explainexplains why the first unassigned shard can't be placed (disk, filters, node left...)
  3. ?prettyindent the JSON

Also known as: unassigned shards · cluster yellow · allocation explain

Elasticsearch: List indices by size

Safe

Biggest indices first, with doc count and health.

curl -s "<es-url>/_cat/indices?v&s=store.size:desc" | head -20
What each part does
  1. curl -sHTTP request, quiet
  2. /_cat/indicesone line per index: health, status, docs, size
  3. ?vadd a header row
  4. &s=store.size:descsort by size, biggest first
  5. | head -20keep the header + top 19

Also known as: _cat/indices · index size

Elasticsearch: Search recent error logs

Safe

Last 20 error-level documents. Change the field to match your log schema.

curl -s "<es-url>/<index-pattern>/_search?q=level:error&size=20&sort=@timestamp:desc&pretty"
What each part does
  1. curl -sHTTP request, quiet
  2. <index-pattern>/_searchsearch across matching indices, e.g. logs-*
  3. q=level:errorLucene query string: documents where level is error
  4. size=20return 20 hits
  5. sort=@timestamp:descnewest first
  6. prettyindent the JSON

Also known as: kibana search · elk logs

Elasticsearch: Clear read-only block after disk full

Caution

Re-enables writes once you've freed disk space below the flood-stage watermark.

curl -X PUT "<es-url>/_all/_settings" -H 'Content-Type: application/json' -d '{"index.blocks.read_only_allow_delete": null}'
What each part does
  1. curl -X PUTHTTP PUT: update settings
  2. "<es-url>/_all/_settings"the settings of every index
  3. -H 'Content-Type: application/json'tell ES the body is JSON
  4. -d '{"index.blocks.read_only_allow_delete": null}'body: reset the block ES set when disk hit the flood-stage watermark

My note Recent versions clear the block automatically once disk drops under the high watermark.

Also known as: FORBIDDEN/12/index read-only / allow delete (api) · flood stage disk watermark · read_only_allow_delete

Elasticsearch: Delete old indices

Destructive

Drop an index to free disk. Prefer ILM policies for routine retention.

curl -X DELETE "<es-url>/<index-name>"
What each part does
  1. curl -X DELETEHTTP DELETE
  2. "<es-url>/<index-name>"the index to delete for good; wildcards may be blocked by action.destructive_requires_name

My note Wildcard deletes are blocked by default (action.destructive_requires_name). Keep it that way.

Also known as: delete index · free disk elasticsearch

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…