系统性能监控脚本
#!/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)
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)
[[ ${free_mem} -lt 1048576
]] && \
echo "$local_time Free memory not enough.
Free_mem:$free_mem on $local_ip" | \
mail -s Warning root@localhost
[[ ${free_disk} -lt 10485760
]] && \
echo "$local_time Free disk not enough.
Free_disk:$free_disk on $local_ip" | \
mail -s Warning root@localhost
[[ ${free_disk} -lt 10485760
]] && \
echo "$local_time Free disk not enough.
Free_disk:$free_disk on $local_ip" | \
mail -s Warning root@localhost
[[ ${login_user} -gt 3
]] && \
echo "$local_time too many user.
$login_user users login to $local_ip" | \
mail -s Warning root@localhost
[[ ${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