Jump to content
Sign in to follow this  
buzzkillb

Crypto Stats using Grafana, InfluxDB, Denarius Daemon

Recommended Posts

How to pull stats from a coin daemon and throw them into influxdb, and then use Grafana to create a pretty graph.

Imgur Image

Setup a VM, VPS, raspberry pi, whatever to play on this. Anything breaks its pretty easy to delete stuff. Using ubuntu/debian for the example.

The idea is relatively simple. Ask the daemon for information, throw that into influxdb and then have Grafana pull from that database.

Install denarius daemon
Install python denarius rpc
https://github.com/buzzkillb/python-denariusrpc
Install grafana
https://grafana.com/docs/grafana/latest/installation/debian/
Install influxdb
https://docs.influxdata.com/influxdb/v1.7/introduction/installation/

One this is all done we need a test to see if python is working with the daemon. Switch in rpc_user and rpc_password that's inside of denarius.conf

test.py

from denariusrpc.authproxy import AuthServiceProxy, JSONRPCException

# rpc_user and rpc_password are set in the denarius.conf file
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:32369"%(rpc_user, rpc_password))
best_block_hash = rpc_connection.getbestblockhash()
print(rpc_connection.getblock(best_block_hash))

# batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips:
commands = [ [ "getblockhash", height] for height in range(100) ]
block_hashes = rpc_connection.batch_(commands)
blocks = rpc_connection.batch_([ [ "getblock", h ] for h in block_hashes ])
block_times = [ block["time"] for block in blocks ]
print(block_times)

test run

python test.py

Lets double check another test run to get current Denarius price from Coinvex.

testcoinvex.py

from denariusrpc.authproxy import AuthServiceProxy, JSONRPCException

import requests, urllib, json

#southexchange
#coinvex
coinvex_url = requests.get('https://coinvex.org/api/v1/public/getlastmarketdata')
coinvex_data = json.loads(coinvex_url.text)
coinvex_price = coinvex_data['result']['coins']
for r in coinvex_price:
    if r["name"] == "Denarius":
        coinvex_last = float(r["price"])
        print format(coinvex_last, '0.8f')

test run

python testcoinvex.py

Works? The next post will show how to take this data and put into influxdb.

  • Like 1

Join Denarius Discord - https://discord.gg/JQEmXwb

Share this post


Link to post
Share on other sites

Create a new database ininfluxdb. Run influx to get into the command prompt.

influx

create database

CREATE DATABASE mydb

use database

USE mydb

from a second command line, lets make a sample.py

sample.py

from denariusrpc.authproxy import AuthServiceProxy, JSONRPCException

import time
import sys
import datetime
import urllib
import json
from influxdb import InfluxDBClient

# rpc_user and rpc_password are set in the denarius.conf file
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:32369"%("rpc_user", "rpc_password"))

#test
blocktest = rpc_connection.getblockcount()
print(blocktest)

# Configure InfluxDB connection variables
host = "127.0.0.1" # My Ubuntu NUC
port = 8086 # default port
user = "admin" # the user/password created for the pi, with write access
password = "admin" 
dbname = "mydb" # the database we created earlier
interval = 60 # Sample period in seconds

# Create the InfluxDB client object
client = InfluxDBClient(host, port, user, password, dbname)

# think of measurement as a SQL table, it's not...but...
measurement = "measurement"
# location will be used as a grouping tag later
blockchain = "denarius"

blockcount = rpc_connection.getblockcount()
block = rpc_connection.getblockbynumber(blockcount)
grafanatime = block['time'] * 1000000000
hash = block['hash']
size = block['size']
height = block['height']
version = block['version']
merkleroot = block['merkleroot']
mint = int(block['mint'])
timed = block['time']
nonce = block['nonce']
bits = block['bits']
difficulty = float(block['difficulty'])
blocktrust = block['blocktrust']
chaintrust = block['chaintrust']
chainwork = block['chainwork']
previousblockhash = block['previousblockhash']
#nextblockhash = block['nextblockhash']
flags = block['flags']
proofhash = block['proofhash']
entropybit = block['entropybit']
modifier = block['modifier']
modifierchecksum = block['modifierchecksum']

data = [
  {
      "measurement": measurement,
          "tags": {
              "blockchain": blockchain,
          },
          "time": grafanatime,
          "fields": {
             #"block" : i,
              "hash" : hash,
              "size" : size,
	      "height" : height,
	      "version" : version,
	      "merkleroot" : merkleroot,
              "mint" : mint,
	      "time" : timed,
	      "nonce" : nonce,
	      "bits" : bits,
	      "difficulty" : difficulty,
	      "blocktrust" : blocktrust,
	      "chaintrust" : chaintrust,
	      "chainwork" : chainwork,
#	      "nextblockhash" : nextblockhash,
	      "flags" : flags,
	      "proofhash" : proofhash,
	      "entropybit" : entropybit,
	      "modifier" : modifier,
	      "modifierchecksum" : modifierchecksum
              }
          }
        ]
    # Send the JSON data to InfluxDB
print(difficulty)
client.write_points(data)

If you run python sample.py you should see data appear in your influxdb. Run below a few times to throw data in.

python sample.py

In the other command line running influx already, make sure you are using the correct database and lets see if the data is inside.

use mydb
SELECT * FROM /.*/ LIMIT 10

image.thumb.png.dec0528fa729f44d8857e2f2c2c04aee.png

You should see output, now we need to connect influxdb to grafana.

  • Like 1

Join Denarius Discord - https://discord.gg/JQEmXwb

Share this post


Link to post
Share on other sites

Login to your localhost grafana.

http://192.168.1.337:3000/

on the left click the gear -> data sources

add data source

search for influxdb

use settings like this

image.thumb.png.5e8a568b30d197615bb08853646b1b27.png

save and test

Go back to the grafana homepage and click + and add query

image.png.ca567d3c4f452d813fb4e88db2f3ac63.png

Your query would look something like this. Remember to select your mydb database, my screenshot is using another name for the DB.

image.thumb.png.a22aca0bdac08192ba864b4696a9b102.png

Visualization

image.thumb.png.fb69e00418324f4b5624b28e997a12ee.png

General

image.thumb.png.d7c6a8623fe48e540cb73b380215315f.png

Click Save at the top right

image.thumb.png.c9723486538abbf47ce1907d6ce807db.png

Should see something like this on your homepage.

image.png.3476c29496c8c97c923e9dd06c993714.png

  • Like 1

Join Denarius Discord - https://discord.gg/JQEmXwb

Share this post


Link to post
Share on other sites

Lets make sure to run that python file over and over and over on every new block.

Go into denarius.conf and add this line. I have my files in a influx directory.

blocknotify=/home/USERNAME/influx/block.sh

Lets create block.sh in the influx directory and throw our sample.py in there

#!/bin/bash
python /home/USERNAME/influx/sample.py

make block.sh executable

chmod +x block.sh

stop and restart daemon to pick this change up

denariusd stop
denariusd

If it all went right your grafana page should start updating the block count.

  • Like 1

Join Denarius Discord - https://discord.gg/JQEmXwb

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...