forked from GuillaumeHullin/freebox-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbx_monitor.py
348 lines (264 loc) · 8.92 KB
/
fbx_monitor.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
"""Get Foreebox metrics for monitoring"""
import requests
import logging
from appdirs import AppDirs
from os.path import exists, join as path_join
from configparser import ConfigParser
import sys
log = logging.getLogger("Fbx-Monitor")
class ApiError (Exception):
"""
Class for not authorized access
"""
body = None
def __init__(self, body):
super(__class__, self).__init__()
self.body = body
class NotAuthorized(ApiError):
pass
class AuthRequired(ApiError):
pass
class InvalidToken(ApiError):
pass
class ApiRequest:
"""
Low level Api calls and authentication
"""
api_url = "http://mafreebox.freebox.fr/api/v4/"
challenge = None
app_id = "com.wcentric.zabbix"
app_name = "FH Monitoring"
app_version = "0.0.1"
app_token = None
session_token = None
def is_logged(self):
"""
Check if this application is logged on the Freebox
"""
result = self.get("login")
return result["logged_in"]
def request_authorize(self):
"""
Register the application on the Freebox
"""
from socket import gethostname
payload = {
"app_id": self.app_id,
"app_name": self.app_name,
"app_version": self.app_version,
"device_name": gethostname()
}
print("You need to press YES on the box.")
app_register = self.post("login/authorize", payload)
self.app_token = app_register["app_token"]
return app_register["track_id"]
def get_track(self, track_id):
"""
Check application authorization status
"""
result = self.get("login/authorize/" + str(track_id))
return result["status"]
def authenticate(self):
"""
Ask the user to allow the application (require operation on the box)
"""
from time import sleep
if self.is_logged():
return True
if self.app_token is not None:
try:
self.start_session()
return True
except NotAuthorized:
log.warning("Not authorized.")
self.session_token = None
track_id = self.request_authorize()
status = "pending"
while status == "pending":
sleep(5)
status = self.get_track(track_id)
if status != "granted":
log.error("Application not granted")
raise
self.start_session()
def parse(self, answer):
"""
Parse API answer and raise an exception if the answer is an error
"""
body = answer.json()
log.debug(answer.status_code)
log.debug(body)
if "result" in body and "challenge" in body["result"]:
self.challenge = body["result"]["challenge"]
if answer.status_code == 403:
if body['error_code'] == "invalid_token":
self.session_token = None
log.warning("Invalid token, delete existing token.")
raise InvalidToken(body)
elif body['error_code'] == 'auth_required':
log.debug('Require authentication.')
raise AuthRequired(body)
log.warning("Not authenticated.")
raise ApiError(body)
if answer.status_code != 200:
log.critical("Status code not handled.")
raise
if not body["success"]:
log.error("Message body not success.")
raise
if "result" in body:
return body["result"]
else:
return True
def _send_request(self, method, url, json = None):
log.debug("Using session %s, Call %s (%s)" % (self.session_token, url, method.__name__))
if self.session_token is not None:
headers = {"X-Fbx-App-Auth": self.session_token}
else:
headers = None
if json is not None:
return self.parse(method(self.api_url + url, json=json, headers=headers))
else:
return self.parse(method(self.api_url + url, headers=headers))
def _call_api(self, method, url, json = None):
try:
return self._send_request(method, url, json)
except AuthRequired:
self.start_session()
return self._send_request(method, url, json)
def get(self, url):
"""
Make a get request on API
"""
return self._call_api(requests.get, url)
def post(self, url, data):
"""
Make a post request on API Server
"""
return self._call_api(requests.post, url, json=data)
def start_session(self):
"""
Start a new session
"""
import hmac as HMAC_Factory
from hashlib import sha1
if self.app_token is None:
raise InvalidToken(None)
if self.challenge is None:
self.is_logged()
log.debug("Start session")
log.debug(self.challenge)
log.debug(self.app_token)
hmac = HMAC_Factory.new(bytes(self.app_token, "utf-8"), bytes(self.challenge, "utf-8"), 'sha1')
password = hmac.digest()
password_hex = ''.join('{:02x}'.format(x) for x in password)
log.debug(password_hex)
payload = {
"app_id": self.app_id,
"password": password_hex
}
log.debug(payload)
result = self.post("login/session", payload)
self.session_token = result["session_token"]
log.debug("Session Opened")
log.debug(result)
log.debug(self.session_token)
class MonitoringAgent:
"""
The high level API Client
"""
api = ApiRequest()
def authorize(self):
self.api.app_token = None
self.api.session_token = None
self.api.authenticate()
def system(self):
"""
Get System metrics and configuration
:return: dict of values
"""
return self.api.get("system")
def connection(self):
"""
Get Connection status
:return:
"""
return self.api.get("connection")
def switch(self):
"""
Get Switch status
:return:
"""
return self.api.get("switch/status/")
def freeplugs(self):
"""
Get Freeplugs
:return:
"""
return self.api.get("freeplug")
def wifi_ap(self):
"""
Get AP connected to Wifi
:return:
"""
return self.api.get("wifi/ap")
class Settings:
config = ConfigParser()
app_dir = AppDirs(appname="freebox-monitoring")
file_path_cache = path_join(app_dir.site_config_dir, "config.ini")
instances = []
def __init__(self):
log.debug("Load file %s", self.file_path_cache)
self.config.read([self.file_path_cache])
def save(self):
if not exists(self.app_dir.site_config_dir):
from os import makedirs
makedirs(self.app_dir.site_config_dir)
for i in self.instances:
self.save_object(i)
with open(self.file_path_cache, 'w') as fp:
self.config.write(fp)
def handle(self, instance):
self.load_object(instance)
self.instances.append(instance)
def load_object(self, instance):
section = instance.__class__.__name__
log.debug("Load instance of %s" % section)
if self.config.has_section(section):
for key in self.config.options(section):
if hasattr(instance, key):
setattr(instance, key, self.config.get(section, key))
def save_object(self, instance):
section = instance.__class__.__name__
if not self.config.has_section(section):
self.config.add_section(section)
for key in instance.__dict__:
val = getattr(instance, key)
if val is not None:
self.config.set(section, key, val)
if __name__ == "__main__":
from argparse import ArgumentParser
config = Settings()
monitoring = MonitoringAgent()
config.handle(monitoring)
config.handle(monitoring.api)
callables = [func for func in dir(MonitoringAgent) if callable(getattr(MonitoringAgent, func)) and not func.startswith('__')]
command = ArgumentParser(description="Call Freebox API.")
command.add_argument('action', action='store', choices=callables, help='Action or API method to execute')
command.add_argument('-d', '--debug', action='store_true', help="Set debug mode")
args = command.parse_args()
if args.debug:
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler(sys.stderr))
if args.action == "authorize":
monitoring.authorize()
else:
try:
import json
print(json.dumps(getattr(monitoring, args.action)()))
except InvalidToken:
print("ERROR: Invalid token, need to authorize application.")
config.save()
exit(1)
config.save()