点击查看代码
#!/bin/bash
echo `date`
# 获取最新的错误计数
new_error_count_8080=$(grep "Too many open files" /var/log/router/8080/error.log | wc -l)
new_error_count_8181=$(grep "Too many open files" /var/log/router/8181/error.log | wc -l)
# 读取之前的错误计数(如果文件不存在,则默认为0)
old_error_count_8080=$(cat /root/8080_error_count.txt 2>/dev/null || echo 0)
old_error_count_8181=$(cat /root/8181_error_count.txt 2>/dev/null || echo 0)
#计算错误计数变化量
increase_8080=$((new_error_count_8080 - old_error_count_8080))
increase_8181=$((new_error_count_8181 - old_error_count_8181))
# 推送错误计数变化量数据到prometheus
echo "error_increase_8080 $increase_8080"
echo "error_increase_8080 $increase_8080" | curl --data-binary @- http://172.16.6.130:9091/metrics/job/obs_router/endpoint/router1/instance/172.16.3.132
echo "error_increase_8181 $increase_8181"
echo "error_increase_8181 $increase_8181" | curl --data-binary @- http://172.16.6.130:9091/metrics/job/obs_router/endpoint/router1/instance/172.16.3.132
#更新错误计数文件
echo $new_error_count_8080 > /root/8080_error_count.txt
echo $new_error_count_8181 > /root/8181_error_count.txt
#推送进程句柄数指标到prometheus
PID_8080=$(ps -ef | grep 8080 | grep -v grep | awk '{print $2}')
fd_count_8080=$(ls /proc/$PID_8080/fd/ | wc -l 2>/dev/null)
PID_8181=$(ps -ef | grep 8181 | grep -v grep | awk '{print $2}')
fd_count_8181=$(ls /proc/$PID_8181/fd/ | wc -l 2>/dev/null)
echo "fd_count_8080 $fd_count_8080" | curl --data-binary @- http://172.16.6.130:9091/metrics/job/obs_router/endpoint/router1/instance/172.16.3.132
echo "fd_count_8080 $fd_count_8080"
echo "fd_count_8181 $fd_count_8181" | curl --data-binary @- http://172.16.6.130:9091/metrics/job/obs_router/endpoint/router1/instance/172.16.3.132
echo "fd_count_8181 $fd_count_8181"