sys.stdout = open("logs.txt", "w")
# Faktor für Bandbreiten kontrollierenif args.interval >= 2:
args.interval -= 2def main():
dt_string = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
print("Zeitstempel:\n\t", dt_string)
# Hostname Info hostname = socket.gethostname()
print("Hostname:", hostname)
# CPU Info cpu_count = psutil.cpu_count()
cpu_usage = psutil.cpu_percent(interval=1)
print("CPU:\n\tAnzahl:", cpu_count, "\n\tVerwendet:", cpu_usage)
# RAM Info memory_stats = psutil.virtual_memory()
memory_total = memory_stats.total
memory_used = memory_stats.used
memory_used_percent = memory_stats.percent
print("RAM:\n\tProzent:", memory_used_percent, "\n\tGesamt:", memory_total / 1e+6, "MB", "\n\tVerwendet:", memory_used / 1e+6, "MB")
# Festplatten Info disk_info = psutil.disk_partitions()
print("Festplatte:")
disks = []
for x in disk_info:
try:
disk = {
"name" : x.device, "mount_point" : x.mountpoint, "type" : x.fstype, "total_size" : psutil.disk_usage(x.mountpoint).total, "used_size" : psutil.disk_usage(x.mountpoint).used, "percent_used" : psutil.disk_usage(x.mountpoint).percent
}
disks.append(disk)
print("\tFestplatten Name",disk["name"], "\tMount Point:", disk["mount_point"], "\tTyp",disk["type"], "\tGröße:", disk["total_size"] / 1e+9,"\tVerwendet:", disk["used_size"] / 1e+9, "\t% Verwendet:", disk["percent_used"])
except:
print("")
# Bandbreiten Info network_stats = get_bandwidth()
print("Netzwerk:\n\tTraffic in:",network_stats["traffic_in"] / 1e+6,"\n\tTraffic out:",network_stats["traffic_out"] / 1e+6)
# Netzwerk Infos nics = []
print("NICs:")
for name, snic_array in psutil.net_if_addrs().items():
# NIC Objekt erstellen nic = {
"name": name, "mac": "", "addresse": "", "addresse6": "", "netzmask": "" }
# NIC Werte for snic in snic_array:
if snic.family == -1:
nic["mac"] = snic.address
elif snic.family == 2:
nic["addresse"] = snic.address
nic["netzmask"] = snic.netmask
elif snic.family == 23:
nic["addresse6"] = snic.address
nics.append(nic)
print("\tNIC:",nic["name"], "\tMAC:", nic["mac"], "\tIPv4 Addresse:",nic["addresse"], "\tIPv4 Subnet:", nic["netzmask"], "\tIPv6 Addresse:", nic["addresse6"])
# OS Info system = {
"name" : platform.system(), "version" : platform.release()
}
print("OS:\n\t",system["name"],system["version"])
# Zeitstempel timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
uptime = int(time.time() - psutil.boot_time())
print("System Uptime:\n\t",uptime)
# System UUID sys_uuid = uuid.getnode()
def get_bandwidth():
# netzwerk net1_out = psutil.net_io_counters().bytes_sent
net1_in = psutil.net_io_counters().bytes_recv
time.sleep(1)
# net2_out = psutil.net_io_counters().bytes_sent
net2_in = psutil.net_io_counters().bytes_recv
# Ausrechnung der derzeitigen Geschwindigkeit if net1_in > net2_in:
current_in = 0 else:
current_in = net2_in - net1_in
if net1_out > net2_out:
current_out = 0 else:
current_out = net2_out - net1_out
network = {"traffic_in" : current_in, "traffic_out" : current_out}
return network
def email_alert():
msg = EmailMessage()
msg.set_content(body)
msg["From"] = 'monitoring@xiii.local' msg["Subject"] = 'Logs' msg["To"] = 'monitoring@xiii.local' msg.set_content("Logs versendet")
msg.add_attachment(open(filename, "r").read(), filename="logs.txt")
s = smtplib.SMTP('smtp.xiii.local', 25)
s.login(monitoring@xiii.local, Admin123)
s.send_message(msg)
while True:
main()
print("-----------------------------------------------------------------")
time.sleep(args.interval)
sys.stdout.close()