-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (68 loc) · 2.45 KB
/
app.py
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
# -*- coding: utf-8 -*-
import json
import logging
import os
import time
import requests
import yaml
from prometheus_client import Gauge,start_http_server
from concurrent.futures import ThreadPoolExecutor
FORMAT = '%(asctime)s - %(levelname)s - %(funcName)s - %(threadName)s - %(message)s'
DATE_FORMAT = "%Y/%m/%d %H:%M:%S %p"
logging.basicConfig(format=FORMAT,level=logging.INFO)
logger = logging.getLogger(__name__)
def get_config(file):
try:
with open(file,'r') as f:
config=yaml.safe_load(f)
logger.info("config is %s"%(config))
return(config)
except FileNotFoundError:
logger.warning('config file {} do not exist'%(file))
exit(1)
def getHeight(item):
global heightGauge
headers = {"Content-Type": "application/json"}
data = {"jsonrpc":"2.0","id":"id","method":"eth_blockNumber"}
try:
response = requests.post(item['url'], headers=headers, json=data, timeout=5)
if response.status_code != 200 :
logger.warning("status_code not 200")
else:
height=int(json.loads(response.content).get('result'),16)
heightGauge.labels(node = item['node'],url=item['url']).set(height)
logger.info("%s current height is %s"%(item['node'],height))
except Exception as e:
logging.error('Error in http request: %s', e)
def getGas(item):
global gasGauge
headers = {"Content-Type": "application/json"}
data = {"jsonrpc":"2.0","id":"id","method":"eth_gasPrice"}
try:
response = requests.post(item['url'], headers=headers, json=data, timeout=5)
if response.status_code != 200 :
logger.warning("status_code not 200")
else:
gas=int(json.loads(response.content).get('result'),16)
gasGauge.labels(node = item['node'],url=item['url']).set(gas)
logger.info("%s current gas is %s"%(item['node'],gas))
except Exception as e:
logging.error('Error in http request: %s', e)
def conCurrent(urls:list()):
# create thread pool
with ThreadPoolExecutor(max_workers=5) as executor:
# insert your func into thread pool
executor.map(getHeight, urls)
executor.map(getGas, urls)
if __name__ == '__main__':
absdir=os.path.dirname(os.path.realpath(__file__))
config_file = absdir + os.sep + "config/config.yaml"
urls=get_config(config_file)
port = 8000
heightGauge = Gauge ('rpc_height', 'height of rpc',['node','url'])
gasGauge = Gauge ('gas_price', 'gas price of rpc',['node','url'])
conCurrent(urls)
start_http_server(port)
while True:
time.sleep(30)
conCurrent(urls)