#2624 Add Telegraf to SSH menus (#4788)

* #2624 add telegraf to SSH SETTINGS menu
* #2624 add telegraf to main SSH menu
This commit is contained in:
/rootzoll
2024-10-06 21:35:21 +02:00
committed by GitHub
parent 7038bf6ea2
commit 57ddeab520
6 changed files with 248 additions and 136 deletions

View File

@@ -1,6 +1,7 @@
## What's new in Version 1.11.3 of RaspiBlitz? ## What's new in Version 1.11.3 of RaspiBlitz?
- New: Tailscale (SSH menu > SETTINGS) [details](https://en.wikipedia.org/wiki/Tailscale) - New: Tailscale (SSH menu > SETTINGS) [details](https://en.wikipedia.org/wiki/Tailscale)
- New: Telegraf Metrics for InfluxDB/Grafana (SSH menu > SETTINGS) [details](https://github.com/raspiblitz/raspiblitz/tree/dev/home.admin/assets/telegraf/README.md)
- New: Connect Zeus to CLN via CLNrest (using the clnrest plugin and runes) - New: Connect Zeus to CLN via CLNrest (using the clnrest plugin and runes)
- Update: Core Lightning v24.08.1 [details](https://github.com/ElementsProject/lightning/releases/tag/v24.08.1) - Update: Core Lightning v24.08.1 [details](https://github.com/ElementsProject/lightning/releases/tag/v24.08.1)
- Update: Thunderhub v0.13.31 [details](https://github.com/apotdevin/thunderhub/releases/tag/v0.13.31) - Update: Thunderhub v0.13.31 [details](https://github.com/apotdevin/thunderhub/releases/tag/v0.13.31)
@@ -10,7 +11,6 @@
- Update: Balance of Satoshis 19.3.4 (bos) [details](https://github.com/alexbosworth/balanceofsatoshis/blob/v19.3.4/CHANGELOG.md#1934) - Update: Balance of Satoshis 19.3.4 (bos) [details](https://github.com/alexbosworth/balanceofsatoshis/blob/v19.3.4/CHANGELOG.md#1934)
- Update: Labelbase 2.2.2 [details](https://x.com/labelbase_space) - Update: Labelbase 2.2.2 [details](https://x.com/labelbase_space)
- Update: LNbits 0.12.11 [details](https://github.com/lnbits/lnbits/releases/tag/0.12.11) - Update: LNbits 0.12.11 [details](https://github.com/lnbits/lnbits/releases/tag/0.12.11)
- Update: Telegraf Metrics for InfluxDB/Grafana [details](https://github.com/raspiblitz/raspiblitz/tree/dev/home.admin/assets/telegraf/README.md)
- Update: RTL v0.15.2 [details](https://github.com/Ride-The-Lightning/RTL/releases/tag/v0.15.2) - Update: RTL v0.15.2 [details](https://github.com/Ride-The-Lightning/RTL/releases/tag/v0.15.2)
- Update: Jam (JoinMarket Web UI) v0.3.0 [details](https://github.com/joinmarket-webui/jam/releases/tag/v0.3.0) - Update: Jam (JoinMarket Web UI) v0.3.0 [details](https://github.com/joinmarket-webui/jam/releases/tag/v0.3.0)
- Update: CLBOSS 0.13.3 [details](https://github.com/ZmnSCPxj/clboss/releases/tag/v0.13.3) - Update: CLBOSS 0.13.3 [details](https://github.com/ZmnSCPxj/clboss/releases/tag/v0.13.3)

View File

@@ -174,6 +174,9 @@ fi
if [ "${tailscale}" == "on" ]; then if [ "${tailscale}" == "on" ]; then
OPTIONS+=(TAILSCALE "Tailscale VPN") OPTIONS+=(TAILSCALE "Tailscale VPN")
fi fi
if [ "${telegraf}" == "on" ]; then
OPTIONS+=(TELEGRAF "Telegraf InfluxDB/Grafana Metrics")
fi
# dont offer to switch to "testnet view for now" - so no wswitch back to mainnet needed # dont offer to switch to "testnet view for now" - so no wswitch back to mainnet needed
@@ -341,6 +344,9 @@ case $CHOICE in
TAILSCALE) TAILSCALE)
sudo /home/admin/config.scripts/internet.tailscale.sh menu sudo /home/admin/config.scripts/internet.tailscale.sh menu
;; ;;
TELEGRAF)
/home/admin/config.scripts/bonus.telegraf.sh menu
;;
FINTS) FINTS)
sudo /home/admin/config.scripts/bonus.fints.sh menu sudo /home/admin/config.scripts/bonus.fints.sh menu
;; ;;

View File

@@ -17,6 +17,7 @@ if [ ${#clAutoUnlock} -eq 0 ]; then clAutoUnlock="off"; fi
if [ ${#clWatchtowerClient} -eq 0 ]; then clWatchtowerClient="off"; fi if [ ${#clWatchtowerClient} -eq 0 ]; then clWatchtowerClient="off"; fi
if [ ${#blitzapi} -eq 0 ]; then blitzapi="off"; fi if [ ${#blitzapi} -eq 0 ]; then blitzapi="off"; fi
if [ ${#tailscale} -eq 0 ]; then tailscale="off"; fi if [ ${#tailscale} -eq 0 ]; then tailscale="off"; fi
if [ ${#telegraf} -eq 0 ]; then telegraf="off"; fi
# detect if LND auto-unlock is active # detect if LND auto-unlock is active
source <(/home/admin/config.scripts/lnd.autounlock.sh status) source <(/home/admin/config.scripts/lnd.autounlock.sh status)
@@ -114,6 +115,8 @@ OPTIONS+=(t 'Run behind Tor' ${runBehindTor})
OPTIONS+=(z 'ZeroTier' ${zerotierSwitch}) OPTIONS+=(z 'ZeroTier' ${zerotierSwitch})
OPTIONS+=(l 'Tailscale VPN' ${tailscale}) OPTIONS+=(l 'Tailscale VPN' ${tailscale})
OPTIONS+=(g 'Telegraf InfluxDB/Grafana Metrics' ${telegraf})
if [ ${#runBehindTor} -eq 0 ] || [ "${runBehindTor}" = "off" ]; then if [ ${#runBehindTor} -eq 0 ] || [ "${runBehindTor}" = "off" ]; then
OPTIONS+=(y ${dynDomainMenu} ${domainValue}) OPTIONS+=(y ${dynDomainMenu} ${domainValue})
fi fi
@@ -337,6 +340,21 @@ else
echo "tailscale setting unchanged." echo "tailscale setting unchanged."
fi fi
# Telegraf process choice
choice="off"; check=$(echo "${CHOICES}" | grep -c "g")
if [ ${check} -eq 1 ]; then choice="on"; fi
if [ "${telegraf}" != "${choice}" ]; then
echo "telegraf setting changed .."
anychange=1
error=""
sudo -u admin /home/admin/config.scripts/bonus.telegraf.sh ${choice}
if [ "${choice}" = "on" ]; then
sudo -u admin /home/admin/config.scripts/bonus.telegraf.sh menu
fi
else
echo "telegraf setting unchanged."
fi
# LND choice # LND choice
choice="off"; check=$(echo "${CHOICES}" | grep -c "m") choice="off"; check=$(echo "${CHOICES}" | grep -c "m")
if [ ${check} -eq 1 ]; then choice="on"; fi if [ ${check} -eq 1 ]; then choice="on"; fi

View File

@@ -735,7 +735,7 @@ else
fi fi
# Telegraf # Telegraf
if [ "${telegrafMonitoring}" = "on" ]; then if [ "${telegraf}" = "on" ]; then
echo "Provisioning Telegraf - run config script" >> ${logFile} echo "Provisioning Telegraf - run config script" >> ${logFile}
/home/admin/_cache.sh set message "Setup Telegraf" /home/admin/_cache.sh set message "Setup Telegraf"
sudo -u admin /home/admin/config.scripts/bonus.telegraf.sh on >> ${logFile} 2>&1 sudo -u admin /home/admin/config.scripts/bonus.telegraf.sh on >> ${logFile} 2>&1

View File

@@ -5,7 +5,7 @@
# all telegraf switches and configuration variables # all telegraf switches and configuration variables
telegrafMonitoring=on telegraf=on
telegrafInfluxUrl='http://192.168.2.46:8086' telegrafInfluxUrl='http://192.168.2.46:8086'
telegrafInfluxDatabase='telegraf' telegrafInfluxDatabase='telegraf'
telegrafInfluxUsername='telegraf' telegrafInfluxUsername='telegraf'

View File

@@ -10,7 +10,8 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "# config script to switch the telegraf metric collection" echo "# config script to switch the telegraf metric collection"
echo "# detailed setup info: github.com/raspiblitz/raspiblitz/tree/dev/home.admin/assets/telegraf" echo "# detailed setup info: github.com/raspiblitz/raspiblitz/tree/dev/home.admin/assets/telegraf"
echo "# bonus.telegraf.sh status ---> get status of telegraf service" echo "# bonus.telegraf.sh status ---> get status of telegraf service"
echo "# bonus.telegraf.sh on ---> install & config" echo "# bonus.telegraf.sh on ---> install"
echo "# bonus.telegraf.sh menu ---> info & config"
echo "# bonus.telegraf.sh off ---> uninstall & reset config" echo "# bonus.telegraf.sh off ---> uninstall & reset config"
exit 1 exit 1
fi fi
@@ -21,20 +22,48 @@ source /mnt/hdd/raspiblitz.conf
# this variables is used repeatedly in this script # this variables is used repeatedly in this script
resources_dir=/home/admin/assets/telegraf/etc-telegraf resources_dir=/home/admin/assets/telegraf/etc-telegraf
# source and target dir for copy operation
telegraf_source_dir=${resources_dir}
telegraf_target_dir=/etc/telegraf
# full path to telegraf config file for sed-replace operation
telegraf_conf_file=${telegraf_target_dir}/telegraf.conf
############################### ###############################
# give status # give status
if [ "$1" = "status" ]; then if [ "$1" = "status" ]; then
echo "##### STATUS TELEGRAF SERVICE" echo "##### STATUS TELEGRAF SERVICE"
# check if "telegrafMonitoring" is enabled ("1"|"on") in raspiblitz.conf # check if "telegraf" is enabled ("1"|"on") in raspiblitz.conf
if [ "${telegrafMonitoring}" = "1" ] || [ "${telegrafMonitoring}" = "on" ]; then if [ "${telegraf}" = "1" ] || [ "${telegraf}" = "on" ]; then
echo "configured=1" echo "configured=1"
else else
echo "configured=0" echo "configured=0"
fi fi
serviceInstalled=$(sudo systemctl status telegraf --no-page 2>/dev/null | grep -c "telegraf.service - The plugin-driven") # check if config data in raspiblitz.conf is available
configMissing=0
if [ ${#telegrafInfluxUrl} -eq 0 ]; then
echo "# Missing telegrafInfluxUrl in raspiblitz.conf"
configMissing=1
fi
if [ ${#telegrafInfluxDatabase} -eq 0 ]; then
echo "# Missing telegrafInfluxDatabase in raspiblitz.conf"
configMissing=1
fi
if [ ${#telegrafInfluxUsername} -eq 0 ]; then
echo "# Missing telegrafInfluxUsername in raspiblitz.conf"
configMissing=1
fi
if [ ${#telegrafInfluxPassword} -eq 0 ]; then
echo "# Missing telegrafInfluxPassword in raspiblitz.conf"
configMissing=1
fi
echo "configMissing=${configMissing}"
serviceInstalled=$(sudo systemctl status telegraf --no-page 2>/dev/null | grep -q 'Loaded: loaded' && echo 1 || echo 0)
echo "serviceInstalled=${serviceInstalled}" echo "serviceInstalled=${serviceInstalled}"
if [ ${serviceInstalled} -eq 0 ]; then if [ ${serviceInstalled} -eq 0 ]; then
echo "infoMessage='Telegraf service not installed'" echo "infoMessage='Telegraf service not installed'"
@@ -48,6 +77,13 @@ if [ "$1" = "status" ]; then
echo "infoMessage='Not running - check: sudo journalctl -u telegraf'" echo "infoMessage='Not running - check: sudo journalctl -u telegraf'"
fi fi
errorReport=""
countReportError=$(sudo journalctl -u telegraf.service -n 5 | grep -c "Failed to write metric")
if [ ${countReportError} -gt 0 ]; then
errorReport='failed to write metric to server'
fi
echo "errorReport='${errorReport}'"
exit 0 exit 0
fi fi
@@ -70,6 +106,46 @@ function validate_url() {
fi fi
} }
function config_telegraf() {
echo "# *** telegraf installation: replace influxDB url and creds"
sudo systemctl stop telegraf.service 2>/dev/null
# make sure that raspiblitz.conf has the telegraf-variables properly set
# telegrafInfluxUrl
# telegrafInfluxDatabase
# telegrafInfluxUsername
# telegrafInfluxPassword
source /mnt/hdd/raspiblitz.conf
echo "# *** telegraf installation: telegrafInfluxUrl = '${telegrafInfluxUrl}'"
# due to the occurrence of '/' in the ${telegrafInfluxUrl} we need to switch to '#' as the sed-separator
sudo sed -i "s#^urls = .*#urls = \[\"${telegrafInfluxUrl}\"\]#g" ${telegraf_conf_file}
#
# the other replacements work with the std separator '/'
#
# CAUTION: make sure that *none* of the following variables (especially "password") contains a '/'
# this would break the sed-replacement
echo "*** telegraf installation: telegrafInfluxDatabase = '${telegrafInfluxDatabase}'"
sudo sed -i "s/^database = .*/database = \"${telegrafInfluxDatabase}\"/g" ${telegraf_conf_file}
echo "*** telegraf installation: telegrafInfluxUsername = '${telegrafInfluxUsername}'"
sudo sed -i "s/^username = .*/username = \"${telegrafInfluxUsername}\"/g" ${telegraf_conf_file}
echo "*** telegraf installation: telegrafInfluxPassword = '${telegrafInfluxPassword}'"
sudo sed -i "s/^password = .*/password = \"${telegrafInfluxPassword}\"/g" ${telegraf_conf_file}
echo "*** telegraf installation: restart telegraf service with updated config files"
# restart telegraf service
sudo systemctl start telegraf.service
# ...and push some status into the logfile
sleep 2
sudo systemctl status telegraf.service --no-page 2>/dev/null
}
############################### ###############################
# switch on # switch on
if [ "$1" = "1" ] || [ "$1" = "on" ]; then if [ "$1" = "1" ] || [ "$1" = "on" ]; then
@@ -77,30 +153,108 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "*** INSTALL TELEGRAF ***" echo "*** INSTALL TELEGRAF ***"
# check installed by looking for service # check installed by looking for service
serviceInstalled=$(sudo systemctl status telegraf --no-page 2>/dev/null | grep -c "telegraf.service - The plugin-driven") source <(/home/admin/config.scripts/bonus.telegraf.sh status)
if [ ${serviceInstalled} -eq 1 ]; then if [ ${serviceInstalled} -eq 1 ]; then
echo "# Telegraf service is installed." echo "# Telegraf service is installed."
echo "# If you want to reset config and reinstall, please switch off first." echo "# If you want to reset config and reinstall, please switch off first."
exit 0 exit 0
fi fi
# check if config data in raspiblitz.conf is available echo "*** telegraf installation: apt-get part"
configMissing=0 # get the repository public key for apt-get
if [ ${#telegrafInfluxUrl} -eq 0 ]; then curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
echo "# Missing telegrafInfluxUrl in raspiblitz.conf" DISTRIB_ID=$(lsb_release -c -s)
configMissing=1 #
# changed according suggestion from @frennkie in #1501
echo "deb https://repos.influxdata.com/debian ${DISTRIB_ID} stable" | sudo tee -a /etc/apt/sources.list.d/influxdb.list >/dev/null
#
# as the key is untrusted, this is a dirty fix
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D8FF8E1F7DF8B07E
sudo apt-get update
sudo apt-get install -y telegraf || exit 1
echo "*** telegraf installation: usermod part"
# enable telegraf user to call "/opt/vc/bin/vcgencmd" for frequency and temperatures measurements
sudo usermod -aG video telegraf
#
# enable telegraf as admin for lnd
sudo usermod telegraf -a -G lndadmin
#
# add telegraf to sudoers (for later application with smartmontools)
sudo usermod telegraf -a -G sudo
# stop telegraf service
sudo systemctl stop telegraf.service 2>/dev/null
echo "*** telegraf installation: copying telegraf config templates"
# copy custom "telegraf.conf" template to the telegraf target dir
# the telegraf inputs part goes into telegraf.d subdir
# this split into "telegraf.conf" and "telegraf.d/telegraf_inputs.conf" is necessary
# as the the [[inputs.***]] part contains lines with the keywords
# "urls", "database", "username" "password"
# so the sed-replacement-part would get confused
#
# Note: the apt-get install should have already created the path /etc/telegraf and /etc/telegraf/telegraf.d
#
sudo cp -v ${telegraf_source_dir}/telegraf.conf ${telegraf_target_dir}/telegraf.conf
sudo cp -v ${telegraf_source_dir}/telegraf.d/telegraf_inputs.conf ${telegraf_target_dir}/telegraf.d/telegraf_inputs.conf
#
# copy shell script for service uptime metrics
sudo cp -v ${telegraf_source_dir}/getserviceuptime.sh ${telegraf_target_dir}/getserviceuptime.sh
sudo chmod 755 ${telegraf_target_dir}/getserviceuptime.sh
#
# copy shell script for IP address tracking
sudo cp -v ${telegraf_source_dir}/getraspiblitzipinfo.sh ${telegraf_target_dir}/getraspiblitzipinfo.sh
sudo chmod 755 ${telegraf_target_dir}/getraspiblitzipinfo.sh
echo "*** telegraf installation: set 'telegraf=on' in config file 'raspiblitz.conf'"
/home/admin/config.scripts/blitz.conf.sh set telegraf "on"
echo "*** install telegraf done ***"
# run config if data is set in raspiblitz.conf
source <(/home/admin/config.scripts/bonus.telegraf.sh status)
if [ ${configMissing} -eq 0 ]; then
config_telegraf
else
echo "# missing config data - run 'bonus.telegraf.sh menu' to enter"
fi fi
if [ ${#telegrafInfluxDatabase} -eq 0 ]; then
echo "# Missing telegrafInfluxDatabase in raspiblitz.conf" exit 0
configMissing=1
fi fi
if [ ${#telegrafInfluxUsername} -eq 0 ]; then
echo "# Missing telegrafInfluxUsername in raspiblitz.conf" ###############################
configMissing=1 # switch off
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
echo "*** REMOVE TELEGRAF ***"
# let apt-get remove the package
sudo apt-get remove -y telegraf
echo "*** telegraf switch off and remove config ***"
/home/admin/config.scripts/blitz.conf.sh set telegraf "off"
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUrl
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxDatabase
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUsername
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxPassword
echo "*** remove telegraf done ***"
exit 0
fi fi
if [ ${#telegrafInfluxPassword} -eq 0 ]; then
echo "# Missing telegrafInfluxPassword in raspiblitz.conf" ###############################
configMissing=1 # menu
if [ "$1" = "menu" ]; then
echo "# get status"
source <(/home/admin/config.scripts/bonus.telegraf.sh status)
# check if telegraf is installed
if [ ${serviceInstalled} -eq 0 ]; then
echo "# telegraf is not installed - no menu"
exit 1
fi fi
# enter config if missing (first init) # enter config if missing (first init)
@@ -153,6 +307,11 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
whiptail --msgbox "Input cannot be empty. Please enter a valid database name." 8 78 whiptail --msgbox "Input cannot be empty. Please enter a valid database name." 8 78
continue continue
fi fi
# check that database name does not contain a '/' or a '"'
if [[ $telegrafInfluxDatabase == *"/"* ]] || [[ $telegrafInfluxDatabase == *"\""* ]]; then
whiptail --msgbox "Database name cannot contain a '/'. Please enter a valid database name." 8 78
continue
fi
break break
done done
@@ -168,6 +327,11 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
whiptail --msgbox "Input cannot be empty. Please enter a valid username." 8 78 whiptail --msgbox "Input cannot be empty. Please enter a valid username." 8 78
continue continue
fi fi
# check that username does not contain a '/' or a '"'
if [[ $telegrafInfluxUsername == *"/"* ]] || [[ $telegrafInfluxUsername == *"\""* ]]; then
whiptail --msgbox "Username cannot contain a '/' or a '\"'. Please enter a valid username." 8 78
continue
fi
break break
done done
@@ -183,6 +347,12 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
whiptail --msgbox "Input cannot be empty. Please enter a valid password." 8 78 whiptail --msgbox "Input cannot be empty. Please enter a valid password." 8 78
continue continue
fi fi
# check that password does not contain a '/' or a '"'
if [[ $telegrafInfluxPassword == *"/"* ]] || [[ $telegrafInfluxPassword == *"\""* ]]; then
whiptail --msgbox "Password cannot contain a '/' or a '\"'. Please enter a valid password." 8 78
continue
fi
break break
done done
@@ -191,123 +361,41 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
/home/admin/config.scripts/blitz.conf.sh set telegrafInfluxDatabase "${telegrafInfluxDatabase}" /home/admin/config.scripts/blitz.conf.sh set telegrafInfluxDatabase "${telegrafInfluxDatabase}"
/home/admin/config.scripts/blitz.conf.sh set telegrafInfluxUsername "${telegrafInfluxUsername}" /home/admin/config.scripts/blitz.conf.sh set telegrafInfluxUsername "${telegrafInfluxUsername}"
/home/admin/config.scripts/blitz.conf.sh set telegrafInfluxPassword "${telegrafInfluxPassword}" /home/admin/config.scripts/blitz.conf.sh set telegrafInfluxPassword "${telegrafInfluxPassword}"
# run the config function
config_telegraf
echo "# config data saved - telegraf starting up ... wait 10sec"
sleep 10
fi fi
# source and target dir for copy operation echo "# ... "
telegraf_source_dir=${resources_dir}
telegraf_target_dir=/etc/telegraf
# full path to telegraf config file for sed-replace operation
telegraf_conf_file=${telegraf_target_dir}/telegraf.conf
echo "*** telegraf installation: apt-get part"
# get the repository public key for apt-get
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
DISTRIB_ID=$(lsb_release -c -s)
#
# changed according suggestion from @frennkie in #1501
echo "deb https://repos.influxdata.com/debian ${DISTRIB_ID} stable" | sudo tee -a /etc/apt/sources.list.d/influxdb.list >/dev/null
#
# as the key is untrusted, this is a dirty fix
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D8FF8E1F7DF8B07E
sudo apt-get update
sudo apt-get install -y telegraf
echo "*** telegraf installation: usermod part"
# enable telegraf user to call "/opt/vc/bin/vcgencmd" for frequency and temperatures measurements
sudo usermod -aG video telegraf
#
# enable telegraf as admin for lnd
sudo usermod telegraf -a -G lndadmin
#
# add telegraf to sudoers (for later application with smartmontools)
sudo usermod telegraf -a -G sudo
# stop telegraf service
sudo systemctl stop telegraf.service
echo "*** telegraf installation: copying telegraf config templates"
# copy custom "telegraf.conf" template to the telegraf target dir
# the telegraf inputs part goes into telegraf.d subdir
# this split into "telegraf.conf" and "telegraf.d/telegraf_inputs.conf" is necessary
# as the the [[inputs.***]] part contains lines with the keywords
# "urls", "database", "username" "password"
# so the sed-replacement-part would get confused
#
# Note: the apt-get install should have already created the path /etc/telegraf and /etc/telegraf/telegraf.d
#
sudo cp -v ${telegraf_source_dir}/telegraf.conf ${telegraf_target_dir}/telegraf.conf
sudo cp -v ${telegraf_source_dir}/telegraf.d/telegraf_inputs.conf ${telegraf_target_dir}/telegraf.d/telegraf_inputs.conf
#
# copy shell script for service uptime metrics
sudo cp -v ${telegraf_source_dir}/getserviceuptime.sh ${telegraf_target_dir}/getserviceuptime.sh
sudo chmod 755 ${telegraf_target_dir}/getserviceuptime.sh
#
# copy shell script for IP address tracking
sudo cp -v ${telegraf_source_dir}/getraspiblitzipinfo.sh ${telegraf_target_dir}/getraspiblitzipinfo.sh
sudo chmod 755 ${telegraf_target_dir}/getraspiblitzipinfo.sh
echo "*** telegraf installation: replace influxDB url and creds"
# here comes the sed-replace-part
#
# make sure that raspiblitz.conf has the telegraf-variables properly set
# telegrafInfluxUrl
# telegrafInfluxDatabase
# telegrafInfluxUsername
# telegrafInfluxPassword
#
echo "*** telegraf installation: telegrafInfluxUrl = '${telegrafInfluxUrl}'"
# due to the occurrence of '/' in the ${telegrafInfluxUrl} we need to switch to '#' as the sed-separator
sudo sed -i "s#^urls = .*#urls = \[\"${telegrafInfluxUrl}\"\]#g" ${telegraf_conf_file}
#
# the other replacements work with the std separator '/'
#
# CAUTION: make sure that *none* of the following variables (especially "password") contains a '/'
# this would break the sed-replacement
#
echo "*** telegraf installation: telegrafInfluxDatabase = '${telegrafInfluxDatabase}'"
sudo sed -i "s/^database = .*/database = \"${telegrafInfluxDatabase}\"/g" ${telegraf_conf_file}
#
echo "*** telegraf installation: telegrafInfluxUsername = '${telegrafInfluxUsername}'"
sudo sed -i "s/^username = .*/username = \"${telegrafInfluxUsername}\"/g" ${telegraf_conf_file}
#
echo "*** telegraf installation: telegrafInfluxPassword = '${telegrafInfluxPassword}'"
sudo sed -i "s/^password = .*/password = \"${telegrafInfluxPassword}\"/g" ${telegraf_conf_file}
echo "*** telegraf installation: restart telegraf service with updated config files"
# restart telegraf service
sudo systemctl start telegraf.service
# ...and push some status into the logfile
sleep 2 sleep 2
sudo systemctl status telegraf.service --no-page 2>/dev/null source <(/home/admin/config.scripts/bonus.telegraf.sh status)
if [ ${serviceRunning} -eq 0 ]; then
echo "*** telegraf installation: set 'telegrafMonitoring=on' in config file 'raspiblitz.conf'" echo "# telegraf is not running"
/home/admin/config.scripts/blitz.conf.sh set telegrafMonitoring "on" sleep 3
exit 1
echo "*** install telegraf done ***"
exit 0
fi fi
# whiptail info with option to reset config
if [ ${#errorReport} -gt 0 ]; then
infoText="The Telegraf service is running but reports an error:\n${errorReport}\n\nCheck error logs for details:\nsudo journalctl -u telegraf.service -n 20\n\nUse RESET-CONFIG to re-enter the InfluxDB credentials."
else
infoText="Telegraf is running.\n\nInfluxDB: ${telegrafInfluxUrl}\nDatabase: ${telegrafInfluxDatabase}\nUsername: ${telegrafInfluxUsername}\n\nCheck logs for details:\nsudo journalctl -u telegraf.service -n 20"
fi
############################### whiptail --title " Telegraf " --yes-button "OK" --no-button "RESET-CONFIG" --yesno "${infoText}" 0 0
# switch off if [ $? -eq 1 ]; then
if [ "$1" = "0" ] || [ "$1" = "off" ]; then sudo systemctl stop telegraf.service 2>/dev/null
echo "*** REMOVE TELEGRAF ***"
# let apt-get remove the package
sudo apt-get remove -y telegraf
echo "*** telegraf switch off and remove config ***"
/home/admin/config.scripts/blitz.conf.sh set telegrafMonitoring "off"
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUrl /home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUrl
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxDatabase /home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxDatabase
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUsername /home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxUsername
/home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxPassword /home/admin/config.scripts/blitz.conf.sh delete telegrafInfluxPassword
echo "# config reset"
echo "*** remove telegraf done ***" sleep 3
/home/admin/config.scripts/bonus.telegraf.sh menu
fi
exit 0 exit 0
fi fi