Linux 编写一个shell脚本实现监控进程的CPU、内存超过95告警到钉钉群
#!/bin/bash
获取进程名
process_name="your_process_name"
获取进程的PID
pid=$(pgrep $process_name)
获取CPU和内存使用率
cpu_usage=$(top -b -n 1 -p $pid | awk '{if ($1 == '$pid') print $9}') memory_usage=$(top -b -n 1 -p $pid | awk '{if ($1 == '$pid') print $10}')
将CPU和内存使用率转换为整数
cpu_usage_int=${cpu_usage%.} memory_usage_int=${memory_usage%.}
判断CPU和内存使用率是否超过95%
if [ $cpu_usage_int -gt 95 ] || [ $memory_usage_int -gt 95 ]; then
发送告警信息到钉钉群
curl -H "Content-Type: application/json" -X POST -d '{"msgtype": "text", "text": {"content": "进程'$process_name'的CPU或内存使用率超过95%!"}}' https://oapi.dingtalk.com/robot/send?access_token=your_access_token fi
结束脚本
exit
原文地址: https://www.cveoy.top/t/topic/hL9v 著作权归作者所有。请勿转载和采集!