-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
executable file
·130 lines (109 loc) · 5.2 KB
/
crypto.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
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
#!/usr/bin/env python3
import time
import sys
import os
import argparse
import logging
import requests
import json
import sys
from prometheus_client import start_http_server, Summary
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# VARIABLES
LPORT = 9510
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
SRV = "api.coinmarketcap.com"
stats = {}
# ARGUMENTS
parser = argparse.ArgumentParser(description='Cryptocurrency Exporter', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--debug', action='store_true', default=False, help='Enable debug logging')
parser.add_argument('-c', '--currency', default='USD', help='Set currency to convert')
parser.add_argument('-t', '--timer', default=5, help='Scrape timing in seconds')
parser.add_argument('-T', '--test', action='store_true', default=False, help='Testing function')
# Try to connect
def exporter():
db = {}
### GET CRYPTO
try:
logging.debug("Downloading data: %s" % SRV)
prepare = requests.get('https://' + SRV + '/v1/ticker/?convert=' + args.currency, verify=False)
if prepare.status_code in stats:
stats.update({prepare.status_code: stats.get(prepare.status_code) + 1})
else:
stats.update({prepare.status_code: 1})
result = json.loads(prepare.text)
db.update({"crypto": result})
except:
logging.error("Unable to download data from {}, because server returned status: {}".format(SRV, prepare.status_code))
db.update({"requests": stats})
###
return db
class CustomCollector(object):
def collect(self):
# Requests data status
try:
exporter_requests = CounterMetricFamily('exporter_requests', 'Exporter request status for getting data', labels=['server', 'status'])
for key, val in get_data["requests"].items():
exporter_requests.add_metric([SRV, str(key)], float(val))
yield exporter_requests
except:
logging.error("No metrics (exporter stats)")
# Cryptocurrency metrics
try:
crypto_converted = CounterMetricFamily('crypto_converted', 'Cryptocurrency to ' + args.currency, labels=['name', 'id', 'symbol', 'btc', 'currency', 'rank'])
crypto_percent = CounterMetricFamily('crypto_percent', 'Cryptocurrency percent change by time', labels=['name', 'id', 'symbol', 'time', 'rank'])
crypto_last_update = CounterMetricFamily('crypto_last_update', 'Cryptocurrency last update', labels=['name', 'id', 'symbol', 'rank'])
for item in get_data["crypto"]:
try:
crypto_converted.add_metric([item['name'], item['id'], item['symbol'], item['price_btc'], args.currency, item['rank']], float(item['price_' + args.currency.lower()]))
except:
logging.debug("No metrics (converted)")
try:
crypto_percent.add_metric([item['name'], item['id'], item['symbol'], "1h", item['rank']], float(item['percent_change_1h']))
crypto_percent.add_metric([item['name'], item['id'], item['symbol'], "24h", item['rank']], float(item['percent_change_24h']))
crypto_percent.add_metric([item['name'], item['id'], item['symbol'], "7d", item['rank']], float(item['percent_change_7d']))
except:
logging.debug("No metrics (percent)")
try:
crypto_last_update.add_metric([item['name'], item['id'], item['symbol'], item['rank']], float(item['last_updated']))
except:
logging.debug("No metrics (last_update)")
yield crypto_converted
yield crypto_percent
yield crypto_last_update
except:
logging.error("No metrics (cryptocurrencies)")
if __name__ == '__main__':
args = parser.parse_args()
## LOGGING
if args.debug:
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
else:
logging.basicConfig(level=logging.INFO, format=FORMAT)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.ERROR)
logging.info('Server starting...')
logging.debug('Debug mode enabled')
# Start up the server to expose the metrics.
start_http_server(LPORT)
logging.info('Server listening on port: %s' % LPORT)
# Generate some requests.
get_data = exporter()
REGISTRY.register(CustomCollector())
try:
while True:
# Sleep to next scrape from server
time.sleep(args.timer)
get_data = exporter()
if args.test:
logging.info('Testing instance ends after 2 minutes.')
logging.info('Ready to scrape...')
time.sleep(120)
sys.exit(0)
except SystemExit:
logging.info('Server has been stopped!')
except KeyboardInterrupt:
logging.info('Server has been stopped!')
except:
logging.fatal("Server has been unexpectedly stopped!")