案例1-Linux系统性能监控脚本

tech2024-04-04  58

系统性能监控脚本

#!/bin/bash #获取系统性能参数,根据预设阈值决定是否给管理员发送邮件 local_time=$(date +"%Y%m%d %H:%M:%S") local_ip=$(ifconfig eth0 | grep netmask | tr -s " " | cut -d" " -f3) #整理分割 free_mem=$(cat /proc/meminfo | grep Avai | tr -s "" | cut -d" " -f2) free_disk=$(df | grep "/$" | tr -s " " | cut -d" " -f4) #正则匹配 cpu_load=$(cat /proc/loadavg | cut -d" " -f3) login_user=$(who | wc -l) procs=$(ps aux | wc -l) #vmstat命令可以查看系统中CPU的中断数,上下文切换数量(前两行是头部信息、第三行是开机到当前平均值、第四行开始是每秒实时数据) #tail -n +4表示删除前3行,从第4行开始显示 irq=$(vmstat 1 2 | tail -n +4 | tr -s " " | cut -d" " -f12) cs=$(vmstat 1 2 | tail -n +4 | tr -s " " | cut -d" " -f13) usertime=$(vmstat 1 2 | tail -n +4 | tr -s " " | cut -d" " -f14) systime=$(vmstat 1 2 | tail -n +4 | tr -s " " | cut -d" " -f15) iowait=$(vmstat 1 2 | tail -n +4 | tr -s " " | cut -d" " -f17) #设置规则 #内存小于1G时给root发送报警邮件 [[ ${free_mem} -lt 1048576 ]] && \ echo "$local_time Free memory not enough. Free_mem:$free_mem on $local_ip" | \ mail -s Warning root@localhost #磁盘小于10G时给root发送报警邮件 [[ ${free_disk} -lt 10485760 ]] && \ echo "$local_time Free disk not enough. Free_disk:$free_disk on $local_ip" | \ mail -s Warning root@localhost #CPU15min内的平均负载超过4时给root发送报警邮件 [[ ${free_disk} -lt 10485760 ]] && \ echo "$local_time Free disk not enough. Free_disk:$free_disk on $local_ip" | \ mail -s Warning root@localhost #系统实时在线人数超过3人时给root发送报警邮件 [[ ${login_user} -gt 3 ]] && \ echo "$local_time too many user. $login_user users login to $local_ip" | \ mail -s Warning root@localhost #CPU消耗大量时间等待磁盘I/O时给root发送报警邮件 [[ ${iowait} -gt 40 ]] && \ echo "$local_time Disk to slow. CPU spend too many time wait disk I/O:$iowait on $local_ip" | \ mail -s Warning root@localhost
最新回复(0)