node_heart_check.sh
#!/bin/bash
scriptPath=$(dirname "$0")
for ip in `cat /etc/hosts | grep 主机名关键字 | awk '{print $2}'`;do
# ping命令返回3次,自行中断
ping -c 3 ${ip}
if [ $? -ne 0 ];then
echo ${ip}"通信异常" >>${scriptPath}/log_nodeHeart_warn.log
fi
done
脚本作用:监控集群节点网络通信情况,如果发现ping 3次失败,则为通信异常,可根据短信接口向运维人员发送短信,及时进行处理。
pod_status_check.sh
#!/bin/bash
scriptPath=$(dirname "$0")
IFS=$'\n'
kubectl get pod --all-namespace -owide | grep 0/
if [ $? -ne 0 ];then
echo "$(date +'%Y-%m-%d %H:%M:%S') [INFO] 集群Pod正常">>${scriptPath}/log_podStatus_check.log
else
podList=`kubectl get pod --all-namespaces -owide | grep 0/`
for pod in `echo ${podList}`
do
# 获取租户名称
nsName=`echo ${pod} | awk '{print $1}'`
# 获取pod名称
podName=`echo ${pod} | awk '{print $2}'`
# 获取容器就绪情况
readyNum=`echo ${pod} | awk '{print $3}'`
# 获取pod状态
podStatus=`echo ${pod} | awk '{print $4}'`
# 获取重启次数
restartCount=`echo ${pod} | awk '{print $5}'`
# 获取对应节点
podNode=`echo ${pod} | awk '{print $8}'`
# 记录
echo "$(date +'%Y-%m-%d %H:%M:%S') [INFO] 集群Pod异常 ${nsName} ${podName} ${readyNum} ${podStatus} ${restartCount} ${podNode}" >>${scriptPath}/log_podStatus_warn.log
done
fi
IFS=${IFS_old}
脚本作用:监控所有Pod状态,出现Pod状态异常则触发短信告警(脚本中已省略)
mysql_heart_check.sh
#!/bin/bash
scriptPath=$(dirname "$0")
mysql -h主机名 -u用户名 -p密码 -P端口号 -e "select now();"
result=$?
if [ 0 != ${result} ];then
echo "$(date +'%Y-%m-%d %H:%M:%S') [warn] mysql心跳异常">>${scriptPath}/log_mysqlHeart_warn.log
fi
脚本作用:监控mysql心跳是否正常,如果异常,则触发短信告警(脚本中已省略)
标签:Shell,log,echo,集群,scriptPath,print,awk,pod,合集 From: https://www.cnblogs.com/houhuilinblogs/p/17971378