Ambari集成Prometheus-打包软件

35 篇文章 0 订阅
13 篇文章 1 订阅

目录结构

集成自定义的服务,首先要准备好服务的rpm安装包,Prometheus官方本身发布的就是开箱即用的二进制文件,所以不需要重新编译,但是为了集成Ambari,我们要做一些目录结构的修改,并且自定义一些打包动作,首先是目录结构,更改后结构如下:

prometheus-2.31.1.linux-amd64
|-- bin
|   |-- prometheus
|   `-- promtool
|-- conf
|   `-- prometheus.yml
|-- console_libraries
|   |-- menu.lib
|   `-- prom.lib
|-- consoles
|   |-- index.html.example
|   |-- node-cpu.html
|   |-- node-disk.html
|   |-- node.html
|   |-- node-overview.html
|   |-- prometheus.html
|   `-- prometheus-overview.html
|-- LICENSE
|-- NOTICE
`-- sbin
    |-- prometheus-env.sh
    `-- prometheus.sh

这其中不需要关注consoles和console_libraries目录,这两个都是Prometheus自己带的。

主要是增加了sbin目录,并把配置文件放在conf中,可运行的二进制文件放在bin下,这样安排的目的是我们要用自己的shell脚本控制prometheus启停,我们一个个说。

bin目录

二进制文件放bin下,方便日后集成其他的exporter到Prometheus服务,比如node_exporter等,我们都作为Prometheus这个Service的一个Component,这样也更便于管理;

conf目录

也是为了便于管理,所有exporter的配置和Prometheus的配置都放在这里,包括alertmanger;

sbin目录

启动脚本和环境脚本的存放目录,借鉴了其他大数据组件的目录结构,这里针对prometheus组件编写了两个脚本:

环境设置脚本prometheus-env.sh

这里的会编写默认配置,包括了日志目录、pid号目录等:

#!/bin/bash
PROMETHEUS_LOG_DIR='/var/log/prometheus'
PROMETHEUS_PID_DIR='/var/run/prometheus'
PROMETHEUS_SERVER_PORT='9090'
PROMETHEUS_DATA_DIR='/var/lib/prometheus/data'
PROMETHEUS_CONF='conf/prometheus.yml'
PROMETHEUS_LIFECYCLE='30'
PROMETHEUS_WEB_LIFECYCLE_ENABLE='true'
PROMETHEUS_WEB_ADMINAPI_ENABLE='true'

启动脚本prometheus.sh

启动脚本用于管理prometheus的生命周期,上面的环境设置脚本会被在此优先调用,脚本内容如下:

#!/bin/bash
# 额外增加一个prometheus-env.sh文件用来设定prometheus的变量
# 第一部分获取脚本的执行目录等信息
SCRIPT_FILE=$0
cd $(dirname $SCRIPT_FILE)
SCRIPT_FILE=$(basename $SCRIPT_FILE)
while [ -L "$SCRIPT_FILE" ]
do
    SCRIPT_FILE=$(readlink $SCRIPT_FILE)
    cd $(dirname $SCRIPT_FILE)
    SCRIPT_FILE=$(basename $SCRIPT_FILE)
done
PHYS_DIR=$(pwd -P)
SCRIPT_DIR=$PHYS_DIR
HOME_DIR=$(dirname $SCRIPT_DIR)
# 这里我们调用环境设置脚本,读入必要变量
source "${SCRIPT_DIR}/prometheus-env.sh"
 
# 使用方法的函数,不多解释,只是打印脚本使用方法和基本信息
usage(){
    echo "The script is used to manage the Prometheus Server [start|stop|status|reload|restart|usage]"
    echo "Prometheus Version:v2.31.1"
    echo "Build by Lijiadong"
    exit 1
}
 
# 检测Prometheus是否还在运行,在运行返回1,否则返回0
status(){
    # 这里的方法是直接调用Prometheus的服务端口,如果能掉通,则任务Prometheus在运行,不过这不代表Prometheus是能够正常提供服务的,以后有时间会尝试增加这里的逻辑,更加完善
    is_healthy=`curl -s http://localhost:9090/-/healthy`
    if [ $? -ne 0 ];then
        echo "Prometheus is not running"
        return 1
    else
        # 如果服务正则运行,则刷新一下获取到的pid号
        pid=`ps -ef|grep prometheus|grep -v grep |grep conf |awk '{print $2}'`
        echo "Prometheus is running"
        echo $pid > $PROMETHEUS_PID_DIR/prometheus.pid
        return 0
    fi
}
 
# 启动函数
start(){
    # 启动函数中,先调用status函数来确认Prometheus的状态,根据返回值决定是否需要启动
    status
    if [ $? -eq "0" ];then
        echo "Service URL is http://localhost:${PROMETHEUS_SERVER_PORT},PID is ${pid}"
    else
        # 返回值为1时,代表Prometheus没有运行,接下来启动服务
        echo "Start Prometheus Server..."
        # 在exec_opt中我进行了启动参数的拼接,目前允许用户更改默认配置文件路径、服务端口、数据目录、数据存储周期等。
        # 如果手动关闭了管理api等参数,在此处也会进行判断,根据参数的true和false决定是否加入对应的启动参数
        exec_opt="
            --config.file=${PROMETHEUS_CONF}
            --web.listen-address=0.0.0.0:${PROMETHEUS_SERVER_PORT}
            --storage.tsdb.path=${PROMETHEUS_DATA_DIR}
            --storage.tsdb.retention.time=${PROMETHEUS_LIFECYCLE}d"
        if [ $PROMETHEUS_WEB_LIFECYCLE_ENABLE == "true" ];then
            exec_opt=$exec_opt" --web.enable-lifecycle"
        fi
        if [ $PROMETHEUS_WEB_ADMINAPI_ENABLE == "true" ];then
            exec_opt=$exec_opt" --web.enable-admin-api"
        fi
        # 运行参数设置完成后,我们检测日志目录是否存在,不存在则创建并授权,因为我这里是要以prometheus用户运行程序
        cd ${HOME_DIR}
        if [ ! -d "${PROMETHEUS_LOG_DIR}" ];then
            mkdir -vp ${PROMETHEUS_LOG_DIR}
            chown prometheus:prometheus ${PROMETHEUS_LOG_DIR}
        fi
        # 我们在Prometheus的安装目录使用nohup执行命令,这样可以把日志定向到上面创建的日志目录;
        nohup ./bin/prometheus $exec_opt >> $PROMETHEUS_LOG_DIR/prometheus.run.log 2>&1 &
        pid=`ps -ef|grep prometheus|grep -v grep |grep conf |awk '{print $2}'`
        # 启动时,刷新pid到文件中
        echo $pid > $PROMETHEUS_PID_DIR/prometheus.pid
    fi
}
 
#停止函数
stop(){
    status
    if [ $? -eq "0" ];then
        echo "PID ${pid} will be killed"
        # 传递的信号为SIGINT,这样保证prometheus数据会保存不至于重启恢复太久,这里我没使用SIGKILL信号的原因就是保证Prometheus的优雅退出
        kill -2 $pid
    else
        echo "Nothing to do."
    fi
    # 清空pid文件
    true > /var/run/prometheus/prometheus.pid
}
 
#重启函数
restart(){
    stop
    sleep 3
    start
}
 
#重载函数
reload(){
    # 重载 函数的目的就是重载刷新Prometheus配置文件
    is_reload=`curl -s http://localhost:9090/-/reload`
    if [ $? -ne 0 ];then
        echo "Prometheus is not running"
        return 1
    else
        echo "Prometheus configure has been reload"
        return 0
    fi
}
 
# 根据用户给出的启动命令执行对应函数
case "$1" in
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "status")
        status
        ;;
    "restart")
        restart
        ;;
    "reload")
        reload
        ;;
    *)
        usage
        ;;
esac

SPEC文件编写

由于要使用rpmbuild进行rpm包的制作,这里就要编写SPEC文件,这部分不多解释了,感兴趣可以留言问:

%global debug_package %{nil}
 
 
Name:            prometheus
Version: 2.31.1
Release: 1%{?dist}
Summary: Prometheus make by Lijiadong from source code.
License: ASL 2.0
URL:     https://prometheus.io
Provides: prometheus
 
Source0: source/prometheus-%{version}.linux-amd64.tar.gz
 
#%{?systemd_requires}
Requires(pre): shadow-utils
# 默认安装位置,适配hdp的使用版本
Prefix: /usr/hdp/3.1.5.0-152
 
%description
 
Prometheus is a systems and service monitoring system. It collects metrics from
configured targets at given intervals, evaluates rule expressions, displays the
results, and can trigger alerts if some condition is observed to be true.
 
%prep
%setup -q -n prometheus-%{version}.linux-amd64
 
%build
/bin/true
%install
#创建安装目录
mkdir -vp %{buildroot}%{prefix}/prometheus
mkdir -vp %{buildroot}/var/log/prometheus
#创建默认数据目录
mkdir -vp %{buildroot}/var/lib/prometheus
mkdir -vp %{buildroot}/var/run/prometheus
#安装系统命令prometheus和promtool
install -D -m 755 prometheus %{buildroot}%{prefix}/prometheus
install -D -m 755 promtool %{buildroot}%{prefix}/prometheus/promtool
for dir in console_libraries consoles; do
  for file in ${dir}/*; do
    # 对所有文件授权644
    install -D -m 644 ${file} %{buildroot}%{prefix}/prometheus/${file}
  done
done
 
#启动脚本赋权755
for file in sbin/*; do
    install -D -m 755 ${file} %{buildroot}%{prefix}/prometheus/${file}
done
 
install -D -m 644 conf/prometheus.yml %{buildroot}%{prefix}/prometheus/conf/prometheus.yml
install -D -m 644 LICENSE %{buildroot}%{prefix}/prometheus
install -D -m 644 NOTICE %{buildroot}%{prefix}/prometheus
 
 
#添加用户和组
%pre
getent group prometheus >/dev/null || groupadd -r prometheus
getent passwd prometheus >/dev/null || \
  useradd -g prometheus -d /home/prometheus -s /bin/bash \
          -c "Prometheus services" prometheus
exit 0
 
# 卸载后删除安装文件
%post
if [ ! -d "/usr/hdp/current" ];then
    mkdir -vp /usr/hdp/current
fi
ln -s %{prefix}/prometheus /usr/hdp/current/prometheus
 
 
%postun
userdel -r prometheus
rm -rf %{prefix}/prometheus
rm -rf /var/lib/prometheus
rm -rf /var/log/prometheus
rm -rf /var/run/prometheus
 
 
%files
%defattr(-,root,root,-)
%{prefix}/prometheus/prometheus
%{prefix}/prometheus/promtool
%config(noreplace) %{prefix}/prometheus/conf/prometheus.yml
%{prefix}/prometheus/console_libraries
%{prefix}/prometheus/consoles
%{prefix}/prometheus/sbin
%dir %attr(755, prometheus, prometheus)%{prefix}/prometheus
%attr(755, prometheus, prometheus)%{prefix}/prometheus/*
%dir %attr(755, prometheus, prometheus)/var/log/prometheus
%dir %attr(755, prometheus, prometheus)/var/lib/prometheus
%dir %attr(755, prometheus, prometheus)/var/run/prometheus
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Meepoljd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值