#!/usr/bin/env python3 import re import time import subprocess import json import paho.mqtt.client as mqtt XmeshShift=0 YmeshShift=0 XpubPos=-2 YpubPos=-2 mem_util=-1 cpu_util=-1 cpu_temp=-1 hostnumber=-1 def host2pos(hostname): Xpos=-2 Ypos=-2 hostnumber=int(hostname.replace('apu', '')) #maybe insert check here! if(hostnumber >= 50 and hostnumber < 55): Xpos=0 Ypos=hostnumber-50 if(hostnumber >= 0 and hostnumber < 25 ): Xpos=(hostnumber%5)+XmeshShift Ypos=(hostnumber/5)+YmeshShift if(hostnumber >= 60 and hostnumber < 85): #error1:exists but no in the testbed Xpos=-1 Ypos=-1 if(hostnumber >= 85): #error2:do not exist Xpos=-2 Ypos=-2 return hostnumber, int(Xpos), int(Ypos) def get_established_neighbors(interface): """ Extracts MAC addresses of established (`ESTAB`) mesh links for a given interface. """ try: # output = subprocess.check_output(f"iw dev {interface} station dump", shell=True, encoding='utf-8') # Run the command to get mesh station information result = subprocess.run(['iw', 'dev', interface, 'station', 'dump'], capture_output=True, text=True) output = result.stdout # Regex patterns mac_pattern = re.compile(r"^Station ([0-9A-Fa-f:]+)") plink_pattern = re.compile(r"^\s*mesh plink:\s*(\w+)") established_links = [] current_mac = None for line in output.split('\n'): # Match MAC addresses mac_match = mac_pattern.match(line) if mac_match: current_mac = mac_match.group(1) # Store MAC address # Check if the link is ESTAB (print debug info) plink_match = plink_pattern.match(line) if plink_match and current_mac: plink_state = plink_match.group(1) if plink_state == "ESTAB": established_links.append(current_mac) current_mac = None # Reset for next station return established_links except subprocess.CalledProcessError as e: print(f"Error executing iw dev {interface} station dump: {e}") return [] # MQTT Settings MOSQUITTO_IP = "192.168.0.1" MOSQUITTO_PORT = 1883 # Get hostname hostname = str(subprocess.check_output("hostname", shell=True), encoding='utf-8') hostname_clean = re.sub(r"\s+", "", hostname) # uptime in seconds uptime_cmd = "awk '{print $1}' /proc/uptime" uptime = int(float(re.sub(r"\s+", "", str(subprocess.check_output(uptime_cmd, shell=True), encoding='utf-8')))) #print(type(uptime)) #print(uptime) # core temperature cpu_temp_cmd = "cat /sys/class/thermal/thermal_zone0/temp" cpu_temp = int(int(subprocess.check_output(cpu_temp_cmd,shell=True))/1000) # utilizations mem_util = int(re.sub(r"\s+", "", str(subprocess.check_output("bash /opt/scripts/mem_util.sh",shell=True), encoding='utf-8'))) cpu_util = int(float(re.sub(r"\s+", "", str(subprocess.check_output("bash /opt/scripts/cpu_util.sh",shell=True), encoding='utf-8')))) # positions #hostname_clean="apu10" #debug hostnumber, XpubPos, YpubPos = host2pos(hostname_clean) # Get mesh neighbors for each interface print(get_established_neighbors("mesh0")) print(get_established_neighbors("mesh1")) mesh0_neighbors = get_established_neighbors("mesh0") mesh1_neighbors = get_established_neighbors("mesh1") apu_string = [ { "uptime":uptime, "cpu_temp":cpu_temp, "mem_util":mem_util, "cpu_util":cpu_util, "XPosition":XpubPos, "YPosition":YpubPos, "mesh0_neighbors": mesh0_neighbors, "mesh1_neighbors": mesh1_neighbors }, { "id":hostname_clean } ] apu_json=json.dumps(apu_string) client = mqtt.Client() client.connect(MOSQUITTO_IP,MOSQUITTO_PORT,60) client.publish("apu_tb/" + hostname_clean, apu_json); client.disconnect();