id: tidb-tikv-region-peer-health-sentinel
namespace: company.data
inputs:
- id: tidb_host
type: STRING
displayName: TiDB Host
defaults: "tidb.internal"
description: Hostname or IP address of the TiDB server exposing the MySQL wire
protocol port.
- id: tidb_port
type: INT
displayName: TiDB Port
defaults: 4000
description: Port number for the MySQL wire protocol interface on TiDB (default 4000).
- id: tidb_database
type: STRING
displayName: Database Name
defaults: "information_schema"
description: Target database name to establish the session against (default
information_schema).
- id: slack_channel
type: STRING
displayName: Slack Channel
defaults: "#data-platform-alerts"
description: Slack channel destination for operational TiDB cluster incident cards.
- id: down_seconds_threshold
type: INT
displayName: Down Seconds Threshold
defaults: 300
description: Maximum tolerated elapsed seconds a TiKV peer may remain offline
before triggering critical incident alerts.
- id: inspect_offline_stores
type: BOOL
displayName: Inspect Offline Stores
defaults: true
description: Automatically query tikv_store_status to correlate degraded region
peers with offline host storage daemons.
triggers:
- id: every_fifteen_minutes
type: io.kestra.plugin.core.trigger.Schedule
cron: "*/15 * * * *"
tasks:
- id: probe_degraded_region_peers
type: io.kestra.plugin.jdbc.mysql.Query
url: "jdbc:mysql://{{ inputs.tidb_host }}:{{ inputs.tidb_port }}/{{
inputs.tidb_database }}"
username: "{{ secret('TIDB_USERNAME') }}"
password: "{{ secret('TIDB_PASSWORD') }}"
fetchType: FETCH
allowFailure: true
sql: |
SELECT
COALESCE(s.db_name, 'system') AS db_name,
COALESCE(s.table_name, 'internal') AS table_name,
p.region_id,
p.peer_id,
p.store_id,
p.status AS peer_status,
COALESCE(p.down_seconds, 0) AS down_seconds
FROM information_schema.tikv_region_peers p
LEFT JOIN information_schema.tikv_region_status s
ON p.region_id = s.region_id
WHERE p.status IN ('DOWN', 'PENDING')
ORDER BY p.status ASC, p.down_seconds DESC
LIMIT 50;
- id: route_by_cluster_health
type: io.kestra.plugin.core.flow.Switch
value: >-
{% if outputs.probe_degraded_region_peers.rows is null %} PROBE_FAILED {%
else %}
{% set critical_down_count = 0 %}
{% set pending_peer_count = 0 %}
{% for row in outputs.probe_degraded_region_peers.rows %}
{% if row.peer_status == 'DOWN' and row.down_seconds >= inputs.down_seconds_threshold %}
{% set critical_down_count = critical_down_count + 1 %}
{% elseif row.peer_status == 'PENDING' or row.peer_status == 'DOWN' %}
{% set pending_peer_count = pending_peer_count + 1 %}
{% endif %}
{% endfor %}
{% if critical_down_count > 0 %}
CRITICAL
{% elseif pending_peer_count > 0 %}
WARNING
{% else %}
HEALTHY
{% endif %}
{% endif %}
cases:
CRITICAL:
- id: alert_critical_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"text": "[CRITICAL] TiDB TiKV Region Peers Offline Exceeding Threshold - Cluster Quorum at Risk",
"attachments": [
{
"color": "#D32F2F",
"title": "TiDB TiKV Region Replication Failure Incident",
"fields": [
{
"title": "Cluster Host",
"value": "{{ inputs.tidb_host }}:{{ inputs.tidb_port }}",
"short": true
},
{
"title": "Degraded Peers Count",
"value": "{{ outputs.probe_degraded_region_peers.size }}",
"short": true
},
{
"title": "Severity",
"value": "CRITICAL - Persistent Down Peers",
"short": true
},
{
"title": "Threshold (Seconds)",
"value": "{{ inputs.down_seconds_threshold }}s",
"short": true
}
],
"footer": "Kestra TiDB Sentinel | Execution ID: {{ execution.id }}"
}
]
}
- id: inspect_stores_critical
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.inspect_offline_stores == true }}"
then:
- id: query_offline_stores
type: io.kestra.plugin.jdbc.mysql.Query
url: "jdbc:mysql://{{ inputs.tidb_host }}:{{ inputs.tidb_port }}/{{
inputs.tidb_database }}"
username: "{{ secret('TIDB_USERNAME') }}"
password: "{{ secret('TIDB_PASSWORD') }}"
fetchType: FETCH
sql: |
SELECT
store_id,
address,
state_name
FROM information_schema.tikv_store_status
WHERE state_name != 'Up';
- id: log_offline_stores
type: io.kestra.plugin.core.log.Log
message: "Degraded or offline TiKV storage nodes identified: {{
outputs.query_offline_stores.rows }}"
WARNING:
- id: alert_warning_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"text": "[WARNING] TiDB TiKV Pending or Transient Offline Peers Detected - Replication Lagging",
"attachments": [
{
"color": "#F57C00",
"title": "TiDB TiKV Region Replication Warning",
"fields": [
{
"title": "Cluster Host",
"value": "{{ inputs.tidb_host }}:{{ inputs.tidb_port }}",
"short": true
},
{
"title": "Pending or Brief Down Peers",
"value": "{{ outputs.probe_degraded_region_peers.size }}",
"short": true
},
{
"title": "Severity",
"value": "WARNING - Transient Lag or Node Catchup",
"short": true
},
{
"title": "Inspect Stores",
"value": "{{ inputs.inspect_offline_stores ? 'Enabled' : 'Disabled' }}",
"short": true
}
],
"footer": "Kestra TiDB Sentinel | Execution ID: {{ execution.id }}"
}
]
}
PROBE_FAILED:
- id: alert_probe_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"text": "[CRITICAL] TiDB Sentinel Probe Failed - Unable to query information_schema.tikv_region_peers. Verify network routing and database grants."
}
HEALTHY:
- id: log_cluster_healthy
type: io.kestra.plugin.core.log.Log
message: "TiDB TiKV cluster region health is normal. All evaluated Raft region
peers are in NORMAL state."
- id: log_audit_summary
type: io.kestra.plugin.core.log.Log
message: "TiDB TiKV region health audit completed. Status: {{
outputs.route_by_cluster_health.value ?? 'PROCESSED' }}. Evaluated
degraded peers count: {{ outputs.probe_degraded_region_peers.size ?? 0
}}."
errors:
- id: on_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"text": "[ERROR] TiDB TiKV Region Peer Sentinel flow execution aborted unexpectedly. Execution ID: {{ execution.id }}."
}