众客华禹

 找回密码
立即注册

QQ登录

只需一步,快速开始

查看: 522|回复: 0

Centos7安装Promethus(普罗米修斯)监控系统完整版

[复制链接]

70

主题

70

帖子

378

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
378
发表于 2022-1-27 15:44:53 | 显示全部楼层 |阅读模式
一、yum安装prometheus
1、添加yum源
  1. curl -s https://packagecloud.io/install/repositories/prometheus-rpm/release/script.rpm.sh | sudo bash
  2. 或者手动编写脚本
  3. vim prometheus_install_yum.sh
  4. #!/bin/bash

  5. unknown_os ()
  6. {
  7.   echo "Unfortunately, your operating system distribution and version are not supported by this script."
  8.   echo
  9.   echo "You can override the OS detection by setting os= and dist= prior to running this script."
  10.   echo "You can find a list of supported OSes and distributions on our website: https://packagecloud.io/docs#os_distro_version"
  11.   echo
  12.   echo "For example, to force CentOS 6: os=el dist=6 ./script.sh"
  13.   echo
  14.   echo "Please email support@packagecloud.io and let us know if you run into any issues."
  15.   exit 1
  16. }

  17. curl_check ()
  18. {
  19.   echo "Checking for curl..."
  20.   if command -v curl > /dev/null; then
  21.     echo "Detected curl..."
  22.   else
  23.     echo "Installing curl..."
  24.     yum install -d0 -e0 -y curl
  25.   fi
  26. }


  27. detect_os ()
  28. {
  29.   if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then
  30.     if [ -e /etc/os-release ]; then
  31.       . /etc/os-release
  32.       os=${ID}
  33.       if [ "${os}" = "poky" ]; then
  34.         dist=`echo ${VERSION_ID}`
  35.       elif [ "${os}" = "sles" ]; then
  36.         dist=`echo ${VERSION_ID}`
  37.       elif [ "${os}" = "opensuse" ]; then
  38.         dist=`echo ${VERSION_ID}`
  39.       elif [ "${os}" = "opensuse-leap" ]; then
  40.         os=opensuse
  41.         dist=`echo ${VERSION_ID}`
  42.       else
  43.         dist=`echo ${VERSION_ID} | awk -F '.' '{ print $1 }'`
  44.       fi

  45.     elif [ `which lsb_release 2>/dev/null` ]; then
  46.       # get major version (e.g. '5' or '6')
  47.       dist=`lsb_release -r | cut -f2 | awk -F '.' '{ print $1 }'`

  48.       # get os (e.g. 'centos', 'redhatenterpriseserver', etc)
  49.       os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'`

  50.     elif [ -e /etc/oracle-release ]; then
  51.       dist=`cut -f5 --delimiter=' ' /etc/oracle-release | awk -F '.' '{ print $1 }'`
  52.       os='ol'

  53.     elif [ -e /etc/fedora-release ]; then
  54.       dist=`cut -f3 --delimiter=' ' /etc/fedora-release`
  55.       os='fedora'

  56.     elif [ -e /etc/redhat-release ]; then
  57.       os_hint=`cat /etc/redhat-release  | awk '{ print tolower($1) }'`
  58.       if [ "${os_hint}" = "centos" ]; then
  59.         dist=`cat /etc/redhat-release | awk '{ print $3 }' | awk -F '.' '{ print $1 }'`
  60.         os='centos'
  61.       elif [ "${os_hint}" = "scientific" ]; then
  62.         dist=`cat /etc/redhat-release | awk '{ print $4 }' | awk -F '.' '{ print $1 }'`
  63.         os='scientific'
  64.       else
  65.         dist=`cat /etc/redhat-release  | awk '{ print tolower($7) }' | cut -f1 --delimiter='.'`
  66.         os='redhatenterpriseserver'
  67.       fi

  68.     else
  69.       aws=`grep -q Amazon /etc/issue`
  70.       if [ "$?" = "0" ]; then
  71.         dist='6'
  72.         os='aws'
  73.       else
  74.         unknown_os
  75.       fi
  76.     fi
  77.   fi

  78.   if [[ ( -z "${os}" ) || ( -z "${dist}" ) ]]; then
  79.     unknown_os
  80.   fi

  81.   # remove whitespace from OS and dist name
  82.   os="${os// /}"
  83.   dist="${dist// /}"

  84.   echo "Detected operating system as ${os}/${dist}."

  85.   if [ "${dist}" = "8" ]; then
  86.     _skip_pygpgme=1
  87.   else
  88.     _skip_pygpgme=0
  89.   fi
  90. }

  91. finalize_yum_repo ()
  92. {
  93.   if [ "$_skip_pygpgme" = 0 ]; then
  94.     echo "Installing pygpgme to verify GPG signatures..."
  95.     yum install -y pygpgme --disablerepo='prometheus-rpm_release'
  96.     pypgpme_check=`rpm -qa | grep -qw pygpgme`
  97.     if [ "$?" != "0" ]; then
  98.       echo
  99.       echo "WARNING: "
  100.       echo "The pygpgme package could not be installed. This means GPG verification is not possible for any RPM installed on your system. "
  101.       echo "To fix this, add a repository with pygpgme. Usualy, the EPEL repository for your system will have this. "
  102.       echo "More information: https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F"
  103.       echo

  104.       # set the repo_gpgcheck option to 0
  105.       sed -i'' 's/repo_gpgcheck=1/repo_gpgcheck=0/' /etc/yum.repos.d/prometheus-rpm_release.repo
  106.     fi
  107.   fi

  108.   echo "Installing yum-utils..."
  109.   yum install -y yum-utils --disablerepo='prometheus-rpm_release'
  110.   yum_utils_check=`rpm -qa | grep -qw yum-utils`
  111.   if [ "$?" != "0" ]; then
  112.     echo
  113.     echo "WARNING: "
  114.     echo "The yum-utils package could not be installed. This means you may not be able to install source RPMs or use other yum features."
  115.     echo
  116.   fi

  117.   echo "Generating yum cache for prometheus-rpm_release..."
  118.   yum -q makecache -y --disablerepo='*' --enablerepo='prometheus-rpm_release'

  119.   echo "Generating yum cache for prometheus-rpm_release-source..."
  120.   yum -q makecache -y --disablerepo='*' --enablerepo='prometheus-rpm_release-source'
  121. }

  122. finalize_zypper_repo ()
  123. {
  124.   zypper --gpg-auto-import-keys refresh prometheus-rpm_release
  125.   zypper --gpg-auto-import-keys refresh prometheus-rpm_release-source
  126. }

  127. main ()
  128. {
  129.   detect_os
  130.   curl_check


  131.   yum_repo_config_url="https://packagecloud.io/install/repositories/prometheus-rpm/release/config_file.repo?os=${os}&dist=${dist}&source=script"

  132.   if [ "${os}" = "sles" ] || [ "${os}" = "opensuse" ]; then
  133.     yum_repo_path=/etc/zypp/repos.d/prometheus-rpm_release.repo
  134.   else
  135.     yum_repo_path=/etc/yum.repos.d/prometheus-rpm_release.repo
  136.   fi

  137.   echo "Downloading repository file: ${yum_repo_config_url}"

  138.   curl -sSf "${yum_repo_config_url}" > $yum_repo_path
  139.   curl_exit_code=$?

  140.   if [ "$curl_exit_code" = "22" ]; then
  141.     echo
  142.     echo
  143.     echo -n "Unable to download repo config from: "
  144.     echo "${yum_repo_config_url}"
  145.     echo
  146.     echo "This usually happens if your operating system is not supported by "
  147.     echo "packagecloud.io, or this script's OS detection failed."
  148.     echo
  149.     echo "You can override the OS detection by setting os= and dist= prior to running this script."
  150.     echo "You can find a list of supported OSes and distributions on our website: https://packagecloud.io/docs#os_distro_version"
  151.     echo
  152.     echo "For example, to force CentOS 6: os=el dist=6 ./script.sh"
  153.     echo
  154.     echo "If you are running a supported OS, please email support@packagecloud.io and report this."
  155.     [ -e $yum_repo_path ] && rm $yum_repo_path
  156.     exit 1
  157.   elif [ "$curl_exit_code" = "35" -o "$curl_exit_code" = "60" ]; then
  158.     echo
  159.     echo "curl is unable to connect to packagecloud.io over TLS when running: "
  160.     echo "    curl ${yum_repo_config_url}"
  161.     echo
  162.     echo "This is usually due to one of two things:"
  163.     echo
  164.     echo " 1.) Missing CA root certificates (make sure the ca-certificates package is installed)"
  165.     echo " 2.) An old version of libssl. Try upgrading libssl on your system to a more recent version"
  166.     echo
  167.     echo "Contact support@packagecloud.io with information about your system for help."
  168.     [ -e $yum_repo_path ] && rm $yum_repo_path
  169.     exit 1
  170.   elif [ "$curl_exit_code" -gt "0" ]; then
  171.     echo
  172.     echo "Unable to run: "
  173.     echo "    curl ${yum_repo_config_url}"
  174.     echo
  175.     echo "Double check your curl installation and try again."
  176.     [ -e $yum_repo_path ] && rm $yum_repo_path
  177.     exit 1
  178.   else
  179.     echo "done."
  180.   fi

  181.   if [ "${os}" = "sles" ] || [ "${os}" = "opensuse" ]; then
  182.     finalize_zypper_repo
  183.   else
  184.     finalize_yum_repo
  185.   fi

  186.   echo
  187.   echo "The repository is setup! You can now install packages."
  188. }

  189. main
复制代码

2、更新yum源
  1. yum clean all
  2. yum repolist
  3. yum search prometheus
复制代码
3、安装prometheus
  1. yum install -y prometheus2
复制代码
4、启动prometheus,并加入自启
  1. systemctl start prometheus
  2. systemctl enable prometheus
复制代码
二、二进制安装prometheus
1、下载prometheus
https://prometheus.io/download/ 下载相应版本,安装到服务器上官网提供的是二进制版,解压就能用,不需要编译
上传prometheus-2.28.1.linux-amd64.tar.gz
2、解压安装包
  1. tar -zxvf prometheus-2.28.1.linux-amd64.tar.gz -C /usr/local/
  2. mv /usr/local/prometheus-2.28.1.linux-amd64/ /usr/local/prometheus
复制代码
3、解压后当前目录会包含默认的Prometheus配置文件promethes.yml,下面配置文件做下简略的解析:
  1. # 全局配置
  2. global:
  3.   scrape_interval:     15s # 设置抓取间隔,默认为1分钟
  4.   evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
  5.   # scrape_timeout  #默认抓取超时,默认为10s

  6. # Alertmanager相关配置
  7. alerting:
  8.   alertmanagers:
  9.   - static_configs:
  10.     - targets:
  11.       # - alertmanager:9093

  12. # 规则文件列表,使用'evaluation_interval' 参数去抓取
  13. rule_files:
  14.   # - "first_rules.yml"
  15.   # - "second_rules.yml"

  16. #  抓取配置列表
  17. scrape_configs:
  18.   - job_name: 'prometheus'
  19.     static_configs:
  20.     - targets: ['localhost:9090']
复制代码
4、直接使用默认配置文件启动
  1. /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
复制代码
5、验证启动
  1. [root@prometheus opt]# netstat -nutlp|grep 9090
  2. tcp6       0      0 :::9090                 :::*                    LISTEN      4726/prometheus
复制代码
6、访问prometheus界面
通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面
%E6%99%AE%E7%BD%97%E7%B1%B3%E4%BF%AE%E6%96%AF%E9%A6%96%E9%A1%B5-1024x310.png

7、做成服务,设置成开启自启动
  1. vim /etc/systemd/system/prometheus.service
  2. [Unit]
  3. Description=Prometheus Monitoring System
  4. Documentation=Prometheus Monitoring System

  5. [Service]
  6. ExecStart=/usr/local/prometheus/prometheus
  7.   --storage.tsdb.path=/usr/local/prometheus/data
  8.   --config.file=/usr/local/prometheus/prometheus.yml
  9.   --web.listen-address=:9090

  10. [Install]
  11. WantedBy=multi-user.target
复制代码
8、创建目录
  1. mkdir /usr/local/prometheus/data
复制代码
9、设置开机自启
启动前先kill掉原来的服务,否则不能成功启动
  1. systemctl daemon-reload
  2. systemctl enable prometheus
  3. systemctl start prometheus
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|众客华禹 ( 蜀ICP备2021016660号 )

GMT+8, 2022-5-17 09:16 , Processed in 0.049735 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表