Files
homelab-scripts/zabbix_agent.sh

177 lines
7.7 KiB
Bash

#!/bin/bash
## Windows
# Command to run for PSK ID and PSK with powershell
# PSK ID: (1..16 | %{ '{0:x}' -f (Get-Random -Max 16) }) -join ''
# PSK: (1..64 | %{ '{0:x}' -f (Get-Random -Max 16) }) -join ''
# installs the zabbix agent and configures it
# make sure we are running as sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# get OS ditro and version
OS=$(hostnamectl | awk '/Operating System/ {print $3}')
VM=$(hostnamectl | awk '/Virtualization/ {print $2}')
if [ ${OS} == 'Ubuntu' ]; then
OS_VERSION=$(hostnamectl | awk '/Operating System/ {print $4}')
else
OS_VERSION=$(hostnamectl | awk '/Operating System/ {print $5}')
fi
if [ "${VM}" == "vmware" ] || [ "${VM}" == "kvm" ] || [ "${VM}" == "lxc" ]; then
VM="true"
else
VM="false"
fi
# zabbix server
ZABBIX_SERVER="10.0.10.16"
if [ ${OS} == 'Debian' ]; then
# Install the repository configuration package. This package contains apt (software package manager) configuration files.
if [[ ${OS_VERSION} =~ ^9.* ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/debian/pool/main/z/zabbix-release/zabbix-release_6.0-4+debian9_all.deb
dpkg -i /tmp/zabbix-release_6.0-4+debian9_all.deb
fi
if [[ ${OS_VERSION} =~ ^10.* ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/debian/pool/main/z/zabbix-release/zabbix-release_6.0-4+debian10_all.deb
dpkg -i /tmp/zabbix-release_6.0-4+debian10_all.deb
fi
if [[ ${OS_VERSION} =~ ^11.* ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/debian/pool/main/z/zabbix-release/zabbix-release_6.0-4+debian11_all.deb
dpkg -i /tmp/zabbix-release_6.0-4+debian11_all.deb
fi
if [[ ${OS_VERSION} =~ ^12.* ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/debian/pool/main/z/zabbix-release/zabbix-release_6.0-5+debian12_all.deb
dpkg -i /tmp/zabbix-release_6.0-5+debian12_all.deb
fi
# create missing directory
mkdir /etc/zabbix/zabbix_agentd.conf.d
# update the cache
apt update
# install the agent
apt install -y zabbix-agent
# make a dir
mkdir /etc/zabbix/zabbix_agentd.d
elif [ ${OS} == 'Ubuntu' ]; then
# Install the repository configuration package. This package contains apt (software package manager) configuration files.
if [[ ${OS_VERSION} =~ 20.04 ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb
dpkg -i /tmp/zabbix-release_6.0-4+ubuntu20.04_all.deb
fi
if [[ ${OS_VERSION} =~ 22.04 ]]; then
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu22.04_all.deb
dpkg -i /tmp/zabbix-release_6.0-4+ubuntu22.04_all.deb
fi
# update the cache
apt update
# install the agent
apt install -y zabbix-agent
elif [ ${OS} == 'CentOS' ] || [ "${OS}" == "Rocky" ]; then
# Install the repository configuration package. This package contains yum (software package manager) configuration files.
if [ ${OS_VERSION} == 7 ]; then
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
yum clean all
yum install -y zabbix-agent
fi
if [ ${OS_VERSION} == 8 ]; then
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
dnf clean all
dnf install -y zabbix-agent
fi
if [ "${OS_VERSION}" == "9.0" ]; then
# zabbix is also supplied via the EPEL Repo, we need to exlude it from there
if [ $(grep -c "^excludepkgs=" /etc/yum.repos.d/epel.repo) -eq 1 ]; then
sed --in-place '/^excludepkgs=/ s/$/ zabbix*/' /etc/yum.repos.d/epel.repo
else
sed --in-place '/^enabled=1/ s/$/\nexcludepkgs=zabbix*/' /etc/yum.repos.d/epel.repo
fi
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm
dnf clean all
dnf install -y zabbix-agent
fi
cat << EOF >> /etc/firewalld/services/zabbix-agent.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>zabbix agent</short>
<description>zabbix agent</description>
<port protocol="tcp" port="10050"/>
</service>
EOF
sleep 5
firewall-cmd --add-service=zabbix-agent --permanent
firewall-cmd --reload
else
echo "You are running an unsupported OS"
echo "Support OSes: Debian 9/10/11, Ubuntu 20.04/22.04, CentOS 7/8, Rocky 9 with EPEL"
echo "Your OS: ${OS} ${OS_VERSION}"
exit
fi
# test if not a VM and if so, install the smartctl stuff
if [ "${VM}" == "false" ]; then
# download the sudo file
wget -O /etc/sudoers.d/sudoers_zabbix_smartctl https://raw.githubusercontent.com/v-zhuravlev/zbx-smartctl/master/sudoers_zabbix_smartctl
chmod 440 /etc/sudoers.d/sudoers_zabbix_smartctl
# make the directories
mkdir /etc/zabbix/zabbix_agentd.d /etc/zabbix/scripts
# download the conf and discovery scripts
wget -O /etc/zabbix/zabbix_agentd.d/zabbix_smartctl.conf https://raw.githubusercontent.com/v-zhuravlev/zbx-smartctl/master/zabbix_smartctl.conf
wget -O /etc/zabbix/scripts/smartctl-disks-discovery.pl https://raw.githubusercontent.com/v-zhuravlev/zbx-smartctl/master/discovery-scripts/nix/smartctl-disks-discovery.pl
chown zabbix:zabbix /etc/zabbix/scripts/smartctl-disks-discovery.pl
chmod u+x /etc/zabbix/scripts/smartctl-disks-discovery.pl
fi
#cheeck before running this as it adds lines to keep comments
if [ $(grep -c "^Server=127.0.0.1" /etc/zabbix/zabbix_agentd.conf) -eq 1 ]; then
# ask for the hostname
read -e -p "Enter Hostname: " -i "$(cat /etc/hostname)" hostname
# set the include to add the smartctl config directory
# sed --in-place $'s/^# Hostname=/# Hostname=\\\nHostname='${hostname}'/' /etc/zabbix/zabbix_agentd.conf
sed --in-place $'s/^Include=\/etc\/zabbix\/zabbix_agentd.conf.d\/\*.conf/Include=\/etc\/zabbix\/zabbix_agentd.conf.d\/\*.conf\\\nInclude=\/etc\/zabbix\/zabbix_agentd.d\/\*.conf/' /etc/zabbix/zabbix_agentd.conf
# configure the hostname, check for older config and newer config
if [ $(grep -c "^Hostname=Zabbix" /etc/zabbix/zabbix_agentd.conf) -eq 1 ]; then
sed --in-place 's/^Hostname=Zabbix\ server/Hostname='${hostname}'/' /etc/zabbix/zabbix_agentd.conf
else
sed --in-place $'s/^# Hostname=/# Hostname=\\\nHostname='${hostname}'/' /etc/zabbix/zabbix_agentd.conf
fi
# configure the zabbix server
sed --in-place 's/^Server=127.0.0.1/Server='"${ZABBIX_SERVER}"'/' /etc/zabbix/zabbix_agentd.conf
# configure the zabbix server
sed --in-place 's/^ServerActive=127.0.0.1/ServerActive='"${ZABBIX_SERVER}"'/' /etc/zabbix/zabbix_agentd.conf
# configure the encryption
sed --in-place $'s/^# TLSAccept=unencrypted/# TLSAccept=unencrypted\\\nTLSAccept=psk/' /etc/zabbix/zabbix_agentd.conf
sed --in-place $'s/^# TLSConnect=unencrypted/# TLSConnect=unencrypted\\\nTLSConnect=psk/' /etc/zabbix/zabbix_agentd.conf
# generate random PSK ID
PSKID=$(openssl rand -hex 8)
sed --in-place $'s/^# TLSPSKIdentity=/# TLSPSKIdentity=\\\nTLSPSKIdentity='${PSKID}'/' /etc/zabbix/zabbix_agentd.conf
# generate psk
TLSPSK=$(openssl rand -hex 32 | tee /etc/zabbix/zabbix_agentd.psk)
sed --in-place $'s/^# TLSPSKFile=/# TLSPSKFile=\\\nTLSPSKFile=\/etc\/zabbix\/zabbix_agentd.psk/' /etc/zabbix/zabbix_agentd.conf
echo "############################################################"
echo "## Encryption Details ##"
echo "## Please enter the PSK ID and PSK into the Zabbix Server ##"
echo "############################################################"
echo "PSK Identity = ${PSKID}"
echo "PSK = ${TLSPSK}"
fi
systemctl restart zabbix-agent
systemctl enable zabbix-agent