Linx监控分享--网络状态监控+邮件提醒


网络状态监控

网络状态:netstat 各个状态的总计,详情;以及重点端口的详细连接情况(22,25,80,3306,8080),打印客户端连接数最多的ip。

邮件报告当前状态。

对于每个端口的连接数,自己可以设置阈值,加个if判断就可以完成报检的功能。

在手机上安装网易的邮件客户端,就可以达到实时提醒的效果。

#!/bin/sh
source /etc/profile


IP=`/sbin/ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p'|grep -v '127.0.0.1'`
warn_pct=1 #75
warn_name=[NET-State]
performance_path=/monitor/performance
mailtmp=/u01/soft/smonitor/mailtmplet/"$warn_name"tmp.mail
infotmp=/u01/soft/smonitor/mailtmplet/"$warn_name"tmp.tmp
maillist=490073687@qq.com,zhou.xiangxing210@163.com 

DT=`date +"%Y-%m-%d %H:%M:%S"`
netstat -nap > $infotmp
cat $infotmp|grep '^tcp' >  $infotmp.tmp

stat_help="\n-----------------------------Stat Mean-------------------------------"
stat_help="$stat_help""\n LISTEN:Listening for a connection.侦听来自远方的TCP端口的连接请求"
stat_help="$stat_help""\n\n SYN-SENT:Active; sent SYN. Waiting for a matching connection request after having sent a connection request."
stat_help="$stat_help""\n 再发送连接请求后等待匹配的连接请求."
stat_help="$stat_help""\n\n SYN-RECEIVED:Sent and received SYN. Waiting for a confirming connection request acknowledgment "
stat_help="$stat_help""\n after having both received and sent connection requests.再收到和发送一个连接请求后等待对方对连接请求的确认"
stat_help="$stat_help""\n\n ESTABLISHED:Connection established.代表一个打开的连接"
stat_help="$stat_help""\n\n FIN-WAIT-1:Closed; sent FIN.等待远程TCP连接中断请求,或先前的连接中断请求的确认"
stat_help="$stat_help""\n\n FIN-WAIT-2:Closed; FIN is acknowledged; awaiting FIN.从远程TCP等待连接中断请求"
stat_help="$stat_help""\n\n CLOSE-WAIT:Received FIN; waiting to receive CLOSE.等待从本地用户发来的连接中断请求"
stat_help="$stat_help""\n\n CLOSING:Closed; exchanged FIN; waiting for FIN.等待远程TCP对连接中断的确认"
stat_help="$stat_help""\n\n LAST-ACK:Received FIN and CLOSE; waiting for FIN ACK.等待原来的发向远程TCP的连接中断请求的确认"
stat_help="$stat_help""\n\n TIME-WAIT:In 2 MSL (twice the maximum segment length) quiet wait after close. 等待足够的时间以确保远程TCP接收到连接中断请求的确认"
stat_help="$stat_help""\n\n CLOSED:Connection is closed.没有任何连接状态"
stat_help="$stat_help""\n------------------------------------------------------------"


echo "您好:" >$mailtmp
echo -e "     [$DT]服务器:$IP 网络连接信息如下。\n总计:" >>$mailtmp
cat $infotmp.tmp|awk '{print $6}'|sort |uniq -c|sort >>$mailtmp
echo "[$DT]详细:">>$mailtmp
cat $infotmp.tmp|awk '{print $7"        "$6 }'|sort |uniq -c |sort >>$mailtmp
echo "------------------------------------------------------------" >>$mailtmp


#mysql连接数 3306
cat  $infotmp.tmp|grep 3306 |grep mysqld|grep -v LISTEN > $infotmp.tmp.3306
mysql_conn=`cat  $infotmp.tmp.3306|wc -l`
echo "mysql 3306端口连接数:$mysql_conn" >>$mailtmp
echo "mysql 3306端口每个ip连接数统计如下:" >>$mailtmp
cat $infotmp.tmp.3306|awk '{print $5}'|awk -F ':' '{print $4}'|sort |uniq -c|sort >>$mailtmp
echo "------------------------------------------------------------">>$mailtmp

#ssh连接数 22
cat  $infotmp.tmp|grep 22 |grep sshd|grep -v LISTEN > $infotmp.tmp.22
ssh_conn=`cat  $infotmp.tmp.22|wc -l`
echo "ssh 22端口连接数:$ssh_conn" >>$mailtmp
echo "ssh 22端口每个ip连接数统计如下:" >>$mailtmp
cat $infotmp.tmp.22|awk '{print $5}'|awk -F ':' '{print $1}'|sort |uniq -c|sort >>$mailtmp
echo "------------------------------------------------------------" >>$mailtmp

#httpd连接数 80
cat  $infotmp.tmp|grep 80 |grep httpd|grep -v LISTEN > $infotmp.tmp.80
httpd_conn=`cat  $infotmp.tmp.80|wc -l`
echo "http 80端口连接数:$httpd_conn" >>$mailtmp
echo "http 80端口每个ip连接数统计如下:" >>$mailtmp
cat $infotmp.tmp.80|awk '{print $5}'|awk -F ':' '{print $4}'|sort |uniq -c|sort >>$mailtmp
echo "------------------------------------------------------------" >>$mailtmp

#ssh连接数 8080
cat  $infotmp.tmp|grep 8080 |grep java|grep -v LISTEN > $infotmp.tmp.8080
apache_conn=`cat  $infotmp.tmp.8080|wc -l`
echo "tomcat 8080端口连接数:$apache_conn" >>$mailtmp
echo "tomcat 8080端口每个ip连接数统计如下:" >>$mailtmp
cat $infotmp.tmp.8080|awk '{print $5}'|awk -F ':' '{print $4}'|sort |uniq -c|sort >>$mailtmp
echo "------------------------------------------------------------" >>$mailtmp

echo -e "$stat_help">>$mailtmp


DT=`date +"%Y-%m-%d %H:%M:%S"`
echo -e "\nBest Wishes! \n------------------------------------ \nCA system Monitor \n$DT" >> $mailtmp

cat $mailtmp
#cat $mailtmp| mailx -s "$warn_name[网络监测]:服务器:$IP" $maillist

 

邮件提醒效果

\
 

相关内容