Skip to content

Instantly share code, notes, and snippets.

@christianchristensen
Last active January 21, 2024 04:34
Show Gist options
  • Save christianchristensen/f73214bc9e69bdbece234e1ac6b8329e to your computer and use it in GitHub Desktop.
Save christianchristensen/f73214bc9e69bdbece234e1ac6b8329e to your computer and use it in GitHub Desktop.

ATT Humax BGW320-500 Network Status montioring script and Zabbix template+config

$ grep BGW320 /etc/zabbix/zabbix_agentd.conf
UserParameter=BGW320500[*],/home/pi/BGW320500/ATTHumaxBGW320500.py '$1' '$2' '$3' $4 $5

$ zabbix_agentd -t "BGW320500[Transmit Packets,Transmit Packets,,1,2]"
BGW320500[Transmit Packets,Transmit Packets,,1,2] [t|2741335]

$ zabbix_agentd -t "BGW320500[Transmit Packets,Receive Packets,,1,1]"
BGW320500[Transmit Packets,Receive Packets,,1,1] [t|78197396]

$ zabbix_agentd -t "BGW320500[MAC Address,MAC Address,LLD]"
BGW320500[MAC Address,MAC Address,LLD]        [t|[{"{#MACADDRESS}": "00:de:ad:be:ef:aa"}, {"{#MACADDRESS}": "f4:00:f4:aa:88:44"}]]
#!/usr/bin/env python
# Parser for ATT Humax BGW320-500 Network Status page
# ATTHumaxBGW320500.py <tablename> <rowname> '' <rowindex=1> <tableindex=1>
# Specity tablename and rowname to return the value by index
# ATTHumaxBGW320500.py <tablename> <colname> <macid or 'LLD'>
# Specify the tablename to return all available values (Zabbix LLD format)
# Data based on Model: BGW320-500, Version: 4.25.4
import json
import os
import sys
import time
import urllib.request
from html.parser import HTMLParser
# Purpose: Simple class for parsing an (x)html string to extract tables.
# Author: Josua Schmid
# Original: https://github.com/schmijos/html-table-parser-python3
class HTMLTableParser(HTMLParser):
""" This class serves as a html table parser. It is able to parse multiple
tables which you feed in. You can access the result per .tables field.
"""
def __init__(
self,
decode_html_entities=False,
data_separator=' ',
):
HTMLParser.__init__(self)
self._parse_html_entities = decode_html_entities
self._data_separator = data_separator
self._in_td = False
self._in_th = False
self._current_table = []
self._current_row = []
self._current_cell = []
self.tables = []
def handle_starttag(self, tag, attrs):
""" We need to remember the opening point for the content of interest.
The other tags (<table>, <tr>) are only handled at the closing point.
"""
if tag == 'td':
self._in_td = True
if tag == 'th':
self._in_th = True
def handle_data(self, data):
""" This is where we save content to a cell """
if self._in_td or self._in_th:
self._current_cell.append(data.strip())
def handle_charref(self, name):
""" Handle HTML encoded characters """
if self._parse_html_entities:
self.handle_data(self.unescape('&#{};'.format(name)))
def handle_endtag(self, tag):
""" Here we exit the tags. If the closing tag is </tr>, we know that we
can save our currently parsed cells to the current table as a row and
prepare for a new row. If the closing tag is </table>, we save the
current table and prepare for a new one.
"""
if tag == 'td':
self._in_td = False
elif tag == 'th':
self._in_th = False
if tag in ['td', 'th']:
final_cell = self._data_separator.join(self._current_cell).strip()
self._current_row.append(final_cell)
self._current_cell = []
elif tag == 'tr':
self._current_table.append(self._current_row)
self._current_row = []
elif tag == 'table':
self.tables.append(self._current_table)
self._current_table = []
# ---
def url_get_contents(url):
""" Opens a website and read its binary contents (HTTP Response Body) """
#req = urllib.Request(url)
req = urllib.request.Request(url)
r = urllib.request.urlopen(req)
return r.read()
def main():
url = 'http://192.168.1.254/cgi-bin/lanstatistics.ha'
xhtml = False # cache signal
# Cache
cachefile = '/tmp/.ATTHumaxBGW320500.py.cache'
try:
if os.path.exists(cachefile):
age = time.time() - os.path.getmtime(cachefile)
if age < 120:
f = open(cachefile, "r")
xhtml = f.read()
f.close()
except:
xhtml = False
if not xhtml:
xhtml = url_get_contents(url).decode('utf-8')
try:
f = open(cachefile, 'w')
f.write(xhtml)
f.close()
except:
pass
p = HTMLTableParser()
p.feed(xhtml)
# CLI parse
tablename = sys.argv[1] # 'Transmit Packets'
rcname = sys.argv[2] if len(sys.argv)>2 else '' # 'Recieve Packets'
macid = sys.argv[3] if len(sys.argv)>3 else -1
rowix = int(sys.argv[4]) if len(sys.argv)>4 else 1
tableix = int(sys.argv[5]) if len(sys.argv)>5 else 1
for t in p.tables:
if t[0][0] == tablename:
tableix = tableix - 1
if tableix == 0:
if macid == -1 or macid == '':
for r in t:
if r[0] == rcname:
print(r[rowix])
else: # LLD output based on columns available
if macid == 'LLD':
macroname = t[0][0]
macroname = '{#' + ''.join(macroname.upper().split()) + '}' # tranform to Zabbix format
lld = []
t.pop(0) # Header
for r in t:
lld.append( { macroname: r[0] } )
print(json.dumps(lld))
else:
rowcntid = t.pop(0).index(str(rcname))
for r in t:
if r[0] == macid:
print(r[rowcntid])
if __name__ == '__main__':
main()
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>6.4</version>
<template_groups>
<template_group>
<uuid>7df96b18c230490a9a0a9e2307226338</uuid>
<name>Templates</name>
</template_group>
</template_groups>
<templates>
<template>
<uuid>1343f366229844ef9686b6c6da0ee83b</uuid>
<template>Template NET ATT Humax BGW320-500</template>
<name>Template NET ATT Humax BGW320-500</name>
<description>Monitoring for BGW320-500 Network Stats</description>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<items>
<item>
<uuid>72aca142b87b42f19441def41b05207b</uuid>
<name>IPv4 Receive Discards</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Receive Discards,,1,1]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>28a4dec794604f9ca2c581a6ccf0d596</uuid>
<name>IPv4 Receive Errors</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Receive Errors,,1,1]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>e27617d4a3b54958a2054408d173f296</uuid>
<name>IPv4 Receive pps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Receive Packets,,1,1,]</key>
<delay>5m</delay>
<value_type>FLOAT</value_type>
<units>pps</units>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>ebdf11bc1f6b405d99f5bd1d761ea575</uuid>
<name>IPv4 Receive Packets</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Receive Packets,,1,1]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>2cf4f0c878b84abeb5bb5bf16fab8fbe</uuid>
<name>IPv4 Transmit Discards</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Discards,,1,1]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>b272dfde51764bbba93fa0090bf606cd</uuid>
<name>IPv6 Transmit Discards</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Discards,,1,2]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>e99ccc7c71cd4c1180cc7f7d98cc5a9c</uuid>
<name>IPv4 Transmit Errors</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Errors,,1,1]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>b89fe05701514b74aeb241115bffa2b6</uuid>
<name>IPv6 Transmit Errors</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Errors,,1,2]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>17a5fb749a1545fe820926203561b0c5</uuid>
<name>IPv4 Transmit pps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Packets,,1,1,]</key>
<delay>5m</delay>
<value_type>FLOAT</value_type>
<units>pps</units>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>6c07e2b18a84436cbcb907065821ee8c</uuid>
<name>IPv4 Transmit Packets</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Packets,,1,1]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>3c2bf23b74df4bb29fb3683d8f9d823e</uuid>
<name>IPv6 Transmit pps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Packets,,1,2,]</key>
<delay>5m</delay>
<value_type>FLOAT</value_type>
<units>pps</units>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
<item>
<uuid>6264862795f44d79abf2d3d942bcbd1b</uuid>
<name>IPv6 Transmit Packets</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[Transmit Packets,Transmit Packets,,1,2]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item>
</items>
<discovery_rules>
<discovery_rule>
<uuid>3bb9d4aa53ca407bb65da327a4588822</uuid>
<name>Device MAC stats</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,MAC Address,LLD]</key>
<delay>1h</delay>
<filter>
<conditions>
<condition>
<macro>{#MACADDRESS}</macro>
<value>.*</value>
<formulaid>A</formulaid>
</condition>
</conditions>
</filter>
<item_prototypes>
<item_prototype>
<uuid>e381977225c8460fb062618475092fed</uuid>
<name>{#MACADDRESS} Deauth Count</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Deauth Count,{#MACADDRESS}]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>248d0e429b6549709b243ad35deb1f80</uuid>
<name>{#MACADDRESS} Disassoc Count</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Disassoc Count,{#MACADDRESS}]</key>
<delay>5m</delay>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>ea0098cf57154fd6b5a790e361908305</uuid>
<name>{#MACADDRESS} Receive bps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address, Receive Bytes,{#MACADDRESS},]</key>
<delay>5m</delay>
<value_type>FLOAT</value_type>
<units>bps</units>
<preprocessing>
<step>
<type>MULTIPLIER</type>
<parameters>
<parameter>8</parameter>
</parameters>
</step>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>10292d635a8241ad837226ef909d1e8a</uuid>
<name>{#MACADDRESS} Receive Bytes</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Receive Bytes,{#MACADDRESS}]</key>
<delay>5m</delay>
<status>DISABLED</status>
<units>B</units>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>24c8730fab6a4482afbb4a274bb2a8a8</uuid>
<name>{#MACADDRESS} Receive pps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address, Receive Packets,{#MACADDRESS},]</key>
<delay>5m</delay>
<status>DISABLED</status>
<value_type>FLOAT</value_type>
<units>pps</units>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>a2e8402af4004d29b9e25229da601229</uuid>
<name>{#MACADDRESS} Receive Packets</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address, Receive Packets,{#MACADDRESS}]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>db59e7c5730b4e9c800ef75d01cbb177</uuid>
<name>{#MACADDRESS} Trans Errors</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Trans Errors,{#MACADDRESS}]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>9e73e51a9903401b85d7b9d2e4a94993</uuid>
<name>{#MACADDRESS} Transmit bps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Transmit Bytes,{#MACADDRESS},]</key>
<delay>5m</delay>
<value_type>FLOAT</value_type>
<units>bps</units>
<preprocessing>
<step>
<type>MULTIPLIER</type>
<parameters>
<parameter>8</parameter>
</parameters>
</step>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>5216db76ae8a48c5b439e7cb132ba39f</uuid>
<name>{#MACADDRESS} Transmit Bytes</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Transmit Bytes,{#MACADDRESS}]</key>
<delay>5m</delay>
<status>DISABLED</status>
<units>B</units>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>b6d9c49d27b44c7aa9856b14db7a8406</uuid>
<name>{#MACADDRESS} Transmit pps</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Transmit Packets,{#MACADDRESS},]</key>
<delay>5m</delay>
<status>DISABLED</status>
<value_type>FLOAT</value_type>
<units>pps</units>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<parameters>
<parameter/>
</parameters>
</step>
</preprocessing>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
<item_prototype>
<uuid>35ca3a17e9184bf1b56170701d920144</uuid>
<name>{#MACADDRESS} Transmit Packets</name>
<type>ZABBIX_ACTIVE</type>
<key>BGW320500[MAC Address,Transmit Packets,{#MACADDRESS}]</key>
<delay>5m</delay>
<status>DISABLED</status>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</item_prototype>
</item_prototypes>
</discovery_rule>
</discovery_rules>
<tags>
<tag>
<tag>Net</tag>
<value>BGW320</value>
</tag>
</tags>
</template>
</templates>
</zabbix_export>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment