@@ -37,3 +37,241 @@ du -sh
3737``` sh
3838htop
3939```
40+
41+ ## Metrics
42+
43+ Prometheus maintains a database of metrics (trin & system). Grafana converts metrics into graphs. Node exporter provides system information.
44+ ``` mermaid
45+ graph TD;
46+ Browser-->Grafana-->Prometheus
47+ Prometheus-->Trin & node[Node Exporter]
48+ ```
49+
50+ ### Install prometheus
51+
52+ Download the latest prometheus [ https://prometheus.io/download/ ] ( https://prometheus.io/download/ )
53+
54+ ``` sh
55+ curl -LO < link>
56+ ```
57+ Checksum
58+ ``` sh
59+ sha256sum < filename>
60+ ```
61+ Extract
62+ ``` sh
63+ tar xvf < filename>
64+ ```
65+ The directory will contain the binaries for prometheus and promtool. Copy these.
66+ ``` sh
67+ cd < dirname>
68+ sudo cp prometheus /usr/local/bin/
69+ sudo cp promtool /usr/local/bin/
70+ ```
71+ Copy the console files
72+ ``` sh
73+ sudo cp -r consoles /etc/prometheus
74+ sudo cp -r console_libraries /etc/prometheus
75+ ```
76+ Remove the downloaded files
77+ ``` sh
78+ cd ~
79+ rm < tar filename>
80+ rm -r < extracted prometheus directory>
81+ ```
82+ Make a prometheus user
83+ ``` sh
84+ sudo useradd --no-create-home --shell /bin/false prometheus
85+ ```
86+ Create a prometheus data directory
87+ ``` sh
88+ sudo mkdir -p /var/lib/prometheus
89+ ```
90+ Make a config file
91+ ``` sh
92+ sudo nano /etc/prometheus/prometheus.yml
93+ ```
94+ Put this in the config file:
95+ ``` yml
96+ global :
97+ scrape_interval : 15s
98+ evaluation_interval : 15s
99+
100+ alerting :
101+ alertmanagers :
102+ - static_configs :
103+ - targets :
104+ rule_files :
105+
106+ scrape_configs :
107+ - job_name : ' node_exporter'
108+ static_configs :
109+ - targets : ['localhost:9100']
110+ - job_name : ' trin'
111+ static_configs :
112+ - targets : ['localhost:9101']
113+ ` ` `
114+ The ` node_exporter` job will gather system data by listening to port `9100`.
115+ The `trin` job will gather system data by listening to port `9101`.
116+
117+ Update the permissions
118+ ` ` ` sh
119+ sudo chown -R prometheus:prometheus /etc/prometheus
120+ sudo chown -R prometheus:prometheus /var/lib/prometheus
121+ ` ` `
122+ Prometheus will use port 9090 by default. Check it is not used by something else :
123+ ` ` ` sh
124+ sudo lsof -i:9090
125+ ` ` `
126+ Create a service for prometheus
127+ ` ` ` sh
128+ sudo nano /etc/systemd/system/prometheus.service
129+ ` ` `
130+ Include the following, pick another port if 9090 is already in use.
131+ ` ` ` ini
132+ [Unit]
133+ Description=Prometheus
134+ Wants=network-online.target
135+ After=network-online.target
136+
137+ [Service]
138+ Type=simple
139+ User=prometheus
140+ Group=prometheus
141+ Restart=always
142+ RestartSec=5
143+ ExecStart=/usr/local/bin/prometheus \
144+ --config.file /etc/prometheus/prometheus.yml \
145+ --storage.tsdb.path /var/lib/prometheus/ \
146+ --web.console.templates=/etc/prometheus/consoles \
147+ --web.console.libraries=/etc/prometheus/console_libraries \
148+ --web.listen-address="localhost:9090"
149+ ExecReload=/bin/kill -HUP $MAINPID
150+
151+ [Install]
152+ WantedBy=multi-user.target
153+ ` ` `
154+ Start the service
155+ ` ` ` sh
156+ sudo systemctl daemon-reload
157+ sudo systemctl start prometheus
158+ sudo systemctl status prometheus
159+ sudo systemctl enable prometheus
160+ ` ` `
161+
162+ # ## Install node exporter
163+
164+ Download the latest node exporter [https://prometheus.io/download/#node_exporter](https://prometheus.io/download/#node_exporter)
165+
166+ ` ` ` sh
167+ curl -LO <link>
168+ ` ` `
169+ Checksum
170+ ` ` ` sh
171+ sha256sum <filename>
172+ ` ` `
173+ Extract
174+ ` ` ` sh
175+ tar xvf <filename>
176+ ` ` `
177+ The directory will contain the binary for node exporter. Copy this.
178+ ` ` ` sh
179+ cd <dirname>
180+ sudo cp node_exporter /usr/local/bin/
181+ ` ` `
182+ Remove the downloaded files
183+ ` ` ` sh
184+ cd ~
185+ rm <tar filename>
186+ rm -r <extracted node_exporter directory>
187+ ` ` `
188+ Make a node_exporter user and give it permission to the binary.
189+ ` ` ` sh
190+ sudo useradd --no-create-home --shell /bin/false node_exporter
191+ sudo chown -R node_exporter:node_exporter /usr/local/bin/node_exporter
192+ ` ` `
193+ Make a service file :
194+ ` ` ` sh
195+ sudo nano /etc/systemd/system/node_exporter.service
196+ ` ` `
197+ Start the service
198+ ` ` ` sh
199+ sudo systemctl daemon-reload
200+ sudo systemctl start node_exporter
201+ sudo systemctl status node_exporter
202+ sudo systemctl enable node_exporter
203+ ` ` `
204+ Node explorer uses port 9100 by default.
205+
206+ # ## Install grafana
207+
208+ Install
209+ ` ` ` sh
210+ sudo apt install grafana
211+ ` ` `
212+ Open config
213+ ` ` ` sh
214+ sudo nano /etc/grafana/grafana.ini
215+ ` ` `
216+ Modify the `http_adr` line to use localhost
217+ ` ` ` ini
218+ [server]
219+ ;http_addr = # Before
220+ http_addr = localhost # After
221+ ` ` `
222+ Start grafana
223+ ` ` ` sh
224+ sudo systemctl daemon-reload
225+ sudo systemctl start grafana-server
226+ sudo systemctl status grafana-server
227+ sudo systemctl enable grafana-server
228+ ` ` `
229+ This will serve metrics over port 3000.
230+
231+ Generate a grafana dashboard. From trin root directory :
232+ ` ` ` sh
233+ cargo run -p trin-cli -- create-dashboard
234+ ` ` `
235+ This will create a new monitoring database for trin. This will
236+ be visible in the grafana GUI, or directly at a URL similar to :
237+ http://localhost:3000/d/trin-app-metrics/trin-app-metrics
238+
239+ If you would like to run the create-dashboard command again, the
240+ data source and the dashboard must be deleted, which can be done in the grafana GUI.
241+
242+ # ## Start trin with metrics on
243+
244+ The metrics port must match the trin job set in : ` /etc/prometheus/prometheus.yml` .
245+
246+ ` ` ` sh
247+ cargo run -p trin -- \
248+ --enable-metrics-with-url 127.0.0.1:<metrics job port> \
249+ --web3-http-address http://127.0.0.1:<http port> \
250+ --web3-transport http
251+ ` ` `
252+ For example :
253+
254+ ` ` ` sh
255+ cargo run -p trin -- \
256+ --enable-metrics-with-url 127.0.0.1:9101 \
257+ --web3-http-address http://127.0.0.1:8545 \
258+ --web3-transport http
259+ ` ` `
260+
261+ # ## View metrics remotely
262+
263+
264+ Trin metrics on a remote machine can be monitored by listening to the grafana
265+ address on a local machine.
266+
267+ On local run :
268+ ` ` ` sh
269+ ssh -N -L <port>:127.0.0.1:<port> <user>@<host>
270+ ` ` `
271+ For example
272+ ` ` ` sh
273+ ssh -N -L 3000:127.0.0.1:3000 username@mycomputer
274+ ` ` `
275+ Then navigate to [http://127.0.0.1:3000](http://127.0.0.1:3000)` in a browser and login
276+ with username : admin, password: admin. Then navigate to the trin-app-metrics dashboard.
277+
0 commit comments