Skip to content

Commit

Permalink
Add multi wifi card
Browse files Browse the repository at this point in the history
  • Loading branch information
flob82 committed Feb 9, 2020
1 parent f3532bc commit 7bbab33
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 35 deletions.
3 changes: 2 additions & 1 deletion freebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
app_name = 'Freebox-OS-munin'
# debian's systemd limitation : https://github.com/munin-monitoring/munin/issues/1236
# ProtectSystem=full
freebox_config_file = '/var/lib/munin-node/plugin-state/nobody/' + app_name + '/freebox.json'
write_dir = '/var/lib/munin-node/plugin-state/nobody/'
freebox_config_file = write_dir + app_name + '/freebox.json'
app_id = 'freebox-revolution-munin' # Script legacy name. Changing this would break authentication
app_version = '1.0.0'
device_name = socket.gethostname()
Expand Down
113 changes: 79 additions & 34 deletions freebox_
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,35 @@ def print_config():
print('{}.max 1'.format(field))
print('{}.label {}'.format(field, field))
print('{}.draw AREASTACK'.format(field))
elif mode == mode_wifi_stations:
print('graph_title Wifi stations')
elif mode.startswith('wifi-stations'):
wifi_index = mode[-1]
result = get_wifi_ap()
wifi_name = result.get(int(wifi_index))
print('graph_title Wifi {} stations'.format(wifi_name))
print('graph_vlabel #')
print('graph_total Total')
print('graph_args --lower-limit 0')
stations = get_wifi_stations()
stations = get_wifi_stations(wifi_index)
for station in stations:
print('{}.label {}'.format(station, station))
print('{}.draw AREASTACK'.format(station))
elif mode == mode_wifi_bytes:
print('graph_title Wifi bytes up/down')
elif mode.startswith('wifi-bytes-log'):
wifi_index = mode[-1]
result = get_wifi_ap()
wifi_name = result.get(int(wifi_index))
print('graph_title Wifi {} bytes up/down'.format(wifi_name))
print('graph_scale yes')
print('graph_vlabel bytes per second')
print('graph_args --logarithmic')
print('rx_bytes.type COUNTER')
print('rx_bytes.label down (bytes/s)')
print('tx_bytes.type COUNTER')
print('tx_bytes.label up (bytes/s)')
elif mode.startswith('wifi-bytes'):
wifi_index = mode[-1]
result = get_wifi_ap()
wifi_name = result.get(int(wifi_index))
print('graph_title Wifi {} bytes up/down'.format(wifi_name))
print('graph_scale yes')
print('graph_vlabel byte in (-) / out (+) per second')
print('rx_bytes.type COUNTER')
Expand All @@ -278,15 +296,6 @@ def print_config():
print('tx_bytes.label bytes/s')
print('tx_bytes.type COUNTER')
print('tx_bytes.negative rx_bytes')
elif mode == mode_wifi_bytes_log:
print('graph_title Wifi bytes up/down')
print('graph_scale yes')
print('graph_vlabel bytes per second')
print('graph_args --logarithmic')
print('rx_bytes.type COUNTER')
print('rx_bytes.label down (bytes/s)')
print('tx_bytes.type COUNTER')
print('tx_bytes.label up (bytes/s)')


def query_data():
Expand All @@ -311,10 +320,12 @@ def query_data():
switch_index = mode[-1]
mode2 = mode[:-1]
query_switch(switch_index, mode2)
elif mode == mode_wifi_stations:
query_wifi_stations()
elif mode in [mode_wifi_bytes, mode_wifi_bytes_log]:
query_wifi_bytes()
elif mode.startswith('wifi-stations'):
wifi_index = mode[-1]
query_wifi_stations(wifi_index)
elif mode.startswith('wifi-byte'):
wifi_index = mode[-1]
query_wifi_bytes(wifi_index)
else:
query_rrd_data()

Expand Down Expand Up @@ -375,12 +386,26 @@ def query_ftth():
print('{}.value {}'.format(field, value))


def get_wifi_stations():
data = freebox.api('wifi/ap/0/stations/')
def get_wifi_ap():
result = dict()
r_json = freebox.api('wifi/ap/')
for wifi in r_json:
result[wifi['id']] = wifi['name']
#for key, value in wifi.items():
# if key.startswith('id'):
# name += str(value)
# if key.startswith('name'):
# name += value + '<->'
#name += '\n'
return(result)
#print (name)

def get_wifi_stations(idx_station):
data = freebox.api('wifi/ap/' + idx_station + '/stations/')

# the filename is something like
# /var/lib/munin-node/plugin-state/nobody/freebox-wifi-stations-
wifi_filename = os.environ.get('MUNIN_STATEFILE', mode + '-STATEFILE')
wifi_filename = os.environ.get('MUNIN_STATEFILE', mode + idx_station + '-STATEFILE')
try:
with open(wifi_filename) as f:
stations = json.load(f)
Expand All @@ -407,8 +432,8 @@ def get_wifi_stations():

return stations

def query_wifi_stations():
stations = get_wifi_stations()
def query_wifi_stations(idx_wifi):
stations = get_wifi_stations(idx_wifi)

current_time = time.time()
for station in stations:
Expand All @@ -419,16 +444,18 @@ def query_wifi_stations():
value = 0
print('{}.value {}'.format(station, value))

def query_wifi_bytes():
data = freebox.api('wifi/ap/0/stations/')
def query_wifi_bytes(idx_wifi):
data = freebox.api('wifi/ap/' + idx_wifi + '/stations/')

fields = dict()
for field in get_fields(mode):
mode_wifi = mode[:-1]

for field in get_fields(mode_wifi):
fields[field] = 0
for station in data:
for field in get_fields(mode):
for field in get_fields(mode_wifi):
fields[field] += station[field]
for field in get_fields(mode):
for field in get_fields(mode_wifi):
print('{}.value {}'.format(field, fields[field]))


Expand Down Expand Up @@ -588,7 +615,13 @@ if len(sys.argv) > 1:
print('yes')
sys.exit(0)
elif args.arg == "suggest":
print('{}'.format('\n'.join(modes)))
result = get_wifi_ap()
for mode in modes:
if 'wifi' in mode:
for wifi in result:
print('{}{}'.format(mode, wifi))
else:
print('{}'.format(mode))
sys.exit(0)

# Great for testing
Expand All @@ -598,13 +631,25 @@ if mode == 'all':
'red': '\033[31m',
'normal': '\033[0m',
}
result = get_wifi_ap()
for m in modes:
mode = m
print(COLORS['red'] + "Testing mode:", mode)
print(COLORS['blue'] + "config:" + COLORS['normal'])
print_config()
print(COLORS['blue'] + "data:" + COLORS['normal'])
query_data()
if mode.startswith('wifi'):
for wifi in result:
temp = mode
mode += str(wifi)
print(COLORS['red'] + "Testing mode:", mode)
print(COLORS['blue'] + "config:" + COLORS['normal'])
print_config()
print(COLORS['blue'] + "data:" + COLORS['normal'])
query_data()
mode = temp
else:
print(COLORS['red'] + "Testing mode:", mode)
print(COLORS['blue'] + "config:" + COLORS['normal'])
print_config()
print(COLORS['blue'] + "data:" + COLORS['normal'])
query_data()

sys.exit(0)

Expand Down

0 comments on commit 7bbab33

Please sign in to comment.