Zabbix agent deployment and configuration

Installation and configuration of the Zabbix agent, the counterpart of the Zabbix server, that collects and sends data to the server to be shown, is quite easy and can be done with a simple bash script.

#!/bin/bash
# File: client.sh

SERVER=$1
HOSTNAME=$2

# Zabbix version
ZV=5.4

# File name version
FNV=${ZV}-1

if [ -z $HOSTNAME ]
then
        HOSTNAME=$(cat /etc/hostname)
fi

if [ -z $SERVER ]
then
        SERVER=zabbix.example.com
fi

wget https://repo.zabbix.com/zabbix/${ZV}/ubuntu/pool/main/z/zabbix-release/zabbix-release_${FNV}+ubuntu20.04_all.deb
dpkg -i zabbix-release_${FNV}+ubuntu20.04_all.deb
apt update
apt install -y zabbix-agent

sed -i "s/^Server=127\.0\.0\.1$/Server=${SERVER}/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/^Hostname=Zabbix server$/Hostname=${HOSTNAME}/g" /etc/zabbix/zabbix_agentd.conf

systemctl restart zabbix-agent.service

Now let’s imagine you have 1000 hosts and would have to do that on each and every one of them. That’s going to be a long day of work.

My assumption here is, that you as the administrator have ssh root access to all these machines via a ssh public key and each host has a individual hostname (For example: vm1, vm2, vm3, …, vmN or GUIDs).

If this is given the shown script can be transferred to the host via “scp” and executed via “ssh”. I will show this with 4 hosts but with a little bash magic this can be extended to a lot more.

#!/bin/bash
# File: server.sh

SERVER=zabbix.example.com

for HOST in vm1 vm2 vm3 vm4
do
        scp client.sh root@$host:/root/client.sh
        ssh root@$host "chmod u+x ./client.sh; ./client.sh $SERVER $HOST; rm ./client.sh;"
done

In this script the server securely copies the file “client.sh” shown above to the hosts one by one and executes it to install and configure Zabbix agent. The agent is restarted afterwards and can be added to the Zabbix server. I am planing to write another tutorial on how to do this via API. This can then be integrated into the server script.

The client script can also be used to add the Zabbix agent manually. Just check the current version and modify the script if necessary.

As always: I hope this saves somebody some time and makes administration a little bit easier.