prometheus-install.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. RD=`echo "\033[01;31m"`
  4. BL=`echo "\033[36m"`
  5. GN=`echo "\033[1;92m"`
  6. CL=`echo "\033[m"`
  7. RETRY_NUM=10
  8. RETRY_EVERY=3
  9. NUM=$RETRY_NUM
  10. CM="${GN}✓${CL}"
  11. CROSS="${RD}✗${CL}"
  12. BFR="\\r\\033[K"
  13. HOLD="-"
  14. set -o errexit
  15. set -o errtrace
  16. set -o nounset
  17. set -o pipefail
  18. shopt -s expand_aliases
  19. alias die='EXIT=$? LINE=$LINENO error_exit'
  20. trap die ERR
  21. function error_exit() {
  22. trap - ERR
  23. local reason="Unknown failure occurred."
  24. local msg="${1:-$reason}"
  25. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  26. echo -e "$flag $msg" 1>&2
  27. exit $EXIT
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. while [ "$(hostname -I)" = "" ]; do
  45. 1>&2 echo -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. ((NUM--))
  48. if [ $NUM -eq 0 ]
  49. then
  50. 1>&2 echo -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  51. exit 1
  52. fi
  53. done
  54. msg_ok "Set up Container OS"
  55. msg_ok "Network Connected: ${BL}$(hostname -I)"
  56. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else msg_error "Internet NOT Connected"; exit 1; fi;
  57. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  58. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi;
  59. msg_info "Updating Container OS"
  60. apt-get update &>/dev/null
  61. apt-get -y upgrade &>/dev/null
  62. msg_ok "Updated Container OS"
  63. msg_info "Installing Dependencies"
  64. apt-get install -y curl &>/dev/null
  65. apt-get install -y sudo &>/dev/null
  66. msg_ok "Installed Dependencies"
  67. msg_info "Installing Prometheus"
  68. mkdir -p /etc/prometheus
  69. mkdir -p /var/lib/prometheus
  70. wget https://github.com/prometheus/prometheus/releases/download/v2.36.2/prometheus-2.36.2.linux-amd64.tar.gz &>/dev/null
  71. tar -xvf prometheus-2.36.2.linux-amd64.tar.gz &>/dev/null
  72. cd prometheus-2.36.2.linux-amd64
  73. mv prometheus promtool /usr/local/bin/
  74. mv consoles/ console_libraries/ /etc/prometheus/
  75. mv prometheus.yml /etc/prometheus/prometheus.yml
  76. msg_ok "Installed Prometheus"
  77. msg_info "Creating Service"
  78. service_path="/etc/systemd/system/prometheus.service"
  79. echo "[Unit]
  80. Description=Prometheus
  81. Wants=network-online.target
  82. After=network-online.target
  83. [Service]
  84. User=root
  85. Restart=always
  86. Type=simple
  87. ExecStart=/usr/local/bin/prometheus \
  88. --config.file=/etc/prometheus/prometheus.yml \
  89. --storage.tsdb.path=/var/lib/prometheus/ \
  90. --web.console.templates=/etc/prometheus/consoles \
  91. --web.console.libraries=/etc/prometheus/console_libraries \
  92. --web.listen-address=0.0.0.0:9090
  93. [Install]
  94. WantedBy=multi-user.target" > $service_path
  95. sudo systemctl enable --now prometheus &>/dev/null
  96. msg_ok "Created Service"
  97. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  98. if [[ $PASS != $ ]]; then
  99. msg_info "Customizing Container"
  100. chmod -x /etc/update-motd.d/*
  101. touch ~/.hushlogin
  102. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  103. mkdir -p $(dirname $GETTY_OVERRIDE)
  104. cat << EOF > $GETTY_OVERRIDE
  105. [Service]
  106. ExecStart=
  107. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  108. EOF
  109. systemctl daemon-reload
  110. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  111. msg_ok "Customized Container"
  112. fi
  113. msg_info "Cleaning up"
  114. apt-get autoremove >/dev/null
  115. apt-get autoclean >/dev/null
  116. rm -rf /var/{cache,log}/* /var/lib/apt/lists/* /root/prometheus-2.36.2.linux-amd64 /root/prometheus-2.36.2.linux-amd64.tar.gz
  117. msg_ok "Cleaned"