-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
228 lines (176 loc) · 6.22 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Inventory Updater is a tool that retrieves the inventory of a server
using Redfish and updates it in Netbox. The tool can be run in two modes:
as a daemon that periodically checks the inventory of the servers and updates it in Netbox,
or as an API that listens for requests to check the inventory of a server.
"""
import argparse
import logging
import os
import warnings
import time
import gc # Garbage collection module
import sys
from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
from socketserver import ThreadingMixIn
import yaml
import falcon
from handler import WelcomePage, InventoryCollector, HandlerException
from netbox import NetboxConnection, NetboxConnectionException
def get_args():
"""
Get the command line options
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--config",
help="Specify config yaml file",
metavar="FILE",
required=False,
default="config.yaml"
)
parser.add_argument(
"-l", "--logging",
help="Log all messages to a file",
metavar="FILE",
required=False,
default="./logfile.txt"
)
parser.add_argument(
"-s",
"--servers",
help="Use a file with a list of servers instead of pulling it from Netbox.",
metavar="FILE",
required=False
)
parser.add_argument(
"-a",
"--api",
help="Start in API mode and listen for requests to check the inventory of a server.",
action="store_true",
required=False
)
parser.add_argument(
"-d",
"--debug",
help="Debugging mode",
action="store_true",
required=False
)
arguments = parser.parse_args()
return arguments
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."""
daemon_threads = True
def falcon_app(config, connection):
"""
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 on Port %s", port)
api = falcon.API()
api.add_route("/inventory", InventoryCollector(config, connection))
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
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)20s:%(lineno)-4d %(levelname)-7s %(message)s'
)
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
if filename:
try:
fh = logging.FileHandler(filename, mode='w')
except FileNotFoundError as err:
logging.error("Could not open logfile %s: %s", filename, err)
sys.exit(1)
fh.setFormatter(formatter)
logger.addHandler(fh)
def get_config(filename):
"""
Read the config
"""
try:
with open(filename, 'r', encoding="utf8") as config_file:
file_config = yaml.load(config_file.read(), Loader=yaml.FullLoader)
except FileNotFoundError as err:
logging.error("Config File not found: %s", err)
sys.exit(1)
return file_config
def get_serverlist(config, connection):
"""
Get the serverlist from the file or from Netbox
"""
serverlist = []
if config.get('servers'):
logging.info("==> Retrieving server list from file %s", config['servers'])
try:
with open(config['servers'], 'rt', encoding="utf8") as f:
serverlist = f.readlines()
except FileNotFoundError as err:
logging.error("Serverlist File not found: %s", err)
sys.exit(1)
else:
logging.info("==> Retrieving server list from %s", connection.netbox_url)
servers = connection.get_devices()
for server in servers:
serverlist.append(f"{server['name']}.cc.{server['site']['slug'][:7]}.cloud.sap")
logging.info(" %s device(s) found.", len(serverlist))
return serverlist
def run_inventory_loop(config, connection):
"""
Loop to check the inventory of the servers
"""
scrape_interval = os.getenv('SCRAPE_INTERVAL', config['scrape_interval'])
serverlist = []
while True:
try:
try:
serverlist = get_serverlist(config, connection)
except NetboxConnectionException as err:
logging.error(err)
for server in serverlist:
try:
server = server.replace('\r','').replace('\n','')
collector= InventoryCollector(config, connection)
collector.check_server_inventory(server)
except (HandlerException, NetboxConnectionException) as err:
logging.error(err)
del collector
gc.collect() # trigger garbage collection
except KeyboardInterrupt:
logging.info("Keyboard Interrupt. Stopping Inventory Updater...")
sys.exit()
logging.info("==> Sleeping for %s seconds.", scrape_interval)
time.sleep(scrape_interval)
if __name__ == '__main__':
call_args = get_args()
warnings.filterwarnings("ignore")
enable_logging(call_args.logging, call_args.debug)
configuration = get_config(call_args.config)
if call_args.servers:
configuration['servers'] = call_args.servers
netbox_connection = NetboxConnection(configuration)
if call_args.api:
falcon_app(configuration, netbox_connection)
else:
run_inventory_loop(configuration, netbox_connection)