-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
135 lines (107 loc) · 4.84 KB
/
Copy pathscript.py
File metadata and controls
135 lines (107 loc) · 4.84 KB
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
import time, os
from prometheus_client.core import GaugeMetricFamily, REGISTRY
from prometheus_client import start_http_server
from binance.client import Client
from binance.exceptions import BinanceAPIException
import configparser
import requests
import json
# reads the configuration from settings file
config = configparser.ConfigParser()
config_file = os.path.join(os.path.dirname(__file__), 'config.ini')
try:
config.read(config_file)
except:
print('Error! Please make sure that "config.ini" file exists and properly set.')
exit(1)
API_KEY = config['api']['API_KEY']
API_SECRET = config['api']['API_SECRET']
EXPORTER_TICKER = config['exporter']['ticker']
EXPORTER_LENDING = config['exporter']['lending']
EXPORTER_CUSTOMIZED_FIXED = config['exporter']['customized_fixed']
client = Client(API_KEY, API_SECRET)
class BinanceAPICollector(object):
def collect(self):
if EXPORTER_TICKER == 'yes':
tickers = ['BTCUSDT', 'ETHUSDT']
ticker_metrics = GaugeMetricFamily(
'binance_ticker_price',
'Binance API ticker price',
labels=['symbol']
)
for t in tickers:
try:
ticker = client.get_symbol_ticker(symbol=t)
except BinanceAPIException as e:
print(e)
price = ticker.get('price', None)
ticker_metrics.add_metric([t], price)
yield ticker_metrics
if EXPORTER_LENDING == 'yes':
try:
lendings = client.get_lending_product_list(timestamp=time.time())
except BinanceAPIException as e:
print(e)
# The metrics we want to export.
statuses = ['avgAnnualInterestRate', 'purchasedAmount', 'upLimit']
lending_metrics = {}
# use "for loop" to create gauge list
for s in statuses:
lending_metrics[s] = GaugeMetricFamily(
'binance_lending_{0}'.format(s),
'Binance API lendings product ' + s + ' data',
labels=["asset"])
# just get the value which only in the statuses list
for product in lendings:
asset_name = product.get('asset')
for key in product:
if key in statuses:
lending_metrics[key].add_metric([asset_name], product.get(key, 0))
for m in lending_metrics.values():
yield m
if EXPORTER_CUSTOMIZED_FIXED == 'yes':
try:
projects = client.get_fixed_activity_project_list(
type='CUSTOMIZED_FIXED',
status='ALL',
timestamp=time.time()
)
except BinanceAPIException as e:
print(e)
# print(projects)
customized_fixed_purchased_metrics = GaugeMetricFamily(
'binance_customized_fixed_purchased',
'Binance API Customized Fixed Project purchased data',
labels=['projectId', 'duration', 'asset']
)
customized_fixed_uplimit_metrics = GaugeMetricFamily(
'binance_customized_fixed_uplimit',
'Binance API Customized Fixed Project uplimit data',
labels=['projectId', 'duration', 'asset']
)
customized_fixed_rate_metrics = GaugeMetricFamily(
'binance_customized_fixed_rate',
'Binance API Customized Fixed Project interest rate data',
labels=['projectId', 'duration', 'asset']
)
for project in projects:
asset = project.get('asset', None)
duration = project.get('duration', None)
lotSize = project.get('lotSize', None)
lotsPurchased = project.get('lotsPurchased', None)
lotsUpLimit = project.get('lotsUpLimit', None)
projectId = project.get('projectId', None)
interestRate = project.get('interestRate', None)
purchased = int(lotsPurchased) * float(lotSize)
uplimit = int(lotsUpLimit) * float(lotSize)
customized_fixed_purchased_metrics.add_metric([projectId, str(duration), asset], purchased)
customized_fixed_uplimit_metrics.add_metric([projectId, str(duration), asset], uplimit)
customized_fixed_rate_metrics.add_metric([projectId, str(duration), asset], interestRate)
yield customized_fixed_purchased_metrics
yield customized_fixed_uplimit_metrics
yield customized_fixed_rate_metrics
if __name__ == "__main__":
REGISTRY.register(BinanceAPICollector())
start_http_server(5000)
while True:
time.sleep(10)