Linux 检查端口gps命令

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

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

#!/bin/bash

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

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[31mErrore[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
' "${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
" "$Port_name" "$Port_online"
    done

    printf "%-25s%14s
" "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

原文地址:https://www.cnblogs.com/Mrhuangrui/p/6419928.html