文章目录

  • 脚本简介
  • 脚本注解
  • 脚本结构
  • 注意事项
  • 脚本修改
  • 变量修改
  • 执行方式
  • 平台结果
  • 脚本内容
  • Run.sh
  • funtion_momitor.sh


脚本简介

  1. 基于运维统一脚本中,9、监控服务下的Prometheus(新版)选项
  2. 使用【docker环境安装】,系统版本Centos7

脚本注解

  1. 该脚本为了在Centos7服务器中docker环境上部署Prometheus+grafana+node_export服务,可通过选择单独启动一个还是所有的启动
  2. 前三个是单独的服务,第四个是把三个服务同时在同一台宿主机上启动,且会通过获取当前宿主机的IP地址,生成Prometheus的配置文件
    注意:
#部署服务的主机ip,如果无法自动生成正确的结果,建议填写
#localip_addr="192.168.1.131"
localip_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.17.0.1|awk '{print $2}'|tr -d "addr:"`
  1. 如果分别在不同的机器上启动,则需要根据实际情况填写ip+端口
#生成Prometheus配置文件的关键!!!
#如果需要添加其他的export,则可以在后面添加,注意:名称,地址,端口  一一对应,切记^~^
server_name="prometheus node_exporter"
server_ipaddr="${localip_addr} ${localip_addr}"
server_port="9090 9100"
  1. 使用此脚本时需要提前检查当前宿主机中是否安装docker环境(自行安装docker环境安装)

脚本结构

  1. 函数文件,funtion_momitor.sh,与Run.sh存在在相同路径下即可使用
  2. 启动文件,Run.sh

注意事项

  1. 采用docker环境启动服务,所需的进行需要从官方镜像仓库中pull,如果网速较慢,请耐心等待
  2. 如果pull失败,绝大所述是因为网络文件,本人采用虚拟机测试的时候,就是因为网速下载太慢,导致调试脚本用时较长
  3. 最后使用虚拟机我没有pull成功,然后我采用阿里云的云主机把镜像手动下载并上传到我测试有的虚拟机上^~^
  4. 该脚本是基于运维统一脚本中的选项进行拆分,敬请关注

脚本修改

变量修改

#根据实际情况修改,如果端口冲突,则可以自行修改映射的端口即可,新装的机器则可以不用修改
dir_name="/opt/monitor"
####################################################
grafana_name="grafana"
grafana_port="3000"
grafana_images="grafana/grafana:8.4.7"
grafana_volume="${dir_name}/grafana"
####################################################
node_exporter_name="node_exporter"
node_exporter_port="9100"
node_exporter_images="prom/node-exporter"
####################################################
################### 
#prometheuss      #
###################
prometheus_name="prometheus"
prometheus_port="9090"
#prometheus_images="prom/prometheus"
prometheus_images="bitnami/prometheus:2.35.0-debian-10-r14"
prometheus_cpu="--cpuset-cpus 1"
prometheus_men="-m 2G"
promethues_path="${dir_name}/prometheus"
promethues_template="${base_dir}/template/prometheus"
source ${basepath}/funtion_momitor.sh
#部署服务的主机ip,如果无法自动生成正确的结果,建议填写
#localip_addr="192.168.1.131"
localip_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.17.0.1|awk '{print $2}'|tr -d "addr:"`
server_name="prometheus node_exporter"
server_ipaddr="${localip_addr} ${localip_addr}"
server_port="9090 9100"

执行方式

sh Run.sh

执行结果如下图所示

一键docker脚本 docker部署脚本_运维

平台结果

Prometheus
http://IP:9090一键docker脚本 docker部署脚本_docker_02
grafana
一键docker脚本 docker部署脚本_自动生成_03

  1. 基本配置及使用再次篇中不做过多的描述,可见之前博客

脚本内容

Run.sh

#!/bin/bash
#该脚本用于部署prometheus
#所有者:北城半夏
#
#当前版本
#v1.0.1
####################################################
basepath=$(cd `dirname $0`;pwd)
dir_name="/opt/monitor"
####################################################
grafana_name="grafana"
grafana_port="3000"
grafana_images="grafana/grafana:8.4.7"
grafana_volume="${dir_name}/grafana"
####################################################
node_exporter_name="node_exporter"
node_exporter_port="9100"
node_exporter_images="prom/node-exporter"
####################################################
################### 
#prometheuss      #
###################
prometheus_name="prometheus"
prometheus_port="9090"
#prometheus_images="prom/prometheus"
prometheus_images="bitnami/prometheus:2.35.0-debian-10-r14"
prometheus_cpu="--cpuset-cpus 1"
prometheus_men="-m 2G"
promethues_path="${dir_name}/prometheus"
promethues_template="${base_dir}/template/prometheus"
source ${basepath}/funtion_momitor.sh
#部署服务的主机ip,如果无法自动生成正确的结果,建议填写
#localip_addr="192.168.1.131"
localip_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.17.0.1|awk '{print $2}'|tr -d "addr:"`
server_name="prometheus node_exporter"
server_ipaddr="${localip_addr} ${localip_addr}"
server_port="9090 9100"
#######################################################################################################################
#grafana_run
#node_exporter_run
#启动方式
#start_prometheus

index_run(){
 info3
 echo  "1、node_exporter" 
 echo  "2、grafana"
 echo  -e "\033[32m3、prometheus\033[0m"
 echo -e "4、上述所有服务(all)"
 echo  -e "\033[91m5、删除容器\033[0m"
echo  -e "\033[31m6、清理上述服务的持久化挂载目录(数据被删除,不可恢复)\033[0m"
 read   -p "请输入对应的数字编号:" check
}
run(){
  index_run
    if [ -z ${check} ];then
              info6 "请重新输入"    
              run
    else
       case ${check} in 
         '1')
            node_exporter_run;;
         '2')
            grafana_run;;
          '3')
            start_prometheus;;
          'all'|'ALL'|'4')
            node_exporter_run
            info3
            grafana_run
            info3
            start_prometheus;;
          'rm'|'5')
            read -p "选择1.${grafana_name},2.${node_exporter_name},3.${prometheus_name},all:" rm_start_run1
             if [ -z ${rm_start_run1} ];then
               info6 "请重新输入"    
             else
                 case $rm_start_run1 in
                   '1')
                     docker rm -f ${grafana_name}  &>/dev/null
                   [ $? -eq 0 ] && info " ${grafana_name} RM successful";;
                  '2')
                     docker rm -f ${node_exporter_name}  &>/dev/null
                     [ $? -eq 0 ] && info " ${node_exporter_name} RM successful";;
                   '3')
                    docker rm -f ${prometheus_name}  &>/dev/null
                    [ $? -eq 0 ] && info " ${prometheus_name} RM successful";;
                   'all'|'ALL')
                    docker rm -f ${grafana_name} ${node_exporter_name} ${prometheus_name}  &>/dev/null
                    [ $? -eq 0 ] && info " ${grafana_name} ${node_exporter_name} ${prometheus_name} RM successful";;
                esac
         fi;;
          'clear'|'6')
            read -p "选择1.${grafana_name},2.${prometheus_name},all:" rm_start_run1
             if [ -z ${rm_start_run1} ];then
               info6 "请重新输入"    
             else
                 case $rm_start_run1 in
                   '1')
                   rm -rf  ${grafana_volume}  &>/dev/null
                   [ $? -eq 0 ] && info "RM disk ${grafana_volume} successful";;
                  '2')
                     rm -rf ${promethues_path}  &>/dev/null
                    [ $? -eq 0 ] && info "RM disk ${promethues_path} successful";;
                   'all'|'ALL')
                    rm -rf ${promethues_path} ${grafana_volume} &>/dev/null
                   [ $? -eq 0 ] && info " ${grafana_volume} ${promethues_path}  RM successful";;
                esac
         fi;;
              
           
       esac
    fi
            
}
run

funtion_momitor.sh

#!/bin/bash


####################################################

info(){
  echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info1(){
  echo -e "\033[36m $1 \033[0m "
}
info2(){
  echo -e "\033[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
info3(){
 echo -e "\033[36m------------------------------------------------------------------------------\033[0m" 
}
info4(){
  echo -e "\033[32m $1 \033[0m "
}
info5(){
 info1 "==============================================================================" 
}
info6(){
  echo -e "\033[33m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[91m$1\033[0m "
}
info7(){
  echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[32m$1\033[0m "
}

info8(){
  echo -e "\033[31m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
info9(){
  echo -e "\033[32m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
grafana_run(){
 local registory=`docker images --format {{.Repository}}:{{.Tag}}|grep ${grafana_images}|wc -l`
 ###########################docker node_exporter##################
  [ ! -d ${grafana_volume} ] && mkdir -p ${grafana_volume}
   if [ ${registory} -ne 1 ];then
      info2 "Pull the image ${grafana_images} Wait !!!"
      docker pull ${grafana_images} &> /dev/null
      [ $? -eq 0 ] && info2 "Pull the image ${grafana_images} success"
   fi
 a=`docker ps -a | grep ${grafana_name}|wc -l`
 if [ $a -eq 1 ];then
     info2 "Start ${grafana_name} to the root user"
     docker rm -f ${grafana_name} &>/dev/null
       [ $? -eq 0 ] && info6 "${grafana_name} rm success"
      docker run -d --name ${grafana_name} --restart=always -p ${grafana_port}:3000  \
            -v ${grafana_volume}:/opt/grafana ${grafana_images} >/dev/null
 else
  info2 "Start ${grafana_name} to the root user"
      docker run -d --name ${grafana_name} --restart=always -p ${grafana_port}:3000  \
            -v ${grafana_volume}:/opt/grafana ${grafana_images} >/dev/null
 fi
   [ $? -eq 0 ] && info "Start the  ${grafana_name} success"
}

node_exporter_run(){
  local registory=`docker images --format {{.Repository}}:{{.Tag}}|grep ${node_exporter_images}|wc -l`
 ###########################docker node_exporter##################
   if [ ${registory} -ne 1 ];then
      info2 "Pull the image ${node_exporter_images} Wait !!!"
      docker pull ${node_exporter_images} &> /dev/null
      [ $? -eq 0 ] && info2 "Pull the image ${node_exporter_images} success"
   fi
 a=`docker ps -a | grep ${node_exporter_name}|wc -l`
 if [ $a -eq 1 ];then
     info2 "Start ${node_exporter_name} to the root user"
     docker rm -f ${node_exporter_name} &>/dev/null
       [ $? -eq 0 ] && info6 "${node_exporter_name} rm success"
        docker run -d --name ${node_exporter_name} --hostname ${node_exporter_name} --restart=always \
             -p ${node_exporter_port}:9100  -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro"   \
             -v "/:/rootfs:ro" ${node_exporter_images} >/dev/null
 else
  info2 "Start ${node_exporter_name} to the root user"
           docker run -d --name ${node_exporter_name} --hostname ${node_exporter_name} --restart=always \
             -p ${node_exporter_port}:9100  -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro"   \
             -v "/:/rootfs:ro" ${node_exporter_images} >/dev/null
 fi
   [ $? -eq 0 ] && info "Start the  ${node_exporter_name} success"
}

#拷贝文件
copy_prometheus(){
   local dir_name=${promethues_path}
    if [  -d  ${promethues_path}/config ] && [ -d ${promethues_path}/data ];then
       info6 "${promethues_path}/config 目录已存在"
     else
        mkdir -p ${promethues_path}/config
        mkdir -p ${promethues_path}/data
        chmod -R 200 ${promethues_path}/data
       [ $? -eq 0 ] && info "${promethues_path}/config 目录创建成功"
     fi
   # cp -rf  ${promethues_template}/prometheus.yml  ${promethues_path}/config
   # [ $? -eq 0 ] && info "prometheus.yml模板已拷贝成功,准备修改配置"
 }
#配置模板
template(){
   prometheus_yaml="global:
  scrape_interval:     60s
  evaluation_interval: 60s

scrape_configs:
${prometheus_host1[*]}"
}
#生成配置文件
Build_configuration(){
  for name in ${server_name};do server_name1[${#server_name1[*]}]=$name ;done
  for ipaddr in ${server_ipaddr};do server_ipaddr1[${#server_ipaddr1[*]}]=$ipaddr ;done
  for port in ${server_port};do server_port1[${#server_port1[*]}]=$port ;done
 local num=${#server_name1[*]}
 local num_new=$(( num -1 ))
for num_1 in `seq 0 ${num_new}`
   do
    prometheus_host1[${#prometheus_host1[*]}]="
   - job_name: ${server_name1[num_1]}
     static_configs:
      - targets: ['${server_ipaddr1[num_1]}:${server_port1[num_1]}']
        labels:
          instance: ${server_name1[num_1]}
          "
   done
 template
 echo "${prometheus_yaml}" > ${promethues_path}/config/prometheus.yml
 [ $? -eq 0 ] && info "${promethues_path}/config/prometheus.yml文件生成成功"
}

   #启动服务
prometheus_run(){
  local registory=`docker images --format {{.Repository}}:{{.Tag}}|grep ${prometheus_images}|wc -l`

 info2 "Pull the image ${prometheus_images} Wait !!!"
   if [ ${registory} -ne 1 ];then
         docker pull ${prometheus_images} &> ${log_dir_name}/pull_prometheuse.log
   fi
 [ $? -eq 0 ] && info2 "Pull the image  ${prometheus_images} success"
 info2 "Start ${prometheus_name} to the root user"

 if [ `docker ps -a | grep ${prometheus_name}|wc -l` -eq 1 ];then
     docker rm -f ${prometheus_name} &>/dev/null
      [ $? -eq 0 ] && info6 "${prometheus_name} rm success"
      docker run  -d --name ${prometheus_name} --restart=always -p ${prometheus_port}:9090  \
           ${prometheus_cpu} ${prometheus_men} \
         -v ${promethues_path}/config/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml\
         -v ${promethues_path}/data:/opt/bitnami/prometheus/data -u root --privileged=true \
          ${prometheus_images} >/dev/null
       #prom/prometheus
       #docker run  -d --name ${prometheus_name} --restart=always -p ${prometheus_port}:9090  ${prometheus_cpu} ${prometheus_men} \
       #  -v ${promethues_path}/config/prometheus.yml:/etc/prometheus/prometheus.yml ${prometheus_images} >/dev/null
         [ $? -eq 0 ] && info9 " ${prometheus_name} start success "
   else
      docker run  -d --name ${prometheus_name} --restart=always -p ${prometheus_port}:9090  \
           ${prometheus_cpu} ${prometheus_men} \
         -v ${promethues_path}/config/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml\
         -v ${promethues_path}/data:/opt/bitnami/prometheus/data -u root --privileged=true \
          ${prometheus_images} >/dev/null
         [ $? -eq 0 ] && info9 " ${prometheus_name} start success"
 fi

}
#启动函数
start_prometheus(){
   #清理文件
   rm -rf ${promethues_path}
   #拷贝文件
   copy_prometheus
   #生成配置
    Build_configuration
   #启动服务
   prometheus_run
 }