Shell之-Tomcat资源检测

Tomcat资源检测

首先,大家如果看到有什么不懂的地方,欢迎吐槽!!!
我会在当天或者第二天及时回复,并且改进~~

#!/bin/bash 
#空闲内存 JVM Free_menory
#总内存 JVM Total_memory
#最大内存 JVM Max_memory
#最大线程 http-bio Max_threads
#当前线程数 http-bio Current_thread_count
#当前繁忙线程数 http-bio Current_thread_busy
#最大处理时间 http-bio Max_processing_time
#最短处理时间 http-bio Processing_time
#请求数 http-bio Request_count
#错误数 http-bio Error_count
#接收字节 http-bio Bytes_received
#发送字节 http-bio Bytes_sent

#####Get_Resource#####
    function Get_Resource () {
    Free_memory=$(ssh ${Tomcat_Server[$i]} "$curl"          | awk -F'Free memory: ' '{print $2}' | awk -F'Total' '{print $1}')
    Total_memory=$(ssh ${Tomcat_Server[$i]} "$curl"         | awk -F'Total memory: ' '{print $2}' | awk -F'Max memory' '{print $1}')
    Max_memory=$(ssh ${Tomcat_Server[$i]} "$curl"           | awk -F'Max memory: ' '{print $2}' | awk -F'</p>' '{print $1}')
    Max_threads=$(ssh ${Tomcat_Server[$i]} "$curl"          | awk -F'http-bio' '{print $2}' | awk -F'ds: ' '{print $2}' | awk -F' C' '{print $1}')
    Current_thread_count=$(ssh ${Tomcat_Server[$i]} "$curl" | awk -F'http-bio' '{print $2}' | awk -F'count: ' '{print $2}' | awk -F'Current' '{print $1}')
    Current_thread_busy=$(ssh ${Tomcat_Server[$i]} "$curl"  | awk -F'http-bio' '{print $2}' | awk -F'busy: ' '{print $2}' | awk -F'<br>' '{print $1}')
    Max_processing_time=$(ssh ${Tomcat_Server[$i]} "$curl"  | awk -F'http-bio' '{print $2}' | awk -F'Max processing time: ' '{print $2}' | awk -F' Processing' '{print $1}')
    Processing_time=$(ssh ${Tomcat_Server[$i]} "$curl"      | awk -F'http-bio' '{print $2}' | awk -F'Max processing time:' '{print $2}' | awk -F'time: ' '{print $2}' | awk -F' Request' '{print $1}')
    Request_count=$(ssh ${Tomcat_Server[$i]} "$curl"        | awk -F'http-bio' '{print $2}' | awk -F'Request count: ' '{print $2}' | awk -F' Error' '{print $1}')
    Error_count=$(ssh ${Tomcat_Server[$i]} "$curl"          | awk -F'http-bio' '{print $2}' | awk -F'Error count: ' '{print $2}' | awk -F' Bytes' '{print $1}')
    Bytes_received=$(ssh ${Tomcat_Server[$i]} "$curl"       | awk -F'http-bio' '{print $2}' | awk -F'Bytes received: ' '{print $2}' | awk -F' Bytes sent: ' '{print $1}')
    Bytes_sent=$(ssh ${Tomcat_Server[$i]} "$curl"           | awk -F'http-bio' '{print $2}' | awk -F'Bytes sent: ' '{print $2}' | awk -F'</p>' '{print $1}')
}

#####Mysql_Auth#####
User="root"
Pass="p-0p-0p-0"
Host="192.168.0.225"
Date=$(date +%Y-%m-%d-%H-%M)
Tomcat_Path="/usr/local/tomcats"
Tomcat_Server=(192.168.0.223 192.168.0.221 192.168.0.211 192.168.0.209 192.168.0.206  192.168.0.205 192.168.0.203)

function Create_Databse () {
    mysql -u${User} -p${Pass} -h${Host} -e "show databases;" | grep Tomcat_${Database}  &> /dev/null
    if [ ! $? -eq 0 ] ;then
        mysql -u${User} -p${Pass} -h${Host} -e "create database Tomcat_${Database}" &> /dev/null
    fi
}

function Create_Table () {
    Table_Name=$(echo ${Tomcat_List[$n]} |awk -F'-' '{print $1}')
    mysql -u${User} -p${Pass} -h${Host} -e "use Tomcat_${Database};show tables" | grep -w ${Table_Name} &> /dev/null
    if [ ! $? -eq 0 ] ; then
        mysql -u${User} -p${Pass} -h${Host} -e "use Tomcat_${Database};create table ${Table_Name} (Date CHAR(48),Free_memory CHAR(48),Total_memory CHAR(48),Max_memory CHAR(48),Max_threads CHAR(48),Current_thread_count CHAR(48),Currnt_thread_busy CHAR(48),Max_processing_time CHAR(48),Processing_time CHAR(48),Request_count CHAR(48),Error_count CHAR(48),Bytes_received CHAR(48),Bytes_sent CHAR(48));" &>/dev/null
    fi
}

function Insert_Table () {
    mysql -u${User} -p${Pass} -h${Host} -e "insert into Tomcat_${Database}.${Table_Name} Value('$Date','$Free_memory','$Total_memory','$Max_memory','$Max_threads','$Current_thread_count','$Currnt_thread_busy','$Max_processing_time','$Processing_time','$Request_count','$Error_count','$Bytes_received','$Bytes_sent')"
}

for (( i = 0; i < ${#Tomcat_Server[@]}; i++)) 
do
    Database=$(echo ${Tomcat_Server[$i]} | awk -F'.' '{print $NF}')

    Create_Databse

    Tomcat_List=($(ssh ${Tomcat_Server[$i]} ls -l ${Tomcat_Path} | grep ^d | egrep -v "(D:|deploy)" | awk '{print $NF}'))
    for (( n = 0; n < ${#Tomcat_List[@]}; n++))
    do
        Table_Name=$(echo ${Tomcat_List[$n]} |awk -F'-' '{print $1}')
        port=$(ssh ${Tomcat_Server[$i]} "cat $Tomcat_Path/${Tomcat_List[$n]}/conf/server.xml" | egrep 'Connector port=' |egrep -v "(SSL|redir)" | awk -F'"' '{print $2}')
        curl="curl -s -u yunjee:yunjee0515 ${Tomcat_Server[$i]}:$port/manager/status | egrep '<h1>JVM</h1>'"

    Get_Resource

    Create_Table

    Insert_Table    
    done
done

当然,如果需要查看可以通过navicat连接数据库进行查看,也可以通过select 查看表。由于只是通过Shell写的,比较简单,并没有好的呈现出来~~

你可能感兴趣的:(tomcat,shell)