监控服务器信息脚本

#!/bin/bash
echo Version:2.0.0 2020-12-11
#####循环间隔(秒)#####
INTERVAL_TIME=300
#####需要统计并发的端口#####
L_PORTS="80 8080 8009 88"

############################################使用方法 nohup /opt/shell/s-monitorlite2.0.sh & ####################################################################################

cd `dirname $0`
while : ; do
    PORT_STATUS=''
    TOMCAT_STATUS=''
    Apache_STATUS=''
    Apache_CPUSUM=0
    Apache_MEMSUM=0
    Nginx_STATUS=''
    START_TIME=`date +'%Y-%m-%d %H:%M:%S'`
    CPU_USED=$(( 100 - `top -d 0.1 -b -n 2 | grep -w Cpu | tail -1 | awk -F',' '{print $4}' | awk -F'.' '{print $1}'` ))
    SER_LOAD=`uptime | awk -F':' '{print $NF}'`
    MEM=`free -m`
	
    for i in $L_PORTS; do
        L_PORT=$i
        PORT_COUNT=`netstat -an |grep  ":$i "|grep ESTABLISHED |wc -l`
        PORT_STATUS=$PORT_STATUS"PORT${L_PORT}COUNT:$PORT_COUNT "
    done

    TOMCAT_TOTAL=`ps -ef | grep java | grep 'Dcatalina.base=' | wc -l` 
    if [ $TOMCAT_TOTAL -ge 1 ] ; then
        TOMCAT_PIDS=`ps -ef | grep java | grep 'Dcatalina.base=' | awk '{print $2}'`
        for i in $TOMCAT_PIDS; do
            TOMCAT_PID=$i
            TOMCAT_CPU=`top -b -n 1 | grep java | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $9}'`
            TOMCAT_MEM=`top -b -n 1 | grep java | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $6}'`
            TOMCAT_STATUS=$TOMCAT_STATUS"TOMCAT_PID:$TOMCAT_PID TOMCAT_CPU:$TOMCAT_CPU TOMCAT_MEM:$TOMCAT_MEM\n"
        done
    fi

   Apache_TOTAL=`ps -ef | grep httpd | grep -v 'grep' | wc -l`
    if [ $Apache_TOTAL -ge 1 ] ; then
        Apache_PIDS=`pidof httpd`
        for i in $Apache_PIDS; do
            Apache_PID=$i
            Apache_CPU=`top -b -n 1 | grep httpd | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $9}'`
            Apache_MEM=`top -b -n 1 | grep httpd | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $6}'`
            Apache_CPU=`echo $Apache_CPU | awk -F'.' '{print $1}'`
            Apache_MEM=`echo $Apache_MEM | awk -F'.' '{print $1}'`
            let Apache_CPUSUM=$Apache_CPUSUM+$Apache_CPU
            let Apache_MEMSUM=$Apache_MEMSUM+$Apache_MEM
        done
         Apache_STATUS=$Apache_STATUS"Apache_PID:$Apache_PID Apache_CPU:$Apache_CPU Apache_MEM:$Apache_MEM"kb"
    fi
   
   Nginx_TOTAL=`ps -ef | grep nginx | grep -v 'grep' | wc -l`
    if [ $Nginx_TOTAL -ge 1 ] ; then
        Nginx_PIDS=`pidof nginx`
        for i in $Nginx_PIDS; do
            Nginx_PID=$i
            Nginx_CPU=`top -b -n 1 | grep nginx | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $9}'`
            Nginx_MEM=`top -b -n 1 | grep nginx | sed 's/^[ \t]*//g' | grep "^$i" | awk '{print $6}'`
            Nginx_STATUS=$Nginx_STATUS"Nginx_PID:$Nginx_PID Nginx_CPU:$Nginx_CPU Nginx_MEM:$Nginx_MEM"kb"\n"
        done
    fi

    echo -e "TIME:$START_TIME\nCPU_USED:${CPU_USED}%\nSERVER_LOAD:$SER_LOAD\n$PORT_STATUS\n$TOMCAT_STATUS$Apache_STATUS$Nginx_STATUS$MEM\n">> ./s-monitorlite-`date +%F`.log
    sleep $INTERVAL_TIME
done

你可能感兴趣的:(服务器,linux,shell)