1. 前期准备
-
离线安装包
-
ubuntu deb包
-
centos rpm包
-
-
samba配置
- 共享windows文件夹,可挂载
-
配置文件
- 提前配置代理指向位置,避免出错
2. 脚本
#!/bin/bash
# 00.vars
mount_share_dir=“挂载路径”
mount_dir=/mnt
agent_dir=$mount_dir/package/
conf_dir=$mount_dir/conf
# 01.mount
mount $mount_share_dir $mount_dir
# 02.select proxy
cat <<EOF
请选择您需要配置的指向代理:01 - ***02 - ***03 - ***04 - ***
EOF
read -p "请选择代理,输入数字即可:" num
# 03.chk num
if ! [[ $num =~ ^[0-9]+$ ]];thenecho "Usage: 只输入数字即可01,02,03..."
elif [ $num -ge 05 ];thenecho "请输入正确数字"
fi
# 04.Judgement the file of os-release exist?
exist_file(){
if [ -f /etc/os-release ];then. /etc/os-release
elseecho "该系统版本不适用于此脚本,请检查系统版本再选择合适的脚本"exit
fi
}
# 05. Judgement hostname and hosts
judge_name(){
ip=$(hostname -I|awk '{print $1}')
hostname=$(hostname)
hosts_ubt_db=$(grep 127.0.1.1 /etc/hosts |awk '{print $2}')
hosts_centos_su=$(grep $ip /etc/hosts |awk '{print $2}')
case $ID inubuntu|debian)if [ -z "${hosts_ubt_db}" ];thenecho "$ip $hostname" >>/etc/hostselif [ "${hosts_ubt_db}" != "${hostname}" ];thensed -ir "s#${ip}.*#$ip $hostname#g" /etc/hostsfi;;suse|centos|rocky|rhel)if [ -z "${hosts_centos_su}" ];thenecho "$ip $hostname" >>/etc/hostselif [ "${hosts_centos_su}" != "${hostname}" ];thensed -ir "s#${ip}.*#$ip $hostname#g" /etc/hostsfi;;*)exit
esac
}
# 06.根据系统划分
install_agent(){
case $ID inubuntu|debian)case $VERSION_ID in18.04)dpkg -i ${agent_dir}*$VERSION_ID* ;;20.04)dpkg -i ${agent_dir}*$VERSION_ID* ;;22.04)dpkg -i ${agent_dir}*$VERSION_ID* ;;24.04)dpkg -i ${agent_dir}*$VERSION_ID* ;;*)echo "该系统版本不适用于此脚本,请检查系统版本再选择合适的脚本"esac;;suse|centos|rocky|rhel)case $VERSION_ID in7|7.*)rpm -i ${agent_dir}*el7* ;;8|8.*)rpm -i ${agent_dir}*el8* ;;9|9.*)rpm -i ${agent_dir}*el9* ;;*)echo "该系统版本不适用于此脚本,请检查系统版本再选择合适的脚本"esac;;*)echo "请联系管理员添加该系统"
esac
}
exist_file
judge_name
install_agent
# 07. cp conf 拷贝配置文件
if [ $num -eq 01 ];thencp $conf_dir/zabbix_agent2.conf /etc/zabbix/
elif [ $num -eq 02 ];thencp $conf_dir/zabbix_agent2.conf /etc/zabbix/
fi
# 08. Determine if the installation was successful?
if [ $? -eq 0 ];thenecho ok
fi
# 09. restart service
systemctl enable --now zabbix-agent2
systemctl restart zabbix-agent2