-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsetup_monitoring.sh
371 lines (315 loc) · 11.5 KB
/
setup_monitoring.sh
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
# v0.5
#Icosa, Hex, Hedron,
#Three shapes in symmetry dance,
#Nature's art is shown.
# By tdslaine aka Peter L Dipslayer TG: @dipslayer369 Twitter: @dipslayer
script_dir=$(dirname "$0")
source "$script_dir/functions.sh"
start_dir=$(pwd)
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
source "./functions.sh"
clear
# Create users
echo "Adding users for prometheus and grafana"
sudo useradd -M -G docker prometheus
sudo useradd -M -G docker grafana
# Prompt the user for the location to store prometheus.yml (default: /blockchain)
read -e -p "Enter the location to store prometheus.yml (default: /blockchain):" config_location
config_location=$(echo "$config_location" | sed 's:/*$::')
# Set the default location to /blockchain if nothing is entered
if [ -z "$config_location" ]; then
config_location="/blockchain"
fi
while true; do
echo "Choose your Client"
echo ""
echo "1. Lighthouse"
echo "2. Prysm"
echo ""
read -p "Enter your choice (1 or 2): " client_choice
case $client_choice in
1|2)
break
;;
*)
echo "Invalid choice. Please enter 1 or 2."
;;
esac
done
echo $client_choice
# Create directories
echo ""
echo "Creating directorys for the prometheus and grafana container"
sudo mkdir -p "$config_location/prometheus"
sudo mkdir -p "$config_location/grafana"
if [[ "$client_choice" == "1" ]]; then
# Define the yml content for use with lighthouse
PROMETHEUS_YML="global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'nodes'
metrics_path: /metrics
static_configs:
- targets: ['localhost:5054']
- job_name: 'validators'
metrics_path: /metrics
static_configs:
- targets: ['localhost:5064']
- job_name: 'geth'
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /debug/metrics/prometheus
scheme: http
static_configs:
- targets: ['localhost:6060']"
elif [[ "$client_choice" == "2" ]]; then
PROMETHEUS_YML="global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'validator'
static_configs:
- targets: ['localhost:8081']
- job_name: 'beacon node'
static_configs:
- targets: ['localhost:8080']
- job_name: 'slasher'
static_configs:
- targets: ['localhost:8082']
- job_name: 'geth'
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /debug/metrics/prometheus
scheme: http
static_configs:
- targets: ['localhost:6060']"
fi
# Create prometheus.yml file
echo ""
echo "Creating the yml file for promethesu"
sudo bash -c "cat > $config_location/prometheus.yml << 'EOF'
$PROMETHEUS_YML
EOF"
# Set ownership and permissions
echo ""
echo "Setting ownership for container-folders"
sudo chown -R prometheus:prometheus "$config_location/prometheus"
sudo chown -R grafana:grafana "$config_location/grafana"
sudo chmod 644 "$config_location/prometheus.yml"
sudo chmod -R 777 "$config_location/grafana"
# Set UFW Rules
echo ""
echo "Setting up firewall rules to allow local connection to metric ports"
sudo ufw allow from 127.0.0.1 to any port 8545 proto tcp
sudo ufw allow from 127.0.0.1 to any port 8546 proto tcp
if [[ "$client_choice" == "1" ]]; then
sudo ufw allow from 127.0.0.1 to any port 5052 proto tcp
sudo ufw allow from 127.0.0.1 to any port 5064 proto tcp
elif [[ "$client_choice" == "2" ]]; then
sudo ufw allow from 127.0.0.1 to any port 8081 proto tcp
sudo ufw allow from 127.0.0.1 to any port 8080 proto tcp
sudo ufw allow from 127.0.0.1 to any port 8082 proto tcp
fi
# Prompt to allow access to Grafana Dashboard in Local Network
function get_local_ip() {
local_ip=$(hostname -I | awk '{print $1}')
echo $local_ip
}
function get_ip_range() {
local_ip=$(get_local_ip)
ip_parts=(${local_ip//./ })
ip_range="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.0/24"
echo $ip_range
}
#debug
local_ip_debug=$(hostname -I | awk '{print $1}')
ip_range=$(get_ip_range)
echo ""
echo "Your Current IP is: $local_ip_debug"
echo ""
read -p "Do you want to allow access to the Grafana Dashboard from within your local network ($ip_range)? (y/n): " local_network_choice
echo ""
if [[ $local_network_choice == "y" ]]; then
echo ""
sudo ufw allow from $ip_range to any port 3000 proto tcp comment 'Grafana Port for private IP range'
fi
echo ""
sudo ufw reload
echo ""
# Define Docker commands as variables
PROMETHEUS_CMD="sudo -u prometheus docker run -dt --name prometheus --restart=always \\
--net='host' \\
-v ${config_location}/prometheus.yml:/etc/prometheus/prometheus.yml \\
-v ${config_location}/prometheus:/prometheus-data \\
prom/prometheus
"
PROMETHEUS_NODE_CMD="sudo -u prometheus docker run -dt --name node_exporter --restart=always \\
--net='host' \\
-v '/:/host:ro,rslave' \\
prom/node-exporter --path.rootfs=/host
"
GRAFANA_CMD="sudo -u grafana docker run -dt --name grafana --restart=always \\
--net='host' \\
-v ${config_location}/grafana:/var/lib/grafana \\
grafana/grafana
"
# Create start_monitoring.sh script
echo ""
echo "Creating start_monitor.sh script"
sudo bash -c "cat > $config_location/start_monitoring.sh << 'EOF'
#!/bin/bash
$PROMETHEUS_CMD
$PROMETHEUS_NODE_CMD
$GRAFANA_CMD
EOF"
get_main_user
# Make start_monitoring.sh executable
sudo chmod +x $config_location/start_monitoring.sh
sudo chmod 770 $config_location/start_monitoring.sh
sudo chown $main_user:docker $config_location/start_monitoring.sh
echo ""
echo "Created Monitoring-Scripts and Set Firewall rules"
cd $config_location
echo "..."
sleep 2
echo ""
echo "Launching prometheus, node-exporter and grafana containers"
echo ""
sudo $config_location/start_monitoring.sh
sleep 2
# checking if they are running
echo ""
echo "Checking if the docker started"
echo ""
if sudo docker ps --format '{{.Names}}' | grep -q '^grafana$'; then
echo "Grafana container is running"
else
echo "Grafana container is not running"
fi
echo ""
if sudo docker ps --format '{{.Names}}' | grep -q '^prometheus$'; then
echo "Prometheus container is running"
else
echo "Prometheus container is not running"
fi
echo ""
if sudo docker ps --format '{{.Names}}' | grep -q '^node_exporter$'; then
echo "Node Exporter container is running"
else
echo "Node Exporter container is not running"
fi
echo ""
sleep 2
# Set variables for the API endpoint, authentication, and datasource configuration
grafana_api="http://localhost:3000/api/datasources"
grafana_auth="admin:admin"
prometheus_url="http://localhost:9090"
datasource_name="Prometheus"
datasource_type="prometheus"
access_mode="proxy"
basic_auth_user=""
basic_auth_password=""
is_default="true"
# Send the POST request to add the datasource using curl
curl -X POST -H "Content-Type: application/json" -d \
'{
"name": "'$datasource_name'",
"type": "'$datasource_type'",
"url": "'$prometheus_url'",
"access": "'$access_mode'",
"basicAuthUser": "'$basic_auth_user'",
"basicAuthPassword": "'$basic_auth_password'",
"isDefault": '$is_default'
}' \
--user "$grafana_auth" \
$grafana_api
sleep 1
echo ""
sudo mkdir -p "${config_location}/Dashboards"
echo "Downloading dashboard JSON..."
sudo wget -qO "${config_location}/Dashboards/002_Geth_dashboard.json" -P "${config_location}/Dashboards" https://gist.githubusercontent.com/karalabe/e7ca79abdec54755ceae09c08bd090cd/raw/dashboard.json >/dev/null
sudo wget -qO "${config_location}/Dashboards/003_System_dashboard.json" -P "${config_location}/Dashboards" https://grafana.com/api/dashboards/11074/revisions/9/download >/dev/null
if [[ "$client_choice" == "1" ]]; then
sudo wget -O "${config_location}/Dashboards/004_Lighthouse_beacon_dashboard.json" -P "${config_location}/Dashboards" https://raw.githubusercontent.com/sigp/lighthouse-metrics/master/dashboards/Summary.json >/dev/null
sudo wget -O "${config_location}/Dashboards/005_Lighthouse_validator_dashboard.json" -P "${config_location}/Dashboards" https://raw.githubusercontent.com/sigp/lighthouse-metrics/master/dashboards/ValidatorClient.json
sudo wget -O "${config_location}/Dashboards/001_Staking_dashboard.json" -P "${config_location}/Dashboards" https://raw.githubusercontent.com/tdslaine/pulse-staking-dashboard/main/Yoldark_ETH_staking_dashboard.json
elif [[ "$client_choice" == "2" ]]; then
sudo wget -qO "${config_location}/Dashboards/001_Prysm_dashboard.json" -P "${config_location}/Dashboards" https://raw.githubusercontent.com/GuillaumeMiralles/prysm-grafana-dashboard/master/less_10_validators.json >/dev/null
sudo wget -qO "${config_location}/Dashboards/000_Prysm_dashboard.json" -P "${config_location}/Dashboards" https://raw.githubusercontent.com/metanull-operator/eth2-grafana/master/eth2-grafana-dashboard-single-source-beacon_node.json >/dev/null
fi
get_main_user
echo ""
echo ""
get_main_user
sudo chown -R $main_user:docker "${config_location}/Dashboards"
sudo chmod -R 777 "${config_location}/Dashboards"
echo ""
echo -e "${GREEN}Congratulations, setup is now complete.${NC}"
echo ""
if [[ $local_network_choice == "y" ]]; then
echo "Access Grafana: http://127.0.0.1:3000 or http://${local_ip_debug}:3000"
echo "Username: admin"
echo "Password: admin"
echo ""
echo "Add dashboards via: http://127.0.0.1:3000/dashboard/import or http://${local_ip_debug}:3000/dashboard/import"
echo "Import JSONs from '${config_location}/Dashboards'"
else
echo "Access Grafana: http://127.0.0.1:3000"
echo "Username: admin"
echo "Password: admin"
echo ""
echo "Add dashboards via: http://127.0.0.1:3000/dashboard/import"
echo "Import JSONs from '${config_location}/Dashboards'"
echo ""
echo "It is adviced to reboot your system after the initial setup has taken place"
fi
echo ""
echo "Brought to you by:
██████__██_██████__███████_██_______█████__██____██_███████_██████__
██___██_██_██___██_██______██______██___██__██__██__██______██___██_
██___██_██_██████__███████_██______███████___████___█████___██████__
██___██_██_██___________██_██______██___██____██____██______██___██_
██████__██_██______███████_███████_██___██____██____███████_██___██_"
echo -e "${GREEN}For Donations use ERC20: 0xCB00d822323B6f38d13A1f951d7e31D9dfDED4AA${NC}"
sleep 1
echo ""
echo "Press enter to continue..."
read -p ""
echo ""
read -e -p "$(echo -e "${GREEN}Would you like to start the logviewer to monitor the client logs? [y/n]:${NC}")" log_it
if [[ "$log_it" =~ ^[Yy]$ ]]; then
echo "Choose a log viewer:"
echo "1. GUI/TAB Based Logviewer (serperate tabs; easy)"
echo "2. TMUX Logviewer (AIO logs; advanced)"
read -p "Enter your choice (1 or 2): " choice
case $choice in
1)
${config_location}/helper/log_viewer.sh
;;
2)
${config_location}/helper/tmux_logviewer.sh
;;
*)
echo "Invalid choice. Exiting."
;;
esac
fi
reboot_prompt
sleep 5
reboot_advice
exit 0