1- import socket , psutil
2-
3- # Hostname Info
4- hostname = socket .gethostname ()
5- print ("Hostname" , hostname )
6-
7- # CPU Info
8- cpu_count = psutil .cpu_count ()
9- cpu_usage = psutil .cpu_percent (interval = 1 )
10- print ("CPU:" )
11- print ("Count" , cpu_count , "Usage" , cpu_usage )
12- memory_stats = psutil .virtual_memory ()
13-
14- # Memory Info
15- memory_total = memory_stats .total / 1e+6
16- memory_used = memory_stats .used / 1e+6
17- memory_used_percent = memory_stats .percent
18- print ("Memory:" )
19- print ("Percent:" , memory_used_percent , "\t Total:" , "%.2f" % memory_total , "MB" , "\t Used:" , "%.2f" % memory_used , "MB" )
20-
21- # Disk Info
22- disk_info = psutil .disk_partitions ()
23- print ("Disks:" )
24- for disk in disk_info :
25- print ("Disk name" ,disk .device ,"\t Mount Point:" ,disk .mountpoint ,"\t Type" ,disk .fstype ,"\t Size:" ,psutil .disk_usage (disk .mountpoint ).total ,"\t Usage:" ,psutil .disk_usage (disk .mountpoint ))
1+ import socket , psutil , requests , time , json , platform
2+
3+ def main ():
4+ # Hostname Info
5+ hostname = socket .gethostname ()
6+ print ("Hostname:" , hostname )
7+
8+ # CPU Info
9+ cpu_count = psutil .cpu_count ()
10+ cpu_usage = psutil .cpu_percent (interval = 1 )
11+ print ("CPU:" )
12+ print ("Count:" , cpu_count , "Usage:" , cpu_usage )
13+
14+ # Memory Info
15+ memory_stats = psutil .virtual_memory ()
16+ memory_total = memory_stats .total
17+ memory_used = memory_stats .used
18+ memory_used_percent = memory_stats .percent
19+ print ("Memory:" )
20+ print ("Percent:" , memory_used_percent , "\t Total:" , memory_total / 1e+6 , "MB" , "\t Used:" , memory_used / 1e+6 , "MB" )
21+
22+ # Disk Info
23+ disk_info = psutil .disk_partitions ()
24+ print ("Disks:" )
25+ disks = []
26+ for x in disk_info :
27+ disk = {
28+ "name" : x .device ,
29+ "mount_point" : x .mountpoint ,
30+ "type" : x .fstype ,
31+ "total_size" : psutil .disk_usage (x .mountpoint ).total ,
32+ "used_size" : psutil .disk_usage (x .mountpoint ).used ,
33+ "percent_used" : psutil .disk_usage (x .mountpoint ).percent
34+ }
35+
36+ disks .append (disk )
37+
38+ print ("Disk name" ,disk ["name" ], "\t Mount Point:" , disk ["mount_point" ], "\t Type" ,disk ["type" ], "\t Size:" , disk ["total_size" ] / 1e+9 ,"\t Usage:" , disk ["used_size" ] / 1e+9 , "\t Percent Used:" , disk ["percent_used" ])
39+
40+ # Network Info
41+ print ("Network:" )
42+ network_stats = get_bandwidth ()
43+ print ("Traffic in:" ,network_stats ["traffic_in" ] / 1e+6 ,"\t Traffic out:" ,network_stats ["traffic_out" ] / 1e+6 )
44+
45+ # Platform Info
46+ print ("OS:" )
47+ system = {
48+ "name" : platform .system (),
49+ "version" : platform .release ()
50+ }
51+ print (system ["name" ],system ["version" ])
52+
53+
54+ ## Set Machine Info
55+ machine_info = {
56+ "hostname" : hostname ,
57+ "system" : system ,
58+ "cpu_count" : cpu_count ,
59+ "cpu_usage" : cpu_usage ,
60+ "memory_total" : memory_total ,
61+ "memory_used" : memory_used ,
62+ "memory_used_percent" : memory_used_percent ,
63+ "drives" : disks ,
64+ "network_up" : network_stats ["traffic_out" ],
65+ "network_down" : network_stats ["traffic_in" ]
66+ }
67+
68+ data = json .dumps (machine_info )
69+ post_data (data )
70+
71+ def get_bandwidth ():
72+ # Get net in/out
73+ net1_out = psutil .net_io_counters ().bytes_sent
74+ net1_in = psutil .net_io_counters ().bytes_recv
75+
76+ time .sleep (1 )
77+
78+ # Get new net in/out
79+ net2_out = psutil .net_io_counters ().bytes_sent
80+ net2_in = psutil .net_io_counters ().bytes_recv
81+
82+ # Compare and get current speed
83+ if net1_in > net2_in :
84+ current_in = 0
85+ else :
86+ current_in = net2_in - net1_in
87+
88+ if net1_out > net2_out :
89+ current_out = 0
90+ else :
91+ current_out = net2_out - net1_out
92+
93+ network = {"traffic_in" : current_in , "traffic_out" : current_out }
94+ return network
95+
96+ def post_data (data ):
97+ ## POST data
98+ try :
99+ endpoint = "https://your-monitoring-server.com"
100+ r = requests .post (url = endpoint , data = data )
101+ print (r )
102+ except requests .exceptions .RequestException as e :
103+ print ("\n POST Error:\n " ,e )
104+
105+ while True :
106+ main ()
107+ print ("-----------------------------------------------------------------" )
108+ time .sleep (5 )
0 commit comments