install.fairie/home/dot_local/bin/post-installx/executable_post-netdata.sh

185 lines
9.2 KiB
Bash
Raw Normal View History

2024-05-03 19:40:44 -07:00
#!/usr/bin/env bash
# @file Netdata
# @brief Connects Netdata with Netdata's free cloud dashboard and applies some system optimizations, if necessary
# @description
# This script connects Netdata with Netdata Cloud if Netdata is installed, the `NETDATA_TOKEN` is provided, and the
# `NETDATA_ROOM` is defined. This allows you to graphically browse through system metrics on all your connected devices
# from a single free web application.
#
# This script installs additional alerts and enables notifications if Netdata is installed. Email notifications are configured
# using the provided primary email address. If the OS is Debian based, Netdata shows the number of CVEs in currently installed packages.
2024-05-27 20:45:14 -07:00
set -Eeuo pipefail
2024-05-27 20:50:11 -07:00
trap "gum log -sl error 'Script encountered an error!'" ERR
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
ensureNetdataOwnership() {
### Ensure /usr/local/var/lib/netdata/cloud.d is owned by user
if [ -d /usr/local/var/lib/netdata ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Ensuring permissions are correct on /usr/local/var/lib/netdata' && sudo chown -Rf netdata:netdata /usr/local/var/lib/netdata 2> /dev/null || sudo chown -Rf netdata:$(id -g -n) /usr/local/var/lib/netdata
2024-05-03 19:40:44 -07:00
elif [ -d /var/lib/netdata ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Ensuring permissions are correct on /var/lib/netdata' && sudo chown -Rf netdata:netdata /var/lib/netdata 2> /dev/null || sudo chown -Rf netdata:$(id -g -n) /var/lib/netdata
2024-05-03 19:40:44 -07:00
elif [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/var/lib/netdata" ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Ensuring permissions are correct on ${HOMEBREW_PREFIX:-/opt/homebrew}/var/lib/netdata" && sudo chown -Rf netdata:netdata "${HOMEBREW_PREFIX:-/opt/homebrew}/var/lib/netdata" 2> /dev/null || sudo chown -Rf netdata:$(id -g -n) "${HOMEBREW_PREFIX:-/opt/homebrew}/var/lib/netdata"
2024-05-03 19:40:44 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'No /var/lib/netdata folder found'
2024-05-03 19:40:44 -07:00
fi
}
2024-05-27 04:15:03 -07:00
### Ensure secrets are available
get-secret --exists NETDATA_ROOM NETDATA_TOKEN
2024-05-03 19:40:44 -07:00
### Claim the instance with Netdata Cloud
if command -v netdata-claim.sh > /dev/null; then
### Add user / group with script in ~/.local/bin/add-usergroup, if it is available
if command -v add-usergroup > /dev/null; then
2024-05-20 00:15:50 -07:00
sudo add-usergroup netdata netdata
2024-05-03 19:40:44 -07:00
sudo add-usergroup "$USER" netdata
fi
2024-05-27 20:45:14 -07:00
2024-05-03 19:40:44 -07:00
### Ensure ownership
ensureNetdataOwnership
2024-05-27 20:45:14 -07:00
### cd /tmp attempts to resolve - job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
cd /tmp
2024-05-03 19:40:44 -07:00
### netdata-claim.sh must be run as netdata user
2024-05-27 20:45:14 -07:00
if sudo -H -u netdata bash -c "yes | netdata-claim.sh -token="$(get-secret NETDATA_TOKEN)" -rooms="$(get-secret NETDATA_ROOM)" -url="https://app.netdata.cloud""; then
2024-05-27 23:55:42 -07:00
gum log -sl info 'Successfully added device to Netdata Cloud account'
2024-05-27 20:45:14 -07:00
fi
2024-05-03 19:40:44 -07:00
### Kernel optimizations
# These are mentioned while installing via the kickstart.sh script method. We are using Homebrew for the installation though.
# Assuming these optimizations do not cause any harm.
if [ -d /Applications ] && [ -d /System ]; then
### macOS
2024-05-27 20:50:11 -07:00
gum log -sl info 'System is macOS so Netdata kernel optimizations are not required'
2024-05-03 19:40:44 -07:00
else
### Linux
if [ -d /sys/kernel/mm/ksm ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Adding Netdata kernel optimization for /sys/kernel/mm/ksm/run'
2024-05-03 19:40:44 -07:00
echo 1 | sudo tee /sys/kernel/mm/ksm/run
2024-05-27 20:50:11 -07:00
gum log -sl info 'Adding Netdata kernel optimization for /sys/kernel/mm/ksm/sleep_millisecs'
2024-05-03 19:40:44 -07:00
echo 1000 | sudo tee /sys/kernel/mm/ksm/sleep_millisecs
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'The /sys/kernel/mm/ksm directory does not exist so Netdata kernel optimizations are not being applied'
2024-05-03 19:40:44 -07:00
fi
fi
### Install additional alerts and enable notifications
if command -v netdata > /dev/null; then
### Copy the additional alert definitions
if [ -d /usr/local/etc/netdata ]; then
NETDATA_ETC='/usr/local/etc/netdata/'
elif [ -d /etc/netdata ]; then
NETDATA_ETC='/etc/netdata'
elif [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/netdata" ]; then
NETDATA_ETC="${HOMEBREW_PREFIX:-/opt/homebrew}/etc/netdata"
else
2024-05-27 20:50:11 -07:00
gum log -sl error 'No etc location found for netdata' && exit 1
2024-05-03 19:40:44 -07:00
fi
2024-05-27 20:50:11 -07:00
gum log -sl info "Copying ${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health.d/ to $NETDATA_ETC"
2024-05-27 20:45:14 -07:00
sudo cp -rf "${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health.d/" "$NETDATA_ETC"
2024-05-03 19:40:44 -07:00
if command -v gsed > /dev/null; then
SED_UTIL="gsed"
else
SED_UTIL="sed"
fi
### Blocky
2024-05-27 20:45:14 -07:00
# TODO - Add this configuration to appropriate configuration file
2024-05-27 20:50:11 -07:00
# gum log -sl info "Adding Blocky metrics collection to $NETDATA_ETC/go.d/prometheus.conf"
2024-05-27 20:45:14 -07:00
# sudo "$SED_UTIL" -i "/jobs:/a\ - name: blocky_local \n url: 'http://127.0.0.1:4000/metrics'" "$NETDATA_ETC/go.d/prometheus.conf"
2024-05-03 19:40:44 -07:00
### SFTPGo
2024-05-27 20:45:14 -07:00
# TODO - Add this configuration to appropriate configuration file
2024-05-27 20:50:11 -07:00
# gum log -sl info "Adding SFTPGo metrics collection to $NETDATA_ETC/go.d/prometheus.conf"
2024-05-27 20:45:14 -07:00
# sudo "$SED_UTIL" -i "/jobs:/a\ - name: sftpgo_local \n url: 'http://127.0.0.1:57500/metrics'" "$NETDATA_ETC/go.d/prometheus.conf"
2024-05-03 19:40:44 -07:00
# Backup current health alarm configuration and apply new one
if [ -d /usr/local/lib/netdata ]; then
NETDATA_LIB='/usr/local/lib/netdata'
elif [ -d /usr/lib/netdata ]; then
NETDATA_LIB='/usr/lib/netdata'
elif [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/lib/netdata" ]; then
NETDATA_LIB="${HOMEBREW_PREFIX:-/opt/homebrew}/lib/netdata"
else
2024-05-27 20:50:11 -07:00
gum log -sl error 'No lib location found for netdata' && exit 1
2024-05-03 19:40:44 -07:00
fi
2024-05-27 20:50:11 -07:00
gum log -sl info "Copying ${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health_alarm_notify.conf to $NETDATA_LIB/conf.d/health_alarm_notify.conf"
2024-05-27 20:45:14 -07:00
sudo cp -f "${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health_alarm_notify.conf" "$NETDATA_LIB/conf.d/health_alarm_notify.conf"
2024-05-03 19:40:44 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'netdata is not available in the PATH or is not installed'
2024-05-03 19:40:44 -07:00
fi
### Ensure the apt command is available before running `debsecan` logic
if command -v apt-get > /dev/null; then
### Configure Netdata to gather information about CVEs in the installed packages
if command -v debsecan > /dev/null; then
DEBSECAN_GIT="${XDG_DATA_HOME:-$HOME/.local/share}/netdata-debsecan"
### Installing the script to generate report on CVEs in installed packages
2024-05-27 20:50:11 -07:00
gum log -sl info 'Installing script to generate report on CVEs in installed packages'
2024-05-27 20:45:14 -07:00
sudo cp -f "$DEBSECAN_GIT/usr_local_bin_debsecan-by-type" "/usr/local/bin/debsecan-by-type"
2024-05-03 19:40:44 -07:00
### Generate initial debsecan reports in /var/log/debsecan/
2024-05-27 20:50:11 -07:00
gum log -sl info 'Generating initial debsecan reports in /var/log/debsecan/'
2024-05-27 20:45:14 -07:00
debsecan-by-type
2024-05-03 19:40:44 -07:00
### Configure dpkg to refresh the file after each run
2024-05-27 20:50:11 -07:00
gum log -sl info 'Configuring dpkg to refresh the file after each run'
2024-05-27 20:45:14 -07:00
sudo cp -f "$DEBSECAN_GIT/etc_apt_apt.conf.d_99debsecan" /etc/apt/apt.conf.d/99-debsecan
2024-05-03 19:40:44 -07:00
### Add a cron job to refresh the file every hour
2024-05-27 20:50:11 -07:00
gum log -sl info 'Adding a cron job to refresh the file every hour'
2024-05-27 20:45:14 -07:00
sudo cp -f "$DEBSECAN_GIT/etc_cron.d_debsecan" /etc/cron.d/debsecan
2024-05-03 19:40:44 -07:00
### Install the module/configuration file
2024-05-27 20:50:11 -07:00
gum log -sl info 'Installing the module and configuration file'
2024-05-03 19:40:44 -07:00
sudo "$DEBSECAN_GIT/debsecan.chart.py" /usr/libexec/netdata/python.d/debsecan.chart.py
sudo "$DEBSECAN_GIT/debsecan.conf" /etc/netdata/python.d/debsecan.conf
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'apt-get is available but debsecan is not available in the PATH or is not installed'
2024-05-03 19:40:44 -07:00
fi
fi
### Ensure / report whether speedtest-cli is installed
if ! command -v speedtest-cli > /dev/null; then
if command -v pipx > /dev/null; then
pipx install speedtest-cli
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'speedtest-cli not installed and pipx is not available'
2024-05-03 19:40:44 -07:00
fi
fi
### Configure Netdata to gather information about Internet connection speed
if command -v speedtest-cli > /dev/null; then
### Installing the script to generate report on Internet connection speed
2024-05-27 20:50:11 -07:00
gum log -sl info 'Installing script to generate report on Internet connection speed'
2024-05-03 19:40:44 -07:00
LIBEXEC_PATH="$(netdata -W buildinfo | grep 'Configure' | sed "s/.*--libexecdir=\([^ \']*\).*/\1/")"
if [ -d /usr/libexec/netdata/charts.d ]; then
sudo cp -f "${XDG_DATA_HOME:-$HOME/.local/share}/netdata-speedtest/speedtest.chart.sh" "/usr/libexec/netdata/charts.d/speedtest.chart.sh"
elif [ -d "$LIBEXEC_PATH/netdata/charts.d" ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info "$LIBEXEC_PATH/netdata/charts.d present on system"
2024-05-03 19:40:44 -07:00
cp -f "${XDG_DATA_HOME:-$HOME/.local/share}/netdata-speedtest/speedtest.chart.sh" "$LIBEXEC_PATH/netdata/charts.d/speedtest.chart.sh"
else
2024-05-27 20:50:11 -07:00
gum log -sl warn "Failed to find appropriate directory to add Netdata speedtest chart script"
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'speedtest-cli is not available in the PATH or is not installed'
2024-05-03 19:40:44 -07:00
fi
### Ensure ownership again
ensureNetdataOwnership
### Restart Netdata service
if command -v systemctl > /dev/null; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Enabling netdata service' && sudo systemctl enable netdata
gum log -sl info 'Restarting netdata service' && sudo systemctl restart netdata
2024-05-03 19:40:44 -07:00
elif [ -d /Applications ] && [ -d /System ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'Starting / enabling netdata service' && brew services restart netdata
2024-05-03 19:40:44 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'systemctl is not available'
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'netdata-claim.sh is not available in the PATH'
2024-05-03 19:40:44 -07:00
fi