Bitcoin Forum
July 06, 2024, 04:16:29 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Make yourself an e-mail alert if your VPS bitcoin node is down  (Read 691 times)
Cryptowatch.com (OP)
Full Member
***
Offline Offline

Activity: 196
Merit: 103


View Profile WWW
April 27, 2015, 01:04:11 AM
 #1

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" yourmail@yourdomain.com
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
jonnybravo0311
Legendary
*
Offline Offline

Activity: 1344
Merit: 1023


Mine at Jonny's Pool


View Profile WWW
April 27, 2015, 10:06:20 PM
 #2

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" address@mail.com
fi

Jonny's Pool - Mine with us and help us grow!  Support a pool that supports Bitcoin, not a hardware manufacturer's pockets!  No SPV cheats.  No empty blocks.
Cryptowatch.com (OP)
Full Member
***
Offline Offline

Activity: 196
Merit: 103


View Profile WWW
April 27, 2015, 10:16:37 PM
 #3

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" address@mail.com
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.
jonnybravo0311
Legendary
*
Offline Offline

Activity: 1344
Merit: 1023


Mine at Jonny's Pool


View Profile WWW
April 27, 2015, 10:31:02 PM
 #4

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" address@mail.com
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

Jonny's Pool - Mine with us and help us grow!  Support a pool that supports Bitcoin, not a hardware manufacturer's pockets!  No SPV cheats.  No empty blocks.
Cryptowatch.com (OP)
Full Member
***
Offline Offline

Activity: 196
Merit: 103


View Profile WWW
April 27, 2015, 10:39:04 PM
 #5

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!