博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zabbix 监控openshift pod状态
阅读量:2228 次
发布时间:2019-05-09

本文共 7325 字,大约阅读时间需要 24 分钟。

需求:

pod中的容器重启一次则报警通知

pod非Runing 状态则报警

pod中的容器非true状态则报警

三个需求其实是有点重叠的

pod重启期间pod肯定会有非Running状态,只要有重启报警那么pod非Runing也会报警,pod非Runing容器状态肯定非true也会报警

所有报警设置为:

pod重启一次就报警

pod非Runing  and 容器非true (#3)  and pod非删除 =报警

 

zabbix server中建一个模板

3.2
2017-11-23T07:48:53Z
OpenShift
模板文件

新建一个自动发现规则,有三个监控项对于上面说的三个需求

 

zabbix agent

在配置文件末尾中加入

# vim zabbix_agentd.conf

UserParameter=oc.pod.discover,/data/app/zabbix/etc/oc_pod_discover.sh

UserParameter=oc.pod.status[*],/data/app/zabbix/etc/oc_pod_monitor.sh $1 $2

 

自动发现脚本

# vim oc_pod_discover.sh

#!/bin/bashTOKEN="123456"ENDPOINT="www.oc.domain.cn:8443"WORKSPACE="/data/tmp/oc_monitor"mkdir -p $WORKSPACE#获取所有pod只保留pod namecurl -k \  -H "Authorization: Bearer $TOKEN" \  -H 'Accept: application/json' \  https://$ENDPOINT/api/v1/pods 2>/dev/null  > $WORKSPACE/all_pods.jsonPod_Name=(`cat $WORKSPACE/all_pods.json |jq -r '.items | .[] | .metadata | .name' |grep -v build |grep -v deploy`)#转换为json格式printf "{\n"printf '\t"data":[\n'for ((i=0;i<${#Pod_Name[@]};i++))do        printf '\t\t{\n'        num=$(echo $((${#Pod_Name[@]}-1)))        if [ "$i" == ${num} ];        then                printf "\t\t\t\"{#POD_NAME}\":\"${Pod_Name[$i]}\"}\n"        else                printf "\t\t\t\"{#POD_NAME}\":\"${Pod_Name[$i]}\"},\n"        fidoneprintf "\t]\n"printf "}\n"

 

监控脚本

# vim oc_pod_monitor.sh

#!/bin/bashTOKEN="123456"ENDPOINT="www.oc.domain.cn:8443"POD_NAME="$1"Monitoring_type="$2"WORKSPACE="/data/tmp/oc_monitor"mkdir -p $WORKSPACE#通过pod name获得pod所在的namespace5分钟更新一次NAMESPACE="`cat $WORKSPACE/all_pods.json |jq -r '.items |.[] |.metadata |.name,.namespace' |grep -A1 $POD_NAME |grep -v $POD_NAME`"#验证pod是否存在if [ ! -n "$NAMESPACE" ]; then  if [ "$Monitoring_type" = "running_true" ]; then    echo "1"    exit 0  fi  echo "Pod deleted"  exit 0fi#获取pod状态数据if [ ! -f "$WORKSPACE/${POD_NAME}.status" ]; then  if [ "$Monitoring_type" = "running_true" ]; then    echo "1"    exit 0  fi  echo "New Pod"  exit 0fiPod_Status="`cat $WORKSPACE/${POD_NAME}.status`"#验证容器是否在Pending状态Pending="`echo "$Pod_Status" |jq -r '.status |.phase'`"if [ "$Pending" = "Pending" ]; then  if [ "$Monitoring_type" = "running_true" ]; then    echo "0"    exit 0  fi  echo "Pending"  exit 0fi#选择要获取的数据case $Monitoring_type in   restarts)#监控pod是否重启过     #获取pod状态数据写到文件里面可供所有项目调用     curl -k \       -H "Authorization: Bearer $TOKEN" \       -H 'Accept: application/json' \       https://${ENDPOINT}/api/v1/namespaces/$NAMESPACE/pods/$POD_NAME/status 2>/dev/null > $WORKSPACE/${POD_NAME}.status       find /data/tmp/oc_monitor/ -type f -mtime +3 -name "*" -exec rm -f {} \;     #获取pod的状态只保留restartCount的值          ##获取上次的值     A_line=`sed -n 1p $WORKSPACE/${POD_NAME}.restartCount`     B_line_null="`sed -n 2p $WORKSPACE/${POD_NAME}.restartCount`"     if [ ! -n "$B_line_null" ]; then  #处理有两个restartCount值的pod       B_line="0"     else       B_line=`sed -n 2p $WORKSPACE/${POD_NAME}.restartCount`     fi     Last_state=`expr $A_line + $B_line`     ##     ##获取本次的值     echo "$Pod_Status" |jq -r '.status |.containerStatuses |.[] |.restartCount' > $WORKSPACE/${POD_NAME}.restartCount     A_line=`sed -n 1p $WORKSPACE/${POD_NAME}.restartCount`     B_line_null="`sed -n 2p $WORKSPACE/${POD_NAME}.restartCount`"     if [ ! -n "$B_line_null" ]; then  #处理有两个restartCount值的pod       B_line="0"       else       B_line=`sed -n 2p $WORKSPACE/${POD_NAME}.restartCount`     fi     Current_state=`expr $A_line + $B_line`     ##      #对比本次拿到的restartCount值与上此的restartCount值     if [ "$Current_state" -gt "$Last_state" ]; then       Restart_status="Warning restart_count=$Current_state"     else       Restart_status="Normal restart_count=$Current_state"     fi     echo "$Restart_status"  ;;   running)#监控pod的运行状态和容器的状态返回字符串     if [ ! -n "$Pod_Status" ]; then       echo "New Pod"       exit 0       fi     running_status=`echo "$Pod_Status" |jq -r '.status |.phase'`     Container_status="`echo "$Pod_Status" |jq -r '.status |.containerStatuses |.[] |.ready' |grep false`"     if [ ! -n "$Container_status" ]; then        Container_status="_true"     else        Container_status="_false"     fi     echo "${running_status}${Container_status}"  ;;   running_true)#监控pod中的容器运行状态返回数字     if [ ! -n "$Pod_Status" ]; then       echo "New Pod"       exit 0       fi     Container_status="`echo "$Pod_Status" |jq -r '.status |.containerStatuses |.[] |.ready' |grep false`"     if [ ! -n "$Container_status" ]; then        Container_status="true"     else        Container_status="false"     fi     if [ "$Container_status" = "true" ]; then        echo "1"     else        echo "0"     fi  ;;    *)     echo "Error parameters"     exit 0  ;;esac

 

转载于:https://www.cnblogs.com/37yan/p/7885404.html

你可能感兴趣的文章
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>