#!/bin/sh
#
####################################################################
# InfluxDB NEB module install 
####################################################################
# Copyright (c) 2026 Nagios Enterprises, LLC. All rights reserved.
####################################################################
#
NAGIOS_CFG="/usr/local/nagios/etc/nagios.cfg"


activate() {

    # Check if the config file exists
    if [ ! -f "$NAGIOS_CFG" ]; then
        printf "Error: %s not found\n" "$NAGIOS_CFG"
        exit 1
    fi

    # Check if the module is already configured (not commented out)
    if grep -q "^broker_module=.*influx_perf_api_v1\.o" "$NAGIOS_CFG"; then
        printf "The InfluxDB module is already configured in nagios.cfg\n"
    else
        # Append the broker module configuration
        printf "\nbroker_module=/usr/local/nagios/bin/influx_perf_api_v1.o http://localhost:8086/write?db=perfdata timeout=10 batch=10\n" >> "$NAGIOS_CFG"
        if [ $? -ne 0 ]; then
            printf "Error: Failed to add module to nagios.cfg\n"
            exit 1
        fi
        printf "\n\nThe InfluxDB module has been *added* to the nagios.cfg file.\n\nPlease restart nagios to activate the module:\n\n systemctl restart nagios\n\n"
    fi

    exit 0
}

deactivate() {

    # Check if the config file exists
    if [ ! -f "$NAGIOS_CFG" ]; then
        printf "Error: %s not found\n" "$NAGIOS_CFG"
        exit 1
    fi


    # Check if the module line exists and is not already commented
    if grep -q "^broker_module=/usr/local/nagios/bin/influx_perf_api_v1\.o" "$NAGIOS_CFG"; then
        # Comment out the broker module line
        sed -i 's|^broker_module=/usr/local/nagios/bin/influx_perf_api_v1\.o|#&|' "$NAGIOS_CFG"
        if [ $? -ne 0 ]; then
            printf "Error: Failed to comment out module in nagios.cfg\n"
            exit 1
        fi
        printf "The InfluxDB module has been removed from nagios.cfg\n"
    else
        printf "The InfluxDB module is not currently active in nagios.cfg\n"
    fi

    exit 0
}

backup() {

    # Create backup of nagios.cfg
    EPOCH=$(date +%s)
    cp "$NAGIOS_CFG" "/usr/local/nagiosxi/tmp/nagios.cfg.$EPOCH"
    if [ $? -ne 0 ]; then
        printf "Error: Failed to backup nagios.cfg\n"
        exit 1
    fi

}

# Check for deactivate argument
if [ "$1" = "--deactivate" ]; then
	backup
    	deactivate
	exit 0  
fi

# Detect OS type
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    echo "Error: Cannot detect OS type"
    exit 1
fi

case "$OS" in
    rhel|centos|ol)
        echo "Setting up InfluxDB repository for RHEL-based system..."
        cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive.key
EOF
        echo "Installing InfluxDB..."
        sudo yum install -y influxdb
        if [ $? -ne 0 ]; then
            echo "Error: InfluxDB installation failed"
            exit 1
        fi
        ;;

    ubuntu|debian)
        echo "Setting up InfluxDB repository for Debian-based system..."
        # Add InfluxData GPG key
        wget -q https://repos.influxdata.com/influxdata-archive.key
#        echo '943666881a1b8d9b849b74caebf02d3465d6beb716510d86a39f6c8e8dac7515 influxdata-archive.key' | sha256sum -c
        if [ $? -ne 0 ]; then
            echo "Error: GPG key verification failed"
            exit 1
        fi
        cat influxdata-archive.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive.gpg > /dev/null
        rm influxdata-archive.key

        # Add repository
        echo "deb https://repos.influxdata.com/debian stable main" | sudo tee /etc/apt/sources.list.d/influxdb.list

        # Update and install
        echo "Updating package lists..."
        sudo apt-get update
        if [ $? -ne 0 ]; then
            echo "Error: apt-get update failed"
            exit 1
        fi

        echo "Installing InfluxDB..."
        sudo apt-get install -y influxdb
        if [ $? -ne 0 ]; then
            echo "Error: InfluxDB installation failed"
            exit 1
        fi
        ;;

    *)
        echo "Error: Unsupported OS: $OS"
        echo "This script supports RHEL, CentOS, Oracle Linux, Ubuntu, and Debian"
        exit 1
        ;;
esac
#
#### Start influx and create the perfdata database. 

systemctl restart influxdb 

printf "create database perfdata\n" | influx

#
backup
activate
