接口探测-shell获取探测接口
背景
为了监控微服务、zk、euerka等故障,需要对RestApi接口做状态探测。公司某平台一个有90台机器,需要插队每台机器机器的业务模块,并配置探测。
实现思路
微服务
微服务业务模块都有暴露探测接口:ip:port/service/health?service=模块名称(如:http://120.01:7982/servic
需要确认的:port、模块名称
现平台的微服务架构:
CtrlBootstrap:程序主入口,web模块,控制端口。负责分发请求到个模块
WorkgroupBootstrap:业务模块,模块名称通过此进程获取
监听端口:
ps|grepCtrlBootstrap:获取到CtrlBootstrap主程序pidnetstat|greppid:获取主程序监听端口
模块名称:
ps|grepWorkgroupBootstrap:可看到启动时传入的模块参数:AMMS
拼接探测URl:
##获取端口
#1 ps -ef出主程序,获取第二列PID,并存储到变量中
ctrlBootstrapTemp=ps -ef |grep CtrlBootstrap|grep appuser|grep -v grep|awk '{print $2}'
#2 通过netstat -anp获取本地ip和端口
# -v不匹配 -P使用正则匹配本地的环回口
netstat -anp|grep $ctrlBootstrapTemp|grep LISTEN|grep -vP ':::[0-9]'|grep -vP '127.0.01'|awk '{print $4}'
##获取模块
#3 $NF为最后一单词
ps -ef WorkgroupBootstrap|grep -v grep|akw '{print $NF}'
##组装为URL
#xargs -i把获取到的多个模块逐个替换{}
获取模块|xargs -i echo 'http://'$(ip和端口语句#2)/service/health?service={}
##拼接curl做请求探测
#-w 输出
url|xargs -i curl {} -w'
'
最后把对于的url粘贴到监控探测平台
实际shell脚本
把这个脚本写成bashshell传入两个参数,一个是主应用,一个是模块得名称。之前没有写过shell,这个只是本地测试shell,仅供参考
#!/bin/shell
#filename=search_health_url
get_main_PID(){
mainProcessName=$1
mainPid=$(ps -ef|grep dyq|grep -v bash|grep $mainProcessName|grep -v grep|awk '{print $2}')
}
get_serverName(){
workName=$1
serverName=$(ps -ef |grep $workName|grep -v grep|grep -v bash|awk '{print $NF}')
}
get_ip_port(){
# 通过netstat -anp获取程序监听ip和端口
ipAndPort=$(netstat -anp|grep LISTEN|grep -vP ':::[0-9]'|grep -vP '127.0.01'|grep $mainPid|awk '{print $4}')
}
test_Url(){
# -s 静默 不显示进度
body=$(curl -s $1)
if [[ $body = *success* ]] ; then
echo '$1 test success'
else
echo '$1 test fail'
fi
}
check_params(){
if [[ -z $ipAndPort ]] ; then
echo 'ip and port is null'
return 1
elif [[ -z $serverName ]] ; then
echo 'serverName is null'
return 1
fi
return 0
}
getUrl(){
get_main_PID $1
get_ip_port
get_serverName $2
if [ check_params == 1 ] ; then
echo 'exit...'
exit 0
else
echo 'check pass'
fi
for name in $serverName
do
echo '===URL==='
url='http://$ipAndPort/health?service=$name'
echo $url
test_Url $url
done
}
脚本运行结果
总结
需要发现规律,使用shell进行提取,提高工作效率
文章为作者独立观点,不代表观点