prometheus监控java程序

可参考链接,需找开发商量jmx端口暴露方式

https://www.jianshu.com/p/8a5e681b18ce 或者 http://www.mamicode.com/info-detail-2323750.html

上图两个链接为不同的jmx暴露方式,实则大同小异,需跟开发商量,以免程序启动不了

下载jmx_exporter的jar包

https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar

创建配置文件namenode.yaml(datanode.yaml)放在任意位置,内容为你想要的metrics

参考配置:

---
startDelaySeconds: 0
hostPort: master:1234 #master为本机IP(一般可设置为localhost);1234为想设置的jmx端口(可设置为未被占用的端口)
#jmxUrl: service:jmx:rmi:///jndi/rmi://127.0.0.1:1234/jmxrmi
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false

 

其他参数参考:

NameDescription
startDelaySecondsstart delay before serving requests. Any requests within the delay period will result in an empty metrics set.
hostPortThe host and port to connect to via remote JMX. If neither this nor jmxUrl is specified, will talk to the local JVM.
usernameThe username to be used in remote JMX password authentication.
passwordThe password to be used in remote JMX password authentication.
jmxUrlA full JMX URL to connect to. Should not be specified if hostPort is.
sslWhether JMX connection should be done over SSL. To configure certificates you have to set following system properties:
-Djavax.net.ssl.keyStore=/home/user/.keystore
-Djavax.net.ssl.keyStorePassword=changeit
-Djavax.net.ssl.trustStore=/home/user/.truststore
-Djavax.net.ssl.trustStorePassword=changeit
lowercaseOutputNameLowercase the output metric name. Applies to default format and name. Defaults to false.
lowercaseOutputLabelNamesLowercase the output metric label names. Applies to default format and labels. Defaults to false.
whitelistObjectNamesA list of ObjectNames to query. Defaults to all mBeans.
blacklistObjectNamesA list of ObjectNames to not query. Takes precedence over whitelistObjectNames. Defaults to none.
rulesA list of rules to apply in order, processing stops at the first matching rule. Attributes that aren‘t matched aren‘t collected. If not specified, defaults to collecting everything in the default format.
patternRegex pattern to match against each bean attribute. The pattern is not anchored. Capture groups can be used in other options. Defaults to matching everything.
attrNameSnakeCaseConverts the attribute name to snake case. This is seen in the names matched by the pattern and the default format. For example, anAttrName to an_attr_name. Defaults to false.
nameThe metric name to set. Capture groups from the pattern can be used. If not specified, the default format will be used. If it evaluates to empty, processing of this attribute stops with no output.
valueValue for the metric. Static values and capture groups from the pattern can be used. If not specified the scraped mBean value will be used.
valueFactorOptional number that value (or the scraped mBean value if value is not specified) is multiplied by, mainly used to convert mBean values from milliseconds to seconds.
labelsA map of label name to label value pairs. Capture groups from pattern can be used in each. name must be set to use this. Empty names and values are ignored. If not specified and the default format is not being used, no labels are set.
helpHelp text for the metric. Capture groups from pattern can be used. name must be set to use this. Defaults to the mBean attribute decription and the full name of the attribute.
typeThe type of the metric, can be GAUGECOUNTER or UNTYPEDname must be set to use this. Defaults to UNTYPED.

前提:

1.java程序的目录结构展示(因断网环境所以没办法用树状图命令tree来展示,只能截图)

如上图所示,一个服务器有一个java程序,其中子程序包括manager(管理端), server,client,PRM, Utilities,关联关系是agent和server要从manager管理端获取各种参数,server再向agent发包,这样程序就算跑起来了。

这里主要监控server和client

下图为manager和server以及其中一个client的目录结构

各conf目录下都有一个namenode.conf,内容如下

startDelaySeconds: 10
jmxUrl: service:jmx:rmi:///jndi/rmi://127.0.0.1:${JMX_PORT}/${APP_NAME}
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false

各conf目录下都有一个namenode.yaml,内容如下(此为manager下conf中的)

各个子程序bin目录下都有关于程序的启动脚本,先执行1脚本启动,然后2脚本是让程序开启一个prometheus jmx端口,以便jmx exporter探针找到这个端口,收集到java程序的metrics信息,达到监控的目的

下图是npds-manager.sh脚本中实现开启jmx端口的脚本信息,完整脚本信息在最下面

这样启动java程序后,访问ip加自定义的端口9210就能访问到manager的mertrics信息了,达到监控的目的

 

然后找开发要他们想要监控的参数,以我为例要的参数如下图:

 

 

 

再以此一个个配置,就能实现监控了

vim npds-manager.sh

#!/bin/bash
then
    echo "=================================================================="
    echo "ERROR: Please set the JAVA_HOME variable in your environment!"
    echo "=================================================================="
    exit 20
fi

cd=$(pwd)

if [ ${cd:0-3} != "bin" ]
then
    echo "=================================================================="
    echo "ERROR: Please execute the script in the bin directory!"
    echo "=================================================================="
    exit 21
fi

APP_HOME=${cd%/bin}

APP_NAME=NPDS-Manager

APP_MAIN_CLASS=cn.com.greattimes.npds.manager.boot.ManagerBootstrap

JPS_FLAG=ManagerBootstrap

LIBRARY_PATH=$APP_HOME/jni


JAVA_OPTS="-server
-Xmx1G
-Xms1G"

DEBUG=0

RET_CODE=0

FORCE_KILL=0

CHECK_STATUS=1

CHECK_STATUS_TIMEOUT=5

check_env() {
    local DIR=${APP_HOME:POS+1}

    if [ ! $DIR == $APP_NAME ]
    then
        echo "=================================================================="
        exit 22
    fi

    if [ ! -d "$APP_HOME/logs" ]
    then
        mkdir $APP_HOME/logs
    fi

    if [ $DEBUG -eq 1 ]
    then
        local DBG_PORT=9310
    fi
}

check_prometheus() {
    if [ -e "$APP_HOME/conf/namenode.conf" ]
    then
        local JMX_PORT=15914
    fi

    if [ -e "$APP_HOME/conf/namenode.yaml" ]
    then
        local WEB_PORT=9210
    fi
}

check() {
    PID=0
    local TMP=$(jps -v | grep $JPS_FLAG | grep $APP_HOME)
    TMP=${TMP%% *}

    if [ ${#TMP} -ne 0 ]
    then
        PID=$TMP
    fi

    if [ $PID -ne 0 ]
    then
        RUNNING=1
    else
        RUNNING=0
    fi
}

print() {
    if [ $QUIET_MODE -eq 1 ]
    then
        return
    fi

    echo "$1" "$2"
}
    if [ ! -n "$1" ]
    then
        return 2
    fi

    ps -p $1 > /dev/null

    if [ $? -eq 0 ]
    then
        kill -9 $1
    fi
}

start() {
    check

    if [ $RUNNING -eq 1 ]
    then
        print "=================================================================="
        print "WARN: $APP_NAME(PID=$PID) is running."
        print "=================================================================="
        return 1
    fi

    info

    if [ $CHECK_STATUS -eq 0 ]
    then
        print -n "..."
        print
        print "------------------------------------------------------------------"
        print "WARN: $APP_NAME is started without checking status."
        print
        return 1
    fi

    local n=0

    for ((; n<$CHECK_STATUS_TIMEOUT; n++))
    do
        if [ $n -lt 3 ]
        then
            sleep 1
            print -n "."
        else
            check

            if [ $RUNNING -eq 1 ]
            then
                print
                print "Done!"
                print
                return 0
            else
                sleep 1
            fi
        fi
    done

    print
    print "------------------------------------------------------------------"
    print "ERROR: $APP_NAME failed to start for unknown reasons. "
    print "       Please refer to the log file for details."
    print
    return 2
stop() {
    check

    if [ $RUNNING -eq 0 ]
    then
        print "=================================================================="
        print "WARN: $APP_NAME is stopped."
        print "=================================================================="
        return 1
    fi

    info
    print -n "Stopping $APP_NAME"

    if [ $FORCE_KILL -eq 1 ]
    then
        print -n "..."
        print

        kill_pid $PID

        print "------------------------------------------------------------------"
        print "WARN: $APP_NAME was killed by force."
        print
        return 1

    if [ $CHECK_STATUS -eq 0 ]
    then
        print -n "..."
        print
        print "------------------------------------------------------------------"
        print "WARN: $APP_NAME is stopped without checking status."
        print
        return 1
    fi

    local n=0

    for ((; n<$CHECK_STATUS_TIMEOUT; n++))
    do
        if [ $n -lt 3 ]
        then
            sleep 1
            print -n "."
        else
            check

            if [ $RUNNING -eq 0 ]
            then
                print
                print "Done!"
                print
                return 0
            else
                sleep 1
            fi
        fi
    done

    kill_pid $PID

    print
    print "------------------------------------------------------------------"
    print "WARN: $APP_NAME was killed for stopping timeout."
    print
    return 1
}

restart() {
    check

    if [ $RUNNING -eq 1 ]
    then
        if [ $CHECK_STATUS -eq 0 ]
        then
            FORCE_KILL=1
        fi
        stop
    fi

    start
}

status() {
    check

    if [ $RUNNING -eq 0 ]
    then
        print "=================================================================="
        print "INFO: $APP_NAME is stopped."
        print "=================================================================="
        return 0
    else
        print "=================================================================="
        print "INFO: $APP_NAME(PID=$PID) is running."
        print "=================================================================="
        return 1
    fi
}

info() {
    if [ $QUIET_MODE -eq 1 ]
    then
        return
    fi

    echo "=================================================================="
    echo "$APP_NAME Information:"
    echo "------------------------------------------------------------------"
    echo "JAVA_HOME=$JAVA_HOME"
    echo "CLASS_PATH=$CLASS_PATH"
    echo
    echo `$JAVA_HOME/bin/java -version`
    echo "APP_HOME=$APP_HOME"
    echo "APP_MAIN_CLASS=$APP_MAIN_CLASS"
    echo "=================================================================="
}

check_env

if [ $# -gt 1 ]
then
    for v in $@
    do
        if [ "$v" == "$1" ]
        then
            continue
        fi
        case "$v" in
            '--quiet')
                QUIET_MODE=1
                ;;
            '--nocheck')
                CHECK_STATUS=0
                ;;
            '--force')
                FORCE_KILL=1
                ;;
            *)
                ;;
        esac
    done
fi

case "$1" in
    'start')
        check_prometheus
        start
        ;;
    'stop')
        check_prometheus
        stop
        ;;
    'restart')
        check_prometheus
        restart
        ;;
    'status')
        status
        ;;
    'info')
        info
        ;;
    *)
        echo "=================================================================="
        echo "Usage: $0 <commands> [options]"
        echo "------------------------------------------------------------------"
        echo "where commands include:"
        echo
        echo -e "      start [options]"
        echo -e "            --quiet\twithout console output"
        echo -e "            --nocheck\twithout checking status"
        echo
        echo -e "       stop [options]"
        echo -e "            --quiet\twithout console output"
        echo -e "            --nocheck\twithout checking status"
        echo -e "            --force\tforce-kill"
        echo
        echo -e "    restart [options]"
        echo -e "            --quiet\twithout console output"
        echo -e "            --nocheck\twithout checking status"
        echo -e "            --force\tstart after force-kill"
        echo
        echo -e "     status \t\tdisplay running status"
        echo
        echo -e "       info \t\tdisplay environment information"
        echo "=================================================================="
        ;;
esac

RET_CODE=`echo $?`

exit $RET_CODE
 

 

### Prometheus Java 应用监控配置示例 为了实现通过 PrometheusJava 应用程序监控,通常需要完成以下几个方面的设置: #### 1. **Spring Boot 集成 Prometheus** 对于基于 Spring Boot 的应用程序,可以利用 Micrometer 提供的支持来暴露指标数据给 Prometheus 抓取。以下是具体的依赖引入和配置方式。 在 `pom.xml` 文件中添加以下依赖项: ```xml <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> ``` 随后,在 `application.properties` 或 `application.yml` 中启用 Prometheus 指标端点: ```properties management.endpoints.web.exposure.include=* management.endpoint.metrics.enabled=true management.metrics.export.prometheus.enabled=true ``` 这一步会使得 `/actuator/prometheus` 成为默认的指标暴露路径[^1]。 #### 2. **Prometheus 配置文件调整** 为了让 Prometheus 正确抓取这些指标,需修改其核心配置文件 `prometheus.yml` 并定义目标地址。下面是一个典型的配置片段: ```yaml scrape_configs: - job_name: 'springboot-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['localhost:8080'] labels: app: 'my-spring-boot-application' ``` 上述配置指定了一个名为 `springboot-app` 的任务,并告知 Prometheus 去访问本地运行的服务实例(假设服务监听于 `http://localhost:8080/actuator/prometheus`)。如果存在多个微服务,则应相应扩展此部分的内容[^3]。 #### 3. **Grafana 可视化展示** 虽然不是严格意义上的模板配置,但 Grafana 是常用的可视化工具之一,配合 Prometheus 使用效果更佳。可以通过导入官方提供的 Dashboard JSON 文件快速构建界面布局。例如针对 JVM 性能分析或者 HTTP 请求统计等方面都有现成方案可供选用[^2]。 #### 示例代码总结 最终完整的流程包括但不限于以上三个主要环节——即客户端侧准备、服务器端采集规则设定以及前端图形化呈现三大部分共同协作才能达成预期目的。 ```python # Python 示例仅用于说明逻辑结构而非实际功能实现 def setup_prometheus_monitoring(): add_maven_dependencies() configure_spring_boot_metrics_endpoint() update_prometheus_scraping_config() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值