On the 1st of December, the first phase of the Ethereum 2.0 transition from Proof of Work to Proof of Stake will start - and we're happy to be part of the genesis. We  followed this guide to set up a lighthouse client (we love Rust) developed by Sigma Prime. But we have the feeling that we're too busy to monitor a Grafana Dashboard all the time (especially when shit hits the fan). So we've decided to add some nice Telegram messaging if something fails:

Setting up a Telegram Bot

The Telegram Bot notifies us when some of the services (Geth, Beacon Chain, Validator Client) cannot be started. The first thing you have to do is use @BotFather to create a new bot and give it the "/newbot" command. Provide the bot with a sounding name, and you'll get a message with an access token for the HTTP API.

After that, send the bot a message in your Telegram client and then use the following command to get the chat id.

$ curl "https://api.telegram.org/bot0123456789:BBDKSJhdsjkHDSJKHDkDHSUU/getUpdates"

You'll receive a JSON with a chat id. Note it down somewhere.

The next step is to create an easy telegram script on your server:

$ nano /usr/local/bin/telegram

paste the following code:

#!/bin/bash
source /etc/telegram/key

URL="https://api.telegram.org/bot$TELEGRAM_KEY/sendMessage"
curl -s -d "chat_id=$CHAT_ID&disable_web_page_preview=1&text=$1" $URL > /dev/null

make it executable by using:

$ chmod 644 /usr/local/bin/telegram

Next step adding the telegram configuration file:

$ nano /etc/telegram/key

Paste your bot credentials:

export TELEGRAM_KEY="0123456789:BBDKSJhdsjkHDSJKHDkDHSUU"
export CHAT_ID=1234567890

Save the file and then you can try to send a message to your chat from the shell:

$ telegram "Ohai it's me - your servers"

Setting up an unit status reporter

Setup a systemd unit status reporter by adding the following file:

$ nano /usr/local/bin/unit-status-telegram

with this code:

#!/bin/bash

UNIT=$1

UNITSTATUS=$(systemctl status $UNIT)
ALERT=$(echo -e "\u26A0")

telegram "$ALERT Unit failed $UNIT $ALERT
Status:
$UNITSTATUS"

Make it executable and try it out:

$ chmod 644 /usr/local/bin/unit-status-telegram
$ unit-status-telegram
$ # or
$ unit-status-telegram geth

it should send you a systemctl status message for the corresponding service.

Wrestling systemd

First, we need to create a service for sending the telegram message. Open the file:

$ nano /etc/systemd/system/unit-status-telegram@.service

and "create" the service:

[Unit]
Description=Unit Status Telegram Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/unit-status-telegram %i

Please note that this service does not have to be enabled or started since it's triggered OnFailure from other services.

Next we adapt these services to report on failure. We do it for geth:

[Unit]
Description=Ethereum go client
After=network.target
Wants=network.target
OnFailure=unit-status-telegram@geth.service
StartLimitBurst=5
StartLimitIntervalSec=33
[Service]
User=goeth
Group=goeth
Type=simple
Restart=always
RestartSec=5
ExecStart=geth --http --cache 4096 --datadir /var/lib/goethereum
[Install]
WantedBy=default.target

For the lighthouse beacon chain:

[Unit]
Description=Lighthouse Beacon Node
Wants=network-online.target
After=network-online.target
OnFailure=unit-status-telegram@lighthousebeacon.service
StartLimitBurst=5
StartLimitIntervalSec=33
[Service]
Type=simple
User=lighthousebeacon
Group=lighthousebeacon
Restart=always
RestartSec=5
ExecStart=/usr/local/bin/lighthouse bn --network mainnet --staking --metrics --datadir /var/lib/lighthouse --eth1-endpoint http://127.0.0.1:8545
[Install]
WantedBy=multi-user.target

and for the validator client:

[Unit]
Description=Lighthouse Validator
Wants=network-online.target
After=network-online.target
OnFailure=unit-status-telegram@lighthousevalidator.service
StartLimitBurst=5
StartLimitIntervalSec=33
[Service]
Type=simple
User=lighthousevalidator
Group=lighthousevalidator
Restart=always
RestartSec=5
ExecStart=/usr/local/bin/lighthouse vc --network mainnet --datadir /var/lib/lighthouse --graffiti "https://eth2.9elements.com"
[Install]
WantedBy=multi-user.target

The lines that has been changed from the original tutorial are: StartLimitBurst, StartLimitIntervalSec and OnFailure.

Note that we added a StartLimitBurst since OnFailure only works if there is a limit. Last but not least reload the systemd:

$ systemctl daemon-reload

You can try if everything works by changing the geth data-dir to something random like /varrrrr/lib/goethereum and restart geth. You should receive a Telegram message with an error after 33 seconds (obviously the data-dir can't be read).

I hope you liked the tutorial! If you need a helping hand with Ethereum Solidity development or web application development, then ping us. For future updates, please follow us on Twitter.

Photo by Nick Chong.