Linux下检查并自动安装应用脚本之函数模式(Zabbix示例)

之前写过一篇关于批量检查并部署应用的脚本,也是以部署zabbix为例,但这篇升级为函数模式,应用安装方法也更改为yum安装,较之前那篇晋商,结构更为清晰,且更易调整。

#!/bin/bash

#--- 定义变量,请根据实际需求自行增删改---#

installlogs="/opt/installlogs.txt"
hostipgrep="10.10|172.16"
dependson=""
softname="zabbix-agent|zabbix_agent|zabbix_agentd"
softrunport="10500"
softnewcfg1="10.10.100.100,10.10.200.200"
softnewcfg2="$HOSTNAME"

#--- 定义函数,请根据实际需求自行增删改---#

# Check application status #
softstatus () {
softchkmath="`ss -anp | egrep $softrunport`"
}

# Check soft install path #
softchkinspath () {
softinspath=`ps aux | awk -v s=$softname '$11~s{print $11}' | head -n1`
}

# Check config file path #
softchkcfgfile () {
softcfgfile="/etc/zabbix/zabbix_agentd.conf"
softcfgfile1="/opt/zabbix/zabbix/etc/zabbix_agentd.conf"
softcfgfile2="/usr/local/etc/zabbix_agentd.conf"
if [ -e "$softcfgfile" ];then
return 0
fi
if [ -e "$softcfgfile1" ];then
softcfgfile=$softcfgfile1
return 0
fi
if [ -e "$softcfgfile2" ];then
softcfgfile="$softcfgfile2"
return 0
fi
softcfgfile=""
echo "WARNING! No config file found, please check it yourself" >> $installlogs
}

# Check soft install time #
softchkinstime () {
softinstime=`stat $softinspath | awk -F[::.] '/^Modify|最近更改/{print $2":"$3":"$4}' | sed 's/^ *//g'`
}

# Check old configuration #
softchkcfg () {
if [ "$softcfgfile" == "" ];then
return 0
fi
softoldcfg1=`cat $softcfgfile | awk -F= '/^Server=/{print $2}'`
softoldcfg2=`cat $softcfgfile | awk -F= '/^Hostname=/{print $2}'`
if [ "$softoldcfg1" == "$softnewcfg1" ];then
if [ "$softoldcfg2" == "$softnewcfg2" ];then
echo -e "All config are the lastest version, don't need to reinstall, exit now\n" >> $installlogs
exit 0
fi
sed -i "/^Hostname=/s/$softoldcfg2/$softnewcfg2/g" $softcfgfile
echo -e "Only modified $softnewcfg2 to the lastest version, don't need to reinstall, exit now\n" >> $installlogs
exit 0
fi
}

# Remove and clean old installation #
softremove (){
#stop application
ps -ef | egrep "$softname" | awk '{print $2}' | xargs kill -9
rpm -qa ${softname%%|*} | xargs rpm -e
softstatus
if [ -n "$softchkmath" ];then
#echo -e "ERROR! Can't stop ${softname%%|*}, exit now\n" >> $installlogs
#exit 1
echo -e "WARNING! Can't stop ${softname%%|*}, force to reinstall" >> $installlogs
else
echo "Stopped ${softname%%|*}" >> $installlogs
fi
#Backup config file
if [ -n "$softcfgfile" ];then
if [ -n "$softcfgfile.old" ];then
rm -f $softcfgfile.old
fi
mv $softcfgfile $softcfgfile.old
echo "Old config file backup to $softcfgfile.old" >> $installlogs
fi
#remove old install dir
softinsdir="zabbix"
softinsdirs=`echo $softinspath | awk -F[/] '{for (i=3;i<NF;i++){print $i}}'`
basedir=`echo $softinspath | awk -F[/] '{print $2}'`
cd $basedir
for i in $softinsdirs;do
if [ "$i" == "$softinsdir" ];then
mv $softinsdir $softinsdir-old
echo "Old ${softname%%|*} install dir removed to $softinsdir-old" >> $installlogs
break
fi
cd $i
done
#Delete other excuse file
softexefile=`ls /etc/init.d/ | egrep "$softname"`
if [ -n "$softexefile" ];then
for i in $softexefile; do
rm -f /etc/init.d/$i
echo "Old excuse file /etc/init.d/$i is deleted" >> $installlogs
done
fi
}

# Check depend applications #
softdepson () {
#Check for depends file
#if [ -f "/lib/ld-linux.so.2" ];then
#echo "Do not need to install any relation package" >> $installlogs
#else
#yum -y install $dependson
#if [ -f "/lib/ld-linux.so.2" ];then
#echo "Depend packages install successful" >> $installlogs
#else
#echo -e "ERROR! Package $dependson installed unsuccessful, exit now\n" >> $installlogs
#exit 1
#fi
#fi
#Check for depends rpm
for i in $dependson; do
if [ -z "`rpm -qa $i`" ];then
yum -y install $i
sleep 1
if [ -n "`rpm -qa $i`" ];then
echo "Package $i install successful" >> $installlogs
else
echo -e "ERROR! $i installed unsuccessful, exit now\n" >> $installlogs
exit 1
fi
echo "Packge $i is installed" >> $installlogs
fi
done
echo "All depends package installed" >> $installlogs
}

# Configure yum resource #
softcfgyum (){
if [ -n "`rpm -qa zabbix-release`" ];then
echo "Yum source for ${softname%%|*} allready installed" >> $installlogs
return 0
fi
osversioni=`echo $osversion | sed 's/[^0-9]//g'`
osversioni=${osversioni:0:1}
if [ "$osversioni" -ne 7 ] && [ "$osversioni" -ne 6 ];then
echo -e "ERROR! This OS havn't suitable yum source, exit now\n" >> $installlogs
exit 0
#You can write yum resource by yourself
#echo -e "*****************************" > /etc/yum.repos.d/$softyumsrc
fi
if [ "$osversioni" -eq 7 ];then
rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
else
rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/6/x86_64/zabbix-release-4.0-1.el6.noarch.rpm
fi
if [ -n "/etc/yum.repos.d/zabbix.repo" ];then
echo -e "ERROR! Get yum source unsuccessful, exit now\n" >> $installlogs
exit 0
fi
yum clean all
yum makecache
echo "Install yum source for $osversion successful" >> $installlogs
}

# Begin to install application #
softinstall () {
yum -y install `echo ${softname%%|*}`
cp /usr/sbin/zabbix_agentd /etc/init.d/
echo "Installed ${softname%%|*} successful" >> $installlogs
#Modify config file
softchkcfgfile
sed -i "/^Server=/s/127.0.0.1/$softnewcfg1/g" $softcfgfile
sed -i "/^ServerActive=/s/127.0.0.1/$softnewcfg1/g" $softcfgfile
sed -i "/^Hostname=/s/Zabbix server/$softnewcfg2/g" $softcfgfile
echo "Modify ${softname%%|*} config file successful" >> $installlogs
}

# Start application #
softstart () {
sleep 3
/etc/init.d/zabbix_agentd
softcfgrc=`cat /etc/rc.local | grep "/etc/init.d/zabbix_agentd"`
if [ -z "$softcfgrc" ];then
echo "/etc/init.d/zabbix_agentd" >> /etc/rc.local
echo "Add ${softname%%|*} with OS start successful" >> $installlogs
fi
#systemctl start zabbix_agentd && systemctl enable zabbix_agentd
sleep 3
softstatus
if [ -n "$softchkmath" ];then
echo -e "SUCCESS! ${softname%%|*} start successful" >> $installlogs
else
echo -e "ERROR! ${softname%%|*} start unsuccessful" >> $installlogs
fi
}

# Check system firewall #
softchkfirewall () {
if [ -f "/etc/sysconfig/iptables" ];then
if [ -n "`cat /etc/sysconfig/iptables | egrep $softrunport`" ];then
echo "Iptables now openning port $softrunport" >> $logfile
else
iptables -I INPUT -p tcp -m multiport --dport $softrunport -j ACCEPT
service iptables save
service iptables restart
echo "Iptables open port $softrunport successful" >> $logfile
fi
else
statfwd=`systemctl status firewalld | grep running`
if [ -n "$statfwd" ];then
if [ -n "`firewall-cmd --zone=public --list-ports | egrep $softrunport`" ];then
echo "Firewalld now openning port $softrunport" >> $logfile
else
firewall-cmd --zone=public --add-port=$softrunport/tcp --permanent
firewall-cmd --reload
echo "Firewalld open port $softrunport successful" >> $logfile
fi
else
echo "No firewall is running on this server, do not need to config port" >> $logfile
fi
fi
}

echo ">>>>>>>>>>>>>>>>>>>>>>>>> ${softname%%|*} install logs <<<<<<<<<<<<<<<<<<<<<<<<<" > $installlogs

#获取主机名和IP
echo -e "Server name\t:\t$HOSTNAME" >> $installlogs
for i in `ip a | awk -F'[ /]+' '$0~/inet/{print $3}' | egrep "$hostipgrep"`; do echo -e "Server IP\t:\t$i" >> $installlogs; done
#Show running hostname, used for remote ssh
#echo $HOSTNAME

#获取操作系统信息
osversion=`cat /etc/redhat-release`
linuxcore=`uname -r`
echo -e "OS version\t:\t$osversion\nLinux core\t:\t$linuxcore" >> $installlogs

#检查是否已经安装该应用
softstatus
if [ -n "$softchkmath" ];then
softchkinspath
softchkcfgfile
softchkinstime
echo -e "Inspath old\t:\t$softinspath\nCfgpath old\t:\t$softcfgfile\nInstime old\t:\t$softinstime" >> $installlogs
softchkcfg
softremove
else
echo "This server does't install ${softname%%|*} before, install now" >> $installlogs
fi

#开始执行安装
softdepson
softcfgyum
softinstall
softstart
softchkfirewall
softchkinspath
softchkcfgfile
softchkinstime
echo -e "Inspath new\t:\t$softinspath\nCfgpath new\t:\t$softcfgfile\nInstime new\t:\t$softinstime\n" >> $installlogs
赞 (0) 打赏

评论 0

感谢您的支持与帮助

支付宝扫一扫打赏

微信扫一扫打赏