diff --git a/lib/bashio.sh b/lib/bashio.sh index aa841de..6da4f72 100644 --- a/lib/bashio.sh +++ b/lib/bashio.sh @@ -58,6 +58,8 @@ source "${__BASHIO_LIB_DIR}/config.sh" source "${__BASHIO_LIB_DIR}/debug.sh" # shellcheck source=lib/exit.sh source "${__BASHIO_LIB_DIR}/exit.sh" +# shellcheck source=lib/dns.sh +source "${__BASHIO_LIB_DIR}/dns.sh" # shellcheck source=lib/hardware.sh source "${__BASHIO_LIB_DIR}/hardware.sh" # shellcheck source=lib/hassos.sh diff --git a/lib/dns.sh b/lib/dns.sh new file mode 100644 index 0000000..a384da5 --- /dev/null +++ b/lib/dns.sh @@ -0,0 +1,236 @@ +#!/usr/bin/env bash +# ============================================================================== +# Community Hass.io Add-ons: Bashio +# Bashio is an bash function library for use with Hass.io add-ons. +# +# It contains a set of commonly used operations and can be used +# to be included in add-on scripts to reduce code duplication across add-ons. +# ============================================================================== + +# ------------------------------------------------------------------------------ +# Updates the DNS to the latest version. +# +# Arguments: +# $1 Version to update to (optional) +# ------------------------------------------------------------------------------ +function bashio::dns.update() { + local version=${1:-} + + bashio::log.trace "${FUNCNAME[0]}:" "$@" + + if bashio::var.has_value "${version}"; then + version=$(bashio::var.json version "${version}") + bashio::api.hassio POST /dns/update "${version}" + else + bashio::api.hassio POST /dns/update + fi + bashio::cache.flush_all +} + +# ------------------------------------------------------------------------------ +# Restarts the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.restart() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::api.hassio POST /dns/restart + bashio::cache.flush_all +} + +# ------------------------------------------------------------------------------ +# Returns the logs created by the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.logs() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::api.hassio GET /dns/logs true +} + +# ------------------------------------------------------------------------------ +# Returns a JSON object with generic version information about the DNS. +# +# Arguments: +# $1 Cache key to store results in (optional) +# $2 jq Filter to apply on the result (optional) +# ------------------------------------------------------------------------------ +function bashio::dns() { + local cache_key=${1:-'dns.info'} + local filter=${2:-} + local info + local response + + bashio::log.trace "${FUNCNAME[0]}" "$@" + + if bashio::cache.exists "${cache_key}"; then + bashio::cache.get "${cache_key}" + return "${__BASHIO_EXIT_OK}" + fi + + if bashio::cache.exists 'dns.info'; then + info=$(bashio::cache.get 'dns.info') + else + info=$(bashio::api.hassio GET /dns/info false) + bashio::cache.set 'dns.info' "${info}" + fi + + response="${info}" + if bashio::var.has_value "${filter}"; then + response=$(bashio::jq "${info}" "${filter}") + fi + + bashio::cache.set "${cache_key}" "${response}" + printf "%s" "${response}" + + return "${__BASHIO_EXIT_OK}" +} + +# ------------------------------------------------------------------------------ +# Returns the Hass.io DNS host. +# ------------------------------------------------------------------------------ +function bashio::dns.host() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns 'dns.info.host' '.host' +} + +# ------------------------------------------------------------------------------ +# Returns the current version of the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.version() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns 'dns.info.version' '.version' +} + +# ------------------------------------------------------------------------------ +# Returns the latest version of the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.last_version() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns 'dns.info.last_version' '.last_version' +} + +# ------------------------------------------------------------------------------ +# Checks if there is an update available for the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.update_available() { + local version + local last_version + + bashio::log.trace "${FUNCNAME[0]}" + + version=$(bashio::dns.version) + last_version=$(bashio::dns.last_version) + + if [[ "${version}" = "${last_version}" ]]; then + return "${__BASHIO_EXIT_NOK}" + fi + + return "${__BASHIO_EXIT_OK}" +} + +# ------------------------------------------------------------------------------ +# Returns a list of DNS servers used by the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.servers() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::supervisor 'dns.info.servers' '.servers[]' +} + +# ------------------------------------------------------------------------------ +# List all available stats about the DNS. +# +# Arguments: +# $1 Cache key to store results in (optional) +# $2 jq Filter to apply on the result (optional) +# ------------------------------------------------------------------------------ +function bashio::dns.stats() { + local cache_key=${1:-'dns.stats'} + local filter=${2:-} + local info + local response + + bashio::log.trace "${FUNCNAME[0]}" "$@" + + if bashio::cache.exists "${cache_key}"; then + bashio::cache.get "${cache_key}" + return "${__BASHIO_EXIT_OK}" + fi + + if bashio::cache.exists 'dns.stats'; then + info=$(bashio::cache.get 'dns.stats') + else + info=$(bashio::api.hassio GET /dns/stats false) + bashio::cache.set 'dns.stats' "${info}" + fi + + response="${info}" + if bashio::var.has_value "${filter}"; then + response=$(bashio::jq "${info}" "${filter}") + fi + + bashio::cache.set "${cache_key}" "${response}" + printf "%s" "${response}" + + return "${__BASHIO_EXIT_OK}" +} + +# ------------------------------------------------------------------------------ +# Returns CPU usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.cpu_percent() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.cpu_percent' '.cpu_percent' +} + +# ------------------------------------------------------------------------------ +# Returns memory usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.memory_usage() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.memory_usage' '.memory_usage' +} + +# ------------------------------------------------------------------------------ +# Returns memory limit from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.memory_limit() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.memory_limit' '.memory_limit' +} + +# ------------------------------------------------------------------------------ +# Returns memory usage in percent from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.memory_percent() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.memory_percent' '.memory_percent' +} + +# ------------------------------------------------------------------------------ +# Returns outgoing network usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.network_tx() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.network_tx' '.network_tx' +} + +# ------------------------------------------------------------------------------ +# Returns incoming network usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.network_rx() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.network_rx' '.network_rx' +} + +# ------------------------------------------------------------------------------ +# Returns disk read usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.blk_read() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.blk_read' '.blk_read' +} + +# ------------------------------------------------------------------------------ +# Returns disk write usage from the DNS. +# ------------------------------------------------------------------------------ +function bashio::dns.blk_write() { + bashio::log.trace "${FUNCNAME[0]}" + bashio::dns.stats 'dns.stats.blk_write' '.blk_write' +}