vlambda博客
学习文章列表

傻瓜式一键部署prometheus全套服务:shell脚本送上

#!/bin/bash

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

# File Name: install.sh

# Version: V1.0

# Author: sanshi

# Organization: 暂缺

# Created Time : 2022/3/28

# Description:

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

set -eu


install_path=`pwd $0`

[[ `id -u` -ne 0 ]] && echo "root permission" && exit 1

[ -f $install_path/prom.tgz ] || \

wget -cP $install_path https://9133w.cn/sources/prom.tgz

conf_path=$install_path/conf

pushd $install_path && tar xf prom.tgz && popd


daemonize_install() {

if ! command -v daemonize &> /dev/null

then

        rpm -q fontconfig urw-fonts gcc gcc-c++ make ansible| \

        grep -oP '(?<=package )\S+' | \

        xargs -r yum install -y

        pushd $install_path/daemonize && \

        /bin/sh configure && \

        make && make install && popd

fi

}


daemonize_install


[[ "$install_path" == "/opt/prom" ]] || \

sed -i "s@/opt/prom@$install_path@g" $install_path/start/*.sh


install_node_exporter() {

if ! ps -ef | grep -vE "$0|grep" | grep -q node_exporter

then

        chmod +x  $install_path/node_exporter

        daemonize -c $install_path/node_exporter \

                -e $install_path/logs/node_exporter.log \

                $install_path/start/node_exporter_start.sh

        sleep 1

        ps -ef| grep -q node_exporter && \

        echo "============node_exporter starting===========" || \

        echo "============node_exporter failure============"

fi

}

install_pushgateway() {

if ! ps -ef | grep -vE "$0|grep" | grep -q pushgateway

then

        chmod +x $install_path/pushgateway

        daemonize -c $install_path/pushgateway \

                -e $install_path/logs/pushgateway.log \

                $install_path/start/pushgateway_start.sh

        sleep 1

        ps -ef| grep -vE "$0|grep" | grep -q pushgateway && \

        echo "============pushgateway starting===========" || \

        echo "============pushgateway failure============"

fi

}

install_alertmanager() {

if ! ps -ef | grep -vE "$0|grep" | grep alertmanager

then

        chmod +x $install_path/alertmanager 

        daemonize -c $install_path/alertmanager \

                -e $install_path/logs/alertmanager.log \

                $install_path/start/alertmanager_start.sh

        sleep 1

        ps -ef | grep -vE "$0|grep" | grep -q alertmanager && \

        echo "============alertmanager starting===========" || \

        echo "============alertmanager failure============"

fi

}

install_prometheus() {

if ! ps -ef | grep -vE "$0|grep" | grep prometheus

then

        chmod +x $install_path/prometheus

        alert_addr=`awk -F 'addr:' '/alertmanager_addr/{print $2}' $conf_path/init.conf`

        node_addr=`awk -F 'addr:' '/node_exporter_addr/{print $2}' $conf_path/init.conf`

        prom_addr=`awk -F 'addr:' '/prometheus_addr/{print $2}' $conf_path/init.conf`

        push_addr=`awk -F 'addr:' '/pushgateway_addr/{print $2}' $conf_path/init.conf`

        cp $conf_path/prometheus.yml.default $conf_path/prometheus.yml

        sed -ie "s/alert_addr/$alert_addr/" $conf_path/prometheus.yml

        sed -ie "s/node_exporter/$node_addr/" $conf_path/prometheus.yml

        sed -ie "s/pushgateway/$push_addr/" $conf_path/prometheus.yml

        sed -ie "s/prometheus/$prom_addr/" $conf_path/prometheus.yml

        daemonize -c $install_path/prometheus \

                -e $install_path/logs/prometheus.log \

                $install_path/start/prometheus_start.sh

        sleep 3

        ps -ef | grep -vE "$0|grep" | grep -q prometheus && \

        echo "============prometheus starting===========" || \

        echo "============prometheus failure============"

fi

}

install_grafana() {

if ! ps -ef | grep -vE "$0|grep" |  grep grafana

then

        pushd $install_path && \

        rpm -ivh grafana.rpm && \

        systemctl enable grafana-server && \

        systemctl start grafana-server

        sleep 1

        ps -ef | grep -vE "$0|grep" | grep -q grafana && \

        echo "============grafana starting===========" || \

        echo "============grafana failure============"

fi

}


args=${1:- }

case $args in

node_exporter)

        install_node_exporter

        ;;

pushgateway)

        install_pushgateway

        ;;

alertmanager)

        install_alertmanager

        ;;

prometheus)

        install_prometheus

        ;;

grafana)

        install_grafana

        ;;

all)

        install_node_exporter

        install_pushgateway

        install_alertmanager

        install_prometheus

        install_grafana

        ;;

*)

        echo "Usage: $0 {all|node_exporter|pushgateway|alertmanager|prometheus|grafana}"

        exit 0

esac

exit 0