脚本如下:
#!/bin/bash
# Function to install Zabbix Agent 2 on RHEL 6
install_zabbix_rhel6() {
# Check if the Zabbix repository is already installed
if rpm -q zabbix-release &>/dev/null; then
echo "Zabbix repository is already installed."
else
# Install Zabbix repository
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/6/x86_64/zabbix-release-6.0-4.el6.noarch.rpm
if [ $? -eq 0 ]; then
echo "Zabbix repository installed successfully."
else
echo "Error installing Zabbix repository. Exiting."
exit 1
fi
fi
# Check if Zabbix Agent 2 is already installed
if rpm -q zabbix-agent2 &>/dev/null; then
echo "Zabbix Agent 2 is already installed."
else
# Install Zabbix Agent 2
yum clean all
yum install zabbix-agent2 zabbix-agent2-plugin-* -y
if [ $? -eq 0 ]; then
echo "Zabbix Agent 2 installed successfully."
else
echo "Error installing Zabbix Agent 2. Exiting."
exit 1
fi
fi
# Modify Zabbix Agent 2 configuration
read -p "Enter the desired hostname for Zabbix Agent: " hostname
sed -i "s/^Server=.*/Server=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^ServerActive=.*/ServerActive=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^Hostname=.*/Hostname=$hostname/" /etc/zabbix/zabbix_agent2.conf
# Enable and start Zabbix Agent 2
service zabbix-agent2 restart
chkconfig --level 35 zabbix-agent2 on
}
# Function to install Zabbix Agent 2 on RHEL 7
install_zabbix_rhel7() {
echo "Installing Zabbix Agent 2 on RHEL 7"
# Check if Zabbix repository is already installed
if rpm -q zabbix-release &>/dev/null; then
echo "Zabbix repository is already installed."
else
# Install Zabbix repository
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
if [ $? -eq 0 ]; then
echo "Zabbix repository installed successfully."
else
echo "Error installing Zabbix repository. Exiting."
exit 1
fi
fi
# Check if Zabbix Agent 2 is already installed
if rpm -q zabbix-agent2 &>/dev/null; then
echo "Zabbix Agent 2 is already installed."
else
# Install Zabbix Agent 2
yum clean all
yum install zabbix-agent2 zabbix-agent2-plugin-* -y
if [ $? -eq 0 ]; then
echo "Zabbix Agent 2 installed successfully."
else
echo "Error installing Zabbix Agent 2. Exiting."
exit 1
fi
fi
# Modify Zabbix Agent 2 configuration
read -p "Enter the desired hostname for Zabbix Agent: " hostname
sed -i "s/^Server=.*/Server=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^ServerActive=.*/ServerActive=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^Hostname=.*/Hostname=$hostname/" /etc/zabbix/zabbix_agent2.conf
# Enable and start Zabbix Agent 2
systemctl enable zabbix-agent2
systemctl start zabbix-agent2
}
# Function to install Zabbix Agent 2 on RHEL 8
install_zabbix_rhel8() {
echo "Installing Zabbix Agent 2 on RHEL 8"
# Check if the Zabbix repository is already installed
if rpm -q zabbix-release &>/dev/null; then
echo "Zabbix repository is already installed."
else
# Install Zabbix repository
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
if [ $? -eq 0 ]; then
echo "Zabbix repository installed successfully."
else
echo "Error installing Zabbix repository. Exiting."
exit 1
fi
fi
# Check if Zabbix Agent 2 is already installed
if rpm -q zabbix-agent2 &>/dev/null; then
echo "Zabbix Agent 2 is already installed."
else
# Install Zabbix Agent 2
dnf clean all
dnf install zabbix-agent2 zabbix-agent2-plugin-* -y
if [ $? -eq 0 ]; then
echo "Zabbix Agent 2 installed successfully."
else
echo "Error installing Zabbix Agent 2. Exiting."
exit 1
fi
fi
# Modify Zabbix Agent 2 configuration
read -p "Enter the desired hostname for Zabbix Agent: " hostname
sed -i "s/^Server=.*/Server=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^ServerActive=.*/ServerActive=10.99.50.112/" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^Hostname=.*/Hostname=$hostname/" /etc/zabbix/zabbix_agent2.conf
# Enable and start Zabbix Agent 2
systemctl enable zabbix-agent2
systemctl start zabbix-agent2
}
# Install Zabbix repository based on the selected OS version
echo "Select the operating system version:"
options=("RHEL 6" "RHEL 7" "RHEL 8" "Quit")
select opt in "${options[@]}"; do
case "$opt" in
"RHEL 6")
install_zabbix_rhel6
break
;;
"RHEL 7")
install_zabbix_rhel7
break
;;
"RHEL 8")
install_zabbix_rhel8
break
;;
"Quit")
echo "Exiting."
exit 0
;;
*)
echo "Invalid option. Please select a valid option."
;;
esac
done
echo "Zabbix Agent 2 installation and configuration complete."