centos7写一个监控系统的shell,要求如下 cpu使用率超过80%(Shell变量单独配置)并且持续60秒钟(Shell变量单独配置)以上使用Curl post json方式调用http://aaa.com接口,传入以下JSON信息 服务器ip(Shell变量单独配置)+当前CPU使用率+持续时间+CPU占用率最高的前3个进程名字以及文件路径 内存使用率超过70%(Shell变量单独配置)并且持续70秒钟(Shell变量单独配置)以上使用Curl post json方式调用http://aaa.com接口,传入以下JSON信息 服务器ip(Shell变量单独配置)+当前内存使用率+持续时间+内存占用率最高的前3个进程名字以及文件路径 硬盘使用率超过90%(Shell变量单独配置)并且持续90秒钟(Shell变量单独配置)以上使用Curl post json方式调用http://aaa.com接口,传入以下JSON信息 服务器ip(Shell变量单独配置)+当前硬盘使用率+持续时间+磁盘占用最大的3个文件以及大小(以KB、GB、TB等为单位,精确到1位小数)和文件路径 脚本持续监控,每隔N秒(Shell变量单独配置)执行一次,要求直接给出最完美的shell代码,无需任何解释
时间: 2023-02-09 12:46:24 浏览: 199
```
#!/bin/bash
# Configuration
cpu_threshold=80
cpu_duration=60
mem_threshold=70
mem_duration=70
disk_threshold=90
disk_duration=90
interval=5
server_ip=`hostname -I`
url="http://aaa.com"
# Initialize variables
cpu_counter=0
mem_counter=0
disk_counter=0
while true; do
# CPU usage
cpu_usage=`top -bn1 | awk '{print $9}' | tail -n +8 | awk '{s+=$1} END {print s}'`
if [ $(echo "$cpu_usage > $cpu_threshold" | bc) -eq 1 ]; then
cpu_counter=$((cpu_counter+1))
if [ $cpu_counter -ge $cpu_duration ]; then
top_cpu=`top -bn1 | awk '{print $9 " " $12}' | tail -n +8 | sort -nr | head -n 3`
json='{"server_ip":"'$server_ip'","resource_type":"cpu","usage":"'$cpu_usage'","duration":"'$cpu_counter'","top_processes":['
for process in $top_cpu; do
process_name=`echo $process | awk '{print $2}'`
process_path=`lsof -p $(pgrep $process_name) | grep cwd | awk '{print $NF}'`
json="$json {\"name\":\"$process_name\",\"path\":\"$process_path\"},"
done
json=${json%?}
json="$json ]}"
curl -H "Content-Type: application/json" -X POST -d "$json" $url
cpu_counter=0
fi
else
cpu_counter=0
fi
# Memory usage
mem_usage=`free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }'`
if [ $(echo "$mem_usage > $mem_threshold" | bc) -eq 1 ]; then
mem_counter=$((mem_counter+1))
if [ $mem_counter -ge $mem_duration ]; then
top_mem=`ps aux --sort=-%mem | awk '{print $4 " " $11}' | head -n 4 | tail -n 3`
json='{"server_ip":"'$server_ip'","resource_type":"memory","usage":"'$mem_usage'","duration":"'$mem_counter'","top_processes":['
for process in $top_mem; do
process_name=`echo $process | awk '{print $2}'`
process_path=`lsof -p $(pgrep $process_name) | grep cwd | awk '{print $NF}'`
json="$json {\"name\":\"$process_name\",\"path\":\"$process_path\"},"
done
json=${json%?}
json="$json ]}"
curl -H "Content-Type: application/json" -X POST -d "$
阅读全文