Jump to content

Crypto Stats using Grafana, InfluxDB, Denarius Daemon


buzzkillb
 Share

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

  • 4 months later...

To send data into influxdb using multiple variables from a bash script.

curl -i -XPOST 'http://localhost:8086/write?db=stat' --data-binary 'crawler,blockchain=denarius address="'"$ipaddress"'",svcs="'"$svcs"'",version="'"$version"'",version2="'"$version2"'",percent_7d="'"$percent_7d"'",country_code="'"$country_code"'",latitude="'"$latitude"'",longitude="'"$longitude"'"'

Which through some use of bash script grabbing a local dns seeder crawler, can now post to a world map panel.

image.png.babf2dc357b30537ba0a415c80be62a7.png

how the query panel looks, I was playing with the options in the SELECT. GROUP BY is very important to select your tag.

image.thumb.png.0a2cc6a3175fcb774faa92b526990ade.png

And the Map Data Options

image.thumb.png.43b04435c62f29cdaed50fb1ed087778.png

  • Like 1
Link to comment
Share on other sites

Now that I have the site on Docker, adding some sample docker run commands.

Grafana

docker run -d \
-p 3000:3000 \
--name=grafana \
--user $ID \
--volume "$PWD/data:/var/lib/grafana" \
-e "GF_INSTALL_PLUGINS=grafana-worldmap-panel" \
-e "GF_USERS_VIEWERS_CAN_EDIT=false" \
-e "GF_USERS_EDITORS_CAN_ADMIN=false" \
-e "GF_USERS_ALLOW_SIGN_UP=false" \
-e "GF_USERS_ALLOW_ORG_CREATE=false" \
-e "GF_AUTH_DISABLE_LOGIN_FORM=false" \
-e "GF_AUTH_ANONYMOUS_ENABLED=true" \
-e "GF_AUTH_ANONYMOUS_ORG_ROLE=Viewer" \
-e "GF_ANALYTICS_GOOGLE_ANALYTICS_UA_ID=UA-157676508-1" \
-e "GF_SERVER_DOMAIN=denarius.pro" \
grafana/grafana

I used this for setting up. https://grafana.com/docs/grafana/latest/installation/configure-docker/

This below line to put the grafana shared volume in /home/username/data and have the container start with current user id.

mkdir data # creates a folder for your data
ID=$(id -u) # saves your user id in the ID variable

To pick and choose environment variables, find a stock grafana.ini and for an example go to [users] and then you will find the USERS options in the docker run commands

InfluxDB

--name="influxdb" \
-p 8086:8086 \
-v /home/USERNAME/influxdb:/var/lib/influxdb \
influxdb -config /etc/influxdb/influxdb.conf

To manipulate influxdb inside the container hop into the container using

docker exec -it influxdb /bin/bash

then go into influx cli

influx

and do your typical commands like

show databases

Practice making some edits on both and doing

docker stop grafana
docker stop influxdb
docker start grafana
docker start influxdb
docker stop grafana
docker rm grafana
rerun the full grafana run command

And see persistence will stick.

My assumption is upgrading will just be

docker stop grafana
docker rm grafana
docker pull grafana/grafana
then the full docker run command

Next to get a docker compose together putting it all together in one command.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...