install.fairie/home/.chezmoiscripts/universal/run_onchange_after_57-netdata.sh.tmpl

161 lines
8.2 KiB
Cheetah
Raw Normal View History

2023-03-19 23:38:30 -07:00
{{- if and (ne .host.distro.family "windows") (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (or (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "NETDATA_TOKEN")) (env "NETDATA_TOKEN")) (or (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "NETDATA_ROOM")) (env "NETDATA_ROOM")) -}}
#!/usr/bin/env bash
Update 55 files - /home/dot_config/rclone/system/s3-docker.service.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_07-docker-plugins.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_13-post-install-packages.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-warp.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-timeshift.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-keybase.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-install-aqua-packages.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_12-install-packages.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_15-install-asdf-packages.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_18-install-sdkman.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_19-gnome-extension-settings.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_19-theme-files.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_20-font.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_21-dconf-settings.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_45-vmware.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_46_virtualbox.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_50-crontab.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_57-netdata.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_59-connect-tailscale.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_61-plymouth-settings.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_64-grub-settings.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_80-bash-completions.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_70-misc-bug-fixes.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_06-bin-symlink.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_40-firefox.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_40-chrome.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_28-privoxy.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_27-tor.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_24-vpn-linux.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_26-system-vscode-node-modules.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_26-vscode-extensions.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_07-docker-plugins.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_05-log-config.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_04-environment-profile.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_11-symlink-ansible-roles.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-warp.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-timeshift.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-keybase.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_14-install-aqua-packages.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_12-install-packages.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_15-install-asdf-packages.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_18-install-sdkman.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_19-gnome-extension-settings.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_19-theme-files.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_20-font.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_21-dconf-settings.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_45-vmware.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_46_virtualbox.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_50-crontab.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_57-netdata.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_59-connect-tailscale.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_61-plymouth-settings.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_64-grub-settings.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_80-bash-completions.sh.tmpl - /home/.chezmoiscripts/universal/run_onchange_after_70-misc-bug-fixes.sh.tmpl
2023-04-11 20:26:25 -07:00
# @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.
2023-05-31 20:10:23 -07:00
#
# 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.
{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}
2023-07-17 23:35:49 -07:00
ensureNetdataOwnership() {
### Ensure /usr/local/var/lib/netdata/cloud.d is owned by user
if [ -d /usr/local/var/lib/netdata ]; then
logg info 'Ensuring permissions are correct on /usr/local/var/lib/netdata'
sudo chown -Rf netdata /usr/local/var/lib/netdata
elif [ -d /var/lib/netdata ]; then
logg info 'Ensuring permissions are correct on /var/lib/netdata'
sudo chown -Rf netdata /var/lib/netdata
fi
}
### Claim the instance with Netdata Cloud
if command -v netdata-claim.sh > /dev/null; then
2023-07-07 01:29:58 -07:00
# Add user / group with script in ~/.local/bin/add-user, if it is available
if command -v add-user > /dev/null; then
sudo add-user netdata
fi
2023-07-17 13:40:40 -07:00
2023-07-17 23:35:49 -07:00
ensureNetdataOwnership
2023-07-31 23:54:04 -07:00
2023-05-29 19:28:25 -07:00
# netdata-claim.sh must be run as netdata user
2023-08-08 22:46:26 -07:00
sudo -H -u netdata bash -c 'export NETDATA_ROOM="{{- if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "NETDATA_ROOM")) -}}{{- includeTemplate "secrets/NETDATA_ROOM" | decrypt | trim -}}{{- else -}}{{- env "NETDATA_ROOM" -}}{{- end -}}" && export NETDATA_TOKEN="{{- if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "NETDATA_TOKEN")) -}}{{- includeTemplate "secrets/NETDATA_TOKEN" | decrypt | trim -}}{{- else -}}{{- env "NETDATA_TOKEN" -}}{{- end -}}" && yes | netdata-claim.sh -token="$NETDATA_TOKEN" -rooms="$NETDATA_ROOM" -url="{{ .netdataClaimURL }}"'
# 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
logg info 'System is macOS so Netdata kernel optimizations are not required'
else
# Linux
if [ -d /sys/kernel/mm/ksm ]; then
2023-11-04 18:46:18 -07:00
logg info 'Adding Netdata kernel optimization for /sys/kernel/mm/ksm/run'
echo 1 | sudo tee /sys/kernel/mm/ksm/run
2023-11-04 18:46:18 -07:00
logg info 'Adding Netdata kernel optimization for /sys/kernel/mm/ksm/sleep_millisecs'
echo 1000 | sudo tee /sys/kernel/mm/ksm/sleep_millisecs
else
2023-11-04 18:46:18 -07:00
logg info 'The /sys/kernel/mm/ksm directory does not exist so Netdata kernel optimizations are not being applied'
fi
fi
2023-05-31 20:10:23 -07:00
### 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
2023-07-31 23:54:04 -07:00
NETDATA_ETC='/etc/netdata'
2023-05-31 20:10:23 -07:00
else
logg error 'No etc location found for netdata' && exit 1
fi
logg info "Copying ${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health.d/ to $NETDATA_ETC"
2023-06-03 22:45:26 -07:00
sudo cp -rf "${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health.d/" "$NETDATA_ETC"
2023-05-31 20:10:23 -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'
else
logg error 'No lib location found for netdata' && exit 1
fi
2023-07-31 20:11:43 -07:00
logg info "Copying ${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health_alarm_notify.conf to $NETDATA_LIB/conf.d/health_alarm_notify.conf"
2023-05-31 20:10:23 -07:00
sudo cp -f "${XDG_CONFIG_HOME:-$HOME/.config}/netdata/health_alarm_notify.conf" "$NETDATA_LIB/conf.d/health_alarm_notify.conf"
else
2023-11-04 18:46:18 -07:00
logg warn 'netdata is not available in the PATH or is not installed'
2023-05-31 20:10:23 -07:00
fi
### Ensure the apt command is available before running `debsecan` logic
2023-06-04 00:31:50 -07:00
if command -v apt-get > /dev/null; then
2023-05-31 20:10:23 -07:00
### Configure Netdata to gather information about CVEs in the installed packages
if command -v debsecan > /dev/null; then
2023-05-31 20:48:22 -07:00
DEBSECAN_GIT="${XDG_DATA_HOME:-$HOME/.local/share}/netdata-debsecan"
2023-05-31 20:10:23 -07:00
# Installing the script to generate report on CVEs in installed packages
2023-07-31 23:54:04 -07:00
logg info 'Installing script to generate report on CVEs in installed packages'
2023-06-03 22:45:26 -07:00
sudo cp -f "$DEBSECAN_GIT/usr_local_bin_debsecan-by-type" "/usr/local/bin/debsecan-by-type"
2023-05-31 20:10:23 -07:00
# Generate initial debsecan reports in /var/log/debsecan/
logg info 'Generating initial debsecan reports in /var/log/debsecan/'
2023-05-31 20:48:22 -07:00
debsecan-by-type
2023-05-31 20:10:23 -07:00
# Configure dpkg to refresh the file after each run
logg info 'Configuring dpkg to refresh the file after each run'
2023-05-31 20:48:22 -07:00
sudo cp -f "$DEBSECAN_GIT/etc_apt_apt.conf.d_99debsecan" /etc/apt/apt.conf.d/99-debsecan
2023-05-31 20:10:23 -07:00
# Add a cron job to refresh the file every hour
logg info 'Adding a cron job to refresh the file every hour'
2023-05-31 20:48:22 -07:00
sudo cp -f "$DEBSECAN_GIT/etc_cron.d_debsecan" /etc/cron.d/debsecan
2023-05-31 20:10:23 -07:00
# Install the module/configuration file
2023-06-03 22:45:26 -07:00
logg info 'Installing the module and configuration file'
2023-05-31 20:48:22 -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
2023-05-31 20:10:23 -07:00
else
2023-11-04 18:46:18 -07:00
logg warn 'apt-get is available but debsecan is not available in the PATH or is not installed'
2023-05-31 20:10:23 -07:00
fi
fi
2023-07-17 23:35:49 -07:00
2023-08-03 23:05:20 -07:00
### 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
logg warn 'speedtest-cli not installed and pipx is not available'
fi
fi
2023-07-31 23:54:04 -07:00
### 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
logg info 'Installing script to generate report on Internet connection speed'
2023-08-18 10:13:05 -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
logg info "$LIBEXEC_PATH/netdata/charts.d present on system"
cp -f "${XDG_DATA_HOME:-$HOME/.local/share}/netdata-speedtest/speedtest.chart.sh" "$LIBEXEC_PATH/netdata/charts.d/speedtest.chart.sh"
else
logg warn "Failed to find appropriate directory to add Netdata speedtest chart script"
fi
2023-07-31 23:54:04 -07:00
else
2023-11-04 18:46:18 -07:00
logg warn 'speedtest-cli is not available in the PATH or is not installed'
2023-07-31 23:54:04 -07:00
fi
2023-07-17 23:35:49 -07:00
ensureNetdataOwnership
### Restart Netdata service
if command -v systemctl > /dev/null; then
logg info 'Enabling netdata service'
sudo systemctl enable netdata
logg info 'Restarting netdata service'
sudo systemctl restart netdata
elif [ -d /Applications ] && [ -d /System ]; then
logg info 'Starting / enabling netdata service'
brew services restart netdata
else
2023-11-04 18:46:18 -07:00
logg warn 'systemctl is not available'
2023-07-17 23:35:49 -07:00
fi
2023-07-31 23:54:04 -07:00
else
2023-11-04 18:46:18 -07:00
logg info 'netdata-claim.sh is not available in the PATH'
fi
{{ end -}}