Worklet: Install Zabbix Agent on Linux

  • 22 October 2019
  • 0 replies
  • 151 views

Userlevel 2

Checks to see if Zabbix is installed and if not, installs it. Covers Centos and Ubuntu, but can be extended for other Linux flavors fairly easily.


Evaluation code:


#!/bin/bash
service=zabbix_agent

if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
exit 0
else
exit 1
fi

Remediation code:


#!/bin/bash

#Get OS
os_id="$(grep -w ID /etc/os-release | cut -d'=' -f2)"

#Functions
centos () {
if [ $os_ver == '8.0' ]; then
#Remove all dirty packages
rpm -e zabbix-release-*

#Get the Package
rpm -Uvh http://repo.zabbix.com/zabbix/4.0/rhel/8/x86_64/zabbix-release-4.0-2.el8.noarch.rpm

#Clean and Install with Yum
yum clean all
yum -y install zabbix-agent

#Overwrite the conf file with attached
cp zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf

#Edit config to personalize for new system
CONFIGPATH="/etc/zabbix/zabbix_agentd.conf"
TARGET_KEY="Hostname"
REPLACEMENT_VALUE="$(hostname)"
sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIGPATH

#Enable Zabbix as Service (Automatic Startup at Boot)
systemctl enable zabbix-agent

#Start the agent
systemctl start zabbix-agent

fi

if [ $os_ver == '7' ]; then
#Remove all dirty packages
rpm -e zabbix-release-*

#Get the Package
rpm -Uvh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

#Clean and install with Yum
yum clean all
yum -y install zabbix-agent

#Overwrite the conf file with attached
cp zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf

#Edit config to personalize for new system
CONFIGPATH="/etc/zabbix/zabbix_agentd.conf"
TARGET_KEY="Hostname"
REPLACEMENT_VALUE="$(hostname)"
sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIGPATH

#Enable Zabbix as Service (Automatic Startup at Boot)
systemctl enable zabbix-agent

#Start the agent
systemctl start zabbix-agent

fi

if [ $os_ver == '6' ]; then
#Remove all dirty packages
rpm -e zabbix-release-*

#Get the Package
wget https://repo.zabbix.com/zabbix/4.0/rhel/6/x86_64/zabbix-release-4.0-2.el6.noarch.rpm
rpm -Uvh zabbix-release-4.0-2.el6.noarch.rpm

#Clean and install with Yum
yum clean all
yum -y install zabbix-agent

#Overwrite the conf file with attached
cp zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf

#Edit config to personalize for new system
CONFIGPATH="/etc/zabbix/zabbix_agentd.conf"
TARGET_KEY="Hostname"
REPLACEMENT_VALUE="$(hostname)"
sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIGPATH

#Enable Zabbix as Service (Automatic Startup at Boot)
service zabbix-agent enable

#Start the agent
service zabbix-agent start

fi
}

ubuntu () {
#Get the Package
#wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
#dpkg -i zabbix-release_4.0-3+bionic_all.deb

#Install with apt
apt-get update
apt-get -y install zabbix-agent

#Overwrite the conf file with attached
cp zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf

#Edit config to personalize for new systemctl
CONFIGPATH="/etc/zabbix/zabbix_agentd.conf"
TARGET_KEY="Hostname"
REPLACEMENT_VALUE="$(hostname)"
sed -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIGPATH

#Restart Zabbix Agent
service zabbix-agent restart
}

#Find OS and Forward to Appropriate Function
if (os_id == "ubuntu")
then
ubuntu
else
os_ver="$(rpm -q centos-release | cut -d'-' -f3)"
centos
fi

exit 0

Note for the Ubuntu section, the wget is commented out. If the Zabbix package isn’t already in your Ubuntu install, then uncomment that line.


0 replies

Be the first to reply!

Reply