add metric collection via telegraf as new bonus service (improved version) (#1616)

This commit is contained in:
PatrickScheich
2021-03-22 22:20:26 +01:00
committed by rootzoll
parent ef42363ebb
commit 7897d08b0b
8 changed files with 7859 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
[telegraf](https://www.influxdata.com/time-series-platform/telegraf/) is a metric collection tool by influxData.
It is opensource and works fine with [influxDB](https://www.influxdata.com/products/influxdb-overview/) as the timeseries database and [Grafana](https://grafana.com/grafana/) as the graphics front end.
You may take a look [here](https://github.com/gcgarner/IOTstack) for a nice dockerized installation of influxDB and Grafana on a raspberry pi.
Make sure to have a telegraf section in your `/mnt/hdd/raspiblitz.conf`
you have to manually edit them into `/mnt/hdd/raspiblitz.conf` before calling
```
/home/admin/config.scripts/bonus.telegraf.sh on
```
You have to provide a running influxDB / Grafana infrastructure elsewhere (reachable for the RaspiBlitz)
# telegraf section for raspiblitz.conf
All telegraf switches and configuration variables. You may copy & paste them into your RaspiBlitz configuration at `/mnt/hdd/raspiblitz.conf`, after editing and providing the proper values which match your environment
```
# all telegraf switches and configuration variables
#
# switch telegraf service and metrics capturing on/off
telegrafMonitoring=on
#
# the full url to your influxDB data ingestion point, with port
telegrafInfluxUrl='http://192.168.2.46:8086'
#
# the name of your influxDB database
telegrafInfluxDatabase='raspiblitz'
#
# credentials for this database
telegrafInfluxUsername='telegraf'
telegrafInfluxPassword='metricsmetricsmetricsmetrics'
```

View File

@@ -0,0 +1,240 @@
#!/bin/bash
#
###############################################################################
# File: getraspiblitzipinfo.sh
# Date: 2020-10-04
###############################################################################
# set the "debugLevel"
debugLevel=0
# enable Write to memoryFile
writeMemoryfile=1
# if "logFile" points to an existing file => logging enabled
logFile=/mnt/hdd/temp/raspiblitzipinfo.log
# get the ISO timestamp for log output
sts=$(date --iso-8601='seconds')
if [ -f "${logFile}" ]; then printf "\n---\n%s: %s started\n" "$sts" "$0" >> ${logFile} ;fi
# get the seconds since UNIX epoch
unixTimestamp=$(date +"%s")
if [ -f "${logFile}" ]; then printf "%s: unixTimeStamp = %s\n" "$sts" "$unixTimestamp" >> ${logFile} ;fi
# get active network device (eth0 or wlan0)
networkDevice=$(ip addr | grep -v "lo:" | grep 'state UP' | tr -d " " | cut -d ":" -f2 | head -n 1)
#
if [ -f "${logFile}" ]; then printf "%s: networkDevice = %s\n" "$sts" "$networkDevice" >> ${logFile} ;fi
if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
# create the indexed array "origin" an fill it
# this also creates the "Enumeration"
# 0 <=> publicIP
# 1 <=> bitcoind
# 2 <=> lnd
# 3 <=> IPv6
# 4 <=> IPv4
#
declare -a origin
origin=(publicIP bitcoind lnd IPv6 IPv4)
#
#if [ -f "${logFile}" ]; then for i in $( seq 0 4 ); do printf "%s: origin[ %d ] = %s\n" "$sts" "$i" "${origin[ $i ]}" >> ${logFile} ;done ;fi
#if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
#
# further we need the arrays
declare -a ip_addr_curr
declare -a ip_addr_prev
declare -a creation_ts_curr
declare -a creation_ts_prev
declare -a has_changed
# load local config (but should also work if not available)
source /mnt/hdd/raspiblitz.conf 2>/dev/null
# get the "public IP addresses" from various sources/origins
# [0] -> publicIP (remove square barckets in case of IPv6)
# [1] -> bitcoind
# [2] -> lnd
# [3] -> IPv6 at local network interface (eth0 or wlan0)
# [4] -> IPv4 at local network interface (eth0 or wlan0)
ip_addr_curr[0]=$(echo "${publicIP}" | tr -d '[]')
ip_addr_curr[1]=$(/usr/local/bin/bitcoin-cli -conf=/mnt/hdd/bitcoin/bitcoin.conf getnetworkinfo | jq -r ".localaddresses[0].address" 2>/dev/null)
ip_addr_curr[2]=$(/usr/local/bin/lncli --lnddir=/mnt/hdd/app-data/lnd getinfo | jq -r ".uris[0]" 2>/dev/null | cut -d'@' -f2 | rev | cut -d':' -f2- | rev | tr -d '[]')
ip_addr_curr[3]=$(ip -o -6 address show scope global up dev ${networkDevice} 2>/dev/null | cut -d'/' -f1 | awk '/inet6/{print $4}' | head -n 1)
ip_addr_curr[4]=$(ip -o -4 address show scope global up dev ${networkDevice} 2>/dev/null | cut -d'/' -f1 | awk '/inet/{print $4}' | head -n 1)
#
if [ -f "${logFile}" ]; then for i in $( seq 0 4 ); do printf "%s: ip_addr_curr[ %d ] = %s\n" "$sts" "$i" "${ip_addr_curr[ $i ]}" >> ${logFile} ;done ;fi
if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
#
if [ ${debugLevel} -gt 10 ]; then for i in $( seq 0 4 ); do printf " %2d: %-10s = %s\n" "$i" "${origin[ $i ]}" "${ip_addr_curr[ $i ]}" ;done ;fi
# get the values from a prior run, that file will not be changes as long as all the values stay the same
memoryFile=/mnt/hdd/temp/raspiblitzipinfo.out
source ${memoryFile} 2>/dev/null
# prepare to count the changes
changes=0
# initialize the array of the previous IP addresses from the memory file
for i in $( seq 0 4 ); do
# compose the name of the memory variable
s="${origin[ $i ]}_old"
# assign the prev address array emement with content of that variable
ip_addr_prev[ $i ]=${!s}
# if the variable is still empty, fill it with "N/A"
if [ "ip_addr_prev[ $i ]" == "" ]; then ip_addr_prev[ $i ]="N/A" ; fi
#if [ -f "${logFile}" ]; then printf "%s: from memoryfile variable %30s = %s\n" "$sts" "$s" "${ip_addr_prev[ $i ]}" >> ${logFile} ; fi
if [ ${debugLevel} -gt 10 ]; then printf " %2d: read into ip_addr_prev[%d] from memoryfile variable %30s = %s\n" "$i" "$i" "$s" "${ip_addr_prev[ $i ]}" ; fi
done
#if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
# initialize the Creation TimeStamps with their old values from the memory file
# so it is guaranteed that they contain a proper value if that IP does not change
# otherwise the "unixTimestamp" will be written if a change is detected
for i in $( seq 0 4 ); do
# compose the name of the memory variable
s="${origin[ $i ]}_CreationTS_old"
# get the content of that variable
# and sanitzie if necessary
val=${!s}
if [ "${val}" == "" ]; then val=-1 ; fi
# assign the current and prev timestamp array emements with content of that variable
creation_ts_curr[ $i ]=${val}
creation_ts_prev[ $i ]=${val}
#if [ -f "${logFile}" ]; then printf "%s: from memoryfile variable %30s = %s\n" "$sts" "$s" "${creation_ts_curr[ $i ]}" >> ${logFile} ; fi
if [ ${debugLevel} -gt 10 ]; then printf " %2d: read into creation_ts_curr/prev[%d] from memoryfile variable %30s = %s\n" "$i" "$i" "$s" "${creation_ts_curr[ $i ]}" ; fi
done
#if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
# initialize the "has_changed" flag array
for i in $( seq 0 4 ); do
has_changed[ $i ]=0
done
# check for changes...
# whenever a change is detected the current time will be written into the respective Creation TimeStamp variable
# Additionally the "changes" counter will be incremented
for i in $( seq 0 4 ); do
if [ "${ip_addr_curr[$i]}" != "${ip_addr_prev[$i]}" ]; then
((changes++))
has_changed[ $i ]=1
creation_ts_curr[ $i ]=${unixTimestamp}
if [ -f "${logFile}" ]; then printf "%s: %2d: IP addr change detected for %10s: %40s (new) != %40s (old)\n" "$sts" "$i" "${origin[ $i ]}" "${ip_addr_curr[$i]}" "${ip_addr_prev[$i]}" >> ${logFile} ; fi
if [ ${debugLevel} -gt 0 ]; then printf " %2d: IP addr change detected for %10s: %40s (new) != %40s (old)\n" "$i" "${origin[ $i ]}" "${ip_addr_curr[$i]}" "${ip_addr_prev[$i]}" ; fi
else
if [ -f "${logFile}" ]; then printf "%s: %2d: IP addr --NOT changed-- for %10s: %40s (new) != %40s (old)\n" "$sts" "$i" "${origin[ $i ]}" "${ip_addr_curr[$i]}" "${ip_addr_prev[$i]}" >> ${logFile} ; fi
if [ ${debugLevel} -gt 10 ]; then printf " %2d: IP addr --NOT changed-- for %10s: %40s (new) == %40s (old)\n" "$i" "${origin[ $i ]}" "${ip_addr_curr[$i]}" "${ip_addr_prev[$i]}" ; fi
fi
done
if [ -f "${logFile}" ]; then echo " " >> ${logFile} ;fi
# IF at least one value of the memory file needs to be updated, the whole file will be rewritten.
# the "..._CreationTS" variables will contain their "..._CreationTS_old" counter part (if nothing has changed)
# or "${unixTimestamp}" if that particular IP address has been changed
#
if [ ${changes} -gt 0 ]; then
if [ -f "${logFile}" ]; then printf "%s: *** IP change detected, writing memoryfile %s ***\n" "$sts" "$memoryFile" >> ${logFile} ; fi
if [ ${debugLevel} -gt 0 ]; then echo "*** IP change detected, writing memoryfile ${memoryFile} ***" ; fi
if [ ${writeMemoryfile} -eq 1 ]; then
# truncate file and write header
echo "#############################################################" > ${memoryFile}
echo "# RaspiBlitz IP address memory file." >> ${memoryFile}
echo "# created by script: ${0}" >> ${memoryFile}
echo "#############################################################" >> ${memoryFile}
echo " " >> ${memoryFile}
#
# write a section for each entry in the "origin" array
for i in $( seq 0 4 ); do
echo "# section ${origin[ $i ]}" >> ${memoryFile}
echo "${origin[ $i ]}_old=${ip_addr_curr[$i]}" >> ${memoryFile}
echo "${origin[ $i ]}_CreationTS_old=${creation_ts_curr[ $i ]}" >> ${memoryFile}
echo " " >> ${memoryFile}
done
if [ -f "${logFile}" ]; then echo "===========================================================================================" >> ${logFile} ;fi
if [ -f "${logFile}" ]; then cat ${memoryFile} >> ${logFile} ; fi
if [ -f "${logFile}" ]; then echo "===========================================================================================" >> ${logFile} ;fi
else
# display info on stdout
echo ""
echo "writeMemoryfile=off => show changes"
echo ""
echo "#############################################################"
echo "# RaspiBlitz IP address memory file."
echo "# created by script: ${0}"
echo "#############################################################"
echo " "
for i in $( seq 0 4 ); do
echo "# section ${origin[ $i ]}"
echo "${origin[ $i ]}_old=${ip_addr_curr[$i]}"
echo "${origin[ $i ]}_CreationTS_old=${creation_ts_curr[ $i ]}"
echo " "
done
fi
else
if [ -f "${logFile}" ]; then printf "%s: *** no IP change detected, do nothing... ***\n" "$sts" >> ${logFile} ; fi
if [ ${debugLevel} -gt 0 ]; then echo "*** no IP change detected, do nothing... ***" ; fi
fi
# now create the output for the telegraf "[[inputs.exec]]" section in influx-line-format
#
# measurement: raspiblitz_ip_info
#
# tags
# * host
# * origin
# * ipaddr
# * ipaddr_prev
# * ipaddr_changed
#
# fields
# * created
# * uptime
# * changed
#
for i in $( seq 0 4 ); do
# sanitize tags
if [ "${ip_addr_curr[$i]}" = "" ]; then ip_addr_curr[$i]='empty' ; fi
if [ "${ip_addr_prev[$i]}" = "" ]; then ip_addr_prev[$i]='empty' ; fi
#
# calculate uptime
ipaddr_online=$(( ${unixTimestamp} - ${creation_ts_curr[ $i ]}))
#
# create influx-line-format output
# only if there is a proper creation timestamp
if [ ${creation_ts_curr[ $i ]} -gt 1000000000 ]; then
influxLine="raspiblitz_ip_info,origin=${origin[ $i ]},ipaddr=${ip_addr_curr[$i]},ipaddr_prev=${ip_addr_prev[$i]},ipaddr_changed=${has_changed[ $i ]} created=${creation_ts_curr[ $i ]}i,uptime=${ipaddr_online}i,changed=${has_changed[ $i ]}i"
if [ -f "${logFile}" ]; then printf "%s: === %s\n" "$sts" "$influxLine" >> ${logFile} ; fi
echo "${influxLine}"
else
if [ -f "${logFile}" ]; then printf "%s: creation time ERROR for origin %s \n" "$sts" "${origin[ $i ]}" >> ${logFile} ; fi
fi
done
# -eof-

View File

@@ -0,0 +1,34 @@
#!/bin/bash
#
###############################################################################
# File: getserviceuptime.sh
# Date: 2020-10-12
###############################################################################
# collect the service uptimes into variables
# some of the variables may contain "" as the pidof/pgrep may fail due to non-existens of that process
#
bitcoind_uptime=$(ps -p `pidof bitcoind` -o etimes='' 2>/dev/null | tr -d '[:space:]')
lnd_uptime=$(ps -p `pidof lnd` -o etimes='' 2>/dev/null | tr -d '[:space:]')
electrs_uptime=$(ps -p `pidof electrs` -o etimes='' 2>/dev/null | tr -d '[:space:]')
telegraf_uptime=$(ps -p `pidof telegraf` -o etimes='' 2>/dev/null | tr -d '[:space:]')
sshd_uptime=$(ps -p `pgrep -f /usr/sbin/sshd` -o etimes='' 2>/dev/null | tr -d '[:space:]')
rtl_uptime=$(ps -p `pgrep -f RTL` -o etimes='' 2>/dev/null | tr -d '[:space:]')
btcrpexp_uptime=$(ps -p `pgrep -f "sh -c node ./bin/www"` -o etimes='' 2>/dev/null | tr -d '[:space:]')
mempool_uptime=$(ps -p `pgrep -f "/usr/bin/node --max-old-space-size=2048 dist/index.js"` -o etimes='' 2>/dev/null | tr -d '[:space:]')
#
# whenever a variable contains a valid integer...spit out a line in influx-line-format
# (see https://stackoverflow.com/a/19116862 for details "Test whether string is a valid integer")
#
if [ "$bitcoind_uptime" -eq "$bitcoind_uptime" ] 2>/dev/null; then echo "service_uptime,service=bitcoind uptime=${bitcoind_uptime}i" ;fi
if [ "$lnd_uptime" -eq "$lnd_uptime" ] 2>/dev/null; then echo "service_uptime,service=lnd uptime=${lnd_uptime}i" ;fi
if [ "$electrs_uptime" -eq "$electrs_uptime" ] 2>/dev/null; then echo "service_uptime,service=electrs uptime=${electrs_uptime}i" ;fi
if [ "$telegraf_uptime" -eq "$telegraf_uptime" ] 2>/dev/null; then echo "service_uptime,service=telegraf uptime=${telegraf_uptime}i" ;fi
if [ "$sshd_uptime" -eq "$sshd_uptime" ] 2>/dev/null; then echo "service_uptime,service=sshd uptime=${sshd_uptime}i" ;fi
if [ "$rtl_uptime" -eq "$rtl_uptime" ] 2>/dev/null; then echo "service_uptime,service=RTL uptime=${rtl_uptime}i" ;fi
if [ "$btcrpexp_uptime" -eq "$btcrpexp_uptime" ] 2>/dev/null; then echo "service_uptime,service=btcrpcexplorer uptime=${btcrpexp_uptime}i" ;fi
if [ "$mempool_uptime" -eq "$mempool_uptime" ] 2>/dev/null; then echo "service_uptime,service=mempool uptime=${mempool_uptime}i" ;fi
# -eof-

View File

@@ -0,0 +1,51 @@
# Telegraf Configuration
#
# raspiblitz
# Global tags can be specified here in key="value" format.
[global_tags]
## Environment variables can be used as tags, and throughout the config file
# user = "$USER"
# Configuration for telegraf agent
[agent]
## Default data collection interval for all inputs
interval = "10s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
hostname = ""
omit_hostname = false
###############################################################################
# OUTPUT PLUGINS #
###############################################################################
# Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
##########
# RaspiBlitz Note:
# it is necessary to keep the exact spacing for the sed-part in custom-installs.sh
#
urls = ["URL of influxDB with port"]
database = "influxDB database name"
username = "user with write access to above database"
password = "password of the above influx user"
##########
database_tag = ""
exclude_database_tag = false
skip_database_creation = false
retention_policy = ""
write_consistency = "any"
timeout = "5s"
user_agent = "telegraf"
udp_payload = "512B"

View File

@@ -0,0 +1,268 @@
###############################################################################
# INPUT PLUGINS #
###############################################################################
#### Collect statistics about itself
[[inputs.internal]]
collect_memstats = true
#### Read metrics about cpu usage
[[inputs.cpu]]
percpu = false
totalcpu = true
collect_cpu_time = false
report_active = false
#### Read metrics about disk usage by mount point
[[inputs.disk]]
ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
#### Read metrics about network interface usage
[[inputs.net]]
##interfaces = ["eth0"]
ignore_protocol_stats = false
#### Read metrics about disk IO by device
[[inputs.diskio]]
#### Get kernel statistics from /proc/stat
[[inputs.kernel]]
#### Read metrics about memory usage
[[inputs.mem]]
#### Get the number of processes and group them by status
[[inputs.processes]]
#### Read metrics about swap memory usage
[[inputs.swap]]
#### Read metrics about system load & uptime
[[inputs.system]]
#####################################
#### Bitcoin and Lightning Network related metrics
####
#### Note: [[inputs.exec]] => data_format = "json" only evaluate numeric values.
#### All NON-NUMERIC values will be dropped
####
#####################################
#### Bitcoin related metric
#### basic information about the blockchain
#### --> https://developer.bitcoin.org/reference/rpc/getblockchaininfo.html
##
## Most usefull fields...
## * blocks
## * headers
## * verificationprogress
##
[[inputs.exec]]
interval = "60s"
commands = ["/usr/local/bin/bitcoin-cli -conf=/mnt/hdd/bitcoin/bitcoin.conf getblockchaininfo" ]
name_override = "bitcoin_blockchaininfo"
data_format = "json"
#### Bitcoin related metric
#### information about network traffic, including bytes in, bytes out, and current time window
#### --> https://developer.bitcoin.org/reference/rpc/getnettotals.html
##
## Most usefull fields...
## * totalbytesrecv
## * totalbytessent
##
[[inputs.exec]]
interval = "60s"
commands = ["/usr/local/bin/bitcoin-cli -conf=/mnt/hdd/bitcoin/bitcoin.conf getnettotals" ]
name_override = "bitcoin_nettotals"
data_format = "json"
# #### Bitcoin related metric
# #### total uptime of the bitcoind service in seconds
# #### --> https://developer.bitcoin.org/reference/rpc/uptime.html
# ##
# ## Replaced by "/etc/telegraf/getserviceuptime.sh"
# ##
# [[inputs.exec]]
# interval = "60s"
# commands = ["/usr/local/bin/bitcoin-cli -conf=/mnt/hdd/bitcoin/bitcoin.conf uptime" ]
# name_override = "bitcoin_uptime"
# data_format = "value"
# data_type = "integer"
#### Bitcoin related metric
#### number of connections to other nodes
#### --> https://developer.bitcoin.org/reference/rpc/getconnectioncount.html
##
[[inputs.exec]]
interval = "60s"
commands = ["/usr/local/bin/bitcoin-cli -conf=/mnt/hdd/bitcoin/bitcoin.conf getconnectioncount" ]
name_override = "bitcoin_connectioncount"
data_format = "value"
data_type = "integer"
#### Lightning Network related metric
#### basic information about the LN node
#### --> https://api.lightning.community/#getinfo
##
## Most usefull fields...
## * block_height
## * num_peers
## * num_active_channels
## * num_inactive_channels
## * num_pending_channels
##
[[inputs.exec]]
interval = "60s"
commands = ["/usr/local/bin/lncli --lnddir=/mnt/hdd/app-data/lnd getinfo" ]
name_override = "ln_info"
data_format = "json"
#####################################
#### Various IP addresses
####
#####################################
#### gets the creation timestamp and uptime of several raspiblitz IP addresses
##
## * publicIP from /mnt/hdd/raspiblitz.conf
## * bitcoind node ip address via: bitcoin-cli getnetworkinfo => "localaddresses"
## * lnd ip addess via: lncli getinfo => "uris"
## * IPv6 global from eth0/wlan0
## * IPv4 local network address from eth0/wlan0
##
[[inputs.exec]]
interval = "60s"
commands = ["/etc/telegraf/getraspiblitzipinfo.sh" ]
data_format = "influx"
#####################################
#### Processes and Services
####
#####################################
#### gets the uptime of various important raspiblitz services via
[[inputs.exec]]
# this should match the standard metrics-gathering-interval as defined in the [agent] section
commands = ["/etc/telegraf/getserviceuptime.sh" ]
data_format = "influx"
## check for the systemd services not the plain exe-file names
[[inputs.procstat]]
systemd_unit = "bootstrap.service"
[[inputs.procstat]]
systemd_unit = "telegraf.service"
[[inputs.procstat]]
systemd_unit = "ssh.service"
[[inputs.procstat]]
systemd_unit = "bitcoind.service"
[[inputs.procstat]]
systemd_unit = "lnd.service"
[[inputs.procstat]]
systemd_unit = "RTL.service"
[[inputs.procstat]]
systemd_unit = "electrs.service"
[[inputs.procstat]]
systemd_unit = "btc-rpc-explorer.service"
[[inputs.procstat]]
systemd_unit = "background.service"
[[inputs.procstat]]
systemd_unit = "mempool.service"
#####################################
#### Hardware data: cpu/gpu temperature, voltage, cpu clock
####
#####################################
[[inputs.file]]
files = ["/sys/class/thermal/thermal_zone0/temp"]
name_override = "cpu_temperature"
data_format = "value"
data_type = "integer"
[[inputs.exec]]
commands = ["/opt/vc/bin/vcgencmd measure_temp"]
name_override = "gpu_temperature"
data_format = "grok"
grok_patterns = ["%{NUMBER:value:float}"]
[[inputs.exec]]
commands = ["/opt/vc/bin/vcgencmd measure_volts core"]
name_override = "cpu_volts"
data_format = "grok"
grok_patterns = ["%{NUMBER:value:float}"]
[[inputs.exec]]
commands = ["/opt/vc/bin/vcgencmd measure_clock arm"]
name_override = "cpu_frequency"
data_format = "grok"
grok_patterns = ["=%{NUMBER:value:int}"]
#####################################
#### web services
####
#### JSON web interface for statistical data from "blockchain.com"
####
#### documentation: https://www.blockchain.com/api/charts_api
#### example: https://www.blockchain.com/stats
#### JSON api URL: https://api.blockchain.info/stats
####
#####################################
[[inputs.http]]
interval = "60s"
urls = ["https://api.blockchain.info/stats"]
timeout = "5s"
name_override = "web_bitcoin_info"
data_format = "json"
###############################################################################
# SERVICE INPUT PLUGINS #
###############################################################################
#### Read metrics from one or many prometheus clients
#
# electrs stats
[[inputs.prometheus]]
interval = "60s"
urls = ["http://localhost:4224/metrics"]
metric_version = 2
name_override = "electrs_info"
### -eof-

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
#########################################################################
# this has to go to /mnt/hdd/raspiblitz.conf
# make sure the settings match your local influxDB setup
#########################################################################
# all telegraf switches and configuration variables
telegrafMonitoring=on
telegrafInfluxUrl='http://192.168.2.46:8086'
telegrafInfluxDatabase='telegraf'
telegrafInfluxUsername='telegraf'
telegrafInfluxPassword='metricsmetricsmetricsmetrics'

View File

@@ -0,0 +1,177 @@
#!/bin/bash
#
###############################################################################
# File: bonus.telegraf.sh
# Date: 2020-10-03
###############################################################################
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "config script to switch the telegraf metric collection on or off"
echo "bonus.telegraf.sh [on|off|status]"
exit 1
fi
# CONFIGFILE - configuration of RaspiBlitz
configFile="/mnt/hdd/raspiblitz.conf"
# Check if HDD contains configuration
configExists=$(ls ${configFile} | grep -c '.conf')
if [ ${configExists} -ne 1 ]; then
echo "RaspiBlitz config file '${configFile}' not found"
exit 1
fi
# at this point the config file exists and can be sourced
source ${configFile}
# this variables is used repeatedly in this script
resources_dir=/home/admin/assets/telegraf/etc-telegraf
###############################
# give status
if [ "$1" = "status" ]; then
echo "##### STATUS TELEGRAF SERVICE"
# check if "telegrafMonitoring" is enabled ("1"|"on") in raspiblitz.conf
if [ "${telegrafMonitoring}" = "1" ] || [ "${telegrafMonitoring}" = "on" ]; then
echo "configured=1"
else
echo "configured=0"
fi
serviceInstalled=$(sudo systemctl status telegraf --no-page 2>/dev/null | grep -c "telegraf.service - The plugin-driven")
echo "serviceInstalled=${serviceInstalled}"
if [ ${serviceInstalled} -eq 0 ]; then
echo "infoMessage='Telegraf service not installed'"
fi
serviceRunning=$(sudo systemctl status telegraf --no-page 2>/dev/null | grep -c "active (running)")
echo "serviceRunning=${serviceRunning}"
if [ ${serviceRunning} -eq 1 ]; then
echo "infoMessage='Telegraf service is running'"
else
echo "infoMessage='Not running - check: sudo journalctl -u telegraf'"
fi
exit 0
fi
###############################
# switch on
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "*** INSTALL TELEGRAF ***"
# soure 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
echo "*** telegraf installation: apt-get part"
# get the repository publy 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
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
# 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/teöegraf_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 occurance 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
echo "*** telegraf installation: set 'telegrafMonitoring=on' in config file '${configFile}'"
sudo sed -i "s/^telegrafMonitoring=.*/telegrafMonitoring=on/g" ${configFile}
echo "*** install telegraf done ***"
exit 0
fi
###############################
# 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 remove: set 'telegrafMonitoring=off' in config file '${configFile}'"
sudo sed -i "s/^telegrafMonitoring=.*/telegrafMonitoring=off/g" ${configFile}
echo "*** remove telegraf done ***"
exit 0
fi
echo "FAIL - Unknown Parameter $1"
exit 1