Récupérer les stats internet de votre box SFR NB6

Bonjour,

Ce qui suit est issu de la lecture de https://la-communaute.sfr.fr/t5/wifi-connexion-filaire-et-d%C3%A9bit/nb6-traffic-monitor/td-p/2112893 qui m’a amené à https://forum.fhem.de/index.php?topic=81555.0

Pour ma part, j’ai une box SFR NB6 (depuis longtemps) et un ONT Fibre.
Pour savoir si cela fonctionnera aussi pour vous, il suffit de contrôler avec ce petit script Python dans lequel vous collerez l’IP de votre Box:

#!/usr/bin/env python
# la source https://forum.fhem.de/index.php?topic=81555.0
import socket

SFR_NB6_IP   = 'ww.xx.yy.zz'
SFR_NB6_PORT = 1287
BUFFER_SIZE  = 1024

def send_string(hostname, port, content):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((hostname, port))
    s.sendall(content)
    s.shutdown(socket.SHUT_WR)
    while 1:
        data = s.recv(1024)
        if data == "":
            break
    if data:
        print "Received:", repr(data)
    s.close()

#get string from SFR Neufbox
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((SFR_NB6_IP, SFR_NB6_PORT))
data = s.recv(BUFFER_SIZE)
s.close()

#interpret data into dict
d = dict(item.split("=") for item in data.split(";"))

#debug:
print "down:", d["65"]
print "up:", d["66"]
print "totalite:", d

bytes_down = d["65"]
bytes_up   = d["66"]

Si cela fonctionne, vous pouvez passer à l’étape Node-Red et là aussi, collez-y l’IP de votre Box :

[{"id":"1c4eec4a63d9677c","type":"tab","label":"Stats BOX SFR","disabled":false,"info":"","env":[]},{"id":"a84193f25e0906ae","type":"inject","z":"1c4eec4a63d9677c","name":"listen","props":[{"p":"payload"}],"repeat":"300","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"a","payloadType":"str","x":160,"y":300,"wires":[["1220fd0bfd42071d"]]},{"id":"1220fd0bfd42071d","type":"tcp request","z":"1c4eec4a63d9677c","server":"ww.xx.yy.zz","port":"1287","out":"count","ret":"string","splitc":"900","name":"dataout","x":320,"y":300,"wires":[["d6cd5fa53f9cb24d"]]},{"id":"15109d508fd4c246","type":"ha-entity","z":"1c4eec4a63d9677c","name":"trafic_internet","server":"4ed39f3a.96005","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"trafic internet"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"byte"},{"property":"state_class","value":"total_increasing"},{"property":"last_reset","value":""}],"state":"payload.ipaddr","stateType":"msg","attributes":[{"property":"input","value":"payload.input","valueType":"msg"},{"property":"output","value":"payload.output","valueType":"msg"}],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":720,"y":300,"wires":[[]]},{"id":"d6cd5fa53f9cb24d","type":"function","z":"1c4eec4a63d9677c","name":"","func":"res = msg.payload.match(/0C=([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3});.*;65=(\\d+);66=(\\d+);/);\nmsg.payload = {\n    ipaddr: res[1],\n    input: res[2],\n    output: res[3]\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":300,"wires":[["15109d508fd4c246"]]},{"id":"4ed39f3a.96005","type":"server","name":"Home Assistant","addon":true}]		  

C’est paramétré pour récupérer les stats bytes in & out toutes les 5 minutes.
Et le résultat est rendu dans sensor.trafic_internet dans lequel l’état est votre IP internet et les états input et output sont les stats de trafic.
Je visualise tout ça avec Grafana avec ces 2 requêtes :

SELECT NON_NEGATIVE_DERIVATIVE(mean("input"),5m)/1024  FROM "byte" WHERE $timeFilter GROUP BY time($__interval) fill(null)
SELECT NON_NEGATIVE_DERIVATIVE(mean("output"),5m)/1024 FROM "byte" WHERE $timeFilter GROUP BY time($__interval) fill(null)

Etant donné que le graphique peut connaitre de fortes variations, j’utilise une échelle Log sur base 10.
Et fixez l’unité à Kilobytes/sec.

Et voilà.

3 « J'aime »