前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >运维监控之Prometheus入门安装篇

运维监控之Prometheus入门安装篇

作者头像
lyb-geek
发布2019-07-15 19:38:18
8900
发布2019-07-15 19:38:18
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路
一、前言

上一篇主要简要科普了一下Prometheus是个什么鬼,这一篇主要介绍如何安装Prometheus。本文安装linux环境为centos7

二、安装Prometheus

1、安装go环境

代码语言:javascript
复制
yum install go

2、查看go版本

代码语言:javascript
复制
[root@iZtd8edkkjhg2sxz5wck0rZ ~]#  go version
go version go1.11.4 linux/amd64

3、下载Prometheus安装包,可以从官网https://prometheus.io/download/下载所需的安装,也可以执行如下命令

代码语言:javascript
复制
wget https://github.com/prometheus/prometheus/releases/download/v2.7.1/prometheus-2.7.1.linux-amd64.tar.gz

4、解压安装包

代码语言:javascript
复制
tar -zxvf prometheus-2.7.1.linux-amd64.tar.gz

5、重命名(可选)

代码语言:javascript
复制
mv prometheus-2.7.1.linux-amd64 prometheus

6、创建操作Prometheus用户和用户组并赋予执行权限(可选)

代码语言:javascript
复制
chattr -i /etc/group;chattr -i /etc/gshadow;chattr -i /etc/passwd;chattr -i /etc/shadow
groupadd prometheus
useradd -g prometheus prometheus
chattr +i /etc/group;chattr +i /etc/gshadow;chattr +i /etc/passwd;chattr +i /etc/shadow

chown -R prometheus:prometheus /home/prometheus

7、编写prometheus服务脚本,具体脚本参见附录

8、赋予Prometheus脚本用户可执行权限

代码语言:javascript
复制
chmod 744 /etc/init.d/prometheus

9、修改端口号(可选)

代码语言:javascript
复制
在启动的脚本添加如下:
--web.listen-address=:$appPort(修改的端口号)

10、运行Prometheus

代码语言:javascript
复制
service prometheus start|stop|restart|status

11、Prometheus图像界面

本教程的Prometheus的端口号是9191,通过浏览器输入ip:9191

三、附录Prometheus服务脚本

代码语言:javascript
复制
#!/bin/sh
# chkconfig:35 90 3
# description: prometheus server manage.


appPort=9191
isWhliePid=/var/run/prometheusWhile.pid
runPaths=/home/prometheus


function status(){
    myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
    if [ "$myPid" != "" ]; then
        echo "Started Prometheus. pid = $myPid"
    else
        echo "Stopped Prometheus."
    fi
}

function start(){
    myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
    if [ ! -z "$myPid" ]; then
        echo "Prometheus进程存在,3s后重试,请稍候..."
        sleep 3
        mypid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
        if [ ! -z "$myPid" ]; then
            echo "忽略启动命令:Prometheus正在运行"
            exit 1
        fi
    fi
    echo "Starting Prometheus..."
    su - prometheus -c "nohup /home/prometheus/prometheus --web.listen-address=:$appPort --web.enable-lifecycle --config.file=/home/prometheus/prometheus.yml > /dev/null 2>&1 &"
    printf "Waiting for Prometheus..."
    while true;
    do
        lsof -i:$appPort|grep LISTEN > /dev/null 2>&1
            if [[ $? == 0 ]] ; then break; fi;
                myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
                if [ -z "$myPid" ]; then
                    echo -e "\nFailed to start Prometheus."
                    exit 1
                fi
            printf ".";
                sleep 1;
        done
    echo
        myPid=`lsof -i:$appPort|grep LISTEN|awk '{print $2}'`
    echo "running: PID:$myPid"
}

function stop(){
       echo "Stopping Prometheus..."
    myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
       if [ "$myPid" != "" ]; then
                echo 0 > $isWhliePid
                kill $myPid
        fi
    printf "Waiting for Prometheus..."
        while true;
    do
          myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
        if [ "$myPid" = "" ] ; then break; fi;
          printf ".";
          sleep 1;
    done
    echo
}

case "$1" in
   start)
    start
    status
   ;;
   stop)
    stop
    status
   ;;
   restart)
        $0 stop
        $0 start
   ;;
   status)
       status
   ;;
   *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit $?
   ;;
esac
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二、安装Prometheus
  • 三、附录Prometheus服务脚本
相关产品与服务
Prometheus 监控服务
Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档