0% found this document useful (0 votes)
79 views3 pages

Fritz Data

This Python script collects metrics from a FRITZ!Box router every few seconds and posts the metrics to an InfluxDB database for monitoring and visualization. It retrieves data values using the router's API, converts them to a JSON format, calculates additional metrics like total bandwidth usage, and sends a formatted string of key-value pairs to the InfluxDB HTTP API endpoint. The script runs in a continuous loop to periodically retrieve and report updated metrics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views3 pages

Fritz Data

This Python script collects metrics from a FRITZ!Box router every few seconds and posts the metrics to an InfluxDB database for monitoring and visualization. It retrieves data values using the router's API, converts them to a JSON format, calculates additional metrics like total bandwidth usage, and sends a formatted string of key-value pairs to the InfluxDB HTTP API endpoint. The script runs in a continuous loop to periodically retrieve and report updated metrics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#!

/usr/bin/python
import sys
import time
import datetime
import json
import requests
import argparse
import hashlib
from urllib.request import urlopen
from xml.etree.ElementTree import parse
password='LOLOLOL'
sleepVar=3
dsbps="&dsbps=nqos:settings/stat0/ds_current_bps"
bps="&bps=nqos:settings/stat0/current_bps"
bsentLow="&bsentLow=inetstat:status/Today/BytesSentLow"
brecLow="&brecLow=inetstat:status/Today/BytesReceivedLow"
bsentHigh="&bsentHigh=inetstat:status/Today/BytesSentHigh"
brecHigh="&brecHigh=inetstat:status/Today/BytesReceivedHigh"
bsentLowMonth="&bsentLowMonth=inetstat:status/ThisMonth/BytesSentLow"
brecLowMonth="&brecLowMonth=inetstat:status/ThisMonth/BytesReceivedLow"
bsentHighMonth="&bsentHighMonth=inetstat:status/ThisMonth/BytesSentHigh"
brecHighMonth="&brecHighMonth=inetstat:status/ThisMonth/BytesReceivedHigh"
uptime_hours="&uptime_hours=logic:status/uptime_hours"
uptime_minutes="&uptime_minutes=logic:status/uptime_minutes"
statcpu="&statcpu=cpu:status/StatCPU"
statramcacheused="&statramcacheused=cpu:status/StatRAMCacheUsed"
statramphysfree="&statramphysfree=cpu:status/StatRAMPhysFree"
statramstrictlyused="&statramstrictlyused=cpu:status/StatRAMStrictlyUsed"
sumact="&sumact=power:status/rate_sumact"
wlandevs="&wlandevs=wlan:settings/wlanlist/list(state)"
ethdevs="&ethdevs=power:status/rate_ethact"
temp="&temp=power:status/act_temperature"
cmd = dsbps + bps + bsentLow + brecLow + bsentHigh + brecHigh + bsentLowMonth +
brecLowMonth + bsentHighMonth + brecHighMonth + uptime_hours + uptime_minutes +
statcpu + statramcacheused + statramphysfree + statramstrictlyused + sumact + wl
andevs + ethdevs + temp
def execute():
with urlopen('http://fritz/login_sid.lua') as f:
dom = parse(f)
sid = dom.findtext('./SID')
challenge = dom.findtext('./Challenge')
if sid == '0000000000000000':
md5 = hashlib.md5()
md5.update(challenge.encode('utf-16le'))
md5.update('-'.encode('utf-16le'))
md5.update(password.encode('utf-16le'))
response = challenge + '-' + md5.hexdigest()
uri = 'http://fritz/login_sid.lua?&response=' + response
with urlopen(uri) as f:
dom = parse(f)
sid = dom.findtext('./SID')
if sid == '0000000000000000':
raise PermissionError('access denied')

uri = 'http://fritz/query.lua?sid=' + sid + cmd


return urlopen(uri).read().decode()
def get_json(string):
jsonString = json.loads(string)
jsonString['dsbps'] = jsonString['dsbps'].partition(",")[0]
jsonString['bps'] = jsonString['bps'].partition(",")[0]
jsonString['bsent'] = str(int(jsonString['bsentHigh']) * 4294967295 + int(js
onString['bsentLow']))
jsonString['brec'] = str(int(jsonString['brecHigh']) * 4294967295 + int(json
String['brecLow']))
jsonString['btotal'] = str(int(jsonString['brec']) + int(jsonString['bsent']
))
jsonString['bsentMonth'] = str(int(jsonString['bsentHighMonth']) * 429496729
5 + int(jsonString['bsentLowMonth']))
jsonString['brecMonth'] = str(int(jsonString['brecHighMonth']) * 4294967295
+ int(jsonString['brecLowMonth']))
jsonString['btotalMonth'] = str(int(jsonString['brecMonth']) + int(jsonStrin
g['bsentMonth']))
jsonString['uptime'] = str((int(jsonString['uptime_minutes']) + int(jsonStri
ng['uptime_hours']) * 60) * 60)
days, seconds = divmod(int(jsonString['uptime']), 24*60*60)
hours, seconds = divmod(int(seconds), 60*60)
minutes, seconds = divmod(seconds, 60)
jsonString['uptime_string'] = '"' + str(days) + ' days, ' + str(hours).zfill
(2) + ':' + str(minutes).zfill(2) + '"'
jsonString['statcpu'] = jsonString['statcpu'].partition(",")[0]
jsonString['statramcacheused'] = str(int(jsonString['statramcacheused'].part
ition(",")[0]) / 100 * 245864 * 1024)
jsonString['statramphysfree'] = str(int(jsonString['statramphysfree'].partit
ion(",")[0]) / 100 * 245864 * 1024)
jsonString['statramstrictlyused'] = str(int(jsonString['statramstrictlyused'
].partition(",")[0]) / 100 * 245864 * 1024)
jsonString['statramused'] = str(float(jsonString['statramstrictlyused']) + f
loat(jsonString['statramcacheused']))
jsonString['wlandevs'] = str(json.dumps(jsonString['wlandevs']).count('5'))
return jsonString
def post_data(jsonString):
dataString = 'dsbps value=' + jsonString['dsbps']
dataString += '\nbps value=' + jsonString['bps']
dataString += '\nbsent value=' + jsonString['bsent']
dataString += '\nbrec value=' + jsonString['brec']
dataString += '\nbtotal value=' + jsonString['btotal']
dataString += '\nbsent' + datetime.datetime.now().strftime("%B") + ' value=
' + jsonString['bsentMonth']
dataString += '\nbrec' + datetime.datetime.now().strftime("%B") + ' value=
' + jsonString['brecMonth']
dataString += '\nbtotal' + datetime.datetime.now().strftime("%B") + ' valu
e=' + jsonString['btotalMonth']
dataString += '\nuptime value=' + jsonString['uptime']
dataString += '\nuptime_string value=' + jsonString['uptime_string']
dataString += '\nstatcpu value=' + jsonString['statcpu']

dataString += '\nstatramphysfree value=' + jsonString['statramphysfree']


dataString += '\nstatramused value=' + jsonString['statramused']
dataString += '\nsumact value=' + jsonString['sumact']
dataString += '\nwlandevs value=' + jsonString['wlandevs']
dataString += '\nethdevs value=' + jsonString['ethdevs']
dataString += '\ntemp value=' + jsonString['temp']
requests.post(url='http://localhost:8086/write?db=fritz', data=dataString)
time.sleep(sleepVar)
while True: post_data(get_json(execute()))

You might also like