Author

Topic: Make yourself an e-mail alert if your VPS bitcoin node is down (Read 713 times)

full member
Activity: 196
Merit: 103
Sometimes "good enough" is enough. You are correct concerning that particular edge case, however it must be assumed users advanced enough to tweak settings of their bitcoin daemon is also able to cover such edge cases in their monitor scripts. The ordinary user just running a node on a vps need not concern himself with all the exotic edge cases.
legendary
Activity: 1344
Merit: 1024
Mine at Jonny's Pool
That code actually attempts a connection to the bitcoin daemon, which if you're just checking to see if the process is running might be a bit of overkill.  Why not just do something simple like:
Code:
#!/bin/bash

if [ $(ps -ef | grep -cim1 [b]itcoind) -eq 0 ]; then
  echo "Bitcoind is not running" | mail -s "Alert Message" [email protected]
fi


Thanks for the input. If you only check to see if the process is running, you only know that the bitcoin core process is running, you do not know whether it accepts connections or not. So the script simply checks whether it is possible to connect to the running daemon with the bitcoin protocol. There's endless variants to such scripts, and you define your use case and run with that. Cheers.
Agreed.  You can come up with countless use cases and build monitoring agents accordingly.  I guess my main concern with your script is precisely because it is attempting a connection.  Some people tune their nodes to set max connections.  If your node has already reached the max connections, your script will falsely report it as being down simply because it cannot connect.

I suppose you could do a combination of things like:
1) Check to see if the process is actually running (my suggested script)
2) If 1 is true, check to see if it will accept incoming connections (your script)
3) If 1 is true, but 2 is false, see if you can retrieve data from the daemon using bitcoin-cli
...

I'm sure we could come up with a bunch of other tests to monitor things Smiley
full member
Activity: 196
Merit: 103
That code actually attempts a connection to the bitcoin daemon, which if you're just checking to see if the process is running might be a bit of overkill.  Why not just do something simple like:
Code:
#!/bin/bash

if [ $(ps -ef | grep -cim1 [b]itcoind) -eq 0 ]; then
  echo "Bitcoind is not running" | mail -s "Alert Message" [email protected]
fi


Thanks for the input. If you only check to see if the process is running, you only know that the bitcoin core process is running, you do not know whether it accepts connections or not. So the script simply checks whether it is possible to connect to the running daemon with the bitcoin protocol. There's endless variants to such scripts, and you define your use case and run with that. Cheers.
legendary
Activity: 1344
Merit: 1024
Mine at Jonny's Pool
That code actually attempts a connection to the bitcoin daemon, which if you're just checking to see if the process is running might be a bit of overkill.  Why not just do something simple like:
Code:
#!/bin/bash

if [ $(ps -ef | grep -cim1 [b]itcoind) -eq 0 ]; then
  echo "Bitcoind is not running" | mail -s "Alert Message" [email protected]
fi
full member
Activity: 196
Merit: 103
It is assumed you run a linux distro, the following example works with debian.

Put in your crontab by doing "crontab -e":
Code:
#Check bitcoind once every 3 hours. If it's not active, then send an e-mail alert.
00 */3 * * * /bin/bash /home/myuser/path/checknode.sh IPADDRESSOFNODE

Remember to restart crontab daemon every time you have changed the crontab:
Code:
su service cron restart


checknode.sh:

Code:
#!/bin/bash

#Checks if the bitcoin node is up and accepts connections.
cd /home/myuser/code/bitnodes
#If the server is up and accepts connections, the response below becomes 0, bitcoind down gives 1.
RES=$(/usr/bin/python protocol.py $1 | /bin/grep refused | /usr/bin/wc -l)

if [ $RES -eq 1 ]; then
        echo "Admin maintenance alert, daemon down!" | mail -s "Alert message" [email protected]
fi


The bitnodes code including protocol.py can be fetched from here.

https://github.com/ayeowch/bitnodes

Not sure about the dependencies, you might experiment if you want to have as few files as possible in the bitnodes directory. Easiest is to get all files. The code is the same that's behind https://getaddr.bitnodes.io

example:
git clone https://github.com/ayeowch/bitnodes.git

Considered you've set up everything correctly, the node monitoring will check your node every 3 hours, and send you a notice on e-mail if it's not possible to connect to it with the bitcoin protocol. For testing purposes you might manually shut down the daemon and set crontab intervall to let's say every minute, to ensure the e-mail is sent when the node is down.

This method is nice, as if you rely on manually checking your node, it might be down for a while before you notice. Since your node is not critical to the network, checking that is is operating with a 3 hour interval might be ok, if you think it should be checked more often, set the interval to what you want.

You might also extend the script to restart the daemon automatically if it's not reachable, instead of logging in and doing it manually when you get the e-mail alert. In that case, you could just send yourself an e-mail saying something like: "Daemon went down, but was automatically restarted."

As bitcoin core is quite stable and does not go down very often, a simple e-mail notification might be sufficient. The assumption is that if you fork out the money to run a VPS node, you want it to be running most of the time.  Grin
Jump to: