Zabbix使用自动发现功能监控服务器各JVM进程状态(5)

这个脚本的作用就是通过读取文件里面的包名,然后将包名进行json序列化输出,没什么好讲的,套路套一个循环脚本就行。
接下来就是重要的脚本了,调用jstat获取JVM状态,并缓存到文件中。
#cat get_jvmstatus.sh 
    #!/bin/bash
     
    MAINCLASS="*Main.class"
    scriptPath=/usr/local/etc/scripts
     
    cat $scriptPath/package_path.txt|while read line
    do
    packageName=$(echo $line|awk '{print $1}')
    pgRootPath=$(echo $line|awk '{print $2}')
    if [[ -d $pgRootPath/tomcat ]];then
    pid=$(cat $pgRootPath/tomcat/tomcat.pid)
    else
    mainPath=$(find $pgRootPath -name $MAINCLASS)
    appName=$(echo ${mainPath##*classes/}|sed 's#/#.#g'|sed 's#.class##g')
    pid=$(ps -fC java|grep "$appName"|awk '{print $2}')
    fi
    javaHome=/usr/local/java/jdk1.8.0
    #javaHome=/usr/local/java/latest
    #if [[ -r $pgRootPath/appconfig ]] && [  `grep ^JAVA_HOME= $pgRootPath/appconfig|wc -l` == 1 ] && [ `grep ^JAVA_HOME= $pgRootPath/appconfig|grep 8|wc -l` == 1 ];then
                            #javaHome=$(grep ^JAVA_HOME= $pgRootPath/appconfig 2>/dev/null|awk -F'=' '{print $2}')
    #javaHome=/usr/local/java/jdk1.8.0
            #else
            #        if [[ -r $pgRootPath/webconfig ]] && [ `grep ^'export JAVA_HOME=' $pgRootPath/webconfig|wc -l` == 1 ] && [ `grep ^'export JAVA_HOME=' $pgRootPath/webconfig|grep 8|wc -l` == 1 ];then
            #                #javaHome=$(grep ^'export JAVA_HOME=' $pgRootPath/webconfig 2>/dev/null|awk -F'"' '{print $2}')
            #        javaHome=/usr/local/java/jdk1.8.0
    #fi
    #fi
    #echo --------------------------------$pgRootPath
    #echo $javaHome
    echo -------------------------------$pid
    sleep 5
    #echo -n >$scriptPath/package/$packageName
    #$javaHome/bin/jstat -gccapacity $pid > ./package/$packageName 2>/dev/null
    #$javaHome/bin/jmap -heap $pid>>./package/$packageName 2>/dev/null
    echo gcnew >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -gcnew $pid >> $scriptPath/package/$packageName 2>/dev/null
    echo gcutil >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -gcutil $pid >> $scriptPath/package/$packageName 2>/dev/null
    echo gcnewcapacity >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -gcnewcapacity $pid >> $scriptPath/package/$packageName 2>/dev/null
            echo gccapacity >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -gccapacity $pid >> $scriptPath/package/$packageName 2>/dev/null
            #echo gcold >> $scriptPath/package/$packageName 2>/dev/null
    #$javaHome/bin/jstat -gcold $pid >> $scriptPath/package/$packageName 2>/dev/null
            echo gc >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -gc $pid >> $scriptPath/package/$packageName 2>/dev/null
            echo class >> $scriptPath/package/$packageName 2>/dev/null
    $javaHome/bin/jstat -class $pid >> $scriptPath/package/$packageName 2>/dev/null
    echo cpu >> $scriptPath/package/$packageName 2>/dev/null
    echo -e "CPU\n$( ps aux|grep $pid|grep -v grep|awk '{print $3}')" >> $scriptPath/package/$packageName 2>/dev/null
    echo mem >> $scriptPath/package/$packageName 2>/dev/null
    echo -e "MEM\n$( ps aux|grep $pid|grep -v grep|awk '{print $6}')" >> $scriptPath/package/$packageName 2>/dev/null
     
    done

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6ba62603ccf1355ae41cbdd8c7ccf802.html