Agents/main.py

75 lines
2.2 KiB
Python

import time
import update_check
import os
import platform
import subprocess
import requests
import functions.ipaddress
api_server = "http://api.stines.de:8001/"
api_key = "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"
headers = {"Content-Type":"application/json",
"access_token":f"{api_key}"}
service_ID = []
def restart_service():
check_os = platform.system()
if check_os == ("Linux"):
os.popen("systemctl restart agents.services")
print("Yes Man!")
else:
subprocess.run('sc', 'start', 'Agents')
def get_response(endpoint,data):
response = requests.get(f"{api_server}{endpoint}",headers=headers, json=data)
print(response.text)
return response.text
def post_response(endpoint, data):
response = requests.post(f"{api_server}{endpoint}",headers=headers, json=data)
print(response.text)
return response.text
def check_update():
if update_check.check_version() == True:
restart_service()
post_response("update_checked",{"ipaddress":f'{functions.ipaddress.get_local_ip()}',"version": update_check.current_version})
def ping():
post_response("ping",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
def controller_systeminfo():
## Check RAM
## Check CPU
## Check HDD
print("controller.systeminfo()")
def services_run():
service_ID = get_response("get/service",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
print(service_ID)
def main_loop():
tasks = [
{"interval": 30, "last_run": time.time(), "functions": [ping,services_run]},
{"interval": 60, "last_run": time.time(), "functions": [controller_systeminfo]},
{"interval": 3600, "last_run": time.time(), "functions": [services_run]}
]
while True:
current_time = time.time()
for task in tasks:
if current_time - task["last_run"] >= task["interval"]:
for function in task["functions"]:
function()
task["last_run"] = current_time
# Eine kurze Pause, um die CPU nicht zu überlasten
time.sleep(1)
if __name__ == "__main__":
main_loop()