PATH=/usr/local/mysql/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
echo "usage: c_check_net.sh 作用是:相关服务器的链接数状况报告
该脚本用来检查某台服务器(web或DB)当时的连接情况,如果用在web服务器上,可以检查远程
计算机连接该服务器80端口的情况以及该服务器连接相关DB的情况;如果用在db服务器上,它只
可检查出远程计算机连接该服务器3306端口的情况.
usage: ./c_check_net.sh <ip> <web or db> [mail]
这里的<>表示是必写的参数,[]表示可写可不写,主要用来以邮件的格式发送其检查结果
#!/usr/local/bin/bash
# Author: LG <
[email protected]>
PATH=/usr/local/mysql/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
export PATH
if [ $# -lt 2 ]
then
echo "usage: <ip> <web or db> [mail]"
echo -e "example:\n 10.0.153.118 web "
exit 1
fi
if [ = "10.0.153.126" ]
then
_ssh="ssh -1"
else
_ssh=ssh
fi
_ip=
_mail=
_net="TIME_WAIT|ESTABLISHED|LAST_ACK|CLOSING|FIN_WAIT|CLOSE_WAIT|SYN_RECEVED|SYN_SENT"
_HOST_TYPE=`$_ssh $_ip -- uname`
TMPFILE=/tmp/server.tmp
_DATE=`date`
Subject="!** ldfsys: $_ip net status Report **!"
To="
[email protected]"
function db
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:">$TMPFILE
echo -e " num DB server list">>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
}
function web
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip':80") print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'.80") print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:" >$TMPFILE
echo -e " num DB server list" >>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
echo -e "From under server -->To $_ip:80">>$TMPFILE
echo -e " num remote server list">>$TMPFILE
echo -e "$_STATUS_WEB">>$TMPFILE
}
if [ = "db" ]
then
db
else
web
fi
cat $TMPFILE
if [ $_mail = "mail" ] >/dev/null 2>&1
then
mail -s "$Subject" $To <$TMPFILE
fi
<ip> <web or db> [mail]"
echo -e "example:\n c_check_net.sh 作用是:相关服务器的链接数状况报告
详细说明:
该脚本用来检查某台服务器(web或DB)当时的连接情况,如果用在web服务器上,可以检查远程
计算机连接该服务器80端口的情况以及该服务器连接相关DB的情况;如果用在db服务器上,它只
可检查出远程计算机连接该服务器3306端口的情况.
使用方法:
usage: ./c_check_net.sh <ip> <web or db> [mail]
这里的<>表示是必写的参数,[]表示可写可不写,主要用来以邮件的格式发送其检查结果
#!/usr/local/bin/bash
# Author: LG <
[email protected]>
PATH=/usr/local/mysql/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
export PATH
if [ $# -lt 2 ]
then
echo "usage: <ip> <web or db> [mail]"
echo -e "example:\n 10.0.153.118 web "
exit 1
fi
if [ = "10.0.153.126" ]
then
_ssh="ssh -1"
else
_ssh=ssh
fi
_ip=
_mail=
_net="TIME_WAIT|ESTABLISHED|LAST_ACK|CLOSING|FIN_WAIT|CLOSE_WAIT|SYN_RECEVED|SYN_SENT"
_HOST_TYPE=`$_ssh $_ip -- uname`
TMPFILE=/tmp/server.tmp
_DATE=`date`
Subject="!** ldfsys: $_ip net status Report **!"
To="
[email protected]"
function db
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:">$TMPFILE
echo -e " num DB server list">>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
}
function web
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip':80") print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'.80") print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:" >$TMPFILE
echo -e " num DB server list" >>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
echo -e "From under server -->To $_ip:80">>$TMPFILE
echo -e " num remote server list">>$TMPFILE
echo -e "$_STATUS_WEB">>$TMPFILE
}
if [ = "db" ]
then
db
else
web
fi
cat $TMPFILE
if [ $_mail = "mail" ] >/dev/null 2>&1
then
mail -s "$Subject" $To <$TMPFILE
fi
10.0.153.118 web "
exit 1
fi
if [ = "10.0.153.126" ]
then
_ssh="ssh -1"
else
_ssh=ssh
fi
_ip=
_mail=
_net="TIME_WAIT|ESTABLISHED|LAST_ACK|CLOSING|FIN_WAIT|CLOSE_WAIT|SYN_RECEVED|SYN_SENT"
_HOST_TYPE=`$_ssh $_ip -- uname`
TMPFILE=/tmp/server.tmp
_DATE=`date`
Subject="!** ldfsys: $_ip net status Report **!"
To="
[email protected]"
function db
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:">$TMPFILE
echo -e " num DB server list">>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
}
function web
{
if [ $_HOST_TYPE = "Linux" ]
then
_STATUS=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*:3306/) print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`$_ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip':80") print }' | sed 's#:[0-9]*$##' | sort | uniq -c | sort -r`
else
_STATUS=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'" && ~/10.0.153.*.3306/) print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
_STATUS_WEB=`ssh $_ip -- netstat -an | grep -iE $_net | awk '{if(~"'$_ip'.80") print }' | sed 's#.[0-9]*$##' | sort | uniq -c | sort -r`
fi
echo -e "From $_ip -->To under DB status:" >$TMPFILE
echo -e " num DB server list" >>$TMPFILE
echo -e "$_STATUS">>$TMPFILE
echo -e "From under server -->To $_ip:80">>$TMPFILE
echo -e " num remote server list">>$TMPFILE
echo -e "$_STATUS_WEB">>$TMPFILE
}
if [ = "db" ]
then
db
else
web
fi
cat $TMPFILE
if [ $_mail = "mail" ] >/dev/null 2>&1
then
mail -s "$Subject" $To <$TMPFILE
fi