Open zzzlang opened 8 months ago
一个脚本的事
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
结合推送助手,可以实现脚本实现电话短信报警的能力。参考:push.spug.cc
一个脚本的事
!/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