简体   繁体   English

LINUX-检查JVM的内部CPU,RAM使用情况

[英]LINUX- Checking the internal CPU, RAM usage of JVM's

So I am trying to check the internal CPU and RAM usage from JVM's. 因此,我试图从JVM检查内部CPU和RAM的使用情况。

The set up is that we have a single server which hosts 39 JVM's each running its own service and has it's own unique CPU & RAM allocation. 设置是,我们有一个服务器,该服务器托管39个JVM,每个JVM运行自己的服务,并具有自己的唯一CPU和RAM分配。 So for example it could be set up like the following: 因此,例如可以将其设置如下:

      CPU(Cores)   RAM(MB)
JVM1      2          300
JVM2      1          50
JVM3      5          1024

These are fictional as I don't have the actual values to hand. 这些都是虚构的,因为我没有实际的价值。

I know the PID's for each of the JVM's but I am wondering how would I see the CPU and RAM usage of each JVM on it's own dissregarding the Host systems usage. 我知道每个JVM的PID,但是我想知道如何不考虑主机系统的使用情况而如何查看每个JVM的CPU和RAM使用情况。 Also is it possible to pass multiple PID 's into a jstat -gc as I will be looking to script this?. 也可以将多个PID传递给jstat -gc因为我将对此编写脚本?

I know that if I use: 我知道如果我使用:

ps -p <PID> -o %CPU %RAM

That will give me the CPU and RAM of that process on the host machine. 那会给我主机上该进程的CPU和RAM。

Any Help is greatly appreciated. 任何帮助是极大的赞赏。

After playing around for a while I came up with the following script which pulls the memory usage and total memory in use to 2DP: 玩了一段时间后,我想到了以下脚本,该脚本将内存使用情况和总使用内存拉到2DP:

Script 脚本

#!/bin/sh

service=$(mktemp)
jstatop=$(mktemp)
ServiceStatsPre=$(mktemp)
datet=$(date +%Y-%m-%d)
hourt=$(date +%H)

ps -ef | grep  java | grep Service | awk '{key=substr($9,31,match($9,"@")-31); serv[key]++}END{for(name in serv){if($9 ~ /Service/){print name}}}'>$service

printf "%-8sService\t\t\t\t%7s   PID\t%7s  Used\t%7s  Total\n" > $ServiceStatsPre

filename=$service
while read -r line; do
if [ ! -z $line ]; then
pid=$(ps -ef | grep  java | grep Service |awk -v svc="$line" '{if (match($9,svc)){print $2}}')
rncnt=0
rnag=1
        while [ $rnag -eq 1 ]; do
                jstat -gc $pid > $jstatop
                if [ $? -ne 0 ]; then
                        sleep 5;
                        rncnt++;
                else
                rnag=0
                fi
                if [ $rncnt -eq 5 ]; then
                        rnag=0
                fi
        done
cat $jstatop | awk '{if (NR !=1) print}'|awk -v pid="$pid" -v svc="$line" -v d="$datet" -v h="$hourt" '{printf("%-40s %7d %6.2fMB %6.2fMB %11s %3s\n",svc,pid,($1+$5+$7)/1024,($3+$4+$6+$8)/1024,d,h)}' >> $ServiceStatsPre
fi
done < $filename
#printf "Date,Hour,Service,Used,Total\n" > Service_Stats.csv  #Uncomment this line on initial Run to create file with headders
cat $ServiceStatsPre | awk '{if (NR !=1) print}' |
awk '{
printf("%-1s,%1s,%1s,%4.2f,%4.2f\n",$5,$6,$1,$3,$4);
}' >> Service_Stats.csv

rm $service;
rm $jstatop;
rm $ServiceStatsPre;

Breakdown 分解

ps -ef | grep  java | grep Service | awk '{key=substr($9,31,match($9,"@")-31); serv[key]++}END{for(name in serv){if($9 ~ /Service/){print name}}}'>$service

This is returning the name of the Service and putting it to a tempary file $service 这将返回服务的名称并将其放置到临时文件$service

printf "%-8sService\t\t\t\t%7s   PID\t%7s  Used\t%7s  Total\n" > $ServiceStatsPre

This is just creating Headings in a Temp file $ServiceStatsPre (I know this isn't needed but I was originally using it to debug to a file not a temp file) 这只是在临时文件$ServiceStatsPre创建标题(我知道这不是必需的,但我最初是用它来调试文件而不是临时文件的)

Outer While loop 外部While循环

filename=$service
if [ ! -z $line ]; then
while read -r line; do
pid=$(ps -ef | grep  java | grep Service |awk -v svc="$line" '{if (match($9,svc)){print $2}}')
rncnt=0
rnag=1
...
cat $jstatop | awk '{if (NR !=1) print}'|awk -v pid="$pid" -v svc="$line" -v d="$datet" -v h="$hourt" '{printf("%-40s %7d %6.2fMB %6.2fMB %11s %3s\n",svc,pid,($1+$5+$7)/1024,($3+$4+$6+$8)/1024,d,h)}' >> $ServiceStatsPre
fi
done < $filename

This is first checking to see if the line that it is passing in is not empty ( if [ ! -z $line ]; then ) then it is extracting the PID for each of the services in the temp file $service and is setting a check ( rnag ) + retry counter ( rncnt ). 这是先检查传入的行是否不为空( if [ ! -z $line ]; then ),然后为临时文件$service中的每个服务提取PID ,并设置一个检查( rnag )+重试计数器( rncnt )。 The final part of this outer while loop is calculating the stats, memory usage in MB ( ($1+$5+$7)/1024 ), Total Memory used ( ($3+$4+$6+$8)/1024 ) as well as retuning the PID ,Service Name,Date and Hour. 该外部while循环的最后一部分是计算统计信息,以MB内存使用量( ($1+$5+$7)/1024 ),已使用的总内存( ($3+$4+$6+$8)/1024 )以及重新调整PID ,服务名称,日期和小时。

Inner While Loop 内边循环

...
while [ $rnag -eq 1 ]; do
                jstat -gc $pid > $jstatop
                if [ $? -ne 0 ]; then
                        sleep 5;
                        rncnt++;
                else
                rnag=0
                fi
                if [ $rncnt -eq 5 ]; then
                        rnag=0
                fi
        done
...

This is to basically ensure that the jstat is running correctly and isn't returning an error. 这是从根本上确保jstat正常运行并且不返回错误。

The final part of this script: 该脚本的最后一部分:

#printf "Date,Hour,Service,Used,Total\n" > Service_Stats.csv  #Uncomment this line on initial Run to create file with headders
cat $ServiceStatsPre | awk '{if (NR !=1) print}' |
awk '{
printf("%-1s,%1s,%1s,%4.2f,%4.2f\n",$5,$6,$1,$3,$4);
}' >> Service_Stats.csv

is just to resolve the formatting and put it into a format so that it can be used in a CSV . 只是为了解决格式问题并将其放入一种格式中,以便可以在CSV使用它。

Output 输出量

This script out puts like the following: 该脚本如下所示:

Date,Hour,Service,Used,Total
2016-07-03,07,undMFSAdapterServiceVM,331.00,188.87
2016-07-03,07,entSCSServiceVM,332.50,278.19
2016-07-03,07,kServiceVM,457.50,132.91
2016-07-03,07,ServiceTuTrackVm,432.00,282.66
2016-07-03,07,ter1WMSAdapterServiceVm,233.00,77.02
2016-07-03,07,kingMFSAdapterServiceVM,451.50,126.69
2016-07-03,07,erBuilderServiceVM,261.50,211.27
2016-07-03,07,ter3MFSAdapterServiceVM,449.50,210.23
2016-07-03,07,rServiceVM1,1187.00,529.26
2016-07-03,07,rServiceVM2,597.50,398.43
2016-07-03,07,rServiceVM3,2786.00,819.30
2016-07-03,07,rServiceVM4,451.50,163.13
2016-07-03,07,MessagingServiceVm,457.50,357.11
2016-07-03,07,viceVM,444.50,263.59
2016-07-03,07,ServiceVM,1910.50,909.19
2016-07-03,07,undPackingMFSAdapterServiceVM,208.00,113.51
2016-07-03,07,gisticLockServiceVM,245.00,173.05
2016-07-03,07,kingMFSAdapterServiceVM,781.50,327.13
2016-07-03,07,ferWMSAdapterServiceVm,196.00,84.02
2016-07-03,07,geMFSAdapterServiceVM,499.50,256.91
2016-07-03,07,ferMFSAdapterServiceVM,456.50,246.89
2016-07-03,07,kingWMSAdapterServiceVm,195.00,73.70
2016-07-03,07,AdapterServiceVm,149.50,72.62
2016-07-03,07,ter2MFSAdapterServiceVM,455.00,136.02
2016-07-03,07,ionServiceVM,484.00,240.46
2016-07-03,07,ter3WMSAdapterServiceVm,266.00,138.70
2016-07-03,07,iceVm,135.50,106.07
2016-07-03,07,viceVm,3317.00,1882.15
2016-07-03,07,entBCSServiceVM,356.50,143.93
2016-07-03,07,ServiceVm,951.00,227.12
2016-07-03,07,lingServiceVM,145.50,76.61
2016-07-03,07,entDBHServiceVM,182.50,4.63
2016-07-03,07,kingWMSAdapterServiceVm,208.00,103.13
2016-07-03,07,gingServiceVM,1529.50,235.84
2016-07-03,07,ServiceVM,249.50,131.78
2016-07-03,07,ter1MFSAdapterServiceVM,453.00,394.11
2016-07-03,07,AdapterServiceVM,461.00,208.41
2016-07-03,07,ter2WMSAdapterServiceVm,178.50,79.93
2016-07-03,07,AdapterServiceVm,395.00,131.08
2016-07-03,07,ingServiceVM,184.50,126.28

The $3 has been trimmed just to hide service names etc.. 削减$3只是为了隐藏服务名称等。

Helpful Links 有用的网址

A nice explanation of jstat -gc output jstat -gc输出的一个很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM