forked from sapcc/redfish-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (88 loc) · 3.06 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
from handler import metricsHandler
from handler import welcomePage
from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
from socketserver import ThreadingMixIn
import falcon
import argparse
import yaml
import logging
import os
import warnings
class _SilentHandler(WSGIRequestHandler):
"""WSGI handler that does not log requests."""
def log_message(self, format, *args):
"""Log nothing."""
pass
class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
"""Thread per request HTTP server."""
pass
def falcon_app():
port = int(os.getenv("LISTEN_PORT", config.get("listen_port", 9200)))
addr = "0.0.0.0"
logging.info("Starting Redfish Prometheus Server on Port %s", port)
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
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(f"Could not open logfile {filename}: {e}")
exit(1)
fh.setFormatter(formatter)
logger.addHandler(fh)
if __name__ == "__main__":
# command line options
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
)
args = parser.parse_args()
warnings.filterwarnings("ignore")
enable_logging(args.logging, args.debug)
# get the config
if args.config:
try:
with open(args.config, "r") as config_file:
config = yaml.load(config_file.read(), Loader=yaml.FullLoader)
except FileNotFoundError as err:
print(f"Config File not found: {err}")
exit(1)
falcon_app()