# lib/node_exporter
# Functions to control the installation and configuration of node_exporter

# Save trace setting
_XTRACE_NODE_EXPORTER=$(set +o | grep xtrace)
set +o xtrace

NODE_EXPORTER_BINARY="/usr/local/bin/node_exporter"
NODE_EXPORTER_SYSTEMD_SERVICE="devstack@node-exporter.service"
NODE_EXPORTER_PORT=${NODE_EXPORTER_PORT:-9100}

function pre_install_node_exporter {
    # Install OS packages
    install_package wget tar
}

function install_node_exporter {
    # Download node_exporter
    wget https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz

    # Extract the tarball
    tar -xvf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz

    # Move binaries to /usr/local/bin
    sudo mv node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter /usr/local/bin/

    # Set ownership
    sudo chown $(whoami):$(whoami) ${NODE_EXPORTER_BINARY}
}

function init_node_exporter {

    node_exporter_cmd=${NODE_EXPORTER_BINARY}
    node_exporter_cmd+=" --collector.systemd"
    node_exporter_cmd+=" --collector.systemd.unit-include=(edpm_.*|ovs.*|openvswitch|virt.*|rsyslog)\.service"
    node_exporter_cmd+=" --web.disable-exporter-metrics"
    node_exporter_cmd+=" --no-collector.dmi --no-collector.entropy --no-collector.thermal_zone --no-collector.time"
    node_exporter_cmd+=" --no-collector.timex --no-collector.uname --no-collector.stat --no-collector.hwmon"
    node_exporter_cmd+=" --no-collector.os --no-collector.selinux --no-collector.textfile --no-collector.powersupplyclass"
    node_exporter_cmd+=" --no-collector.pressure --no-collector.rapl"
    if [[ $NODE_EXPORTER_COLLECTOR_EXCLUDE != "" ]]; then
        collector_list=$(echo $NODE_EXPORTER_COLLECTOR_EXCLUDE | tr "," "\n")
        for COLLECTOR in ${collector_list[@]}
        do
            node_exporter_cmd+=" --no-collector.${COLLECTOR}"
        done
    fi

    write_user_unit_file $NODE_EXPORTER_SYSTEMD_SERVICE "$node_exporter_cmd" "" "$STACK_USER"

    enable_service $NODE_EXPORTER_SYSTEMD_SERVICE
}

function start_node_exporter {
    start_service $NODE_EXPORTER_SYSTEMD_SERVICE
}
function stop_node_exporter {
    stop_service $NODE_EXPORTER_SYSTEMD_SERVICE
}

function cleanup_node_exporter {
    stop_node_exporter
    disable_service $NODE_EXPORTER_SYSTEMD_SERVICE

    # Remove systemd unit files
    local unitfile="$SYSTEMD_DIR/$NODE_EXPORTER_SYSTEMD_SERVICE"
    sudo rm -f $unitfile
    $SYSTEMCTL daemon-reload

    # Remove Node Exporter binaries
    sudo rm -rf $NODE_EXPORTER_BINARY
}

function wait_for_data {
    # Adding sleep time so that metrics is pushed by node exporter
    sleep 60
}
function check_data_node_exporter {
    if curl -s --head  --request  GET "http://$HOST_IP:$NODE_EXPORTER_PORT/metrics" | grep "200 OK" > /dev/null; then
        echo "#### Metrics data ####"
        curl "http://$HOST_IP:$NODE_EXPORTER_PORT/metrics"
    else
        die $LINENO "Couldn't get data from node_exporter"
    fi
}

# Restore xtrace
$_XTRACE_NODE_EXPORTER