install_prometheus
#!/usr/bin/bash
url=""
ip=`ip addr |grep inet |grep brd |awk '{print $2}' |awk -F '/' '{print $1}'`
prometheus_status=`systemctl status prometheus |grep Active |awk '{print $3}' `
old_status="(running)"
if [ -e prometheus-2.28.1.linux-amd64.tar.gz ];then
tar -zxvf prometheus-2.28.1.linux-amd64.tar.gz
if [ ! -d /usr/local/prometheus ];then
echo "移动文件"
mv prometheus-2.28.1.linux-amd64 /usr/local/prometheus
echo "修改配置文件的localhost为本机$ip"
sed -ir "s/localhost/$ip/" /usr/local/prometheus/prometheus.yml
cat <<EOF >/usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
Documentation=https://prometheus.io/
After=network-online.target
[Service]
User=root
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo "创建系统服务"
systemctl enable prometheus
echo "设置成开机自启"
systemctl daemon-reload
echo "每次更改prometheus.service都需要执行一次,重新加载一下"
systemctl start prometheus
echo "启动prometheus服务"
systemctl status prometheus &>/dev/null
if [[ $prometheus_status -eq $old_status ]];then
echo "服务install成功,请通过http://$ip:9090进行访问,程序退出。。。"
exit
else
echo "服务安装失败,请检查。。"
exit
fi
else
echo "目录已存在,程序退出"
exit
fi
else
echo "当前目录不存在安装包,将要开始下载安装包"
wget $url
tar -zxvf prometheus-2.28.1.linux-amd64.tar.gz
if [ ! -d /usr/local/prometheus ];then
echo "移动文件"
mv prometheus-2.28.1.linux-amd64 /usr/local/prometheus
echo "修改配置文件的localhost为本机$ip"
sed -ir "s/localhost/$ip/" /usr/local/prometheus/prometheus.yml
cat <<EOF >/usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
Documentation=https://prometheus.io/
After=network-online.target
[Service]
User=root
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo "创建系统服务"
systemctl enable prometheus
echo "设置成开机自启"
systemctl daemon-reload
echo "每次更改prometheus.service都需要执行一次,重新加载一下"
systemctl start prometheus
echo "启动prometheus服务"
systemctl status prometheus &>/dev/null
if [[ $prometheus_status -eq $old_status ]];then
echo "服务install成功,请通过http://$ip:9090进行访问,程序退出。。。"
exit
else
echo "服务安装失败,请检查。。"
exit
fi
else
echo "目录已存在,程序退出"
exit
fi
fi