summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/energid/energid.chart.py
blob: 079c32dc8f7112299a2d519960e5124ad2ffbe60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
# Description: Energi Core / Bitcoin netdata python.d module
# Author: Andrey Galkin <andrey@futoin.org> (andvgal)
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This module is designed for energid, but it should work with many other Bitcoin forks
# which support more or less standard JSON-RPC.
#

import json

from bases.FrameworkServices.UrlService import UrlService

update_every = 5

ORDER = [
    'blockindex',
    'difficulty',
    'mempool',
    'secmem',
    'network',
    'timeoffset',
    'utxo',
    'xfers',
]

CHARTS = {
    'blockindex': {
        'options': [None, 'Blockchain Index', 'count', 'blockchain', 'energi.blockindex', 'area'],
        'lines': [
            ['blockchain_blocks', 'blocks', 'absolute'],
            ['blockchain_headers', 'headers', 'absolute'],
        ]
    },
    'difficulty': {
        'options': [None, 'Blockchain Difficulty', 'difficulty', 'blockchain', 'energi.difficulty', 'line'],
        'lines': [
            ['blockchain_difficulty', 'Diff', 'absolute'],
        ],
    },
    'mempool': {
        'options': [None, 'MemPool', 'MiB', 'memory', 'energid.mempool', 'area'],
        'lines': [
            ['mempool_max', 'Max', 'absolute', None, 1024 * 1024],
            ['mempool_current', 'Usage', 'absolute', None, 1024 * 1024],
            ['mempool_txsize', 'TX Size', 'absolute', None, 1024 * 1024],
        ],
    },
    'secmem': {
        'options': [None, 'Secure Memory', 'KiB', 'memory', 'energid.secmem', 'area'],
        'lines': [
            ['secmem_total', 'Total', 'absolute', None, 1024],
            ['secmem_locked', 'Locked', 'absolute', None, 1024],
            ['secmem_used', 'Used', 'absolute', None, 1024],
        ],
    },
    'network': {
        'options': [None, 'Network', 'count', 'network', 'energid.network', 'line'],
        'lines': [
            ['network_connections', 'Connections', 'absolute'],
        ],
    },
    'timeoffset': {
        'options': [None, 'Network', 'seconds', 'network', 'energid.timeoffset', 'line'],
        'lines': [
            ['network_timeoffset', 'offseet', 'absolute'],
        ],
    },
    'utxo': {
        'options': [None, 'UTXO', 'count', 'UTXO', 'energid.utxo', 'line'],
        'lines': [
            ['utxo_count', 'UTXO', 'absolute'],
        ],
    },
    'xfers': {
        'options': [None, 'UTXO', 'count', 'UTXO', 'energid.xfers', 'line'],
        'lines': [
            ['utxo_xfers', 'Xfers', 'absolute'],
        ],
    },
}

METHODS = {
    'getblockchaininfo': lambda r: {
        'blockchain_blocks': r['blocks'],
        'blockchain_headers': r['headers'],
        'blockchain_difficulty': r['difficulty'],
    },
    'getmempoolinfo': lambda r: {
        'mempool_txcount': r['size'],
        'mempool_txsize': r['bytes'],
        'mempool_current': r['usage'],
        'mempool_max': r['maxmempool'],
    },
    'getmemoryinfo': lambda r: dict([
        ('secmem_' + k, v) for (k, v) in r['locked'].items()
    ]),
    'getnetworkinfo': lambda r: {
        'network_timeoffset': r['timeoffset'],
        'network_connections': r['connections'],
    },
    'gettxoutsetinfo': lambda r: {
        'utxo_count': r['txouts'],
        'utxo_xfers': r['transactions'],
        'utxo_size': r['disk_size'],
        'utxo_amount': r['total_amount'],
    },
}

JSON_RPC_VERSION = '1.1'


class Service(UrlService):
    def __init__(self, configuration=None, name=None):
        UrlService.__init__(self, configuration=configuration, name=name)
        self.order = ORDER
        self.definitions = CHARTS
        self.host = self.configuration.get('host', '127.0.0.1')
        self.port = self.configuration.get('port', 9796)
        self.url = '{scheme}://{host}:{port}'.format(
            scheme=self.configuration.get('scheme', 'http'),
            host=self.host,
            port=self.port,
        )
        self.method = 'POST'
        self.header = {
            'Content-Type': 'application/json',
        }

    def _get_data(self):
        #
        # Bitcoin family speak JSON-RPC version 1.0 for maximum compatibility,
        # but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
        # unspecified (HTTP errors and contents of 'error').
        #
        # 1.0 spec: https://www.jsonrpc.org/specification_v1
        # 2.0 spec: https://www.jsonrpc.org/specification
        #
        # The call documentation: https://github.com/energicryptocurrency/core-api-documentation
        #
        batch = []

        for i, method in enumerate(METHODS):
            batch.append({
                'version': JSON_RPC_VERSION,
                'id': i,
                'method': method,
                'params': [],
            })

        result = self._get_raw_data(body=json.dumps(batch))

        if not result:
            return None

        result = json.loads(result.decode('utf-8'))
        data = dict()

        for i, (_, handler) in enumerate(METHODS.items()):
            r = result[i]
            data.update(handler(r['result']))

        return data