• java进程临时jvm内存监控pushgateway脚本


    对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
  • 相关阅读:
    谈谈golang的netpoll原理(一)
    谈谈高并发秒拍系统架构设计
    Go项目实战:打造高并发日志采集系统(十)
    Go项目实战:打造高并发日志采集系统(九)
    Go项目实战:打造高并发日志采集系统(八)
    Go项目实战:打造高并发日志采集系统(七)
    海思3520v300NDI协议移植
    支持ssl的boa服务器
    多字节和宽字节互相转换
    C++实现websocket协议通讯
  • 原文地址:https://www.cnblogs.com/xiaoleimagic/p/15926589.html
Copyright © 2020-2023  润新知