adding node exporter, loki, and promtail
This commit is contained in:
		
							
								
								
									
										129
									
								
								addon-promtail-main/rootfs/etc/cont-init.d/promtail_setup.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								addon-promtail-main/rootfs/etc/cont-init.d/promtail_setup.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,129 @@
 | 
			
		||||
#!/usr/bin/with-contenv bashio
 | 
			
		||||
# shellcheck shell=bash
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
# Home Assistant Add-on: Promtail
 | 
			
		||||
# This file makes the config file from inputs
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
readonly CONFIG_DIR=/etc/promtail
 | 
			
		||||
readonly CONFIG_FILE="${CONFIG_DIR}/config.yaml"
 | 
			
		||||
readonly BASE_CONFIG="${CONFIG_DIR}/base_config.yaml"
 | 
			
		||||
readonly DEF_SCRAPE_CONFIGS="${CONFIG_DIR}/default-scrape-config.yaml"
 | 
			
		||||
readonly CUSTOM_SCRAPE_CONFIGS="${CONFIG_DIR}/custom-scrape-config.yaml"
 | 
			
		||||
declare cafile
 | 
			
		||||
declare add_stages
 | 
			
		||||
declare add_scrape_configs
 | 
			
		||||
scrape_configs="${DEF_SCRAPE_CONFIGS}"
 | 
			
		||||
 | 
			
		||||
bashio::log.info 'Setting base config for promtail...'
 | 
			
		||||
cp "${BASE_CONFIG}" "${CONFIG_FILE}"
 | 
			
		||||
 | 
			
		||||
# Set up client section
 | 
			
		||||
if ! bashio::config.is_empty 'client.username'; then
 | 
			
		||||
    bashio::log.info 'Adding basic auth to client config...'
 | 
			
		||||
    bashio::config.require 'client.password' "'client.username' is specified"
 | 
			
		||||
    {
 | 
			
		||||
        echo "    basic_auth:"
 | 
			
		||||
        echo "      username: $(bashio::config 'client.username')"
 | 
			
		||||
        echo "      password: $(bashio::config 'client.password')"
 | 
			
		||||
    } >> "${CONFIG_FILE}"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if ! bashio::config.is_empty 'client.cafile'; then
 | 
			
		||||
    bashio::log.info "Adding TLS to client config..."
 | 
			
		||||
    cafile="/ssl/$(bashio::config 'client.cafile')"
 | 
			
		||||
 | 
			
		||||
    if ! bashio::fs.file_exists "${cafile}"; then
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::log.fatal "The file specified for 'cafile' does not exist!"
 | 
			
		||||
        bashio::log.fatal "Ensure the CA certificate file exists and full path is provided"
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::exit.nok
 | 
			
		||||
    fi
 | 
			
		||||
    {
 | 
			
		||||
        echo "    tls_config:"
 | 
			
		||||
        echo "      ca_file: ${cafile}"
 | 
			
		||||
    } >> "${CONFIG_FILE}"
 | 
			
		||||
 | 
			
		||||
    if ! bashio::config.is_empty 'client.servername'; then
 | 
			
		||||
        echo "      server_name: $(bashio::config 'client.servername')" >> "${CONFIG_FILE}"
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if ! bashio::config.is_empty 'client.certfile'; then
 | 
			
		||||
        bashio::log.info "Adding mTLS to client config..."
 | 
			
		||||
        bashio::config.require 'client.keyfile' "'client.certfile' is specified"
 | 
			
		||||
        if ! bashio::fs.file_exists "$(bashio::config 'client.certfile')"; then
 | 
			
		||||
            bashio::log.fatal
 | 
			
		||||
            bashio::log.fatal "The file specified for 'certfile' does not exist!"
 | 
			
		||||
            bashio::log.fatal "Ensure the certificate file exists and full path is provided"
 | 
			
		||||
            bashio::log.fatal
 | 
			
		||||
            bashio::exit.nok
 | 
			
		||||
        fi
 | 
			
		||||
        if ! bashio::fs.file_exists "$(bashio::config 'client.keyfile')"; then
 | 
			
		||||
            bashio::log.fatal
 | 
			
		||||
            bashio::log.fatal "The file specified for 'keyfile' does not exist!"
 | 
			
		||||
            bashio::log.fatal "Ensure the key file exists and full path is provided"
 | 
			
		||||
            bashio::log.fatal
 | 
			
		||||
            bashio::exit.nok
 | 
			
		||||
        fi
 | 
			
		||||
        {
 | 
			
		||||
            echo "      cert_file: $(bashio::config 'client.certfile')"
 | 
			
		||||
            echo "      key_file: $(bashio::config 'client.keyfile')"
 | 
			
		||||
        } >> "${CONFIG_FILE}"
 | 
			
		||||
    fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Add in scrape configs
 | 
			
		||||
{
 | 
			
		||||
    echo
 | 
			
		||||
    echo "scrape_configs:"
 | 
			
		||||
} >> "${CONFIG_FILE}"
 | 
			
		||||
if bashio::config.true 'skip_default_scrape_config'; then
 | 
			
		||||
    bashio::log.info 'Skipping default journald scrape config...'
 | 
			
		||||
    if ! bashio::config.is_empty 'additional_pipeline_stages'; then
 | 
			
		||||
        bashio::log.warning
 | 
			
		||||
        bashio::log.warning "'additional_pipeline_stages' ignored since 'skip_default_scrape_config' is true!"
 | 
			
		||||
        bashio::log.warning 'See documentation for more information.'
 | 
			
		||||
        bashio::log.warning
 | 
			
		||||
    fi
 | 
			
		||||
    bashio::config.require 'additional_scrape_configs' "'skip_default_scrape_config' is true"
 | 
			
		||||
 | 
			
		||||
elif ! bashio::config.is_empty 'additional_pipeline_stages'; then
 | 
			
		||||
    bashio::log.info "Adding additional pipeline stages to default journal scrape config..."
 | 
			
		||||
    add_stages="$(bashio::config 'additional_pipeline_stages')"
 | 
			
		||||
    scrape_configs="${CUSTOM_SCRAPE_CONFIGS}"
 | 
			
		||||
    if ! bashio::fs.file_exists "${add_stages}"; then
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::log.fatal "The file specified for 'additional_pipeline_stages' does not exist!"
 | 
			
		||||
        bashio::log.fatal "Ensure the file exists at the path specified"
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::exit.nok
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    yq -NP eval-all \
 | 
			
		||||
        'select(fi == 0) + [{"add_pipeline_stages": select(fi == 1)}]' \
 | 
			
		||||
        "${DEF_SCRAPE_CONFIGS}" "${add_stages}" \
 | 
			
		||||
    | yq -NP e \
 | 
			
		||||
        '[(.[0] * .[1]) | {"job_name": .job_name, "journal": .journal, "relabel_configs": .relabel_configs, "pipeline_stages": .pipeline_stages + .add_pipeline_stages}]' \
 | 
			
		||||
        - > "${scrape_configs}"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if ! bashio::config.is_empty 'additional_scrape_configs'; then
 | 
			
		||||
    bashio::log.info "Adding custom scrape configs..."
 | 
			
		||||
    add_scrape_configs="$(bashio::config 'additional_scrape_configs')"
 | 
			
		||||
    if ! bashio::fs.file_exists "${add_scrape_configs}"; then
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::log.fatal "The file specified for 'additional_scrape_configs' does not exist!"
 | 
			
		||||
        bashio::log.fatal "Ensure the file exists at the path specified"
 | 
			
		||||
        bashio::log.fatal
 | 
			
		||||
        bashio::exit.nok
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if bashio::config.true 'skip_default_scrape_config'; then
 | 
			
		||||
        yq -NP e '[] + .' "${add_scrape_configs}" >> "${CONFIG_FILE}"
 | 
			
		||||
    else
 | 
			
		||||
        yq -NP eval-all 'select(fi == 0) + select(fi == 1)' \
 | 
			
		||||
            "${scrape_configs}" "${add_scrape_configs}" >> "${CONFIG_FILE}"
 | 
			
		||||
    fi
 | 
			
		||||
else
 | 
			
		||||
    yq -NP e '[] + .' "${scrape_configs}" >> "${CONFIG_FILE}"
 | 
			
		||||
fi
 | 
			
		||||
							
								
								
									
										11
									
								
								addon-promtail-main/rootfs/etc/promtail/base_config.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								addon-promtail-main/rootfs/etc/promtail/base_config.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
---
 | 
			
		||||
server:
 | 
			
		||||
  http_listen_port: 9080
 | 
			
		||||
  grpc_listen_port: 0
 | 
			
		||||
  log_level: "${LOG_LEVEL}"
 | 
			
		||||
 | 
			
		||||
positions:
 | 
			
		||||
  filename: /data/promtail/positions.yaml
 | 
			
		||||
 | 
			
		||||
clients:
 | 
			
		||||
  - url: "${URL}"
 | 
			
		||||
@@ -0,0 +1,27 @@
 | 
			
		||||
---
 | 
			
		||||
- job_name: journal
 | 
			
		||||
  journal:
 | 
			
		||||
    json: false
 | 
			
		||||
    max_age: 12h
 | 
			
		||||
    labels:
 | 
			
		||||
      job: systemd-journal
 | 
			
		||||
    path: "${JOURNAL_PATH}"
 | 
			
		||||
  relabel_configs:
 | 
			
		||||
    - source_labels:
 | 
			
		||||
        - __journal__systemd_unit
 | 
			
		||||
      target_label: unit
 | 
			
		||||
    - source_labels:
 | 
			
		||||
        - __journal__hostname
 | 
			
		||||
      target_label: nodename
 | 
			
		||||
    - source_labels:
 | 
			
		||||
        - __journal_syslog_identifier
 | 
			
		||||
      target_label: syslog_identifier
 | 
			
		||||
    - source_labels:
 | 
			
		||||
        - __journal_container_name
 | 
			
		||||
      target_label: container_name
 | 
			
		||||
  pipeline_stages:
 | 
			
		||||
    - match:
 | 
			
		||||
        selector: '{container_name=~"homeassistant|hassio_supervisor"}'
 | 
			
		||||
        stages:
 | 
			
		||||
          - multiline:
 | 
			
		||||
              firstline: '^\x{001b}'
 | 
			
		||||
							
								
								
									
										15
									
								
								addon-promtail-main/rootfs/etc/services.d/promtail/finish
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								addon-promtail-main/rootfs/etc/services.d/promtail/finish
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
#!/usr/bin/env bashio
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
# Take down the S6 supervision tree when Promtail fails
 | 
			
		||||
# s6-overlay docs: https://github.com/just-containers/s6-overlay
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
 | 
			
		||||
declare APP_EXIT_CODE=${1}
 | 
			
		||||
 | 
			
		||||
if [[ "${APP_EXIT_CODE}" -ne 0 ]] && [[ "${APP_EXIT_CODE}" -ne 256 ]]; then
 | 
			
		||||
  bashio::log.warning "Halt add-on with exit code ${APP_EXIT_CODE}"
 | 
			
		||||
  echo "${APP_EXIT_CODE}" > /run/s6-linux-init-container-results/exitcode
 | 
			
		||||
  exec /run/s6/basedir/bin/halt
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
bashio::log.info "Service restart after closing"
 | 
			
		||||
							
								
								
									
										40
									
								
								addon-promtail-main/rootfs/etc/services.d/promtail/run
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								addon-promtail-main/rootfs/etc/services.d/promtail/run
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
#!/usr/bin/with-contenv bashio
 | 
			
		||||
# shellcheck shell=bash
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
# Home Assistant Add-on: Promtail
 | 
			
		||||
# Runs Promtail
 | 
			
		||||
# ==============================================================================
 | 
			
		||||
readonly PROMTAIL_CONFIG='/etc/promtail/config.yaml'
 | 
			
		||||
declare log_level
 | 
			
		||||
 | 
			
		||||
bashio::log.info 'Starting Promtail...'
 | 
			
		||||
 | 
			
		||||
journal_path='/var/log/journal'
 | 
			
		||||
if ! bashio::fs.directory_exists "${journal_path}" || [ -z "$(ls -A ${journal_path})" ]; then
 | 
			
		||||
    bashio::log.info "No journal at ${journal_path}, looking for journal in /run/log/journal instead"
 | 
			
		||||
    journal_path='/run/log/journal'
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
case "$(bashio::config 'log_level')" in \
 | 
			
		||||
    trace)      ;& \
 | 
			
		||||
    debug)      log_level='debug' ;; \
 | 
			
		||||
    notice)     ;& \
 | 
			
		||||
    warning)    log_level='warn' ;; \
 | 
			
		||||
    error)      ;& \
 | 
			
		||||
    fatal)      log_level='error' ;; \
 | 
			
		||||
    *)          log_level='info' ;; \
 | 
			
		||||
esac;
 | 
			
		||||
bashio::log.info "Promtail log level set to ${log_level}"
 | 
			
		||||
 | 
			
		||||
export "URL=$(bashio::config 'client.url')"
 | 
			
		||||
export "JOURNAL_PATH=${journal_path}"
 | 
			
		||||
export "LOG_LEVEL=${log_level}"
 | 
			
		||||
 | 
			
		||||
promtail_args=("-config.expand-env=true" "-config.file=${PROMTAIL_CONFIG}")
 | 
			
		||||
if [ "${log_level}" == "debug" ]; then
 | 
			
		||||
    bashio::log.debug "Logging full config on startup for debugging..."
 | 
			
		||||
    promtail_args+=("-print-config-stderr=true")
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
bashio::log.info "Handing over control to Promtail..."
 | 
			
		||||
/usr/bin/promtail "${promtail_args[@]}"
 | 
			
		||||
		Reference in New Issue
	
	Block a user