概要
为了方便更好的安装prometheus监控体系,创建了一个一键搭建监控的脚本,但实际需要跟现场环境进行匹配,修改。
脚本使用方法:
1.修改你需要下载的版本号
2.创建一个目录名为/prometheus的目录
3.如需修改为其他目录,需修改脚本文件,以及*.service的目录路径
代码如下:
#!bin/bash
#定义prometheus版本号
PROMETHEUS_VERSION="2.25.0" #选择要下载的版本
NODE_EXPORTER_VERSION="1.1.2"
GRAFANA_VERSION="7.4.3"
#下载链接
DOWNlOAD_PROMETHEUS_URL="https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz"
wget ${DOWNlOAD_PROMETHEUS_URL}
#判断是否下载成功
if [ $? -ne 0]; then
echo "Failed to download Prometheus"
exit 1
fi
#解压prometheus到指定路径 此处在/prometheus/
PROMETHEUS_DIR="/prometheus"
mkdir ${PROMETHEUS_DIR}
tar -xf prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz -C ${PROMETHEUS_DIR}
mv ${PROMETHEUS_DIR}/prometheus-${PROMETHEUS_VERSION}.linux-amd64 ${PROMETHEUS_DIR}/prometheus
cat >> /usr/lib/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/prometheus/prometheus/prometheus --config.file=/prometheus/prometheus/prometheus.yml --web.read-timeout=5m --web.max-connections=10 --storage.tsdb.retention=15d --storage.tsdb.path=/prometheus/prometheus/data --query.max-concurrency=20 --query.timeout=2m
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl restart prometheus
# 检查服务是否成功启动
if ! systemctl is-active --quiet prometheus; then
echo "Failed to start prometheus service"
exit 1
fi
systemctl enable prometheus
echo "prometheus has been installed and started successfully."
#下载node_exporter
DOWNlOAD_NODE_EXPORTER_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
wget ${DOWNlOAD_NODE_EXPORTER_URL}
# 检查下载是否成功
if [ $? -ne 0 ]; then
echo "Failed to download node_exporter"
exit 1
fi
tar xf "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz" -C ${PROMETHEUS_DIR}
# 重命名解压后的目录
mv ${PROMETHEUS_DIR}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/ ${PROMETHEUS_DIR}/node_exporter
# 创建systemd服务文件
cat > /usr/lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Prometheus Node Exporter
[Service]
Restart=on-failure
ExecStart=/prometheus/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service
[Install]
WantedBy=multi-user.target
EOF
# 重新加载systemd以识别新服务
systemctl daemon-reload
# 启动node_exporter服务
systemctl restart node_exporter
# 检查服务是否成功启动
if ! systemctl is-active --quiet node_exporter; then
echo "Failed to start node_exporter service"
exit 1
fi
# 设置node_exporter服务开机自启
systemctl enable node_exporter
echo "node_exporter has been installed and started successfully."
#下载grafana
DOWNlOAD_GRAFANA_URL="wget https://dl.grafana.com/enterprise/release/grafana-enterprise-${GRAFANA_VERSION}.linux-amd64.tar.gz"
wget ${DOWNlOAD_GRAFANA_URL}
# 检查下载是否成功
if [ $? -ne 0 ]; then
echo "Failed to download granafa"
exit 1
fi
tar xf "granafa-enterprise-${GRAFANA_VERSION}.linux-amd64.tar.gz" -C ${PROMETHEUS_DIR}
# 重命名解压后的目录
mv ${PROMETHEUS_DIR}/granafa-${GRAFANA_VERSION}/ ${PROMETHEUS_DIR}/granafa
# 创建systemd服务文件
cat > /usr/lib/systemd/system/grafana.service <<EOF
[Unit]
Description=Grafana
After=network.target
[Service]
ExecStart=/prometheus/grafana/bin/grafana-server -homepath=/prometheus/grafana
[Install]
WantedBy=multi-user.target
EOF
# 重新加载systemd以识别新服务
systemctl daemon-reload
# 启动node_exporter服务
systemctl restart grafana
# 检查服务是否成功启动
if ! systemctl is-active --quiet grafana; then
echo "Failed to start grafana service"
exit 1
fi
# 设置node_exporter服务开机自启
systemctl enable grafana
echo "grafana has been installed and started successfully."
总结:
使用前修改脚本的一些配置,比如说路径、版本号等等 ,更好的去使用该脚本,可能存在修改需要,评论区可以进行讨论,学习。
标签:node,脚本,exporter,service,PROMETHEUS,grafana,prometheus,VERSION,DIR From: https://blog.csdn.net/m0_65196233/article/details/140485851