diff --git a/salt/firewall/containers.map.jinja b/salt/firewall/containers.map.jinja index b39ba2b31..56b2ef127 100644 --- a/salt/firewall/containers.map.jinja +++ b/salt/firewall/containers.map.jinja @@ -1,4 +1,5 @@ {% from 'vars/globals.map.jinja' import GLOBALS %} +{% from 'telegraf/map.jinja' import INFLUX_RETIRED %} {% if GLOBALS.role == 'so-eval' %} {% set NODE_CONTAINERS = [ @@ -109,3 +110,7 @@ {% set NODE_CONTAINERS = [] %} {% endif %} + +{% if INFLUX_RETIRED and 'so-influxdb' in NODE_CONTAINERS %} +{% do NODE_CONTAINERS.remove('so-influxdb') %} +{% endif %} diff --git a/salt/firewall/map.jinja b/salt/firewall/map.jinja index 93af5cace..c0d394154 100644 --- a/salt/firewall/map.jinja +++ b/salt/firewall/map.jinja @@ -1,5 +1,6 @@ {% from 'vars/globals.map.jinja' import GLOBALS %} {% from 'docker/docker.map.jinja' import DOCKERMERGED %} +{% from 'telegraf/map.jinja' import INFLUX_RETIRED %} {% import_yaml 'firewall/defaults.yaml' as FIREWALL_DEFAULT %} {# add our ip to self #} @@ -60,3 +61,12 @@ {% endif %} {% set FIREWALL_MERGED = salt['pillar.get']('firewall', FIREWALL_DEFAULT.firewall, merge=True) %} + +{# filtered after the merge so portgroup lists saved in Configuration lose influxdb too #} +{% if INFLUX_RETIRED and (role.startswith('manager') or role in ['eval', 'standalone', 'import']) %} +{% for hostgroup in FIREWALL_MERGED.role[role].chain['DOCKER-USER'].hostgroups.values() %} +{% if 'influxdb' in hostgroup.get('portgroups', []) %} +{% do hostgroup.update({'portgroups': hostgroup.portgroups | reject('equalto', 'influxdb') | list}) %} +{% endif %} +{% endfor %} +{% endif %} diff --git a/salt/influxdb/init.sls b/salt/influxdb/init.sls index 33b4c3e37..48679a6db 100644 --- a/salt/influxdb/init.sls +++ b/salt/influxdb/init.sls @@ -4,9 +4,12 @@ # Elastic License 2.0. {% from 'influxdb/map.jinja' import INFLUXMERGED %} +{% from 'telegraf/map.jinja' import INFLUX_RETIRED %} include: -{% if INFLUXMERGED.enabled %} +{% if INFLUX_RETIRED %} + - influxdb.retired +{% elif INFLUXMERGED.enabled %} - influxdb.enabled {% else %} - influxdb.disabled diff --git a/salt/influxdb/retired.sls b/salt/influxdb/retired.sls new file mode 100644 index 000000000..6b6050029 --- /dev/null +++ b/salt/influxdb/retired.sls @@ -0,0 +1,31 @@ +# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one +# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at +# https://securityonion.net/license; you may not use this file except in compliance with the +# Elastic License 2.0. + +{% from 'allowed_states.map.jinja' import allowed_states %} +{% if sls.split('.')[0] in allowed_states %} + +include: + - influxdb.disabled + +influxdb_data_purged: + file.directory: + - name: /nsm/influxdb + - clean: True + - require: + - docker_container: so-influxdb + +influxdb_conf_purged: + file.absent: + - name: /opt/so/conf/influxdb + - require: + - docker_container: so-influxdb + +{% else %} + +{{sls}}_state_not_allowed: + test.fail_without_changes: + - name: {{sls}}_state_not_allowed + +{% endif %} diff --git a/salt/influxdb/soc_influxdb.yaml b/salt/influxdb/soc_influxdb.yaml index ff785fc57..a553c044d 100644 --- a/salt/influxdb/soc_influxdb.yaml +++ b/salt/influxdb/soc_influxdb.yaml @@ -1,6 +1,6 @@ influxdb: enabled: - description: Enables the grid metrics collection storage system. Security Onion grid health monitoring requires this process to remain enabled. WARNING - Disabling the process is unsupported, and will cause unexpected results. + description: Enables the grid metrics collection storage system. Security Onion grid health monitoring requires this process to remain enabled. WARNING - Disabling the process is unsupported, and will cause unexpected results. This setting has no effect when telegraf.output is POSTGRES. forcedType: bool helpLink: influxdb config: diff --git a/salt/manager/tools/sbin/so-user b/salt/manager/tools/sbin/so-user index ce5130257..e52e9523b 100755 --- a/salt/manager/tools/sbin/so-user +++ b/salt/manager/tools/sbin/so-user @@ -399,6 +399,16 @@ function syncElastic() { fi } +function syncInfluxdbUser() { + local op=$1 + local email=$2 + if ! docker ps -a --format '{{.Names}}' < /dev/null | grep -qx so-influxdb; then + echo "Skipped InfluxDB $op for $email: the so-influxdb container does not exist, so InfluxDB was not changed." >&2 + return 0 + fi + so-influxdb-manage "$op" "$email" +} + function syncAll() { ensureRoleFileExists @@ -594,9 +604,9 @@ case "${operation}" in createUser "$email" "${role:-$DEFAULT_ROLE}" "${firstName}" "${lastName}" "${note}" syncAll echo "Successfully added new user to SOC" - echo "$password" | so-influxdb-manage useradd "$email" + echo "$password" | syncInfluxdbUser useradd "$email" if [[ "$role" == "superuser" ]]; then - echo "$password" | so-influxdb-manage userpromote "$email" + echo "$password" | syncInfluxdbUser userpromote "$email" fi ;; @@ -616,7 +626,7 @@ case "${operation}" in syncElastic echo "Successfully added role to user" if [[ "$role" == "superuser" ]]; then - echo "$password" | so-influxdb-manage userpromote "$email" + echo "$password" | syncInfluxdbUser userpromote "$email" fi fi ;; @@ -632,7 +642,7 @@ case "${operation}" in syncElastic echo "Successfully removed role from user" if [[ "$role" == "superuser" ]]; then - echo "$password" | so-influxdb-manage userdemote "$email" + echo "$password" | syncInfluxdbUser userdemote "$email" fi ;; @@ -644,7 +654,7 @@ case "${operation}" in updateUserPassword "$email" syncAll echo "Successfully updated user password" - echo "$password" | so-influxdb-manage userpass "$email" + echo "$password" | syncInfluxdbUser userpass "$email" ;; "profile") @@ -664,7 +674,7 @@ case "${operation}" in updateStatus "$email" 'active' syncAll echo "Successfully enabled user" - so-influxdb-manage userenable "$email" + syncInfluxdbUser userenable "$email" ;; "disable") @@ -675,7 +685,7 @@ case "${operation}" in updateStatus "$email" 'locked' syncAll echo "Successfully disabled user" - so-influxdb-manage userdisable "$email" + syncInfluxdbUser userdisable "$email" ;; "delete") @@ -686,7 +696,7 @@ case "${operation}" in deleteUser "$email" syncAll echo "Successfully deleted user" - so-influxdb-manage userdel "$email" + syncInfluxdbUser userdel "$email" ;; "sync") diff --git a/salt/manager/tools/sbin/soup b/salt/manager/tools/sbin/soup index 14dc8655a..94d10dbf2 100755 --- a/salt/manager/tools/sbin/soup +++ b/salt/manager/tools/sbin/soup @@ -361,6 +361,187 @@ check_fleet_server() { fi } +TELEGRAF_PILLAR=/opt/so/saltstack/local/pillar/telegraf/soc_telegraf.sls +TELEGRAF_CREDS=/opt/so/saltstack/local/pillar/telegraf/creds.sls +INFLUXDB_DIRS=(/nsm/influxdb /opt/so/conf/influxdb) + +telegraf_creds_present() { + local mid=$1 + [[ -n $(so-yaml.py get -r "$TELEGRAF_CREDS" "telegraf.postgres_creds.${mid}.user" 2>/dev/null) ]] && + [[ -n $(so-yaml.py get -r "$TELEGRAF_CREDS" "telegraf.postgres_creds.${mid}.pass" 2>/dev/null) ]] +} + +# An unreadable docker or directory state counts as present so the POSTGRES path +# asks, or refuses on -y, rather than letting a highstate delete data unannounced. +influxdb_container_exists() { + local names + names=$(docker ps -a --format '{{.Names}}' 2>/dev/null) || return 0 + grep -qx so-influxdb <<< "$names" +} + +influxdb_present() { + local dir contents + influxdb_container_exists && return 0 + for dir in "${INFLUXDB_DIRS[@]}"; do + [[ -e $dir ]] || continue + contents=$(ls -A "$dir" 2>/dev/null) || return 0 + [[ -n $contents ]] && return 0 + done + return 1 +} + +set_influxdb_size() { + local dir + local -a existing=() + for dir in "${INFLUXDB_DIRS[@]}"; do + [[ -e $dir ]] && existing+=("$dir") + done + INFLUXDB_SIZE=0 + (( ${#existing[@]} == 0 )) && return 0 + if ! INFLUXDB_SIZE=$(set -o pipefail; du -sch "${existing[@]}" 2>/dev/null | tail -n 1 | cut -f1) || [[ -z $INFLUXDB_SIZE ]]; then + FINAL_MESSAGE_QUEUE+=("WARNING: Unable to measure the size of InfluxDB's data in ${INFLUXDB_DIRS[*]}.") + INFLUXDB_SIZE="an unknown amount" + fi +} + +influxdb_switch_prompt() { + local output answer mid mids + local -a missing=() + + # read the pillar files rather than salt, as influxdb_switch_apply must while salt-master is stopped + output=$(so-yaml.py get -r "$TELEGRAF_PILLAR" telegraf.output 2>/dev/null) || + output=$(so-yaml.py get -r "$DEFAULT_SALT_DIR/salt/telegraf/defaults.yaml" telegraf.output 2>/dev/null) || + output=INFLUXDB + output=${output^^} + + if [[ "$output" == "POSTGRES" ]]; then + if ! telegraf_creds_present "$MINIONID"; then + echo "" + echo "The metrics output is POSTGRES, but this manager has no Telegraf Postgres credentials." + echo "The upgrade cannot complete without them. soup has not changed anything." + echo "" + echo "Add them, then run soup again:" + echo " sudo so-telegraf-cred add $MINIONID" + echo "" + exit 0 + fi + influxdb_present || return 0 + set_influxdb_size + + if [[ -n $UNATTENDED ]]; then + if [[ -n $DELETES_ACCEPTED ]]; then + INFLUX_DELETE_ACCEPTED=1 + echo "InfluxDB ($INFLUXDB_SIZE) is still present on this POSTGRES grid; its deletion was accepted with --deletes-accepted." + return 0 + fi + echo "" + echo "The metrics output is POSTGRES, so this upgrade would remove InfluxDB and delete" + echo "all of its data ($INFLUXDB_SIZE). The deletion cannot be undone. soup has not changed anything." + echo "" + echo "To continue, choose one of the following:" + echo " - Accept the deletion: sudo soup -y --deletes-accepted" + echo " - Confirm the deletion at the prompt: sudo soup" + echo " - Save the data if needed and remove InfluxDB by hand, then run sudo soup -y again:" + # the installed release's scheduled highstate would otherwise start InfluxDB again before soup runs + echo " sudo so-yaml.py replace /opt/so/saltstack/local/pillar/minions/$MINIONID.sls influxdb.enabled false" + echo " sudo docker rm -f so-influxdb" + echo " sudo find ${INFLUXDB_DIRS[*]} -mindepth 1 -delete" + echo "" + exit 0 + fi + + echo "" + echo "The metrics output is POSTGRES, but InfluxDB is still on this manager with $INFLUXDB_SIZE" + echo "of old data. This upgrade removes InfluxDB and deletes all of that data, and the" + echo "deletion cannot be undone." + echo "Exit now if you want to save or export the data first, then run soup again." + echo "" + read -rp "Press C to continue with the upgrade or E to exit: " answer + if [[ "$answer" != [cC] ]]; then + echo "Exiting soup" + exit 0 + fi + return 0 + fi + + # releases before 3.1.0 have no Telegraf credentials; influxdb_switch_apply creates them on a switch + [[ $INSTALLEDVERSION =~ ^(2\.|3\.0\.) ]] && INFLUX_CREATE_CREDS=1 + + if ! mids=$(salt-key --out=json --list=accepted 2>/dev/null | jq -r '.minions[]?' 2>/dev/null) || [[ -z $mids ]]; then + missing=("(unable to list accepted minions)") + else + INFLUX_SWITCH_MINIONS=$mids + if [[ -z $INFLUX_CREATE_CREDS ]]; then + for mid in $mids; do + telegraf_creds_present "$mid" || missing+=("$mid") + done + fi + fi + if (( ${#missing[@]} > 0 )); then + FINAL_MESSAGE_QUEUE+=("NOTE: soup did not offer to move metrics from InfluxDB to Postgres because these nodes have no Telegraf Postgres credentials: ${missing[*]}") + FINAL_MESSAGE_QUEUE+=(" - Add them with sudo so-telegraf-cred add ; the next soup will offer the switch.") + return 0 + fi + + if [[ -n $UNATTENDED ]]; then + FINAL_MESSAGE_QUEUE+=("NOTE: InfluxDB will be removed in a future Security Onion release, and this grid still stores metrics in it (telegraf.output is $output).") + FINAL_MESSAGE_QUEUE+=(" - Setting telegraf.output to POSTGRES in Configuration switches the grid to Postgres and deletes InfluxDB and all its data.") + return 0 + fi + + echo "" + echo "InfluxDB will be removed in a future Security Onion release. This grid stores its" + echo "metrics in InfluxDB (telegraf.output is $output)." + echo "" + echo "Every grid must move its metrics to Postgres before that release. Existing InfluxDB" + echo "metrics are not migrated." + echo "" + echo "Switching now points Telegraf at Postgres and removes InfluxDB, deleting all of its" + echo "data, during this upgrade. This cannot be undone, and switching back afterwards is" + echo "not supported. InfluxDB's alarm checks are deleted with it; the Postgres recommended" + echo "alarms can be added from the Grid page." + echo "" + read -rp "Switch metrics to Postgres now? [y/N]: " answer + if [[ "$answer" == [yY] ]]; then + INFLUX_SWITCH_ACCEPTED=1 + else + echo "Keeping telegraf.output set to $output." + fi +} + +influxdb_switch_apply() { + local mid + local -a failed=() + [[ -n $INFLUX_SWITCH_ACCEPTED ]] || return 0 + if [[ -n $INFLUX_CREATE_CREDS ]]; then + echo "Creating Telegraf Postgres credentials for accepted minions." + for mid in $INFLUX_SWITCH_MINIONS; do + bash "$UPDATE_DIR/salt/manager/tools/sbin/so-telegraf-cred" add "$mid" || failed+=("$mid") + done + if (( ${#failed[@]} > 0 )); then + FINAL_MESSAGE_QUEUE+=("WARNING: Unable to create Telegraf Postgres credentials for these nodes, so the grid stays on its current metrics output: ${failed[*]}") + FINAL_MESSAGE_QUEUE+=(" - Once their credentials exist, set telegraf.output to POSTGRES in Configuration to switch.") + return 0 + fi + fi + echo "Setting telegraf.output to POSTGRES in $TELEGRAF_PILLAR." + if ! { mkdir -p "$(dirname "$TELEGRAF_PILLAR")" && touch "$TELEGRAF_PILLAR" && + so-yaml.py replace "$TELEGRAF_PILLAR" telegraf.output POSTGRES; }; then + FINAL_MESSAGE_QUEUE+=("WARNING: Unable to set telegraf.output to POSTGRES in $TELEGRAF_PILLAR, so metrics stay in InfluxDB. Set it in Configuration to switch.") + return 0 + fi + chown socore:socore "$TELEGRAF_PILLAR" 2>/dev/null || true +} + +influxdb_retire_check() { + [[ "$(so_telegraf_output 2>/dev/null)" == "POSTGRES" ]] || return 0 + if influxdb_container_exists; then + FINAL_MESSAGE_QUEUE+=("NOTE: InfluxDB is still on this manager. The next highstate tries again to remove it and delete all its data.") + elif [[ -n $INFLUX_DELETE_ACCEPTED ]]; then + FINAL_MESSAGE_QUEUE+=("NOTE: This upgrade removed InfluxDB and deleted all its data, as accepted with --deletes-accepted.") + fi +} + check_saltmaster_status() { set +e echo "Waiting on the Salt Master service to be ready." @@ -1249,6 +1430,18 @@ remove_auto_apply_batch_wait() { done } +check_soc_postgres_upgrade() { + local image + [[ "$(so_telegraf_output 2>/dev/null)" != "INFLUXDB" ]] || return 0 + telegraf_creds_present "$MINIONID" || return 0 + image=$(docker inspect --format '{{.Config.Image}}' so-soc 2>/dev/null) || image= + if [[ "$image" != *":$NEWVERSION" ]]; then + echo "" + echo "WARNING: SOC was not upgraded to $NEWVERSION because the Telegraf Postgres role setup failed. Fix Postgres, then run a highstate:" + echo " - sudo salt-call state.highstate" + fi +} + post_to_3.5.0() { # Generate 9.4.10 elastic agent installers echo "Regenerating Elastic Agent Installers" @@ -2164,6 +2357,10 @@ main() { check_cluster_health check_fleet_server + if [[ "$is_hotfix" != "true" ]]; then + influxdb_switch_prompt + fi + echo "Checking for Salt Master and Minion updates." upgrade_check_salt set -e @@ -2253,6 +2450,7 @@ main() { echo "Running so-config-backup script." /sbin/so-config-backup fi + influxdb_switch_apply echo "" echo "Copying new Security Onion code from $UPDATE_DIR to $DEFAULT_SALT_DIR." @@ -2330,7 +2528,7 @@ main() { fi set -e - highstate + highstate || { rc=$?; check_soc_postgres_upgrade; exit $rc; } check_saltmaster_status postupgrade_changes [[ $is_airgap -eq 0 ]] && unmount_update @@ -2404,6 +2602,8 @@ For more information, please see $DOC_BASE_URL/soup#distributed-deployments. EOF fi + + influxdb_retire_check fi if [ "$NOTIFYCUSTOMELASTICCONFIG" = true ] ; then @@ -2431,6 +2631,16 @@ fi echo "### soup has been served at $(date) ###" } +soup_args=() +for arg in "$@"; do + if [[ "$arg" == "--deletes-accepted" ]]; then + DELETES_ACCEPTED=true + else + soup_args+=("$arg") + fi +done +set -- "${soup_args[@]}" + while getopts ":b:f:y" opt; do case ${opt} in b ) @@ -2454,7 +2664,7 @@ while getopts ":b:f:y" opt; do ISOLOC="$OPTARG" ;; \? ) - echo "Usage: soup [-b] [-y] [-f ]" + echo "Usage: soup [-b] [-y] [--deletes-accepted] [-f ]" exit 1 ;; : ) diff --git a/salt/nginx/etc/nginx.conf b/salt/nginx/etc/nginx.conf index 0da187ff7..e90c7fd9e 100644 --- a/salt/nginx/etc/nginx.conf +++ b/salt/nginx/etc/nginx.conf @@ -1,9 +1,8 @@ {%- from 'vars/globals.map.jinja' import GLOBALS %} {%- from 'docker/docker.map.jinja' import DOCKERMERGED %} {%- from 'nginx/map.jinja' import NGINXMERGED %} +{%- from 'telegraf/map.jinja' import INFLUX_RETIRED %} {%- set role = grains.id.split('_') | last %} -{%- set influxpass = salt['pillar.get']('secrets:influx_pass') %} -{%- set influxauth = ('so:' + influxpass) | base64_encode %} {%- set all_names = [GLOBALS.hostname, GLOBALS.url_base] + NGINXMERGED.ssl.alt_names %} {%- set full_server_name = all_names | unique | join(' ') %} @@ -310,6 +309,7 @@ http { autoindex_localtime on; } + {%- if not INFLUX_RETIRED %} location /influxdb/ { auth_request /auth/sessions/whoami; rewrite /influxdb/api/(.*) /api/$1 break; @@ -323,6 +323,7 @@ http { proxy_set_header X-Forwarded-Proto $scheme; proxy_cookie_path /api/ /influxdb/api/; } + {%- endif %} location /app/dashboards/ { auth_request /auth/sessions/whoami; diff --git a/salt/postgres/telegraf_users.sls b/salt/postgres/telegraf_users.sls index 4c63d40b0..6ad36e72c 100644 --- a/salt/postgres/telegraf_users.sls +++ b/salt/postgres/telegraf_users.sls @@ -6,16 +6,12 @@ {% from 'allowed_states.map.jinja' import allowed_states %} {% if sls.split('.')[0] in allowed_states %} {% from 'vars/globals.map.jinja' import GLOBALS %} -{% from 'telegraf/map.jinja' import TELEGRAFMERGED %} {# postgres.enabled declares the so-postgres container and postgres_wait_ready that the requires below reference. Salt de-duplicates the circular include. #} include: - postgres.enabled -{% set TG_OUT = TELEGRAFMERGED.output | upper %} -{% if TG_OUT in ['POSTGRES', 'BOTH'] %} - # Ensure the shared Telegraf database exists. init-db.sh only runs on a # fresh data dir, so hosts upgraded onto an existing /nsm/postgres volume # would otherwise never get so_telegraf. @@ -70,8 +66,6 @@ postgres_telegraf_retention_reconcile: - cmd: postgres_telegraf_group_role - file: postgres_sbin -{% endif %} - {% else %} {{sls}}_state_not_allowed: diff --git a/salt/reactor/pillar_push_map.yaml b/salt/reactor/pillar_push_map.yaml index 038b9446a..282207cfb 100644 --- a/salt/reactor/pillar_push_map.yaml +++ b/salt/reactor/pillar_push_map.yaml @@ -232,10 +232,20 @@ suricata: - state: suricata tgt: 'G@role:so-eval or G@role:so-heavynode or G@role:so-import or G@role:so-sensor or G@role:so-standalone' -# telegraf: universal. +# telegraf: universal, then the manager states that follow the output setting. +# They run after telegraf so InfluxDB is retired and SOC switches metrics +# modules only once the nodes already point at the new output. telegraf: - state: telegraf tgt: '*' + - state: influxdb + tgt: 'G@role:so-eval or G@role:so-import or G@role:so-manager or G@role:so-managerhype or G@role:so-managersearch or G@role:so-standalone' + - state: nginx + tgt: 'G@role:so-eval or G@role:so-import or G@role:so-manager or G@role:so-managerhype or G@role:so-managersearch or G@role:so-standalone' + - state: firewall + tgt: 'G@role:so-eval or G@role:so-import or G@role:so-manager or G@role:so-managerhype or G@role:so-managersearch or G@role:so-standalone' + - state: soc + tgt: 'G@role:so-eval or G@role:so-import or G@role:so-manager or G@role:so-managerhype or G@role:so-managersearch or G@role:so-standalone' # versionlock: universal (top-of-file '*' branch in salt/top.sls). versionlock: diff --git a/salt/soc/config.sls b/salt/soc/config.sls index 53ee7f40a..86da8b712 100644 --- a/salt/soc/config.sls +++ b/salt/soc/config.sls @@ -5,9 +5,14 @@ {% from 'allowed_states.map.jinja' import allowed_states %} {% if sls.split('.')[0] in allowed_states %} +{% from 'soc/merged.map.jinja' import SOCMERGED %} +{% set use_pg_metrics = 'postgresmetrics' in SOCMERGED.config.server.modules %} include: - manager.sync_es_users +{% if use_pg_metrics %} + - postgres.telegraf_users +{% endif %} sigmarepodir: file.directory: @@ -77,6 +82,10 @@ socconfig: - mode: 600 - template: jinja - show_changes: False +{% if use_pg_metrics %} + - require: + - sls: postgres.telegraf_users +{% endif %} socmotd: file.managed: diff --git a/salt/soc/merged.map.jinja b/salt/soc/merged.map.jinja index f5d3232a9..a3a63d57b 100644 --- a/salt/soc/merged.map.jinja +++ b/salt/soc/merged.map.jinja @@ -7,7 +7,7 @@ {% from 'soc/defaults.map.jinja' import SOCDEFAULTS with context %} {% from 'elasticsearch/config.map.jinja' import ELASTICSEARCH_NODES %} {% from 'manager/map.jinja' import MANAGERMERGED %} -{% from 'telegraf/map.jinja' import TELEGRAFMERGED %} +{% from 'telegraf/map.jinja' import PG_METRICS_READY %} {% from 'elastalert/map.jinja' import ELASTALERTMERGED %} {%- set PG_ENTRY = salt['pillar.get']('telegraf:postgres_creds:' ~ grains.id, {}) %} {%- set PG_USER = PG_ENTRY.get('user', '') %} @@ -129,7 +129,7 @@ {% endif %} {# Define the postgresmetrics module if telegraf is setup to only use Postgres #} -{% if TELEGRAFMERGED.output != 'INFLUXDB' and PG_USER and PG_PASS %} +{% if PG_METRICS_READY %} {% do SOCMERGED.config.server.modules.update({ 'postgresmetrics': { 'database': 'so_telegraf', diff --git a/salt/telegraf/config.sls b/salt/telegraf/config.sls index 64ca7070e..7cb527a0c 100644 --- a/salt/telegraf/config.sls +++ b/salt/telegraf/config.sls @@ -158,6 +158,21 @@ tgraf_sbin_jinja: - defaults: CONTAINER_STATS: {{ TELEGRAFMERGED.container_stats }} +{% set tg_output = TELEGRAFMERGED.output | upper %} +{% set pg_creds = salt['pillar.get']('telegraf:postgres_creds:' ~ GLOBALS.minion_id, {}) %} +{% set pg_creds_missing = tg_output in ['POSTGRES', 'BOTH'] and not (pg_creds.get('user') and pg_creds.get('pass')) %} +{% if pg_creds_missing %} +{% set pg_creds_text = 'Telegraf Postgres credentials for ' ~ GLOBALS.minion_id ~ ' are missing from the pillar. On the manager, run so-telegraf-cred add ' ~ GLOBALS.minion_id ~ ', then run a highstate on this node.' %} +telegraf_postgres_creds_missing: +{% if tg_output == 'POSTGRES' %} + test.fail_without_changes: + - comment: {{ pg_creds_text | tojson }} +{% else %} + test.show_notification: + - text: {{ pg_creds_text | tojson }} +{% endif %} +{% endif %} + tgrafconf: file.managed: - name: /opt/so/conf/telegraf/etc/telegraf.conf @@ -171,6 +186,10 @@ tgrafconf: GLOBALS: {{ GLOBALS }} TELEGRAFMERGED: {{ TELEGRAFMERGED }} LOGSTASH_MERGED: {{ LOGSTASH_MERGED }} +{% if pg_creds_missing %} + - require: + - test: telegraf_postgres_creds_missing +{% endif %} # this file will be read by telegraf to send node details (management interface, monitor interface, etc) # into influx diff --git a/salt/telegraf/map.jinja b/salt/telegraf/map.jinja index 511cd0b89..2659063ea 100644 --- a/salt/telegraf/map.jinja +++ b/salt/telegraf/map.jinja @@ -6,6 +6,13 @@ {% from 'vars/globals.map.jinja' import GLOBALS %} {% import_yaml 'telegraf/defaults.yaml' as TELEGRAFDEFAULTS %} {% set TELEGRAFMERGED = salt['pillar.get']('telegraf', TELEGRAFDEFAULTS.telegraf, merge=True) %} +{% set TG_OUTPUT = TELEGRAFMERGED.output | upper %} +{% set TG_PG_ENTRY = salt['pillar.get']('telegraf:postgres_creds:' ~ GLOBALS.minion_id, {}) %} +{% set PG_METRICS_READY = false %} +{% if TG_OUTPUT != 'INFLUXDB' and TG_PG_ENTRY.get('user') and TG_PG_ENTRY.get('pass') %} +{% set PG_METRICS_READY = true %} +{% endif %} +{% set INFLUX_RETIRED = TG_OUTPUT == 'POSTGRES' and PG_METRICS_READY %} {% if GLOBALS.role in ['so-eval', 'so-standalone', 'so-sensor', 'so-heavynode'] %} {% from 'zeek/config.map.jinja' import ZEEKMERGED %} @@ -22,3 +29,10 @@ {% do TELEGRAFMERGED.scripts[GLOBALS.role.split('-')[1]].remove('redis.sh') %} {% endif %} {% endif %} + +{% if INFLUX_RETIRED %} +{% set role_scripts = TELEGRAFMERGED.scripts.get(GLOBALS.role.split('-')[1], []) %} +{% if 'influxdbsize.sh' in role_scripts %} +{% do role_scripts.remove('influxdbsize.sh') %} +{% endif %} +{% endif %} diff --git a/salt/telegraf/soc_telegraf.yaml b/salt/telegraf/soc_telegraf.yaml index d92a89f2f..b54f0a439 100644 --- a/salt/telegraf/soc_telegraf.yaml +++ b/salt/telegraf/soc_telegraf.yaml @@ -5,14 +5,14 @@ telegraf: advanced: True helpLink: influxdb output: - description: Selects the backend(s) Telegraf writes metrics to. INFLUXDB keeps the current behavior; POSTGRES writes to the grid's Postgres instance; BOTH dual-writes for migration validation. When set to BOTH, the grid screen's metrics are pulled from Postgres, and the InfluxDB tool link remains visible. When set to POSTGRES, the InfluxDB tool link is removed. + description: Selects the backend(s) Telegraf writes metrics to. INFLUXDB keeps the current behavior; POSTGRES writes to the grid's Postgres instance; BOTH dual-writes for migration validation. When set to BOTH, the grid screen's metrics are pulled from Postgres, and the InfluxDB tool link remains visible. When set to POSTGRES, the InfluxDB tool link is removed. WARNING - Choosing POSTGRES removes InfluxDB from the manager and permanently deletes all of its data; this cannot be undone. Switching back to INFLUXDB or BOTH afterwards is not supported, because InfluxDB would start again with no data. BOTH keeps InfluxDB running. InfluxDB will be removed in a future release, so grids should move to POSTGRES. options: - INFLUXDB - POSTGRES - BOTH global: True advanced: True - helpLink: influxdb + helpLink: postgresql config: interval: description: Data collection interval.