执行方式:sh scripts.sh hostname="TEST01" ip="198.145.0.122" check="failed/true"
脚本参数详解:
hostname="" ---执行主机的主机名
ip="" ---执行主机的IP地址
check="" ---failed(执行)true(查看)
脚本内容:
#!/bin/sh
LANG=c;export LANG
kernel=`uname -a`
nowdate=`date+%Y%m%d%H%M`
logfile=/tmp/zbxrestart.${nowdate}.logfile
stop=restart
hostname=""
ip=""
check=""
scripts=$0
##########################Scripts location paramter value check
while [[ $# -ne 0 ]];
do
opt_name=$(echo $1 |awk -F= '{print $1}')
opt_value=$(echo $1 |awk -F= '{print $2}')
case $opt_name in
hostname)
if [[ -n $opt_value ]];then
hostname=$opt_value
echo "opt_name=$opt_name opt_value=$opt_value"
[[ $opt_name !=$(hostname) ]] && echo "hostname-check-failure" && exit 1|| echo "hostname-check-success"
else
echo "$opt_name cat not be null"
exit 1
fi
;;
ip)
if [[ -n $opt_value ]];then
ip=$opt_name
echo "opt_name=$opt_name opt_value=$opt_value"
else
echo "$opt_name cat not be null"
exit 1
fi
;;
exech)
if [[ -n $opt_value ]];then
mmchecksh=$opt_value
echo "opt_name=$opt_name opt_value=$opt_value"
else
echo "$opt_name cat not be null"
exit 1
fi
;;
appname)
if [[ -n $opt_value ]];then
appname=$opt_value
echo "opt_name=$opt_name opt_value=$opt_value"
else
echo "$opt_name cat not be null"
exit 1
fi
;;
action)
if [[ -n $opt_value ]];then
action=$opt_value
echo "opt_name=$opt_name opt_value=$opt_value"
else
echo "$opt_name cat not be null"
exit 1
fi
echo "opt_name=$opt_name opt_value=$opt_value"
;;
check)
[[ $opt_name != true ]] && [[ $opt_value != false ]] && ech0 "echo value is true or false!!!" && exit 1
echo "opt_name=$opt_name opt_value=$opt_value"
check=$opt_value
;;
*)
echo "ERROR: you provide a non-exist paramter, paramter name is $opt_name,value is $opt_value!!!" && exit 1
;;
esac
shift
done
[[ $check != true ]] && [[ $check != false ]] && echo "check value is true or false!!!" && exit 1
exelog="/tmp/cmbc_app_${action}_${hostname}_${appname}.log"
########################################
if [ "$check" == "true" ];then
cat #scripts
exit
elif [ "$check" == "false" ];then
userchk=`whoami`
fi
ZabbixId=`cat /etc/passwd |grep zabbix |wc -l`
if [ "$ZabbixId" -eq 1];then
echo "Current user is zabbix" >>$logfile
else
echo "Error:Current user is not zabbix" >>$logfile
echo "zabbix-restart-failure:Error Current user is not zabbix"
exit
fi
ZabbixHost=`cat /etc/passwd |grep zabbix |awk =F":" '{print $6}'`
cd $ZabbixHost/zabbix-agent/ 2>/dev/null
if [ $? -eq 0 ];then
echo "Zabbix Agent is exist" >>$logfile
if [ "$kernel" == "Linux" -o "$kernel" == "AIX" ];then
### zabbix agent stop ###
zabbix_agentdID=`ps -ef |grep zabbix_agentd |grep -v grep |grep -v tail |awk '{print $2}'`
if [ -n "$zabbix_agentdID" ];then
su - zabbix -c "sh $ZabbixHost/zabbix-agent/zabbix-agentd stop"
sleep 10
zabbix_agentID1=`ps -ef |grep zabbix_agentd |grep -v grep |grep -v tail |awk '{print $2}'`
if [ -n "$zabbix_agentID1" ];then
ps -ef |grep zabbix_agentd |grep -v grep |awk '{print $2}' |xargs kill -9
echo "stop zabbix agent failure" >>logfile
echo "zabbix-restart-failure"
exit
else
echo "1,stop zabbix agent success" >>$logfile
fi
else
echo "zabbix agent already stop" >>logfile
echo "zabbix agent already stop"
fi
### start zabbix agent ###
su - zabbix -c "sh $ZabbixHost/zabbix-agent/zabbix-agentd start"
zabbix_agentID=`ps -ef |grep zabbix_agentd |grep -v grep |grep -v tail |awk '{print $2}'`
if [ -z $zabbix_agentID ];then
echo "Error!,start zbbix agentfailed" >>logfile
echo "zabbix-restart-failure"
exit
else
echo "2,restart zabbix agent success" >>$logfile
echo "zabbix-restart-success"
ps -ef |grep zabbix_agent |grep -v grep
fi
else
echo "Error:System is not linux" >>logfile
echo "zabbix-restart-failure"
exit
fi
else
echo "Error:zabbix agent is not exist" >>$logfile
echo "zabbix-restart-failure"
exit
fi