142 lines
4.4 KiB
Python
142 lines
4.4 KiB
Python
import datetime
|
|
import subprocess
|
|
import socket
|
|
import os
|
|
import psutil
|
|
import math
|
|
import cpuinfo
|
|
from multiprocessing import freeze_support
|
|
import requests
|
|
import platform
|
|
import wmi
|
|
import win32serviceutil
|
|
|
|
def request(function,variable):
|
|
url = (f"http://api.stines.de:8001/{function}")
|
|
response = requests.post(url,json = variable,headers={'access_token':'^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8'})
|
|
return response
|
|
|
|
def getdabase():
|
|
return request("getdatabase",{'ipadress':get_local_ip()})
|
|
|
|
def server_exist(hostname):
|
|
return request("info/getserver",{'name': hostname, 'dbase': getdabase()})
|
|
|
|
def get_cpu_brand():
|
|
freeze_support()
|
|
return cpuinfo.get_cpu_info()['brand_raw']
|
|
|
|
def splacores(cpu):
|
|
freeze_support()
|
|
requests = request("cpu_info",{'name':cpu})
|
|
return requests.json()[0][0]
|
|
|
|
def get_cpu_core():
|
|
freeze_support()
|
|
return os.cpu_count()
|
|
|
|
def get_ram_info():
|
|
freeze_support()
|
|
ram_info = psutil.virtual_memory()
|
|
total_ram = ram_info.total / (1024 ** 3) # In Gigabytes
|
|
return math.ceil(total_ram)
|
|
|
|
def get_total_hdd(function):
|
|
freeze_support()
|
|
space = 0
|
|
disk_partitions = psutil.disk_partitions()
|
|
for partition in disk_partitions:
|
|
partition_info = psutil.disk_usage(partition.mountpoint)
|
|
if function == "free":
|
|
space += round(partition_info.free / (1024 ** 3), 2)
|
|
elif function == "used":
|
|
space += round(partition_info.used / (1024 ** 3), 2)
|
|
elif function == "total":
|
|
space += round(partition_info.total / (1024 ** 3), 2)
|
|
return math.ceil(space)
|
|
|
|
def get_winver():
|
|
freeze_support()
|
|
return platform.platform()
|
|
|
|
|
|
def get_total_hdd(function):
|
|
freeze_support()
|
|
space = 0
|
|
disk_partitions = psutil.disk_partitions()
|
|
for partition in disk_partitions:
|
|
partition_info = psutil.disk_usage(partition.mountpoint)
|
|
if function == "free":
|
|
space += round(partition_info.free / (1024 ** 3), 2)
|
|
elif function == "used":
|
|
space += round(partition_info.used / (1024 ** 3), 2)
|
|
elif function == "total":
|
|
space += round(partition_info.total / (1024 ** 3), 2)
|
|
return math.ceil(space)
|
|
|
|
|
|
def get_local_ip():
|
|
try:
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
s.connect(("8.8.8.8", 80))
|
|
local_ip = s.getsockname()[0]
|
|
s.close()
|
|
return local_ip
|
|
except Exception as e:
|
|
print(f"Fehler beim Abrufen der lokalen IP-Adresse: {e}")
|
|
return None
|
|
|
|
def get_mainboard_info():
|
|
c = wmi.WMI()
|
|
for board in c.Win32_BaseBoard():
|
|
return board.Product
|
|
|
|
def lastboot():
|
|
lastboot = psutil.boot_time()
|
|
lastboot = datetime.datetime.fromtimestamp(lastboot).strftime("%d.%m.%Y %H:%M ")
|
|
return lastboot
|
|
|
|
|
|
def find_service():
|
|
services = ["MSExchangeTransport","NTDS","RDMS"]
|
|
service_len = len(services)
|
|
for index, i in enumerate(services):
|
|
try:
|
|
services = win32serviceutil.QueryServiceStatus(i)
|
|
return True
|
|
except:
|
|
if index == service_len - 1:
|
|
return "Software"
|
|
else:
|
|
continue
|
|
|
|
def get_windows_updates():
|
|
c = wmi.WMI()
|
|
updates = c.Win32_QuickFixEngineering()
|
|
update_string = []
|
|
for update in updates:
|
|
update_string.append(f"KB-Artikel: {update.HotFixID} - Beschreibung: {update.Description}")
|
|
return update_string
|
|
|
|
## GET HOSTNAME
|
|
hostname = socket.gethostname()
|
|
|
|
## GET CURRENT DATE
|
|
date = datetime.datetime.now()
|
|
|
|
## GET CPU BRAND (only 1 run)
|
|
cpu = get_cpu_brand()
|
|
|
|
## GET VOLUME SPACE
|
|
spacefree = str(get_total_hdd("free")).replace("['","").replace("]","").replace("'","")
|
|
spaceused = str(get_total_hdd("used")).replace("['","").replace("]","").replace("'","")
|
|
spacetotal = str(get_total_hdd("total")).replace("['","").replace("]","").replace("'","")
|
|
|
|
|
|
request('systeminfo_add', {'createdate':date,'updatedate': date, 'lastboot': lastboot(), 'hostname': hostname,
|
|
'ipaddress': get_local_ip(), 'winver': get_winver(), 'services': find_service(),
|
|
'mainboard': get_mainboard_info(), 'cpu': splacores(cpu), 'realcores': get_cpu_core(),
|
|
'splacores': splacores(cpu), 'ram': str(get_ram_info())
|
|
, 'spacetotal': spacetotal, 'spaceused': spaceused,
|
|
'spacefree': spacefree, 'winupdate': get_windows_updates()})
|