Fritzbox Cable Docsis Parameter auslesen

Aus Neobikers Wiki
Zur Navigation springen Zur Suche springen

Kleines Shellscript um von einer Fritzbox Cable ab Version 7.20 die Kabel-Informationen (DOCSIS) auszulesen und in eine Influxxdb zu speichern. Zur Anzeige der Monitoring-Daten in der InfluxxDB verwende ich Grafana.

Datei:Fritz6490 DOCSIS Monitoring.jpg
#!/bin/bash

fritzbox=<fritz.box>
user=<admin>
pass=<password>
influxdb=<influxdb name>

# --------------------
# cache Login with SID
# --------------------
sidfile=/dev/shm/$fritzbox.sid
[ ! -f $sidfile ] && touch $sidfile
sid=$(cat $sidfile)

# --------------------
# check Login with SID
# --------------------
result=$(curl -s "http://$fritzbox/login_sid.lua?sid=$sid" | grep -c "0000000000000000")
if [ $result -gt 0 ]; then
  challenge=$(curl -s http://$fritzbox/login_sid.lua | grep -o "<Challenge>.*</Challenge>" | sed 's,</*Challenge>,,g')
  hash=$(echo -n "$challenge-$pass" | sed -e 's,.,&\n,g' | tr '\n' '\0' | md5sum | grep -o "[0-9a-z]\{32\}")
  curl -s "http://$fritzbox/login_sid.lua" -d "response=$challenge-$hash" -d 'username='${user} | grep -o "<SID>[a-z0-9]\{16\}" | cut -d'>' -f 2 > $sidfile
fi
sid=$(cat $sidfile)

# -----------------
# read DOCSIS-Infos
# -----------------
docsis=$(curl -s "http://$fritzbox/data.lua" -d "xhr=1&sid=$sid&lang=de&page=docInfo&xhrId=all&no_sidrenew")

# function: write data into influxdb
write_influxdb () {
    curl -i -XPOST 'http://127.0.0.1:8086/write?db='$influxdb --data-binary "$1"
}

# get nr. of up-/downstream channels
channelUs=$(echo ${docsis} | jq ".data.channelUs.docsis30[].powerLevel" | wc -l)
channelDs=$(echo ${docsis} | jq ".data.channelDs.docsis30[].powerLevel" | wc -l)

# read upstream channels
for (( c=0; c<$channelUs; c++ )); do
  channelID[$c]=$(echo ${docsis}  | jq -r ".data.channelUs.docsis30[$c].channelID")
  channel[$c]=$(echo ${docsis}    | jq -r ".data.channelUs.docsis30[$c].channel")
  qam[$c]=$(echo ${docsis}        | jq -r ".data.channelUs.docsis30[$c].type" | sed 's/QAM//g')
  powerLevel[$c]=$(echo ${docsis} | jq -r ".data.channelUs.docsis30[$c].powerLevel")
  frequency[$c]=$(echo ${docsis}  | jq -r ".data.channelUs.docsis30[$c].frequency")

  echo "ID ${channelID[$c]}: Channel ${channel[$c]} (${frequency[$c]}) ${qam[$c]} PowerLevel ${powerLevel[$c]}"
  write_influxdb "docsis,mode=up,channel=${channelID[$c]},param=Modulation value=${qam[$c]}"
  write_influxdb "docsis,mode=up,channel=${channelID[$c]},param=Power\ Level\ [dBmV] value=${powerLevel[$c]}"
  write_influxdb "docsis,mode=up,channel=${channelID[$c]},param=Frequenz\ [MHz] value=${frequency[$c]}"
done

# read downstream channels
for (( c=0; c<$channelDs; c++ )); do
  channelID[$c]=$(echo ${docsis}  | jq -r ".data.channelDs.docsis30[$c].channelID")
  channel[$c]=$(echo ${docsis}    | jq -r ".data.channelDs.docsis30[$c].channel")
  qam[$c]=$(echo ${docsis}        | jq -r ".data.channelDs.docsis30[$c].type" | sed 's/QAM//g')
  powerLevel[$c]=$(echo ${docsis} | jq -r ".data.channelDs.docsis30[$c].powerLevel")
  frequency[$c]=$(echo ${docsis}  | jq -r ".data.channelDs.docsis30[$c].frequency")
  latency[$c]=$(echo ${docsis}  | jq -r ".data.channelDs.docsis30[$c].latency")
  corrErrors[$c]=$(echo ${docsis}  | jq -r ".data.channelDs.docsis30[$c].corrErrors")
  nonCorrErrors[$c]=$(echo ${docsis}  | jq -r ".data.channelDs.docsis30[$c].nonCorrErrors")

  echo "ID ${channelID[$c]}: Channel ${channel[$c]} (${frequency[$c]}) ${qam[$c]} PowerLevel ${powerLevel[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=Modulation value=${qam[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=Power\ Level\ [dBmV] value=${powerLevel[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=Frequenz\ [MHz] value=${frequency[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=Latenz\ [ms] value=${latency[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=korrigierbare\ Fehler value=${corrErrors[$c]}"
  write_influxdb "docsis,mode=down,channel=${channelID[$c]},param=nicht\ korrigierbare\ Fehler value=${nonCorrErrors[$c]}"
done