Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议:希望增加监控,监控主机的内存和cpu情况 #660

Open
zzzlang opened this issue Feb 22, 2024 · 2 comments
Open

建议:希望增加监控,监控主机的内存和cpu情况 #660

zzzlang opened this issue Feb 22, 2024 · 2 comments

Comments

@zzzlang
Copy link

zzzlang commented Feb 22, 2024

No description provided.

@zzzlang zzzlang changed the title 建议:希望增加监控,监控主机的内存和cpu情况Bug: 建议:希望增加监控,监控主机的内存和cpu情况 Feb 22, 2024
@EvanderYang
Copy link

一个脚本的事
#!/bin/bash
#author:yangshu
#检查磁盘使用率
#检查Inode使用率
#检查内存使用率
#v1.0.7 增加CPU监控
#V1.0.8 增加告警阈值红色显示
#V1.0.9 修复告警多个指标判断逻辑
check_disk_usage() {
local alert_percent=$1
local output=$(df -PTl | grep "/dev" | grep -Ev "tmpfs|sr0|cdrom|:" | sed 's/%//g' | awk -v var="$alert_percent" '{if($6>var) print "告警目录: "$7 " 使用率 " $6"%"}')
if [[ -z "$output" ]]; then
return 0
else
echo -e "磁盘使用率超过 $alert_percent% \t $output"
return 1
fi
}

check_inode_usage() {
local alert_percent=$1
local output=$(df -PTil | grep "/dev" | grep -Ev "tmpfs|sr0|cdrom|:" | sed 's/%//g' | awk -v var="$alert_percent" '{if($6>var) print $0}')
if [[ -z "$output" ]]; then
return 0
else
echo -e "innode使用率超过 $alert_percent% \t $output"
return 1
fi
}

check_memory_usage() {
local alert_percent=$1
local output=$(free | grep Mem | awk '{printf("%.0f"), $3/$2*100}')
if [[ $output -gt $alert_percent ]]; then
echo -e "内存使用率超过 $alert_percent% \t 当前为$output%"
return 1
fi
return 0
}

check_cpu_usage() {
local alert_percent=$1
local output=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
if (( $(echo "$output > $alert_percent" | bc -l) )); then
echo -e "CPU使用率超过 $alert_percent% \t 当前为$output%"
return 1
fi
return 0
}

main() {
disk=$(check_disk_usage 90)
inode=$(check_inode_usage 80)
memory=$(check_memory_usage 95)
cpu=$(check_cpu_usage 90)
all_check=$disk$inode$memory$cpu
if [[ -z $all_check ]]; then
echo -e "\t 巡检正常 \t"
return 0
else
echo -e "\t 巡检异常\n \t$all_check \t"
return 1
fi
}

main

@zyupo
Copy link
Member

zyupo commented Aug 28, 2024

结合推送助手,可以实现脚本实现电话短信报警的能力。参考:push.spug.cc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants