#!/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) MOSQUITTO_IP = "192.168.0.1" MOSQUITTO_PORT = 1883 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) apu_string = [ { "uptime":uptime, "cpu_temp":cpu_temp, "mem_util":mem_util, "cpu_util":cpu_util, "XPosition":XpubPos, "YPosition":YpubPos }, { "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();