openspug / spug

开源运维平台:面向中小型企业设计的轻量级无Agent的自动化运维平台,整合了主机管理、主机批量执行、主机在线终端、文件在线上传下载、应用发布部署、在线任务计划、配置中心、监控、报警等一系列功能。
https://spug.cc
GNU Affero General Public License v3.0
10k stars 2.03k forks source link

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

Open zzzlang opened 4 months ago

yangshuDBA commented 3 months ago

一个脚本的事

!/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