paint-brush
Creating a Bash Shell Script That Alerts Via Telegram if Your Zetachain Validator Gets Jailedby@jackblockdaemon
158 reads

Creating a Bash Shell Script That Alerts Via Telegram if Your Zetachain Validator Gets Jailed

by Jack BlockdaemonJune 28th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, we’re going to show you how to create tooling that alerts you via Telegram if your node is jailed.
featured image - Creating a Bash Shell Script That Alerts Via Telegram if Your Zetachain Validator Gets Jailed
Jack Blockdaemon HackerNoon profile picture


It’s important to have proper tooling that alerts you when your infrastructure isn’t performing as it should. When it comes to validators, the stakes (excuse the pun) are high, and so in this article, we’re going to show you how to create tooling that alerts you via Telegram if your node is jailed.


Thanks to @bogkonstantin for his article on creating bash scripts that interact with Telegram.

Creating the Telegram bot

In order to send a message to a Telegram group or channel, the initial step is to establish your own bot. Simply launch the Telegram app, locate @BotFather, and initiate the conversation by typing /start. Proceed to follow the provided instructions for bot creation and acquiring the token necessary for accessing the HTTP API.

Adding your bot to your Telegram channel

Open your Telegram channel and add your newly created bot as a member. In order to get Channel Id, first, post any message to the Channel. Then use this link template to get Channel Id:


<https://api.telegram.org/bot<YourBOTToken>/getUpdates>


Here is a response you can expect.


{
  "ok":true,
  "result": [
    {
      "update_id":123,
      "channel_post": {
        "message_id":48,
        "chat": {
          "id":-123123123, // this is your channel id
          "title":"Notifications",
          "type":"channel"
        },
        "date":1574485277,
        "text":"test"
      }
    }
  ]
}


Create the shell script that checks if the validator is jailed and sends the alert.

The following script runs a command using your zetacored binary to query whether your validator is jailed. Make sure to update your version of the script with your binary location, validator address, and Telegram details. It also logs details of the validator’s status to a log file each time the file is run.


#Simple Script

cmd=$(/root/go/bin/zetacored query staking validator zetavalopercf4aqlv2l3g4i83f3w2mgdqjuiaxu2vkgg532mq | grep 'jailed: false' &> /dev/null )

if [[ $? -eq 0 ]]; then
   echo "Zetachain validator not jailed"
   echo $(date -u) "Zetachain validator not jailed" >> /root/JailedLog/val_jailed_log.txt
else
   echo "Zetachain validator is jailed!"
   echo $(date -u) "Zetachain validator is jailed!" >> /root/JailedLog/val_jailed_log.txt
   curl -X POST -H 'Content-type: application/json' --data '{"text":"Zetachain validator is jailed!"}' https://api.telegram.org/bot2486928301:cCfpaQlV2l3GII8af3W2mGdqJuiaXU/sendMessage?chat_id=-100447288104&text=Zetachain_validator_is_jailed
fi


After creating the shell script, don’t forget to make it executable.


chmod +x check_val_jailed.sh

Setting up the cronjob that calls the script

Finally, we use crontab to call the shell script on a regular time interval. Run crontab -e to edit your crontab file. Add the following to your crontab file:


*/15 * * * * /bin/bash -c "~/scripts/check_val_jailed.sh"


This calls the bash script every fifteen minutes. Configure this as you please and update the file path above to the file path of your script.


That’s it! Of course, ensure that everything is working as you intend.


Thanks for reading.