-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereum-metrics-exporter.sh
executable file
·269 lines (234 loc) · 8.73 KB
/
ethereum-metrics-exporter.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
#!/bin/bash
# Variables
GITHUB_URL=https://api.github.com/repos/ethpandaops/ethereum-metrics-exporter/releases/latest
GITHUB_RELEASE_NODES=https://github.com/ethpandaops/ethereum-metrics-exporter/releases
ETHEREUM_METRICS_EXPORTER_OPTIONS=(
--metrics-port 9099
--consensus-url=http://localhost:5052
--execution-url=http://localhost:8545
)
GRAFANA_DIR=/etc/grafana
ip_current=$(hostname --ip-address)
interface_current=$(ip route | grep default | sed 's/.*dev \([^ ]*\) .*/\1/')
network_current="$(ip route | grep $interface_current | grep -v default | awk '{print $1}')"
# Asks to update
function upgradeBinaries() {
getLatestVersion
if whiptail --title "Update ethereum-metrics-exporter, grafana, prometheus, node-exporter" --yesno "Latest Version of ethereum-metrics-exporter is: $TAG\n\nReminder: Always read the release notes for breaking changes: $GITHUB_RELEASE_NODES\n\nDo you want to update to $TAG?" 10 78; then
downloadClient
upgradeGrafanaPrometheus
promptViewLogs
fi
}
# Asks to view logs
function promptViewLogs() {
if whiptail --title "View Logs" --yesno "Would you like to view logs and confirm everything is running properly?" 8 78; then
sudo bash -c 'journalctl -fu ethereum-metrics-exporter | ccze'
fi
}
# Gets latest tag of ethereum-metrics-exporter
function getLatestVersion() {
TAG=$(curl -s $GITHUB_URL | jq -r .tag_name)
}
# Downloads latest release of ethereum-metrics-exporter
function downloadClient() {
BINARIES_URL="$(curl -s $GITHUB_URL | jq -r ".assets[] | select(.name) | .browser_download_url" | grep linux_amd64.tar.gz$)"
echo Downloading URL: $BINARIES_URL
cd $HOME
# Download
wget -O ethereum-metrics-exporter.tar.gz $BINARIES_URL
# Untar
tar -xzvf ethereum-metrics-exporter.tar.gz -C $HOME
# Cleanup
rm ethereum-metrics-exporter.tar.gz README.md
# Install binary
if systemctl is-active --quiet ethereum-metrics-exporter; then sudo systemctl stop ethereum-metrics-exporter; fi
sudo mv $HOME/ethereum-metrics-exporter-* /usr/local/bin/ethereum-metrics-exporter
sudo systemctl start ethereum-metrics-exporter
}
# Removes ethereum-metrics-exporter, Grafana and Prometheus
function removeAll() {
if whiptail --title "Uninstall Monitoring" --defaultno --yesno "Are you sure you want to remove all monitoring tools?\n(grafana/prometheus/ethereum-metrics-exporter/node-exporter)" 9 78; then
sudo systemctl disable ethereum-metrics-exporter
sudo systemctl stop ethereum-metrics-exporter
sudo rm /etc/systemd/system/ethereum-metrics-exporter.service
sudo rm /usr/local/bin/ethereum-metrics-exporter
sudo systemctl disable grafana-server prometheus prometheus-node-exporter
sudo systemctl stop grafana-server prometheus prometheus-node-exporter
sudo apt remove -y grafana prometheus prometheus-node-exporter
whiptail --title "Uninstall finished" --msgbox "You have uninstalled all monitoring tools." 8 78
fi
}
# Installs Grafana, Prometheus, Node-Exporter
function installGrafanaPrometheus() {
sudo apt-get install -y software-properties-common wget apt-transport-https
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update && sudo apt-get install -y grafana prometheus prometheus-node-exporter
sudo systemctl enable grafana-server prometheus prometheus-node-exporter
sudo systemctl restart grafana-server prometheus prometheus-node-exporter
# Setup prometheus.yml config file
sudo bash -c "cat << 'EOF' > /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'ethereum-metrics-exporter'
static_configs:
- targets: ['localhost:9099']
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
EOF"
}
# Upgrade Grafana, Prometheus, Node-Exporter
function upgradeGrafanaPrometheus() {
sudo apt-get update && sudo apt-get install --only-upgrade -y grafana prometheus prometheus-node-exporter
sudo systemctl restart grafana-server prometheus prometheus-node-exporter
}
# Installs ethereum-metrics-exporter as a systemd service
function installSystemd() {
# Create service user
sudo adduser --system --no-create-home --group ethereum-metrics-exporter
# Create systemd service
sudo bash -c "cat << 'EOF' > /etc/systemd/system/ethereum-metrics-exporter.service
[Unit]
Description=Ethereum Metrics Exporter Service
Wants=network-online.target
After=network-online.target
After=consensus.service
[Service]
Type=simple
User=ethereum-metrics-exporter
Group=ethereum-metrics-exporter
Restart=on-failure
RestartSec=3
KillSignal=SIGINT
TimeoutStopSec=900
ExecStart=/usr/local/bin/ethereum-metrics-exporter $(echo ${ETHEREUM_METRICS_EXPORTER_OPTIONS[@]})
[Install]
WantedBy=multi-user.target
EOF"
sudo systemctl daemon-reload
sudo systemctl enable ethereum-metrics-exporter
}
# Asks whether to open grafana access to local network
function allowLocalAccessToGrafana() {
echo -e "\e[32m:: Open firewall to Grafana for local access ::\e[0m"
echo "Allow access to Grafana from within your local network? [y|n]"
read -rsn1 yn
if [[ ${yn} = [Yy]* ]]; then
sudo ufw allow from $network_current to any port 3000 proto tcp comment 'Allow local LAN access to Grafana Port'
fi
}
# Sets the default Prometheus datasource to http://localhost:9090
function configureDataSource() {
sudo bash -c "cat << 'EOF' > $GRAFANA_DIR/provisioning/datasources/datasources.yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://localhost:9090
access: proxy
isDefault: true
EOF"
}
function showNextSteps() {
cat <<EOF
Congrats!
Successfully installed monitoring tools: ethereum-metrics-exporter, grafana, prometheus, node-exporter
Access Grafana at:
http://127.0.0.1:3000
or
http://${ip_current}:3000
Login to Grafana with:
Username: admin
Password: admin
To view dashboards,
1) Click Dashboards in the primary menu.
EOF
echo "Press ENTER to continue"
read
}
function provisionDashboards() {
# Install jq if not installed
if ! command -v jq >/dev/null 2>&1; then sudo apt-get install jq; fi
# Create yml file to configure provisioning
sudo bash -c "cat << 'EOF' > $GRAFANA_DIR/provisioning/dashboards/dashboard.yml
apiVersion: 1
providers:
- name: 'Prometheus'
orgId: 1
folder: ''
folderUid: ''
type: file
disableDeletion: false
updateIntervalSeconds: 10
allowUiUpdates: true
options:
path: $GRAFANA_DIR/provisioning/dashboards
EOF"
# Download dashboards into provision directory
# Ethereum-Metrics-Exporter Dashboard
ID=16277
REVISION=$(wget -qO - https://grafana.com/api/dashboards/$ID | jq .revision)
URL=https://grafana.com/api/dashboards/$ID/revisions/$REVISION/download
JSON_FILE=$GRAFANA_DIR/provisioning/dashboards/ethereum-metrics-exporter.json
sudo bash -c "wget -qO - $URL | jq 'walk(if . == \"\${DS_PROMETHEUS}\" then \"Prometheus\" else . end)' > $JSON_FILE"
# Node Exporter Dashboard by StarsL
ID=11074
REVISION=$(wget -qO - https://grafana.com/api/dashboards/$ID | jq .revision)
URL=https://grafana.com/api/dashboards/$ID/revisions/$REVISION/download
JSON_FILE=$GRAFANA_DIR/provisioning/dashboards/node-exporter-for-prometheus-dashboard.json
sudo bash -c "wget -qO - $URL | jq 'walk(if . == \"\${DS__VICTORIAMETRICS}\" then \"Prometheus\" else . end)' > $JSON_FILE"
# Delete any failed 0 size dashboards
find $GRAFANA_PROVISION_DIR -type f -size 0 -delete
}
# Displays usage info
function usage() {
cat <<EOF
Usage: $(basename "$0") [-i] [-u] [-r]
Ethereum-Metrics-Exporter Monitoring Helper Script
Options)
-i Install ethereum-metrics-exporter, grafana, prometheus, node-exporter as a systemd service
-u Upgrade ethereum-metrics-exporter, grafana, prometheus, node-exporter
-r Remove all monitoring tools
-h Display help
About Ethereum Metrics Exporter)
- This exporter aims to simplify observation across various clients
- Introduces a unified set of metrics that can be utilized on any dashboard
- Source repo: https://github.com/ethpandaops/ethereum-metrics-exporter
EOF
}
# Process command line options
while getopts :iurh opt; do
case ${opt} in
i)
installGrafanaPrometheus
installSystemd
configureDataSource
provisionDashboards
downloadClient
allowLocalAccessToGrafana
showNextSteps
promptViewLogs
;;
u) upgradeBinaries ;;
r) removeAll ;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
usage
exit 1
;;
esac
done