Monitoring Peers OUT Connections is a metric available out of the box, when you setup Grafana, Prometheus and Prometheus Node Exporter on your Cardano Stake Pool. It allows you to check on your Grafana Dashboard, how many connections are initiated by your nodes. But Peers IN connections is not available by default. It’s a shame because it’s a useful metric, especially on your Relay Node. A relay that has many IN connections from different other relays is a healthy Relay.

So i decided to write a little script, to check every 1 minute, how many IN connections are there, on my Cardano Stake Pool Nodes.

You can find it on my Github as well : https://github.com/Kirael12/Cardano-Monitoring-Peers-In

This is my setup :

– Stake Pool setup with Coincashew Guide.
– 1 Block Producer connected to Relay on private LAN. Prometheus Node Exporter installed
– 1 Relay connected to BP on private LAN, and connected to internet on Public IP. Prometheus Node Exporter installed.
– Monitoring Server with Grafana and Prometheus, which collect all the metrics from the Cardano nodes, and make them available to dashboards from data source Prometheus.

Create script directories and peersin script file

mkdir $HOME/custom-metrics/peersin
mkdir $HOME/custom-metrics/tmp
sudo mkdir /root/scripts
sudo nano /root/scripts/peersin.sh

(As the scripts are going to be run by root, which doesn’t have env variables, i’m going to always use full path. I also use a dedicated directory for those scripts)

peersin.sh (adapt with your nodes IP) :

ON A CARDANO RELAY NODE :

#!/bin/bash
peers_in=$(ss -tnp state established 2>/dev/null | egrep "cardano-node" | egrep "<PUBLIC IP RELAY NODE>:|<PRIVATE IP RELAY NODE>:6000" | grep -v "<PRIVATE IP BLOCK PRODUCER NODE>:6000" | awk '{print $3; }' | sort)
peers_in_count=$(echo "$peers_in" | wc -l)

printf '{\n"peers_in_count":%s}' $peers_in_count > <full path to your $HOME directory>/custom-metrics/peersin/peers_in.json

ON A CARDANO BLOCK PRODUCER NODE :

#!/bin/bash
peers_in=$(ss -tnp state established 2>/dev/null | egrep "cardano-node" | egrep ":6000" | grep -v "<PRIVATE IP RELAY NODE>:6000" | awk '{gsub(/\[::ffff:/,""); gsub(/\]/,""); print $4 " --> "$3; }' | sort)
peers_in_count=$(echo "$peers_in" | wc -l)

printf '{\n"peers_in_count":%s}' $peers_in_count > <full path to your $HOME directory>/custom-metrics/peersin/peers_in.json

Now we are going to create the scrap script that will write a prometheus file from the results of the peersin.sh script, and push it to the node exporter directory.

sudo nano /root/scripts/peersin_Results.sh

peersin_Results.sh :

#!/bin/bash
peersin="<full path to your $HOME directory>/custom-metrics/peersin/peers_in.json"

peers_in_count=`jq .peers_in_count $peersin`

cat << EOF > "<full path to your $HOME directory>/custom-metrics/tmp/peerin.prom.$$"
network_peers_in_count ${peers_in_count}
EOF

mv <full path to your $HOME directory>/custom-metrics/tmp/peerin.prom.$$ /var/lib/prometheus/node-exporter/peerin.prom

Make scripts executable

sudo chmod +x /root/scripts/peersin_Results.sh
sudo chmod +x /root/scripts/peersin.sh

Now we create a root cronjob that will check every 1 minute the IN connections on your Cardano Node, and export it to Prometheus

sudo crontab -e

Add this line to your crontab :

*/1 * * * * /root/scripts/peersin.sh && /root/scripts/peersin_Results.sh

On your Grafana Server, you should now be able to see the custom metric “network_peers_in_count” when exploring your Prometheus Datasource :

Cardano Prometheus Node Exporter

And add it to your Dashboard 🙂

Grafana Cardano Dashboard Peers In