-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (53 loc) · 2.02 KB
/
main.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
#!/usr/bin/env python3
import os
import falcon
import json
import logging
import sys
import threading
from datetime import datetime
from inc.audit import RedisAudit
from __init__ import __version__
############################### INIT LOGGIG ##################################
log = logging.getLogger()
log.setLevel(logging.DEBUG)
stdout = logging.StreamHandler(sys.stdout)
stdout.setLevel(logging.DEBUG)
log.addHandler(stdout)
############################### FALCON CLASSES ##################################
class CORS:
def process_request(self, req, resp):
resp.set_header( 'Access-Control-Allow-Origin', '*' )
class RedisExporterPing:
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
class RedisExporterMetrics:
def on_get(self, req, resp):
try :
with open('redis_keys_metrics.prom', 'r') as f:
resp.body = f.read()
resp.status = falcon.HTTP_200
f.close()
except IOError:
resp.status = falcon.HTTP_204
############################### FALCON ROUTING ##################################
api = falcon.API( middleware=[ CORS(), ] )
api.add_route('/_ping', RedisExporterPing())
api.add_route('/metrics', RedisExporterMetrics())
############################### EXECUTE PERIODICALLY ##################################
def get_metrics() :
log.debug("""[%s] Start scraping Redis.""" % ( datetime.now() ) )
REDIS_DB_LIST = os.environ.get('REDIS_DB_LIST', '0')
REDIS_SCRAPE_INTERVAL = os.environ.get('REDIS_SCRAPE_INTERVAL', 900)
# try to convert string into list
try :
redis_dbs = REDIS_DB_LIST.split(",")
except :
redis_dbs = [0, ]
red = RedisAudit()
red.get_metrics(redis_dbs)
log.debug("""[%s] End scraping Redis.""" % ( datetime.now() ) )
threading.Timer(REDIS_SCRAPE_INTERVAL, get_metrics).start()
############################### INIT APP ##################################
log.info("""[%s] ian Redis Exporter started. Version: %s """ % ( datetime.now(), __version__ ) )
threading.Timer( 5, get_metrics).start()