Hive Engine Witness Monitoring Script

in #hiveengine3 years ago

Hive Engine Witness Monitor Script

monitoring.jpg

Introduction

Over the past month, I've been playing around a TON with my Hive Engine witness, I've probably deployed over 100 instances in testing, and unfortunately I've had a handful of missed blocks. Once you hit 3, your witness is disabled.

Fortunately, I spend nearly every waking moment of my life attached at the hip to my computer, so I did notice that I was down fairly quickly each time. Yet, I had no alert, nothing to tell me of trouble other than my own proactive review.

So, I got to work to hack together a script to let me know if something goes wrong. I have used the word 'hack' appropriately, because I am admittedly no JS dev so there may be HUGE gaps in my code, lol. That said, please share your thoughts, any updates, and I'd be moved to tears to see a pull request!

Setup

In short, while I'm usually at my desk, I am always near my phone so I wanted to be alerted there. Now, in my 'real' projects, I'll bust out some Twilio and send a text, but that's a bit much here. My workaround, uh hack, is to simply have @ginabot send me a message on Discord.

First of all, sign up for @ginabot

If you already have @ginabot setup, the key that took me a minute to find, is to send this message in Discord ..set

@ginabot will reply with a URL that allows you to configure your settings. The key thing to set is for Transfers:

gina_transfer.png

Check the 'others' box and you'll get a notification when any account sends you a transfer.

Once that's setup, we just need to run the script. I use supervisor to manage all of my nodejs scripts. I apologize for glossing over that here, I feel like that's well known and documented, but let me know if you'd like me to bang out a post about that.

Basically, the script makes an API call to a Hive Engine witness, and you can hit your own witness if you'd like!

We're just calling a list of active witnesses, then looping it to see if we're in there. If not, we send a small transfer to our account, which will appear as a Discord notification on your phone.

Hive Engine Witness Votes

So, I am genuinely humbled by the trust of the community in electing me into the top 10 Hive Engine witnesses! I've been even more thankful to watch my vote count slowly grow each day.

The honestly awesome part is that all of the other witnesses have seen their votes growing too, but that has meant that my rank has been continually dropping, moving from 6th, to 7th, and now hanging in at 8th.

All of this is to say, I would deeply appreciate your vote! I have several other projects that I'm working on and I've been anxious to release, but of course, there's always more work to do!

As always, please let me know if you need any help with this script or frankly anything at all! You can reach me directly on Discord: blervin#9759

The Script

I did update my git repo so you can just pull the script directly from there if you'd like.

const rp = require('request-promise');
var hive = require('@hiveio/hive-js');
var fs = require('fs');

// let heUrl = "http://my_local_he_witness_ip_address:5000/contracts";
let heUrl = "https://api.hive-engine.com/rpc/contracts";

var fromAccount = 'fromAccount';
var myAccount = 'myAccount';

var config = JSON.parse(fs.readFileSync("/var/nodejs/config_" + fromAccount + ".json"));
const activeKey = config.active_key;
const loopSeconds = 30;

var requestLoop = setInterval(function(){
    rp({
        url : heUrl,
        method : 'POST',
        body: {"jsonrpc": "2.0","method": "find","params": {"contract": "witnesses","table": "witnesses","query": {}},"id": 1},
        json : true
    }).then(function (response){
        var enabled = false;
        var results = response.result;
        var dateNow = new Date(Date.now());
        dateNow = dateNow.toString().split('+')[0];

        for(var results_i = 0; results_i < results.length; results_i++) {
            var result = results[results_i];
            if(result.account == myAccount){
                enabled = result.enabled;
            }
        }
        if(enabled == false){
            console.log('witness is not enabled');
            transfer(activeKey, fromAccount, myAccount, '0.007 HIVE', 'witness is not enabled');
        } else {
            console.log('witness enabled: ' + dateNow);
        }
    });
}, loopSeconds * 1000);

async function transfer(activeKey, from, to, amount, memo) {
    hive.broadcast.transfer(activeKey, from, to, amount, memo, function (err, response) {
        if (err) {
            console.log('Error sending transfer to @' + to + ' for: ' + amount + ', Error: ' + err);
            process.exit();
        } else {
            var trxId = response.id;
            console.log('Transfer to @' + to + ': ' +  amount + '\n' + ' at trx: ' + trxId);
        }
    });
}
Sort:  

Nice.... will keep for future ideas on my witness monitoring script posted at @atexoras.

Cheers

Awesome, I hope it's helpful!

Yes it will, I am planning on integrating some JS with my script since you need node anyway to run a witness node.

Nice!!! Thanks for all you do : )

Thanks, I appreciate the support!

Congratulations @blervin! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You distributed more than 49000 upvotes.
Your next target is to reach 50000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Feedback from the March 1st Hive Power Up Day

First update. Please consider using the FRIDAY Discord notification bot instead!

Just jump into their Discord, go to the #commands channel, and type .register

And thanks to @deathwing and @rishi556 for building this new notification bot!!!

Second update. The bot will keep sending you transfers forever, lol. I'm working on an update to stop the madness, but I'm guessing anyone using this already threw up a few extra lines of code. Coming shortly...