Created
August 12, 2022 07:17
-
-
Save Benjamin1021523/a21bdc8064e60748824bbf54e4c898a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function checkRoot { | |
if [ "$EUID" -ne 0 ] | |
then echo 請使用root帳號,或sudo指令執行 | |
return 1 | |
fi | |
return 0 | |
} | |
checkRoot || exit | |
config_file=/etc/zabbix/zabbix_agentd.conf | |
if [ -f $config_file ] | |
then | |
old_hostname=`egrep '^Hostname=(.*)' /etc/zabbix/zabbix_agentd.conf | sed 's/^Hostname=\(.*\)/\1/g'` | |
old_server=`egrep '^Server=(.*)' /etc/zabbix/zabbix_agentd.conf | sed 's/^Server=\(.*\)/\1/g'` | |
echo "原有監控名稱為$old_hostname,監控端主機為$old_server" | |
else | |
echo '原本未監控,請先安裝監控套件zabbix,使用指令"yum install -y zabbix-agent"' | |
exit | |
fi | |
new_hostname= | |
new_server= | |
read -p "請輸入新的監控名稱,或者不修改維持現狀( $old_hostname ): " new_hostname | |
read -p "請輸入新的監控主機IP,或者不修改維持現狀( $old_server ): " new_server | |
if [ ! -z "$new_server" ] | |
then | |
old_server=$new_server | |
fi | |
function validIp { | |
IFS='.' | |
ip=$1 | |
cnt=0 | |
for i in $ip | |
do | |
test $i -le 255 2> /dev/null || return 1 | |
((cnt++)) | |
done | |
return `test $cnt -eq 4` | |
} | |
test ! -z "$new_server" && (validIp "$new_server" || (echo "不合要求的ip" ; exit 1)) | |
test ! -z "$new_hostname" && sed -i "s/^Hostname=\(.*\)/Hostname=$new_hostname/g" $config_file | |
test ! -z "$new_server" && sed -i "s/^Server=\(.*\)/Server=$new_server/g" $config_file && sed -i "s/^ServerActive=\(.*\)/ServerActive=$new_server/g" $config_file | |
read -p "更新完成,是否立刻重啟zabbix服務? (y/n)" restart | |
test "$restart" == 'y' && systemctl restart zabbix-agent && echo 重啟完成 | |
test "$restart" == 'y' && systemctl status --no-pager zabbix-agent | |
sleep 1 | |
echo "執行完畢,請繼續至監控端主機( $old_server )完成監控設定。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment