Skip to content

Commit

Permalink
sprintfix: 修复多 Agent 场景下 crontab 越界移除的问题 (fixed TencentBlueKing#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Jul 31, 2023
1 parent 39d0805 commit 1259b63
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 114 deletions.
104 changes: 49 additions & 55 deletions script_tools/gsectl/agent/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,24 @@ watch_by_binary () {
# 获取当前时间的分钟数
minute=$(date +%M)

# 如果文件存在,则读取文件中记录的次数
if [ -f $LAST_RUN_FILE ]; then
run_count=$(cat $LAST_RUN_FILE)
else
run_count=0
fi

# 判断是否为整点时间
if [ "$minute" == "00" ]; then
log "The current time is on the hour, reset the counter, and restart the detection."
if [ -f $LAST_RUN_FILE ];then
if [ -f $LAST_RUN_FILE -a $run_count -gt 0 ];then
log "The current time is on the hour, reset the counter $run_count -> 0, and restart the detection."
echo 0 > $LAST_RUN_FILE
fi
fi

# 设置告警阈值
THRESHOLD=5

# 如果文件存在,则读取文件中记录的次数
if [ -f $LAST_RUN_FILE ]; then
run_count=$(cat $LAST_RUN_FILE)
else
run_count=0
fi

# 检查上一次脚本是否存在
if [ -f /var/run/gsectl_check_status.pid ]; then
pid=`cat /var/run/gsectl_check_status.pid`
Expand Down Expand Up @@ -344,6 +344,7 @@ start_by_crontab () {
}

stop_by_crontab () {
remove_crontab
stop_by_binary
return
}
Expand Down Expand Up @@ -502,31 +503,42 @@ get_os_type () {
fi
}

add_startup_to_boot (){
check_rc_file () {
get_os_type
if [ -f $RC_LOCAL_FILE ]; then
return 0
elif [ -f "/etc/rc.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/rc.d/rc.local"
elif [ -f "/etc/init.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/init.d/rc.local"
elif [ -f "/etc/init.d/boot.local" ]; then
RC_LOCAL_FILE="/etc/init.d/boot.local"
else
RC_LOCAL_FILE="`readlink -f /etc/rc.local`"
fi
}

add_startup_to_boot () {

local module=agent

# 添加启动项到 rc.local
echo "Check startup items, and if not existing, add the startup item to rc.local"
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
fi
echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local"

check_rc_file
local rcfile=$RC_LOCAL_FILE

if [ $OS_TYPE == "ubuntu" ]; then
sed -i "\|\#\!/bin/bash|d" $rcfile
sed -i "1i \#\!/bin/bash" $rcfile
fi

chmod +x $rcfile

# 先删后加,避免重复
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile

echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
}

add_config_to_systemd () {
Expand All @@ -541,11 +553,11 @@ After=network-online.target
[Service]
LimitNOFILE=512000
LimitCORE=infinity
WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin
PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid
ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf
ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --reload
ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --quit
WorkingDirectory=${WORK_HOME}/bin
PIDFile=${WORK_HOME}/bin/run/${module}.pid
ExecStart=${WORK_HOME}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf
ExecReload=${WORK_HOME}/bin/gse_agent --reload
ExecStop=${WORK_HOME}/bin/gse_agent --quit
Type=forking
KillMode=process
User=root
Expand Down Expand Up @@ -573,26 +585,8 @@ EOF
fi

# 删除rc.local里的启动项
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local
fi
fi
fi
check_rc_file
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE

# 删除crontab里的watch条目
remove_crontab
Expand Down
104 changes: 48 additions & 56 deletions script_tools/gsectl/proxy/linux/gsectl
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ start_by_crontab () {
}

stop_by_crontab () {

remove_crontab
for module in $modules
do
stop_by_binary ${module}
Expand Down Expand Up @@ -828,31 +828,43 @@ get_os_type () {
fi
}

add_startup_to_boot (){
check_rc_file () {
get_os_type
if [ -f $RC_LOCAL_FILE ]; then
return 0
elif [ -f "/etc/rc.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/rc.d/rc.local"
elif [ -f "/etc/init.d/rc.local" ]; then
RC_LOCAL_FILE="/etc/init.d/rc.local"
elif [ -f "/etc/init.d/boot.local" ]; then
RC_LOCAL_FILE="/etc/init.d/boot.local"
else
RC_LOCAL_FILE="`readlink -f /etc/rc.local`"
fi
}


add_startup_to_boot () {

local module=$1

# 添加启动项到 rc.local
echo "Check startup items, and if not existing, add the startup item to rc.local"
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then
echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE
fi
fi
echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local"

check_rc_file
local rcfile=$RC_LOCAL_FILE

if [ $OS_TYPE == "ubuntu" ]; then
sed -i "\|\#\!/bin/bash|d" $rcfile
sed -i "1i \#\!/bin/bash" $rcfile
fi

chmod +x $rcfile

# 先删后加,避免重复
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile

echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile
}

add_config_to_systemd () {
Expand All @@ -875,11 +887,11 @@ After=network-online.target
[Service]
LimitNOFILE=512000
LimitCORE=infinity
WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin
PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid
ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_${config}.conf
ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --reload
ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --quit
WorkingDirectory=${WORK_HOME}/bin
PIDFile=${WORK_HOME}/bin/run/${module}.pid
ExecStart=${WORK_HOME}/bin/gse_${module} -f ${WORK_HOME}/etc/gse_${config}.conf
ExecReload=${WORK_HOME}/bin/gse_${module} --reload
ExecStop=${WORK_HOME}/bin/gse_${module} --quit
Type=forking
KillMode=process
User=root
Expand Down Expand Up @@ -909,26 +921,8 @@ EOF
[ -f /tmp/${INSTALL_ENV}_${node_type}_${module}.service ] && rm /tmp/${INSTALL_ENV}_${node_type}_${module}.service

# 删除rc.local里的启动项
get_os_type
if [ -f $RC_LOCAL_FILE ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE
fi
else
if [ -f /etc/rc.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local
fi
elif [ -f /etc/init.d/rc.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local
fi
elif [ -f /etc/init.d/boot.local ];then
if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then
sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local
fi
fi
fi
check_rc_file
sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE

# 删除crontab里的定时任务
remove_crontab
Expand All @@ -950,10 +944,6 @@ setup_crontab () {
local module=$1
local tmpcron

if [ "${module}" != "${MODULES[-1]}" ];then
return 0
fi

if [ -n "`crontab -l | grep \"$WORK_HOME/bin/gsectl\"`" ];then
echo "The watch detection entry is already in the crontab..."
return 0
Expand All @@ -975,12 +965,14 @@ remove_crontab (){
local tmpcron
tmpcron=/tmp/cron.XXXXXX

crontab -l |egrep -v "$WORK_HOME" >$tmpcron
crontab $tmpcron && rm -f $tmpcron
if [ `crontab -l |egrep "$WORK_HOME" |wc -l` -ne 0 ];then
crontab -l |egrep -v "$WORK_HOME" >$tmpcron
crontab $tmpcron && rm -f $tmpcron

# 下面这段代码是为了确保修改的crontab立即生效
if pgrep -x crond &>/dev/null; then
pkill -HUP -x crond
# 下面这段代码是为了确保修改的crontab立即生效
if pgrep -x crond &>/dev/null; then
pkill -HUP -x crond
fi
fi
}

Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_agent.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down
3 changes: 2 additions & 1 deletion script_tools/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ remove_crontab () {
local tmpcron
tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX)

crontab -l | grep -v "bin/gsectl" >"$tmpcron"
# 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响
crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron"
crontab "$tmpcron" && rm -f "$tmpcron"

# 下面这段代码是为了确保修改的crontab能立即生效
Expand Down

0 comments on commit 1259b63

Please sign in to comment.