end13ss / personal-note Goto Github PK
View Code? Open in Web Editor NEWJust for study and sharing
Just for study and sharing
#!/bin/bash
#定义prometheus查询接口的地址
PROMETHEUS_URL="http://172.16.30.135:9090/api/v1/query_range"
#定义飞书webhook机器人的地址
FEISHU_WEBHOOK="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxxx"
#定义promql语句
PROMQL="sum by(instance, name) (rate(container_cpu_usage_seconds_total{name=~\"test-backend0|test-backend1|test-backend3|test-backend4\"}[5m])) * 100"
#定义查询时间范围为近3个小时,步长为5分钟
END_TIME=$(date +%s)
START_TIME=$((END_TIME - 3 * 3600))
STEP=300
#调用prometheus查询接口,获取json格式的结果
RESULT=$(curl -s -G -L --data-urlencode "query=$PROMQL" --data-urlencode "start=$START_TIME" --data-urlencode "end=$END_TIME" --data-urlencode "step=$STEP" "$PROMETHEUS_URL")
#解析json结果,找出cpu使用率最高时的数据
MAX_CPU=0
MAX_TIME=0
MAX_INSTANCE=""
MAX_NAME=""
for row in $(echo "$RESULT" | jq -r '.data.result[] | @base64'); do
_jq() {
echo "$row" | base64 --decode | jq -r "$1"
}
INSTANCE=$(_jq '.metric.instance')
NAME=$(_jq '.metric.name')
for value in $(_jq '.values[] | @base64'); do
_jqv() {
echo "$value" | base64 --decode | jq -r "$1"
}
TIME=$(_jqv '.[0]')
CPU=$(_jqv '.[1]')
if (( $(echo "$CPU > $MAX_CPU" | bc -l) )); then
MAX_CPU=$CPU
MAX_TIME=$TIME
MAX_INSTANCE=$INSTANCE
MAX_NAME=$NAME
fi
done
done
#格式化最高cpu使用率时的时间戳为可读的日期时间,并保留两位小数
MAX_DATETIME=$(date -d "@$MAX_TIME" +"%Y-%m-%d %H:%M:%S")
MAX_CPU=$(printf "%.2f\n" $MAX_CPU)
#构造飞书消息的请求体,内容为最高cpu使用率时的数据
FEISHU_MSG="{\"msg_type\": \"text\", \"content\": {\"text\": \"近3个小时内,容器cpu最高CPU使用率:$MAX_CPU%\\n容器名称:$MAX_NAME\\n实例IP:$MAX_INSTANCE\\n发生时间:$MAX_DATETIME\"}}"
#调用飞书webhook机器人,发送消息
curl -s -X POST -H "Content-Type: application/json" -d "$FEISHU_MSG" "$FEISHU_WEBHOOK"
需要一个url.txt中记录完整路由地址,例如:
https://www.baidu.com
https://www.bing.com
....
#!/bin/bash
# 设置webhook机器人的地址
WEBHOOK="xxxx"
# 设置定时任务的间隔,单位为秒
INTERVAL=30
# 设置要发送的数据,可以根据需要修改
DATA='{"Subject": "测试", "Message": "这是一个测试消息"}'
# 定义一个函数,用于发送报警消息到webhook机器人
send_alert(){
# 使用curl命令发送POST请求,数据格式为JSON,包含msg_type和content字段
curl -X POST -H "Content-Type: application/json" -d "{\"msg_type\":\"text\",\"content\":{\"text\":\"$1\"}}" $WEBHOOK
}
# 使用while循环,每隔一定时间执行一次检测
while true; do
# 使用cat命令读取当前目录下的url.txt文件,每行一个url,使用while循环遍历每个url
cat url.txt | while read URL; do
# 使用curl命令发送HTTP Post请求,指定header为Content-Type: application/json,输出响应头,获取状态码
STATUS=$(curl -I -m 10 -s -o /dev/null -w "%{http_code}" $URL)
# 判断状态码是否为200,如果不是,则调用send_alert函数,传入报警消息和状态码
if [ $STATUS -ne 200 ] && [ $STATUS -ne 301 ] && [ $STATUS -ne 302 ]; then
send_alert "$URL\n http_code: $STATUS\n date: $(date)"
fi
done
# 等待一定时间后再执行下一次检测
sleep $INTERVAL
done
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.