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

系统性能监控脚本

#!/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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值