自动化安装zabbix-agent的脚本

其实,说句心里话,蛮不喜欢写这个脚本的,为啥?

因为太简单了,无非就是一条命令能解决的问题,那为啥还要写这个脚本呢?

无奈,这社会总是存在一些逼着你进步的人!

刚入软件行业的时候,总有一些人给你提一些你没做过的需求,总觉得有人在刁难你。然而,当你实现这个需求被人夸你很屌的时候,你心里总会默默的感谢“这些”人,是他们给了你进步的机会。

服务器操作系统多样化,那安装zabbix就很麻烦需要找对应的版本,于是,懒人的脚本就此诞生了。

以下就是见证奇迹的时刻

#!/bin/bash
# auth by huwj
# date:2019-12
# auto install zabbix-agent

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
export PATH

# 获取操作系统位数
sys_num=`getconf LONG_BIT`
# zabbix服务端IP
ServerIP='192.168.142.10'
# 获取客户端服务器的IP
AgentIP=`ip a show dev ens33|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`
# 配置文件
zabbix_config='/etc/zabbix/zabbix_agentd.conf'
# 获取数据
case $1 in 
    Centos6)
        checkAgent=`rpm -qa | grep 'zabbix-agent'`
        if [ -z $checkAgent ];then
         if [ $sys_num -eq 64 ];then
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/6/x86_64/zabbix-agent-4.2.6-1.el6.x86_64.rpm 
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             service zabbix-agent restart
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         else
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/6/i386/zabbix-agent-4.2.6-1.el6.i686.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             service zabbix-agent restart
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         fi
        else
          echo "Zabbix-agent has been installed in this system !"
        fi 
        ;; 
    Centos7)
        checkAgent=`rpm -qa | grep 'zabbix-agent'`
        if [ -z $checkAgent ];then
         if [ $sys_num -eq 64 ];then
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.6-1.el7.x86_64.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             systemctl restart zabbix-agent
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi           
         else
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.6-1.el7.x86_64.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         fi
        else
          echo "Zabbix-agent has been installed in this system !"
        fi
        ;;
    RedHat6)
        checkAgent=`rpm -qa | grep 'zabbix-agent'`
        if [ -z $checkAgent ];then
         if [ $sys_num -eq 64 ];then
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/6/x86_64/zabbix-agent-4.2.6-1.el6.x86_64.rpm 
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             service zabbix-agent restart
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         else
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/6/i386/zabbix-agent-4.2.6-1.el6.i686.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             service zabbix-agent restart
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         fi
        else
          echo "Zabbix-agent has been installed in this system !"
        fi 
        ;; 
    RedHat7)
        checkAgent=`rpm -qa | grep 'zabbix-agent'`
        if [ -z $checkAgent ];then
         if [ $sys_num -eq 64 ];then
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.6-1.el7.x86_64.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             systemctl restart zabbix-agent
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi           
         else
           rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.6-1.el7.x86_64.rpm
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi
         fi
        else
          echo "Zabbix-agent has been installed in this system !"
        fi
        ;;
    Ubuntu)
        checkAgent=`dpkg -l | grep zabbix-agent`
        if [ -z $checkAgent ];then
           apt-get install zabbix-agent
           if [ -z $checkAgent ];then
             sed -i "s/Server=127.0.0.1/Server=$ServerIP/" $zabbix_config
             sed -i "s/ServerActive=127.0.0.1/ServerActive=$ServerIP/" $zabbix_config
             sed -i "s/Hostname=Zabbix server/Hostname=$AgentIP/" $zabbix_config
             service zabbix-agent restart
           else
             echo 'Zabbix-agent install failed ! please run again .'
           fi           
        else
          echo "Zabbix-agent has been installed in this system !"
        fi
        ;;
        *) 
        echo "Usage:$0(Centos6|Centos7|Ubuntu|RedHat6|RedHat7)" 
        ;; 
esac

该脚本支持 Centos6|Centos7|Ubuntu|RedHat6|RedHat7 安装

脚本中还存在较多冗余的脚本,后期再优化下。

 该脚本上传服务器后,授予执行权限:chmod +x installZabbix.sh 然后脚本后面需输入对应操作系统版本,如Ubuntu安装 ,需执行命令: ./installZabbix.sh Ubuntu

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hu_wenjie

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值