#!/bin/bash

## System Information
os="`cat /etc/system-release`"
osvercheck="`uname -r | cut -b 1`"
if [ $osvercheck -eq 5 ] ; then
        osver=9
elif [ $osvercheck -eq 4 ] ; then
        osver=8
elif [ $osvercheck -eq 3 ] ; then
        osver=7
elif [ $osvercheck -eq 2 ] ; then
        osver=6
else
        echo " Server OS kernel version is not certificated! "
        exit
fi

SERVERIP=""

function os9 {
	rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rocky/9/x86_64/zabbix-release-latest-7.0.el9.noarch.rpm &&
	dnf clean all &&
	dnf install zabbix-agent -y &&
	cp -rp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf_org &&
	echo "PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=$SERVERIP
ListenPort=10060
ListenIP=$(hostname -I | awk '{print $1}')
#ServerActive=$SERVERIP
Hostname=$HOSTNAME
Include=/etc/zabbix/zabbix_agentd.d/*.conf" > /etc/zabbix/zabbix_agentd.conf &&
	systemctl enable --now zabbix-agent &&
	systemctl status zabbix-agent
}

function os8 {
	rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rocky/8/x86_64/zabbix-release-7.0-2.el8.noarch.rpm &&
	dnf clean all &&
	dnf install zabbix-agent -y &&
	cp -rp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf_org &&
	echo "PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=$SERVERIP
ListenPort=10060
ListenIP=$(hostname -I | awk '{print $1}')
#ServerActive=$SERVERIP
Hostname=$HOSTNAME
Include=/etc/zabbix/zabbix_agentd.d/*.conf" > /etc/zabbix/zabbix_agentd.conf &&
	systemctl enable --now zabbix-agent &&
	systemctl status zabbix-agent
}

function os7 {
	rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/7/x86_64/zabbix-release-7.0-1.el7.noarch.rpm &&
	yum clean all &&
	yum install zabbix-agent -y &&
	cp -rp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf_org &&
	echo "PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=$SERVERIP
ListenPort=10060
ListenIP=$(hostname -I | awk '{print $1}')
#ServerActive=$SERVERIP
Hostname=$HOSTNAME
Include=/etc/zabbix/zabbix_agentd.d/*.conf" > /etc/zabbix/zabbix_agentd.conf &&
	systemctl enable --now zabbix-agent &&
	systemctl status zabbix-agent
}

function os6 {
	rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/6/x86_64/zabbix-release-7.0-1.el6.noarch.rpm &&
	yum clean all &&
	yum install zabbix-agent -y &&
	cp -rp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf_org &&
	echo "PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=$SERVERIP
ListenPort=10060
ListenIP=$(hostname -I | awk '{print $1}')
#ServerActive=$SERVERIP
Hostname=$HOSTNAME
Include=/etc/zabbix/zabbix_agentd.d/*.conf" > /etc/zabbix/zabbix_agentd.conf &&
	service zabbix-agent restart &&
	chkconfig --level 35 zabbix-agent on &&
	service zabbix-agent status
}


case $osver  in
9) os9 ;;
8) os8 ;;
7) os7 ;;
6) os6 ;;
esac
