runExporters.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. set +u
  3. set -e
  4. # set -x
  5. base_dir=$(
  6. cd "$(dirname "$0")"
  7. pwd
  8. )
  9. cd $base_dir
  10. # ./auto_del_log.sh
  11. function log_warn() {
  12. local date=$(date "+%Y-%m-%d-%H:%M:%S")
  13. local para=$1
  14. echo "[WARN] $date $1" | tee -a "./log/run_log"
  15. }
  16. function log_info() {
  17. local date=$(date "+%Y-%m-%d-%H:%M:%S")
  18. local para=$1
  19. echo "[INFO] $date $1" | tee -a "./log/run_log"
  20. }
  21. function log_err() {
  22. local date=$(date "+%Y-%m-%d-%H:%M:%S")
  23. local para=$1
  24. echo "[ERROR] $date $1" | tee -a "./log/run_log"
  25. }
  26. node_exporter_log_path="./log/node_exporter/"
  27. postgres_exporter_log_path="./log/postgres_exporter/"
  28. # node_exporter section
  29. if ps -auxc | grep "node_exporter" | grep -v grep > /dev/null; then
  30. log_info "node_exporter 已在运行"
  31. elif [[ ! -f "./lib/node_exporter" ]]; then
  32. log_info "no node_exporter found, skip"
  33. else
  34. if [[ ! -d $node_exporter_log_path ]]; then
  35. mkdir -p "$node_exporter_log_path"
  36. fi
  37. log_node_exporter_file="node_exporter_$(date "+%Y-%m-%d-%H:%M:%S").log"
  38. ./lib/node_exporter &>> "${node_exporter_log_path}${log_node_exporter_file}" &
  39. sleep 1
  40. log_info "node_exporter 启动成功!"
  41. fi
  42. # postgres_exporter section
  43. if ps -auxc | grep "postgres_exporter" | grep -v grep > /dev/null; then
  44. log_info "postgres_exporter 已在运行"
  45. elif [[ ! -f "./lib/postgres_exporter" ]]; then
  46. log_info "no postgres_exporter found, skip"
  47. else
  48. if [[ ! -d $postgres_exporter_log_path ]]; then
  49. mkdir -p "$postgres_exporter_log_path"
  50. fi
  51. #todo 输入用户名和密码
  52. export DATA_SOURCE_NAME=postgresql://username:password@localhost:5432/?sslmode=disable
  53. log_postgres_exporter_file="postgres_exporter_$(date "+%Y-%m-%d-%H:%M:%S").log"
  54. ./lib/postgres_exporter --web.listen-address=:9187 &>> "${postgres_exporter_log_path}${log_postgres_exporter_file}" &
  55. sleep 1
  56. log_info "postgres_exporter 启动成功!"
  57. fi