zabbix-agent 一键安装脚本

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]+$ ]];then
        echo "Usage: 只输入数字即可01,02,03..."
elif [ $num -ge 05 ];then
        echo "请输入正确数字"
fi
# 04.Judgement the file of os-release exist?
exist_file(){
if [ -f /etc/os-release ];then
        .  /etc/os-release
else
        echo "该系统版本不适用于此脚本,请检查系统版本再选择合适的脚本"
        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 in
        ubuntu|debian)
                if [ -z "${hosts_ubt_db}" ];then
                        echo "$ip $hostname"  >>/etc/hosts
                elif [ "${hosts_ubt_db}" != "${hostname}" ];then
                        sed -ir "s#${ip}.*#$ip $hostname#g" /etc/hosts
                fi
                        ;;
        suse|centos|rocky|rhel)
                if [ -z "${hosts_centos_su}" ];then
                        echo "$ip $hostname"  >>/etc/hosts
                elif [ "${hosts_centos_su}" != "${hostname}" ];then
                        sed -ir "s#${ip}.*#$ip $hostname#g" /etc/hosts
                fi
                        ;;
                *)
                        exit
esac
}
# 06.根据系统划分
install_agent(){
case $ID in
        ubuntu|debian)
                case $VERSION_ID in
                        18.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 in
                        7|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 ];then
        cp $conf_dir/zabbix_agent2.conf /etc/zabbix/
elif [ $num -eq 02 ];then
        cp $conf_dir/zabbix_agent2.conf /etc/zabbix/
fi
# 08. Determine if the installation was successful?
if [ $? -eq 0 ];then
        echo ok
fi
# 09. restart service
systemctl enable --now  zabbix-agent2
systemctl  restart zabbix-agent2
posted @ 2025-03-19 20:51  kyle_7Qc  阅读(69)  评论(0)    收藏  举报