Prometheus Server一键部署脚本

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Prometheus Server一键部署脚本

1.脚本内容展示

[root@prometheus-server31 ~]# cat install-prometheus-server.sh 
#!/bin/bash
# auther: 尹正杰
# blog: https://www.cnblogs.com/yinzhengjie


VERSION=2.53.2
ARCH=amd64
SOFTWARE=prometheus-${VERSION}.linux-${ARCH}.tar.gz
URL=https://github.com/prometheus/prometheus/releases/download/v${VERSION}/${SOFTWARE}
DOWNLOAD=./download
INSTALLDIR=/yinzhengjie/softwares
BASEDIR=${INSTALLDIR}/prometheus-${VERSION}.linux-amd64
DATADIR=/yinzhengjie/data/prometheus
LOGDIR=/yinzhengjie/logs/prometheus
HOSTIP=0.0.0.0
PORT=9090
HOSTNAME=`hostname`


function prepare() {
  # 判断目录是否存在,若不存在则创建
  [ -d $INSTALLDIR ] || install -d  ${INSTALLDIR}
  [ -d $DOWNLOAD ] || install -d ${DOWNLOAD}
  [ -d $DATADIR ] || install -d ${DATADIR}
  [ -d $LOGDIR ] || install -d ${LOGDIR}

  . /etc/os-release

  if [ "$ID" == "centos" ];then
    # 判断系统是否安装wget
    [ -f /usr/bin/wget ] || yum -y install wget
  fi

  # 判断文件是否存在,若不存在则下载
  [ -s ${DOWNLOAD}/${SOFTWARE} ] || wget $URL -O ${DOWNLOAD}/${SOFTWARE}

}


function deploy() {
  # 检查环境
  prepare

  # 解压文件软件包
  tar xf ${DOWNLOAD}/${SOFTWARE} -C ${INSTALLDIR}

  # 生成启动脚本
cat > /etc/systemd/system/prometheus-server.service <<EOF
[Unit]
Description=yinzhengjie Linux  Prometheus Server
Documentation=https://www.cnblogs.com/yinzhengjie
After=network.target

[Service]
Restart=on-failure
ExecStart=/bin/bash -c "${BASEDIR}/prometheus \
     --config.file=${BASEDIR}/prometheus.yml \
	 --web.enable-lifecycle \
	 --storage.tsdb.path=${DATADIR} \
	 --storage.tsdb.retention.time=60d  \
	 --web.listen-address=${HOSTIP}:${PORT}  \
	 --web.max-connections=65535  \
	 --storage.tsdb.retention.size=512MB \
	 --query.timeout=10s \
	 --query.max-concurrency=20 \
	 --log.level=info \
	 --log.format=json \
	 --web.read-timeout=5m &>> ${LOGDIR}/prometheus-server.log"
ExecReload=/bin/kill -HUP \$MAINPID
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF

  # 将服务设置为开机自启动
  systemctl daemon-reload
  systemctl enable --now prometheus-server
  systemctl status prometheus-server
  sleep 0.3
  ss -ntl | grep ${PORT}
}


function delete(){
  systemctl disable --now prometheus-server.service
  rm -rf /etc/systemd/system/node-exporter.service $BASEDIR $DATADIR $LOGDIR
}


function main() {
  case $1 in 
    deploy|i)
      deploy
      echo "尹正杰脚本: ${HOSTNAME} 的prometheus-server 已经部署成功![successfully]"
      ;;
    delete|r)
      delete
      echo "尹正杰脚本: ${HOSTNAME} 的prometheus-server 已经卸载成功,期待下次使用~"
      ;;
    *)
      echo "Usage: $0 deploy[i]|delete[r]"
      ;;
  esac
}


main $1
[root@prometheus-server31 ~]# 

2.访问Prometheus的WebUI

如上图所示,输入Prometheus的地址即可访问哟~

二.测试验证

1.安装Prometheus server

如上图所示,可以进行安装Prometheus server。

2.卸载Prometheus server

如上图所示,可以实现Prometheus server的卸载。
posted @ 2024-09-23 02:11  尹正杰  阅读(41)  评论(1编辑  收藏  举报