diff --git a/functions/ipaddress.py b/functions/ipaddress.py new file mode 100644 index 0000000..18e7582 --- /dev/null +++ b/functions/ipaddress.py @@ -0,0 +1,15 @@ +import socket + +def get_local_ip(): + try: + # Ein temporärer Socket erstellen, um die lokale IP-Adresse zu ermitteln + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + # Verbindung zu einem öffentlichen DNS-Server herstellen + s.connect(("8.8.8.8", 80)) + # Die lokale IP-Adresse aus dem Socket abrufen + local_ip = s.getsockname()[0] + s.close() + return local_ip + except Exception as e: + print(f"Fehler beim Ermitteln der lokalen IP-Adresse: {e}") + return None diff --git a/main.py b/main.py index 1f97585..007306c 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,18 @@ +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 = "" +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"): @@ -17,23 +21,54 @@ def restart_service(): else: subprocess.run('sc', 'start', 'Agents') -def get_response(endpoint): - respone = requests.get(f"{api_server}{endpoint}",headers=headers) - return respone.text +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): - respone = requests.post(f"{api_server}{endpoint}",headers=headers, json=data) - return respone.text + 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}) -if update_check.check_version() == True: - restart_service() - post_response("update_checked",update_check.current_version) +def ping(): + post_response("ping",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'}) - -## run ping (10sec) -## run controller(1m) +def controller_systeminfo(): ## Check RAM ## Check CPU ## Check HDD -## run service (60m) \ No newline at end of file + 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() diff --git a/test_loop.py b/test_loop.py new file mode 100644 index 0000000..e33f347 --- /dev/null +++ b/test_loop.py @@ -0,0 +1,41 @@ +import time + + +def ping(): + print("Ping alle 30 Sekunden") + + +def ping1(): + print("Ping1 alle 30 Sekunden") + + +def ping2(): + print("Ping2 alle 60 Sekunden") + + +def ping3(): + print("Ping3 alle 3600 Sekunden") + + +def main_loop(): + tasks = [ + {"interval": 3, "last_run": time.time(), "functions": [ping, ping1]}, + {"interval": 6, "last_run": time.time(), "functions": [ping2]}, + {"interval": 36, "last_run": time.time(), "functions": [ping3]} + ] + + 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() \ No newline at end of file diff --git a/update_check.py b/update_check.py index 88d4c14..6f29102 100644 --- a/update_check.py +++ b/update_check.py @@ -4,8 +4,9 @@ import requests import git import shutil -current_version = [] -new_version = [] +def current_version(): + current_version = open("version", "r").read() + return current_version def get_latest_release(owner, repo): url = f"http://gitlab.stines.de/api/v1/repos/sebastian.serfling/Agents/releases/latest" @@ -14,23 +15,22 @@ def get_latest_release(owner, repo): release_info = response.json() return release_info['name'] -new_version = get_latest_release("","") -current_version = open("version", "r").read() -print(current_version) - -if current_version != new_version: - try: - git.Repo.clone_from("http://172.17.1.251/sebastian.serfling/Agents.git",f'../{get_latest_release("","")}') - except: - next - if os.path.isdir(f"../{new_version}"): +def check_version(): + new_version = get_latest_release("", "") + if current_version() != new_version: try: - shutil.move("../latest", f"../{current_version}") + git.Repo.clone_from("http://172.17.1.251/sebastian.serfling/Agents.git",f'../{get_latest_release("","")}') except: - print("folder not Found") - shutil.move(f"../{new_version}", "../latest") - - + next + if os.path.isdir(f"../{new_version}"): + try: + shutil.move("../latest", f"../{current_version}") + except: + print("folder not Found") + shutil.move(f"../{new_version}", "../latest") + return True + else: + return False