-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
184 lines (161 loc) Β· 5.61 KB
/
setup.sh
File metadata and controls
184 lines (161 loc) Β· 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
set -euo pipefail
# ===============================================
# π§ Monitoring Stack Setup Script (with Auth)
# ===============================================
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run this script with sudo: sudo ./setup.sh"
exit 1
fi
echo ""
echo "========================================"
echo " π Starting Monitoring Stack Setup"
echo "========================================"
echo ""
# -----------------------------
# π§Ή Clean up previous runs
# -----------------------------
echo "π§Ή Cleaning up previous Docker Compose stack..."
sudo docker-compose down -v || true
# -----------------------------
# π§ Load Environment Variables
# -----------------------------
if [ ! -f ".env" ]; then
echo "β ERROR: .env file not found. Please create one before running this script."
exit 1
fi
source .env
# Export alert-related vars for envsubst
export ALERT_SMTP_SMARTHOST ALERT_SMTP_FROM ALERT_SMTP_USER ALERT_SMTP_PASS
export ALERT_EMAIL_TO ALERT_GROUP_WAIT ALERT_GROUP_INTERVAL ALERT_REPEAT_INTERVAL
# -----------------------------
# β
Validate Required Variables
# -----------------------------
echo "π Validating environment variables..."
required_vars=(
HOSTNAME_MONITOR
ALERT_SMTP_SMARTHOST
ALERT_SMTP_FROM
ALERT_SMTP_USER
ALERT_SMTP_PASS
ALERT_EMAIL_TO
GF_SECURITY_ADMIN_USER
GF_SECURITY_ADMIN_PASSWORD
LISTEN_PORT
USE_SELF_SIGNED_TLS
BASIC_AUTH_USER
BASIC_AUTH_PASSWORD
)
for var in "${required_vars[@]}"; do
if [ -z "${!var:-}" ]; then
echo "β Missing required environment variable: $var"
exit 1
fi
done
echo "β
Environment validation complete."
# -----------------------------
# π Prepare Directory Structure
# -----------------------------
echo "π Ensuring directory structure..."
rm -rf data secrets nginx/auth
mkdir -p \
secrets \
nginx/auth \
data/grafana \
data/prometheus \
data/alertmanager
# Set ownership for data
chown -R 472:472 data/grafana
chown -R 65534:65534 data/prometheus data/alertmanager
chmod 700 secrets || true
# -----------------------------
# π Generate TLS Certificates
# -----------------------------
if [ "${USE_SELF_SIGNED_TLS}" = "true" ]; then
if [ ! -f "${TLS_CRT_PATH}" ] || [ ! -f "${TLS_KEY_PATH}" ]; then
echo "π Generating self-signed TLS certificates..."
openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout "${TLS_KEY_PATH}" \
-out "${TLS_CRT_PATH}" \
-subj "/CN=${HOSTNAME_MONITOR:-localhost}"
echo "β
Self-signed certificates created at ${TLS_CRT_PATH} and ${TLS_KEY_PATH}"
else
echo "β
Found existing TLS certificates. Skipping generation."
fi
else
echo "π Using provided TLS certificates..."
if [ ! -f "${TLS_CRT_PATH}" ] || [ ! -f "${TLS_KEY_PATH}" ]; then
echo "β TLS certificates not found:"
echo " ${TLS_CRT_PATH}"
echo " ${TLS_KEY_PATH}"
echo " Please add valid certs or set USE_SELF_SIGNED_TLS=true"
exit 1
fi
fi
# -----------------------------
# π Create Basic Auth File
# -----------------------------
echo "π Configuring Basic Authentication..."
AUTH_FILE="./nginx/auth/monitoring.htpasswd"
# Install apache2-utils if missing (for htpasswd command)
if ! command -v htpasswd &> /dev/null; then
echo "π¦ Installing apache2-utils..."
apt-get update -y >/dev/null
apt-get install -y apache2-utils >/dev/null
fi
htpasswd -bc "${AUTH_FILE}" "${BASIC_AUTH_USER}" "${BASIC_AUTH_PASSWORD}"
# Fix permissions for Docker Nginx access
chmod 644 "${AUTH_FILE}"
chown root:root "${AUTH_FILE}"
chmod 755 ./nginx/auth
echo "β
Basic auth file created and permissions fixed."
# -----------------------------
# βοΈ Configure Nginx
# -----------------------------
echo "βοΈ Preparing Nginx monitoring configuration..."
MONITORING_CONF_DIR="./nginx/conf.d"
MONITORING_CONF_FINAL="${MONITORING_CONF_DIR}/monitoring.conf"
if [ "${USE_SELF_SIGNED_TLS}" = "true" ]; then
cp "${MONITORING_CONF_DIR}/monitoring.tls.conf" "${MONITORING_CONF_FINAL}"
echo "β
Nginx configured for TLS monitoring."
else
cp "${MONITORING_CONF_DIR}/monitoring.nontls.conf" "${MONITORING_CONF_FINAL}"
echo "β
Nginx configured for non-TLS monitoring."
fi
# -----------------------------
# π¬ Configure Alertmanager
# -----------------------------
ALERTMANAGER_TEMPLATE="./alertmanager/alertmanager.yml.template"
ALERTMANAGER_FINAL="./alertmanager/alertmanager.yml"
if [ ! -f "$ALERTMANAGER_TEMPLATE" ]; then
echo "β Missing Alertmanager template: $ALERTMANAGER_TEMPLATE"
exit 1
fi
echo "π Preparing Alertmanager configuration..."
envsubst < "$ALERTMANAGER_TEMPLATE" > "$ALERTMANAGER_FINAL"
chmod 600 "$ALERTMANAGER_FINAL"
chown 65534:65534 "$ALERTMANAGER_FINAL"
# -----------------------------
# π³ Start Docker Stack
# -----------------------------
echo ""
echo "π³ Starting Docker Compose stack..."
sudo docker-compose up -d --build
# -----------------------------
# π Done!
# -----------------------------
echo ""
echo "========================================"
echo " β
Monitoring Stack is Ready!"
echo "----------------------------------------"
echo " π Grafana Dashboard: https://${HOSTNAME_MONITOR}:${LISTEN_PORT}/grafana"
echo " π Prometheus: https://${HOSTNAME_MONITOR}:${LISTEN_PORT}/prometheus"
echo " π¨ Alertmanager: https://${HOSTNAME_MONITOR}:${LISTEN_PORT}/alertmanager"
echo " π§© Blackbox Exporter: https://${HOSTNAME_MONITOR}:${LISTEN_PORT}/blackbox"
echo "----------------------------------------"
echo " π Basic Auth User: ${BASIC_AUTH_USER}"
echo " π Basic Auth Pass: ${BASIC_AUTH_PASSWORD}"
echo "========================================"
echo ""