-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
126 lines (100 loc) · 3.42 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
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
"""
Redfish Prometheus Exporter
"""
import argparse
import logging
import os
import warnings
import sys
from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
from socketserver import ThreadingMixIn
import yaml
import falcon
from handler import MetricsHandler
from handler import WelcomePage
class _SilentHandler(WSGIRequestHandler):
"""WSGI handler that does not log requests."""
def log_message(self, format, *args): # pylint: disable=redefined-builtin
"""Log nothing."""
class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
"""Thread per request HTTP server."""
def falcon_app(config):
"""
Start the Falcon API
"""
port = int(os.getenv("LISTEN_PORT", config.get("listen_port", 9200)))
addr = "0.0.0.0"
logging.info("Starting Redfish Prometheus Server ...")
api = falcon.API()
api.add_route("/health", MetricsHandler(config, metrics_type='health'))
api.add_route("/firmware", MetricsHandler(config, metrics_type='firmware'))
api.add_route("/performance", MetricsHandler(config, metrics_type='performance'))
api.add_route("/", WelcomePage())
with make_server(addr, port, api, ThreadingWSGIServer, handler_class=_SilentHandler) as httpd:
httpd.daemon = True # pylint: disable=attribute-defined-outside-init
logging.info("Listening on Port %s", port)
try:
httpd.serve_forever()
except (KeyboardInterrupt, SystemExit):
logging.info("Stopping Redfish Prometheus Server")
def enable_logging(filename, debug):
"""enable logging"""
logger = logging.getLogger()
formatter = logging.Formatter(
'%(asctime)-15s %(process)d %(filename)24s:%(lineno)-3d %(levelname)-7s %(message)s'
)
if debug:
logger.setLevel("DEBUG")
else:
logger.setLevel("INFO")
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
if filename:
try:
fh = logging.FileHandler(filename, mode='w')
except FileNotFoundError as e:
logging.error("Could not open logfile %s: %s", filename, e)
sys.exit(1)
fh.setFormatter(formatter)
logger.addHandler(fh)
def get_args():
"""
Get the command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Specify config yaml file",
metavar="FILE",
required=False,
default="config.yml"
)
parser.add_argument(
"-l",
"--logging",
help="Log all messages to a file",
metavar="FILE",
required=False
)
parser.add_argument(
"-d", "--debug",
help="Debugging mode",
action="store_true",
required=False
)
return parser.parse_args()
if __name__ == "__main__":
call_args = get_args()
warnings.filterwarnings("ignore")
enable_logging(call_args.logging, call_args.debug)
# get the config
if call_args.config:
try:
with open(call_args.config, "r", encoding="utf8") as config_file:
configuration = yaml.load(config_file.read(), Loader=yaml.FullLoader)
except FileNotFoundError as err:
print(f"Config File not found: {err}")
sys.exit(1)
falcon_app(configuration)