系统: ubuntu20.04
场景 轻量监控docker 容器 ---> eg : per 5min 执行一次,内存利用率大于95% ,发出提示
1. 简易通知服务:(notifyApp.py)
import asyncio import uvicorn from fastapi import FastAPI from starlette.requests import Request from fastapi.middleware.cors import CORSMiddleware app = FastAPI(title="Sea test API") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.post("/sendNotify") async def send_notify( request: Request): request_body = await request.body() try: request_body_str = request_body.decode('utf-8') print("sendNotify " + request_body_str) msg = eq_wechat_notify.send_group_chat_msg(group_id=WX_GROUP_ID, message=request_body_str) # 企业微信 or 钉钉 print("sendNotify result " + str(msg)) return {"code":200,"message": "ok"} except Exception as e: print("send_notify error : " + str(e)) return {"code":250,"message": str(e)} if __name__ == '__main__': async def run_both_services(): # 同时运行 Kafka 消费者和 FastAPI await asyncio.gather( asyncio.to_thread(start), # 将同步函数转换为异步执行 asyncio.to_thread(lambda: uvicorn.run(app, host="0.0.0.0", port=5000)) ) asyncio.run(run_both_services())
使用:
正确的 curl POST 请求格式: 1. 发送原始字符串 "hello": bash curl -X POST http://localhost:5000/sendNotify -d "hello" -H "Content-Type: text/plain" 2. 发送 JSON 数据: bash curl -X POST http://localhost:5000/sendNotify \ -H "Content-Type: application/json" \ -d '{"message": "hello"}' 3. 发送表单数据: bash curl -X POST http://localhost:5000/sendNotify \ -d "message=hello"
脚本1:筛选内存使用率大于80%的容器
简洁版本:
sudo docker stats --no-stream --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}" | awk 'NR==1 || $4+0 > 80'

浙公网安备 33010602011771号