#!/usr/bin/env bash set +u set -e # set -x base_dir=$( cd "$(dirname "$0")" pwd ) cd $base_dir # ./auto_del_log.sh function log_warn() { local date=$(date "+%Y-%m-%d-%H:%M:%S") local para=$1 echo "[WARN] $date $1" | tee -a "./log/run_log" } function log_info() { local date=$(date "+%Y-%m-%d-%H:%M:%S") local para=$1 echo "[INFO] $date $1" | tee -a "./log/run_log" } function log_err() { local date=$(date "+%Y-%m-%d-%H:%M:%S") local para=$1 echo "[ERROR] $date $1" | tee -a "./log/run_log" } node_exporter_log_path="./log/node_exporter/" postgres_exporter_log_path="./log/postgres_exporter/" # node_exporter section if ps -auxc | grep "node_exporter" | grep -v grep > /dev/null; then log_info "node_exporter 已在运行" elif [[ ! -f "./lib/node_exporter" ]]; then log_info "no node_exporter found, skip" else if [[ ! -d $node_exporter_log_path ]]; then mkdir -p "$node_exporter_log_path" fi log_node_exporter_file="node_exporter_$(date "+%Y-%m-%d-%H:%M:%S").log" ./lib/node_exporter &>> "${node_exporter_log_path}${log_node_exporter_file}" & sleep 1 log_info "node_exporter 启动成功!" fi # postgres_exporter section if ps -auxc | grep "postgres_exporter" | grep -v grep > /dev/null; then log_info "postgres_exporter 已在运行" elif [[ ! -f "./lib/postgres_exporter" ]]; then log_info "no postgres_exporter found, skip" else if [[ ! -d $postgres_exporter_log_path ]]; then mkdir -p "$postgres_exporter_log_path" fi #todo 输入用户名和密码 export DATA_SOURCE_NAME=postgresql://username:password@localhost:5432/?sslmode=disable log_postgres_exporter_file="postgres_exporter_$(date "+%Y-%m-%d-%H:%M:%S").log" ./lib/postgres_exporter --web.listen-address=:9187 &>> "${postgres_exporter_log_path}${log_postgres_exporter_file}" & sleep 1 log_info "postgres_exporter 启动成功!" fi