监控主机状态(ping)
#监控方法 ping ICMP协议
#ping通 host up
#ping不通 host down
#关于禁ping 防止DDOS
#禁的是陌生人 禁止所有 允许你的IP
#满足条件
#网络有延迟 what‘s up 假报警问题
#ping的取值 报警阈值 3次全部失败 报警机器down
#ping的频率 秒级 5秒或1秒
for ((i=1;i<4;i++));do
#测试代码
if ping -c1 $1 &>/dev/null;then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
#时间间隔
sleep 1
done
#3次ping失败报警
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ];then
echo "$1 is down"
else
echo "$1 is up"
fi
unset ping_count1
unset ping_count2
unset ping_count3
监控端口状态
# 监控服务端口
#systemctl status XXX
#service xxx status
#lsof -i :port
#ps aux process
#监控方法
#1 通过查看状态 systemctl status service 服务启动状态
#2 lsof 查看端口是否存在
#3 查看进程是否存在
#4 测试端口是否有相应 推荐
port_status () {
temp_file=`mktemp port_status.XXX`
#1 判断以来命令telnet是否存在
[ ! -x /usr/bin/telnet ] && echo "telnet: not found command" && exit 1
#2 测试端口 $1 IP $2 port
(telnet $1 $2 << EOF
quit
EOF
) &> $temp_file
if egrep "\^]" $temp_file &>/dev/null;then
echo "$1 $2 is alive"
else
echo "$1 $2 is dead"
fi
rm -f $temp_file
}
prot_status $1 $2
内存使用率统计
# 内存使用率统计脚本
# free
# cat /proc/meminfo
# 内存使用顺序 free--cache--buffer--swap
memmory_use() {
memmory_used=`head -2 /proc/meminfo |awk 'NR==1{t=$2}NR==2{f=$2;print(t-f)*100/t}'`
memmory_cache=`head -5 /proc/meminfo |awk 'NR==1{t=$2}NR==5{c=$2;print c*100/t}'`
memmory_buff=`head -4 /proc/meminfo |awk 'NR==1{t=$2}NR==4{b=$2;print b*100/t}'`
echo "memmory_use : $memmory_used%"
echo "memmory_cache : $memmory_cache%"
echo "memmory_buff : $memmory_buff%"
echo -e "memmory_used:$memmory_used\tbuffer:$memmory_buff\tcache:$memmory_cache"
}
memmory_use
内存使用前10名
# 统计系统中使用内存前10的进程
memmory(){
# 搜集信息
temp_file_top=`mktemp memmory_top.XXX`
temp_file_ps=`mktemp memmory_ps.XXX`
top -b -n 1 > $temp_file_top
ps aux > $temp_file_ps
# 按进程统计内存使用大小
tail -n +8 $temp_file_top | awk '{array[$NF]+=$6}END{for (i in array) print i,array[i]}' | sort -k 2 -n -r |head -10
tail -n +2 $temp_file_ps | awk '{array[$11]+=$6}END{for (i in array) print i,array[i]}' | sort -k 2 -n -r |head -10
rm -f $temp_file_top
rm -f $temp_file_ps
}
memmory