點選檢視程式碼
#!/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"