You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
9.0 KiB

######################################################################
# @CCOSTAN - Follow Me on X
# For more info visit https://www.vcloudinfo.com/click-here
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# -------------------------------------------------------------------
# MariaDB Monitoring - Snapshot-driven DB health sensors
# Recorder-backed metrics for MariaDB health, capacity, and tuning.
# -------------------------------------------------------------------
# Notes: Uses command_line snapshot helpers so expensive MariaDB queries are not forced to run every 30 seconds by the SQL integration.
# Notes: Live metrics poll hourly; recorder/admin snapshots poll weekly.
# Notes: Numeric template sensors expose state_class where useful so HA can keep long-term statistics efficiently.
######################################################################
command_line:
- sensor:
name: MariaDB Live Snapshot
unique_id: mariadb_live_snapshot
command: "python3 /config/shell_scripts/mariadb_snapshot.py live"
scan_interval: 3600
command_timeout: 30
json_attributes:
- performance
- connections
- questions
- uptime_seconds
value_template: "{{ value_json.status | default('unknown') }}"
- sensor:
name: MariaDB Recorder Snapshot
unique_id: mariadb_recorder_snapshot
command: "python3 /config/shell_scripts/mariadb_snapshot.py recorder"
scan_interval: 604800
command_timeout: 180
json_attributes:
- database_tables_count
- database_oldest_record
- database_total_records
- database_records_per_day
value_template: "{{ value_json.database_size_mib | default('unknown') }}"
- sensor:
name: MariaDB Admin Snapshot
unique_id: mariadb_admin_snapshot
command: "python3 /config/shell_scripts/mariadb_snapshot.py admin"
scan_interval: 604800
command_timeout: 30
json_attributes:
- version
- max_connections
- log_file_size_mib
- tmp_table_size_mib
- io_capacity
- io_threads_read
- io_threads_write
- table_cache
- sort_buffer_mib
- read_buffer_mib
- join_buffer_mib
value_template: "{{ value_json.buffer_pool_gib | default('unknown') }}"
template:
- sensor:
- name: "MariaDB Status"
unique_id: mariadb_status
state: >-
{% set value = states('sensor.mariadb_live_snapshot') %}
{{ value if value not in ['unknown', 'unavailable', 'none', ''] else 'unknown' }}
- name: "MariaDB Version"
unique_id: mariadb_version
state: >-
{% set value = state_attr('sensor.mariadb_admin_snapshot', 'version') %}
{{ value if value is not none else 'unknown' }}
- name: "MariaDB Performance"
unique_id: mariadb_performance
unit_of_measurement: "q/s"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'performance') is not none }}
state: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'performance') | float(0) }}
- name: "Database Size"
unique_id: database_size
unit_of_measurement: "MiB"
state_class: measurement
availability: >-
{{ states('sensor.mariadb_recorder_snapshot') not in ['unknown', 'unavailable', 'none', ''] }}
state: >-
{{ states('sensor.mariadb_recorder_snapshot') | float(0) }}
- name: "Database Tables Count"
unique_id: database_tables_count
unit_of_measurement: "tables"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_tables_count') is not none }}
state: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_tables_count') | int(0) }}
- name: "Database Oldest Record"
unique_id: database_oldest_record
state: >-
{% set value = state_attr('sensor.mariadb_recorder_snapshot', 'database_oldest_record') %}
{{ value if value is not none else 'unknown' }}
- name: "Database Total Records"
unique_id: database_total_records
unit_of_measurement: "records"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_total_records') is not none }}
state: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_total_records') | int(0) }}
- name: "Database Records Per Day"
unique_id: database_records_per_day
unit_of_measurement: "records/day"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_records_per_day') is not none }}
state: >-
{{ state_attr('sensor.mariadb_recorder_snapshot', 'database_records_per_day') | float(0) }}
- name: "MariaDB Uptime"
unique_id: mariadb_uptime
unit_of_measurement: "s"
availability: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'uptime_seconds') is not none }}
state: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'uptime_seconds') | int(0) }}
- name: "MariaDB Connections"
unique_id: mariadb_connections
unit_of_measurement: "connections"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'connections') is not none }}
state: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'connections') | int(0) }}
- name: "MariaDB Questions"
unique_id: mariadb_questions
unit_of_measurement: "queries"
state_class: total_increasing
availability: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'questions') is not none }}
state: >-
{{ state_attr('sensor.mariadb_live_snapshot', 'questions') | int(0) }}
- name: "MariaDB Buffer Pool Size"
unique_id: mariadb_buffer_pool_size
unit_of_measurement: "GiB"
state_class: measurement
availability: >-
{{ states('sensor.mariadb_admin_snapshot') not in ['unknown', 'unavailable', 'none', ''] }}
state: >-
{{ states('sensor.mariadb_admin_snapshot') | float(0) }}
- name: "MariaDB Max Connections"
unique_id: mariadb_max_connections
unit_of_measurement: "connections"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'max_connections') is not none }}
state: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'max_connections') | int(0) }}
- name: "MariaDB Log File Size"
unique_id: mariadb_log_file_size
unit_of_measurement: "MiB"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'log_file_size_mib') is not none }}
state: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'log_file_size_mib') | float(0) }}
- name: "MariaDB Tmp Table Size"
unique_id: mariadb_tmp_table_size
unit_of_measurement: "MiB"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'tmp_table_size_mib') is not none }}
state: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'tmp_table_size_mib') | float(0) }}
- name: "MariaDB IO Capacity"
unique_id: mariadb_io_capacity
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'io_capacity') is not none }}
state: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'io_capacity') | int(0) }}
- name: "MariaDB IO Threads"
unique_id: mariadb_io_threads
state: >-
{% set read = state_attr('sensor.mariadb_admin_snapshot', 'io_threads_read') %}
{% set write = state_attr('sensor.mariadb_admin_snapshot', 'io_threads_write') %}
{% if read is not none and write is not none %}
Read: {{ read }}, Write: {{ write }}
{% else %}
unknown
{% endif %}
- name: "MariaDB Table Cache"
unique_id: mariadb_table_cache
unit_of_measurement: "tables"
state_class: measurement
availability: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'table_cache') is not none }}
state: >-
{{ state_attr('sensor.mariadb_admin_snapshot', 'table_cache') | int(0) }}
- name: "MariaDB Buffer Sizes"
unique_id: mariadb_buffer_sizes
state: >-
{% set sort = state_attr('sensor.mariadb_admin_snapshot', 'sort_buffer_mib') %}
{% set read = state_attr('sensor.mariadb_admin_snapshot', 'read_buffer_mib') %}
{% set join = state_attr('sensor.mariadb_admin_snapshot', 'join_buffer_mib') %}
{% if sort is not none and read is not none and join is not none %}
Sort: {{ sort }}M, Read: {{ read }}M, Join: {{ join }}M
{% else %}
unknown
{% endif %}

Powered by TurnKey Linux.