Skip to content

Commit

Permalink
update mech to ignore HA network devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ironsheep committed Aug 17, 2020
1 parent dc69451 commit fadbe30
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Mon Aug 17 19:46:44 2020 -0600 v1.5.1

- Filter ignoring HA network devices was incomplete - fixed
- Possibly breaking change (UniqueID may change for some users)

Mon Aug 17 01:26:21 2020 -0600 v1.5.0

- Added Memory Reporting
Expand Down
94 changes: 82 additions & 12 deletions ISP-RPi-mqtt-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)

script_version = "1.5.0"
script_version = "1.5.1"
script_name = 'ISP-RPi-mqtt-daemon.py'
script_info = '{} v{}'.format(script_name, script_version)
project_name = 'RPi Reporter MQTT2HA Daemon'
Expand Down Expand Up @@ -400,10 +400,40 @@ def getUptime():
rpi_uptime = rpi_uptime_raw.replace(timeStamp, '').lstrip().replace('up ', '')
print_line('rpi_uptime=[{}]'.format(rpi_uptime), debug=True)

def getNetworkIFs():
global rpi_interfaces
global rpi_mac
out = subprocess.Popen('/sbin/ifconfig | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"',
def getNetworkIFsUsingIP(ip_cmd):
cmd_str = '{} link show | /bin/egrep -v "noque|link"'.format(ip_cmd)
out = subprocess.Popen(cmd_str,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,_ = out.communicate()
lines = stdout.decode('utf-8').split("\n")
interfaceNames = []
line_count = len(lines)
if line_count > 2:
line_count = 2;
if line_count == 0:
print_line('ERROR no lines left by ip(8) filter!',error=True)
sys.exit(1)

for lineIdx in range(line_count):
trimmedLine = lines[lineIdx].lstrip().rstrip()
lineParts = trimmedLine.split()
interfaceName = lineParts[1].replace(':', '')
interfaceNames.append(interfaceName)
print_line('interfaceNames=[{}]'.format(interfaceNames), debug=True)

trimmedLines = []
for interface in interfaceNames:
lines = getSingleInterfaceDetails(interface)
for currLine in lines:
trimmedLines.append(currLine)

loadNetworkIFDetailsFromLines(trimmedLines)

def getSingleInterfaceDetails(interfaceName):
cmdString = '/sbin/ifconfig {} | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"'.format(interfaceName)
out = subprocess.Popen(cmdString,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
Expand All @@ -415,8 +445,13 @@ def getNetworkIFs():
if len(trimmedLine) > 0:
trimmedLines.append(trimmedLine)

print_line('trimmedLines=[{}]'.format(trimmedLines), debug=True)
#
#print_line('interface:[{}] trimmedLines=[{}]'.format(interfaceName, trimmedLines), debug=True)
return trimmedLines

def loadNetworkIFDetailsFromLines(ifConfigLines):
global rpi_interfaces
global rpi_mac
#
# OLDER SYSTEMS
# eth0 Link encap:Ethernet HWaddr b8:27:eb:c8:81:f2
# inet addr:192.168.100.41 Bcast:192.168.100.255 Mask:255.255.255.0
Expand All @@ -433,10 +468,10 @@ def getNetworkIFs():
haveIF = False
imterfc = ''
rpi_mac = ''
for currLine in trimmedLines:
for currLine in ifConfigLines:
lineParts = currLine.split()
print_line('- currLine=[{}]'.format(currLine), debug=True)
print_line('- lineParts=[{}]'.format(lineParts), debug=True)
#print_line('- currLine=[{}]'.format(currLine), debug=True)
#print_line('- lineParts=[{}]'.format(lineParts), debug=True)
if len(lineParts) > 0:
# skip interfaces generated by Home Assistant on RPi
if 'docker' in currLine or 'veth' in currLine or 'hassio' in currLine:
Expand All @@ -446,14 +481,14 @@ def getNetworkIFs():
if 'flags' in currLine: # NEWER ONLY
haveIF = True
imterfc = lineParts[0].replace(':', '')
print_line('newIF=[{}]'.format(imterfc), debug=True)
#print_line('newIF=[{}]'.format(imterfc), debug=True)
elif 'Link' in currLine: # OLDER ONLY
haveIF = True
imterfc = lineParts[0].replace(':', '')
newTuple = (imterfc, 'mac', lineParts[4])
if rpi_mac == '':
rpi_mac = lineParts[4]
print_line('newIF=[{}]'.format(imterfc), debug=True)
print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True)
tmpInterfaces.append(newTuple)
print_line('newTuple=[{}]'.format(newTuple), debug=True)
elif haveIF == True:
Expand All @@ -467,13 +502,36 @@ def getNetworkIFs():
tmpInterfaces.append(newTuple)
if rpi_mac == '':
rpi_mac = lineParts[1]
print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True)
print_line('newTuple=[{}]'.format(newTuple), debug=True)
haveIF = False

rpi_interfaces = tmpInterfaces
print_line('rpi_interfaces=[{}]'.format(rpi_interfaces), debug=True)
print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True)

def getNetworkIFs():
ip_cmd = getIPCmd()
if ip_cmd != '':
getNetworkIFsUsingIP(ip_cmd)
else:
out = subprocess.Popen('/sbin/ifconfig | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,_ = out.communicate()
lines = stdout.decode('utf-8').split("\n")
trimmedLines = []
for currLine in lines:
trimmedLine = currLine.lstrip().rstrip()
if len(trimmedLine) > 0:
trimmedLines.append(trimmedLine)

print_line('trimmedLines=[{}]'.format(trimmedLines), debug=True)

loadNetworkIFDetailsFromLines(trimmedLines)


def getFileSystemDrives():
global rpi_filesystem_space_raw
global rpi_filesystem_space
Expand Down Expand Up @@ -580,6 +638,18 @@ def getVcGenCmd():
desiredCommand = cmd_locn2
return desiredCommand

def getIPCmd():
cmd_locn1 = '/bin/ip'
cmd_locn2 = '/sbin/ip'
desiredCommand = ''
if os.path.exists(cmd_locn1) == True:
desiredCommand = cmd_locn1
elif os.path.exists(cmd_locn2) == True:
desiredCommand = cmd_locn2
if desiredCommand != '':
print_line('Found IP(8)=[{}]'.format(desiredCommand), debug=True)
return desiredCommand

def getSystemTemperature():
global rpi_system_temp
global rpi_gpu_temp
Expand Down
4 changes: 2 additions & 2 deletions Release
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
v1.5.0 Latest
v1.4.4 Stable
v1.5.1 Latest
v1.5.0 Stable

0 comments on commit fadbe30

Please sign in to comment.