def test2(processName): # getProcessObject processObject = getProcessObject(processName) if processObject: loggerDEBUG(f'A process with name {processName} was running') pp(processObject) else: loggerDEBUG(f'No process with name {processName} was found')
def isInterfaceUp(interface): with open(co.INTERFACES[interface][0], "r") as f: result = f.read() if "up" in result: loggerDEBUG(f"{interface} is up") return True else: loggerDEBUG(f"{interface} is NOT up") return False
def runShellCommand(command): try: completed = subprocess.run(command.split(), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) loggerDEBUG( f'shell command {command} - returncode: {completed.returncode}') return completed.returncode except: loggerERROR(f"error on shell command: {command}") return False
def start(self): loggerDEBUG(f"eth0 and wlan0 are both down" + \ "- RAS is NOT connected") if not self.process: self.process = Process( name="wifi-connect", \ target=launcher, \ args=("common.wificonnect",) ) loggerDEBUG(f"starting wifi-connect {self.process}") if not self.process.is_alive(): self.process.start()
def launcher(process): try: # import the process module = importlib.import_module(process) # rename the process loggerDEBUG(f"starting the process with the name {process}") setproctitle(process) # create new context since we forked #messaging.context = messaging.Context() # exec the process module.main() except KeyboardInterrupt: pass #cloudlog.warning("child %s got SIGINT" % proc) except Exception: #crash.capture_exception() raise
def manager_thread(): ras_subscriber = Subscriber("5556") ras_subscriber.subscribe("thermal") log_begin_manager_thread() start_all_daemon_processes() start_all_managed_processes() #thermal = ThermalStatus() # instance of class ThermalStatus while 1: # get thermal status topic, message = ras_subscriber.receive() # BLOCKING #loggerDEBUG(f"received {topic} {message}") if topic == "thermal": counter, temperature, load_5min, memUsed = \ message.split() loggerDEBUG(f"thermal update nr.{counter}: T {temperature}°C," + \ f" CPU load 5 min avg {load_5min}%, mem used {memUsed}%") if False: #thermal.isCritical() terminate_non_essential_managed_processes() else: start_all_managed_processes() log_running_processes_list() time.sleep(co.PERIOD_MAIN_THREAD)
def main(): pub_thermal = Publisher("5556") cpu_count = psutil.cpu_count() counter = 0 while True: # msg.thermal.freeSpace = get_available_percent(default=100.0) / 100.0 # msg.thermal.memUsedPercent = int(round(psutil.virtual_memory().percent)) # msg.thermal.cpuPerc = int(round(psutil.cpu_percent())) #msg.thermal.freeSpace = get_available_percent(default=100.0) / 100.0 memUsedPercent = int(round(psutil.virtual_memory().percent)) cpuPerc = int(round(psutil.cpu_percent())) loadAvg = psutil.getloadavg() temperatures = psutil.sensors_temperatures() temperatureCurrent = int( psutil.sensors_temperatures()['cpu_thermal'][0].current) loadAvgPerc = [int(round(l * 100 / cpu_count)) for l in loadAvg] loggerDEBUG(f"memUsedPercent {memUsedPercent}%") loggerDEBUG(f"cpuPerc {cpuPerc}%") loggerDEBUG( f"loadAvgPerc 1min:{loadAvgPerc[0]}% - 5min:{loadAvgPerc[1]}% - 15min:{loadAvgPerc[2]}%" ) loggerDEBUG(f"current temperature {temperatureCurrent}°C") message = f"{counter} {temperatureCurrent} {loadAvgPerc[1]} {memUsedPercent}" pub_thermal.publish("thermal", message) # temperature max CPU RPi 85°C - Yellow > 80°C - Red > 84°C (self defined limits) counter += 1 time.sleep(co.PERIOD_THERMAL_MANAGER)
def test1(processName): # isProcessRunning if isProcessRunning(processName): loggerDEBUG(f'A process with name {processName} was running') else: loggerDEBUG(f'No process with name {processName} was running')
def killProcess(process): # process is an instance of psutil.Process() loggerDEBUG(f"killing PID {process.pid}") process.kill()
def terminateProcess(process): # process is an instance of psutil.Process() loggerDEBUG(f"terminating PID {process.pid}") try: process.terminate() except Exception as e: loggerERROR(f"exception {e} while terminating PID {process.pid}")
def main(): loggerDEBUG("launching wifi-connect") response = runShellCommand("sudo wifi-connect -s "+ co.SSID_WIFICONNECT) loggerDEBUG(f"wifi-connect with response: {response}")
def logMessages(): loggerDEBUG("Internet (1.1.1.1) can be reached " + \ "and wifi-connect is still running." + \ f" Terminating wifi-connect {self.process}") loggerDEBUG( f"wifi-connect Process PID is {self.process.pid}")
def subscribe(self, topic): loggerDEBUG(f"SUBSCRIPTION TO TOPIC: {topic}") self.socket.setsockopt(zmq.SUBSCRIBE, str(topic).encode('utf-8'))
def handleEth0NotWorking(): loggerDEBUG( \ f"eth0 is up but internet (1.1.1.1) can not be reached-"+ \ " Check the Ethernet Internet Provider. RAS is NOT connected")
def receive(self): topic_bytes, message_bytes = self.socket.recv_multipart() # this call is blocking topic = topic_bytes.decode('utf-8') message = pickle.loads(message_bytes) loggerDEBUG(f"received TOPIC: {topic}, MESSAGE: {message}") return topic, message
def log_running_processes_list(): #running_list = ["%s%s\u001b[0m" % ("\u001b[32m" if running[p].is_alive() else "\u001b[31m", p) for p in running] running_alive = [p for p in running if running[p].is_alive()] running_dead = [p for p in running if p not in running_alive] loggerDEBUG("alive: " + cf.GREEN + ' ; '.join(running_alive) + cf.RESET) loggerDEBUG("dead: " + cf.RED + ' ; '.join(running_dead) + cf.RESET)
def publish(self, topic, message): loggerDEBUG(f"published TOPIC: {topic}, MESSAGE: {message}") topic_bytes = str(topic).encode('utf-8') message_bytes = pickle.dumps(message) self.socket.send_multipart([topic_bytes, message_bytes])