Prometheus Server一键部署脚本

一.Prometheus Server一键部署脚本

1.脚本内容展示

[root@prometheus-server31 ~]# cat install-prometheus-server.sh 
#!/bin/bash
# school: oldboyedu 
# class: linux92
# office: www.oldboyedu.com


VERSION=2.53.3
ARCH=amd64
SOFTWARE=prometheus-${VERSION}.linux-${ARCH}.tar.gz
URL=https://github.com/prometheus/prometheus/releases/download/v${VERSION}/${SOFTWARE}
DOWNLOAD=./download
INSTALLDIR=/oldboyedu/softwares
BASEDIR=${INSTALLDIR}/prometheus-${VERSION}.linux-amd64
DATADIR=/oldboyedu/data/prometheus
LOGDIR=/oldboyedu/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=Oldboyedu Linux  Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
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

2.访问Prometheus的WebUI
image

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

二.测试验证

1.安装Prometheus server
image

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

2.卸载Prometheus server
image

如上图所示,可以实现Prometheus server的卸载。
posted @   almco  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示