对java服务监控,应使用jmx_exporter,但对于一些已经运行的java程序进行临时内存监控,可使用一下脚本将采集数据推送到pushgateway
#!/bin/bash Jid=`jps | grep $1 | awk '{print $1}'` LineC=0 IFS=$'\n' for line in `jmap -heap $Jid` do ((LineC++)) if [[ $line == *"MaxHeapSize"* ]] then MaxHeapSizeKey="MaxHeapSize" MaxHeapSizeValue=`echo $line | awk '{print $3}'` echo "$MaxHeapSizeKey $MaxHeapSizeValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; ########### Eden Space ############# elif [[ $line == *"Eden Space:"* ]] then EdenC=$LineC elif [[ ($EdenC+3 -gt $LineC) && ($line == *"capacity"*) ]] then EdenCapacityKey="EdenCapacity" EdenCapacityValue=`echo $line | awk '{print $3}'` echo "$EdenCapacityKey $EdenCapacityValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; elif [[ ($EdenC+3 -gt $LineC) && ($line == *"used"*) ]] then EdenUsedKey="EdenUsed" EdenUsedValue=`echo $line | awk '{print $3}'` echo "$EdenUsedKey $EdenUsedValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; ########### From Space ############# elif [[ $line == *"From Space:"* ]] then FromC=$LineC elif [[ ($FromC+3 -gt $LineC) && ($line == *"capacity"*) ]] then FromCapacityKey="FromCapacity" FromCapacityValue=`echo $line | awk '{print $3}'` echo "$FromCapacityKey $FromCapacityValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; elif [[ ($FromC+3 -gt $LineC) && ($line == *"used"*) ]] then FromUsedKey="FromUsed" FromUsedValue=`echo $line | awk '{print $3}'` echo "$FromUsedKey $FromUsedValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; ########### To Space ############# elif [[ $line == *"To Space:"* ]] then ToC=$LineC elif [[ ($ToC+3 -gt $LineC) && ($line == *"capacity"*) ]] then ToCapacityKey="ToCapacity" ToCapacityValue=`echo $line | awk '{print $3}'` echo "$ToCapacityKey $ToCapacityValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; elif [[ ($ToC+3 -gt $LineC) && ($line == *"used"*) ]] then ToUsedKey="ToUsed" ToUsedValue=`echo $line | awk '{print $3}'` echo "$ToUsedKey $ToUsedValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; ########### concurrent mark-sweep generation ############# elif [[ $line == *"concurrent mark-sweep generation:"* ]] then OldC=$LineC elif [[ ($OldC+3 -gt $LineC) && ($line == *"capacity"*) ]] then OldCapacityKey="OldCapacity" OldCapacityValue=`echo $line | awk '{print $3}'` echo "$OldCapacityKey $OldCapacityValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; elif [[ ($OldC+3 -gt $LineC) && ($line == *"used"*) ]] then OldUsedKey="OldUsed" OldUsedValue=`echo $line | awk '{print $3}'` echo "$OldUsedKey $OldUsedValue" | curl --data-binary @- http://10.212.155.203:9091/metrics/job/jvm_mem_$1/instance/10.212.155.243; fi done unset IFS
配置crontab,注意source java环境变量
* * * * * source /root/javahome.sh && /bin/bash /root/GetJvmMemStatus.sh HRegionServer > /dev/null 2>&1