#!/bin/bash mem=`free -m |sed -n '2p'|awk '{print $3/$2*100}'` if [ `echo "$mem>10"|bc` -eq 1 ];then echo `date "+%Y/%m/%d %H;%M;%S"`" 内存负载过高 $mem%" >> 1.txt fi 换一种表达方式
时间: 2024-03-19 20:40:54 浏览: 115
#!/bin/bash
mem=$(free -m | awk 'NR==2{printf "%.2f\n", $3*100/$2 }')
if (( $(echo "$mem > 10" | bc -l) )); then
echo "$(date "+%Y/%m/%d %H;%M;%S") 内存负载过高 $mem%" >> 1.txt
fi
相关问题
#!/bin/bash mem=`free -m |sed -n '2p'|awk '{print $3/$2*100}'` if [ `echo "$mem>10"|bc` -eq 1 ];then echo `date "+%Y/%m/%d %H;%M;%S"`" 内存负载过高 $mem%" >> 1.txt fi ~ 改为监管磁盘
可以将上述脚本改为监管磁盘使用情况,具体步骤如下:
1. 将获取内存使用情况的命令`free -m`替换为获取磁盘使用情况的命令`df -h`,并将结果保存到`disk`变量中。
2. 将计算内存使用率的命令`sed -n '2p'|awk '{print $3/$2*100}'`替换为计算磁盘使用率的命令,例如`df -h | grep '/dev/sda1' | awk '{print int($5)}'`,其中`/dev/sda1`是磁盘分区的名称,可以根据实际情况进行修改。该命令会获取磁盘分区的使用率,并将结果保存到`disk_usage`变量中。
3. 将判断内存使用率是否超过阈值的语句`if [ `echo "$mem>10"|bc` -eq 1 ];then`改为判断磁盘使用率是否超过阈值的语句,例如`if [ $disk_usage -gt 90 ]; then`,表示当磁盘使用率超过90%时,执行下一步操作。
4. 将输出内存负载过高的语句`echo `date "+%Y/%m/%d %H;%M;%S"`" 内存负载过高 $mem%" >> 1.txt`改为输出磁盘负载过高的语句,例如`echo `date "+%Y/%m/%d %H;%M;%S"`" 磁盘负载过高 $disk_usage%" >> 1.txt`,表示输出当前时间和磁盘使用率,并将结果保存到`1.txt`文件中。
综上所述,改进后的脚本如下:
```
#!/bin/bash
disk=$(df -h)
disk_usage=$(echo "$disk" | grep '/dev/sda1' | awk '{print int($5)}')
if [ $disk_usage -gt 90 ]; then
echo `date "+%Y/%m/%d %H;%M;%S"`" 磁盘负载过高 $disk_usage%" >> 1.txt
fi
```
该脚本会获取磁盘使用情况,并计算磁盘使用率。当磁盘使用率超过90%时,输出当前时间和磁盘使用率,并将结果保存到`1.txt`文件中。可以将该脚本加入到定时任务中,以便定期监管服务器磁盘使用情况。
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代码,一次性说完,无需任何解释
```
#!/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_
阅读全文
相关推荐
















