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 11:45:42 浏览: 103
```
#!/bin/bash
cpu_threshold=80
cpu_duration=60
mem_threshold=70
mem_duration=70
disk_threshold=90
disk_duration=90
interval=60
url="http://aaa.com"
while true; do
ip=$(hostname -I)
# CPU usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf "%.1f\n", 100 - $1}')
if (( $(echo "$cpu_usage > $cpu_threshold" | bc -l) )); then
cpu_time=$((cpu_time + interval))
if (( $cpu_time > $cpu_duration )); then
cpu_time=0
top_procs=$(ps -eo pid,user,%cpu,cmd --sort=-%cpu | head -n 4)
data="{\"server_ip\":\"$ip\",\"resource_type\":\"cpu\",\"usage\":\"$cpu_usage\",\"duration\":\"$cpu_duration\",\"top_procs\":\"$top_procs\"}"
curl -H "Content-Type: application/json" -X POST -d "$data" "$url"
fi
else
cpu_time=0
fi
# Memory usage
mem_usage=$(free | awk '/Mem/ {printf "%.1f\n", $3/$2 * 100.0}')
if (( $(echo "$mem_usage > $mem_threshold" | bc -l) )); then
mem_time=$((mem_time + interval))
if (( $mem_time > $mem_duration )); then
mem_time=0
top_procs=$(ps -eo pid,user,%mem,cmd --sort=-%mem | head -n 4)
data="{\"server_ip\":\"$ip\",\"resource_type\":\"memory\",\"usage\":\"$mem_usage\",\"duration\":\"$mem_duration\",\"top_procs\":\"$top_procs\"}"
curl -H "Content-Type: application/json" -X POST -d "$data" "$url"
fi
else
mem_time=0
fi
# Disk usage
disk_usage=$(df -h | awk '$NF=="/"{printf "%s\n", $5}')
disk_usage=${disk_usage%?}
if (( $(echo "$disk_usage > $disk_threshold" | bc -l) )); then
disk_time=$((disk_time + interval))
if (( $disk_time > $disk_duration )); then
disk_time=0
top_files=$(du -ah / | sort -nr | head -n 3)
data="{\"server_ip\":\"$ip\",\"resource_type\":\"disk\",\"usage\":\"$disk_usage\",\"duration\":\"$disk_duration\",\"top_files\":\"$top_files\"}"
curl -H "Content-Type: application/json" -X POST -d "$data" "$url"
fi
else
disk_
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)