prometheus-v5-install.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env bash
  2. if [ "$VERBOSE" == "yes" ]; then set -x; STD=""; fi
  3. if [ "$VERBOSE" != "yes" ]; then STD="silent"; fi
  4. silent() { "$@" > /dev/null 2>&1; }
  5. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  6. YW=$(echo "\033[33m")
  7. RD=$(echo "\033[01;31m")
  8. BL=$(echo "\033[36m")
  9. GN=$(echo "\033[1;92m")
  10. CL=$(echo "\033[m")
  11. RETRY_NUM=10
  12. RETRY_EVERY=3
  13. CM="${GN}✓${CL}"
  14. CROSS="${RD}✗${CL}"
  15. BFR="\\r\\033[K"
  16. HOLD="-"
  17. set -Eeuo pipefail
  18. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  19. function error_handler() {
  20. local exit_code="$?"
  21. local line_number="$1"
  22. local command="$2"
  23. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  24. echo -e "\n$error_message\n"
  25. }
  26. function msg_info() {
  27. local msg="$1"
  28. echo -ne " ${HOLD} ${YW}${msg}..."
  29. }
  30. function msg_ok() {
  31. local msg="$1"
  32. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  33. }
  34. function msg_error() {
  35. local msg="$1"
  36. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  37. }
  38. msg_info "Setting up Container OS "
  39. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  40. locale-gen >/dev/null
  41. for ((i=RETRY_NUM; i>0; i--)); do
  42. if [ "$(hostname -I)" != "" ]; then
  43. break
  44. fi
  45. echo 1>&2 -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. done
  48. if [ "$(hostname -I)" = "" ]; then
  49. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  50. echo -e " 🖧 Check Network Settings"
  51. exit 1
  52. fi
  53. msg_ok "Set up Container OS"
  54. msg_ok "Network Connected: ${BL}$(hostname -I)"
  55. set +e
  56. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  57. msg_error "Internet NOT Connected"
  58. read -r -p "Would you like to continue anyway? <y/N> " prompt
  59. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  60. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  61. else
  62. echo -e " 🖧 Check Network Settings"
  63. exit 1
  64. fi
  65. fi
  66. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  67. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  68. set -e
  69. msg_info "Updating Container OS"
  70. $STD apt-get update
  71. $STD apt-get -y upgrade
  72. msg_ok "Updated Container OS"
  73. msg_info "Installing Dependencies"
  74. $STD apt-get install -y curl
  75. $STD apt-get install -y sudo
  76. $STD apt-get install -y mc
  77. msg_ok "Installed Dependencies"
  78. msg_info "Installing Prometheus"
  79. RELEASE=$(curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
  80. mkdir -p /etc/prometheus
  81. mkdir -p /var/lib/prometheus
  82. $STD wget https://github.com/prometheus/prometheus/releases/download/v${RELEASE}/prometheus-${RELEASE}.linux-amd64.tar.gz
  83. $STD tar -xvf prometheus-${RELEASE}.linux-amd64.tar.gz
  84. cd prometheus-${RELEASE}.linux-amd64
  85. mv prometheus promtool /usr/local/bin/
  86. mv consoles/ console_libraries/ /etc/prometheus/
  87. mv prometheus.yml /etc/prometheus/prometheus.yml
  88. msg_ok "Installed Prometheus"
  89. msg_info "Creating Service"
  90. service_path="/etc/systemd/system/prometheus.service"
  91. echo "[Unit]
  92. Description=Prometheus
  93. Wants=network-online.target
  94. After=network-online.target
  95. [Service]
  96. User=root
  97. Restart=always
  98. Type=simple
  99. ExecStart=/usr/local/bin/prometheus \
  100. --config.file=/etc/prometheus/prometheus.yml \
  101. --storage.tsdb.path=/var/lib/prometheus/ \
  102. --web.console.templates=/etc/prometheus/consoles \
  103. --web.console.libraries=/etc/prometheus/console_libraries \
  104. --web.listen-address=0.0.0.0:9090
  105. [Install]
  106. WantedBy=multi-user.target" >$service_path
  107. $STD sudo systemctl enable --now prometheus
  108. msg_ok "Created Service"
  109. echo "export TERM='xterm-256color'" >>/root/.bashrc
  110. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  111. msg_info "Customizing Container"
  112. if [ "$PCT_OSTYPE" == "debian" ]; then rm -rf /etc/motd /etc/update-motd.d/10-uname; else chmod -x /etc/update-motd.d/*; fi
  113. touch ~/.hushlogin
  114. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  115. mkdir -p $(dirname $GETTY_OVERRIDE)
  116. cat <<EOF >$GETTY_OVERRIDE
  117. [Service]
  118. ExecStart=
  119. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  120. EOF
  121. systemctl daemon-reload
  122. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  123. msg_ok "Customized Container"
  124. fi
  125. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  126. msg_info "Cleaning up"
  127. $STD apt-get autoremove
  128. $STD apt-get autoclean
  129. rm -rf /root/prometheus-${RELEASE}.linux-amd64 /root/prometheus-${RELEASE}.linux-amd64.tar.gz
  130. msg_ok "Cleaned"