Skip to content

Instantly share code, notes, and snippets.

@donfanning
Forked from Emantor/zyxel2graphite.py
Created July 14, 2020 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donfanning/717c8d08d17f27bb8ea6113811997a5d to your computer and use it in GitHub Desktop.
Save donfanning/717c8d08d17f27bb8ea6113811997a5d to your computer and use it in GitHub Desktop.
zyxel2graphite.py
#!/usr/bin/python
"""Copyright 2013 Bryan Irvine
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
import re
import sys
import time
import socket
import platform
import subprocess
import pickle
import struct
import telnetlib
CARBON_SERVER = ''
CARBON_PICKLE_PORT = 2004
DELAY = 30
HOST = "192.168.1.1"
USERNAME='admin'
PASSWORD=''
def get_data():
connection = telnetlib.Telnet(HOST)
connection.expect(["Login: ".encode('utf-8')])
connection.write("{}\n".format(USERNAME).encode('utf-8'))
connection.expect(["Password: ".encode('utf-8')])
connection.write("{}\n".format(PASSWORD).encode('utf-8'))
connection.write("xdslctl info --stats\n".encode('utf-8'))
xdsl_data = connection.read_until(b"NTR: mipsCntAtNtr=0 ncoCntAtNtr=0").decode('utf-8').split('\n')
data = {
'zyxel_vdsl.connection.snr.down': xdsl_data[18].split()[2],
'zyxel_vdsl.connection.snr.up': xdsl_data[18].split()[3],
'zyxel_vdsl.connection.attn.down': xdsl_data[19].split()[1],
'zyxel_vdsl.connection.attn.up': xdsl_data[19].split()[2],
'zyxel_vdsl.connection.pwr.down': xdsl_data[20].split()[1],
'zyxel_vdsl.connection.pwr.up': xdsl_data[20].split()[2],
'zyxel_vdsl.bandwidth.max.up': xdsl_data[7].split()[4],
'zyxel_vdsl.bandwidth.max.down': xdsl_data[7].split()[9],
'zyxel_vdsl.bandwidth.real.up': xdsl_data[8].split()[5],
'zyxel_vdsl.bandwidth.real.down': xdsl_data[8].split()[10]
}
return data
def run(sock, delay):
"""Make the client go go go"""
while True:
now = int(time.time())
tuples = ([])
lines = []
#We're gonna report all three loadavg values
try:
data = get_data()
except Exception:
pass
for key,value in iter(data.items()):
tuples.append((key, (now,value)))
lines.append("%s %s %d" % (key, value, now))
message = '\n'.join(lines) + '\n' #all lines must end in a newline
package = pickle.dumps(tuples, 1)
size = struct.pack('!L', len(package))
sock.sendall(size)
sock.sendall(package)
time.sleep(delay)
def main():
"""Wrap it all up together"""
delay = DELAY
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg.isdigit():
delay = int(arg)
else:
sys.stderr.write("Ignoring non-integer argument. Using default: %ss\n" % delay)
sock = socket.socket()
try:
sock.connect( (CARBON_SERVER, CARBON_PICKLE_PORT) )
except socket.error:
raise SystemExit("Couldn't connect to %(server)s on port %(port)d, is carbon-cache.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PICKLE_PORT })
try:
run(sock, delay)
except KeyboardInterrupt:
sys.stderr.write("\nExiting on CTRL-c\n")
sys.exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment