Linux 检查端口gps命令

由于是游戏业务,环境主要是Nginx+Tomcat+Java Program

gps脚本环境以及效果图如下:

#!/bin/bash

function Printf (){
    if [ $1 == 1 ];then
        printf '%-50s\n' "+-----------------+------------------+-----------------+"
        shift 1
    fi
    printf '%1s %-10s %1s %10s  %1s %10s %1s\n' "|"  "$1"  "|" "$2" "|" "$3" "|"
    printf '%-50s\n' "+-----------------+------------------+-----------------+"
}

function GameStatus (){
    Port=$1
    Path=$2
    Name=$3
    local SiteValue=$4
    Ppid=$5
    nc -vz 127.0.0.1 ${Port} >/dev/null 2>&1
    if [ $? == 0 ];then
        Status=$(echo -e "\e[32m OK  \e[0m")
    else
        Status=$(echo -e "\e[31mError\e[0m")
    fi
    if [[ -z ${Ppid} ]];then
        if [ "$USER" == "root" ];then
            Pid=$(netstat -nltp | egrep -w "0.0.0.0:${Port}|0 :::${Port}" | awk '{print $7}' | sed 's#/.*##')
        else
            Pid=$(sudo netstat -nltp | egrep -w "0.0.0.0:${Port}|0 :::${Port}" | awk '{print $7}' | sed 's#/.*##')
        fi
    else
        Pid=$(ps -ef | grep -i "${Path}/" |grep -i "${Name}/" |grep -v "grep" | awk '{print $2}')
    fi
    printf '%-20s %-6s %-15s %-12s %-25s %-6s %-15s\n' "${Date}" "${Pid}" "${Name}" "${SiteValue}" "${Path}" "${Port}" "Status:[${Status}]"
}

function GameCheck (){
    GamePath=(/ccu/server /usr/local/tomcat /data/dandantang)
    GsPort=(8004 8080 9200)
    GameStatus 80 /usr/local/nginx Nginx " "
    echo -e "\e[35m--------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
    for (( i=0;i<3;i++ ))
    do
        GameStatus ${GsPort[$i]} ${GamePath[$i]} GameServer
    echo -e "\e[35m--------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
    done
}

Online_check(){
    echo -e "\e[35m-------------------------- Online Info.  ----------------------------\e[0m"
    local Ports="9200 9300"
    local Sum_online=0
    for Port in $Ports
    do
        local Port_online=`netstat -ntp|awk -v a=0 \
            '/ESTABLISHED/&&$4~":'$Port'"{a+=1}END{print a}'`
        Sum_online=$(($Sum_online + $Port_online))
        case $Port in
            9200)
                Port_name="Gameserver1_port(9200):"
                ;;
            9300)
                Port_name="Gameserver2_port(9300):"
                ;;
        esac
        printf "%-25s%14s\n" "$Port_name" "$Port_online"
    done

    printf "%-25s%14s\n" "Gameonline:" "$Sum_online"
}

Date=`date '+[%Y-%m-%d %H:%M:%S]'`
echo "Main Site:$(hostname)"
echo -e "\e[35m--------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
GameCheck
Online_check
gps

posted @ 2017-02-20 15:55  Mr黄瑞  阅读(1344)  评论(0编辑  收藏  举报