1sys.stdout = open("logs.txt", "w") 2 3# Faktor für Bandbreiten kontrollierenif args.interval >= 2: 4 args.interval -= 2def main(): 5 6 7 dt_string = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") 8 print("Zeitstempel:\n\t", dt_string) 9 10 11 # Hostname Info hostname = socket.gethostname() 12 print("Hostname:", hostname) 13 14 # CPU Info cpu_count = psutil.cpu_count() 15 cpu_usage = psutil.cpu_percent(interval=1) 16 print("CPU:\n\tAnzahl:", cpu_count, "\n\tVerwendet:", cpu_usage) 17 18 # RAM Info memory_stats = psutil.virtual_memory() 19 memory_total = memory_stats.total 20 memory_used = memory_stats.used 21 memory_used_percent = memory_stats.percent 22 print("RAM:\n\tProzent:", memory_used_percent, "\n\tGesamt:", memory_total / 1e+6, "MB", "\n\tVerwendet:", memory_used / 1e+6, "MB") 23 24 # Festplatten Info disk_info = psutil.disk_partitions() 25 print("Festplatte:") 26 disks = [] 27 for x in disk_info: 28 29 try: 30 disk = { 31 "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 32 } 33 34 disks.append(disk) 35 36 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"]) 37 except: 38 print("") 39 40 # Bandbreiten Info network_stats = get_bandwidth() 41 print("Netzwerk:\n\tTraffic in:",network_stats["traffic_in"] / 1e+6,"\n\tTraffic out:",network_stats["traffic_out"] / 1e+6) 42 43 # Netzwerk Infos nics = [] 44 print("NICs:") 45 for name, snic_array in psutil.net_if_addrs().items(): 46 # NIC Objekt erstellen nic = { 47 "name": name, "mac": "", "addresse": "", "addresse6": "", "netzmask": "" } 48 # NIC Werte for snic in snic_array: 49 if snic.family == -1: 50 nic["mac"] = snic.address 51 elif snic.family == 2: 52 nic["addresse"] = snic.address 53 nic["netzmask"] = snic.netmask 54 elif snic.family == 23: 55 nic["addresse6"] = snic.address 56 nics.append(nic) 57 print("\tNIC:",nic["name"], "\tMAC:", nic["mac"], "\tIPv4 Addresse:",nic["addresse"], "\tIPv4 Subnet:", nic["netzmask"], "\tIPv6 Addresse:", nic["addresse6"]) 58 59 # OS Info system = { 60 "name" : platform.system(), "version" : platform.release() 61 } 62 print("OS:\n\t",system["name"],system["version"]) 63 64 # Zeitstempel timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00") 65 uptime = int(time.time() - psutil.boot_time()) 66 print("System Uptime:\n\t",uptime) 67 68 # System UUID sys_uuid = uuid.getnode() 69 70 71 72 73 74def get_bandwidth(): 75 # netzwerk net1_out = psutil.net_io_counters().bytes_sent 76 net1_in = psutil.net_io_counters().bytes_recv 77 78 time.sleep(1) 79 80 # net2_out = psutil.net_io_counters().bytes_sent 81 net2_in = psutil.net_io_counters().bytes_recv 82 83 # Ausrechnung der derzeitigen Geschwindigkeit if net1_in > net2_in: 84 current_in = 0 else: 85 current_in = net2_in - net1_in 86 87 if net1_out > net2_out: 88 current_out = 0 else: 89 current_out = net2_out - net1_out 90 91 network = {"traffic_in" : current_in, "traffic_out" : current_out} 92 return network 93 94def email_alert(): 95 msg = EmailMessage() 96 msg.set_content(body) 97 98 msg["From"] = 'monitoring@xiii.local' msg["Subject"] = 'Logs' msg["To"] = 'monitoring@xiii.local' msg.set_content("Logs versendet") 99 msg.add_attachment(open(filename, "r").read(), filename="logs.txt") 100 s = smtplib.SMTP('smtp.xiii.local', 25) 101 s.login(monitoring@xiii.local, Admin123) 102 s.send_message(msg) 103 104while True: 105 main() 106 print("-----------------------------------------------------------------") 107 time.sleep(args.interval) 108sys.stdout.close()