Skip to content

Monitoring & logging: Prometheus

Prometheus commands from the Monitoring & logging folder.

Index
6 items

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

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/…