-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_monitor.py
128 lines (101 loc) · 3 KB
/
resource_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import time
import base64
import psutil
import requests
import wmi
from win32com.client import GetObject
import pythoncom
####################
# Settings
####################
# Server version (do not edit)
VERSION = '1.5.4'
URL = 'https://sm.hetrixtools.net'
####################
# Functions
####################
def get_win_version():
c = wmi.WMI()
for os_record in c.Win32_OperatingSystem():
win = os_record.Caption
sp = os_record.ServicePackMajorVersion
win = win.replace('Microsoft ','')
if sp and sp > 0:
win += 'SP'+str(sp)
return win
def get_cpu_type():
root_winmgmts = GetObject("winmgmts:root\cimv2")
cpus = root_winmgmts.ExecQuery("Select * from Win32_Processor")
return cpus[0].Name
def gather_data(server_id):
pythoncom.CoInitialize()
# Initial Network Stats
net_stats1 = psutil.net_io_counters()
# Time
sec = int(time.strftime("%S"))
sleep = 60 - sec
# CPU Usage
cpu_usage = psutil.cpu_percent(interval=sleep)
# Secondary Network Stats
net_stats2 = psutil.net_io_counters()
# OS
os_name = base64.b64encode(get_win_version())
# Uptime
uptime = int(time.time() - psutil.boot_time())
# CPU Model
cpu_model = base64.b64encode(get_cpu_type())
# CPU Speed
cpu_speed = int(psutil.cpu_freq().current)
# CPU Cores
cpu_cores = psutil.cpu_count()
# IOWait
cpu_iowait = 0
# RAM Info
ram_info = psutil.virtual_memory()
# RAM Size
ram_size = int(ram_info.total/1000)
# RAM Usage
ram_usage = ram_info.percent
# Swap Info
swap_info = psutil.swap_memory()
# Swap Size
swap_size = int(swap_info.total/1000)
# Swap Usage
swap_usage = swap_info.percent
# Disks
disks = psutil.disk_partitions()
all_disk_data = []
for disk in disks:
if disk.fstype:
try:
disk_usage = psutil.disk_usage(disk.mountpoint)
disk_data = ','.join([disk.mountpoint, str(disk_usage.total), str(disk_usage.used)])
all_disk_data.append(disk_data)
except:
pass
disk_str = '{};'.format(';'.join(all_disk_data))
disks = base64.b64encode(disk_str)
# Network
rx = int((net_stats2.bytes_recv - net_stats1.bytes_recv)/sleep)
tx = int((net_stats2.bytes_sent - net_stats1.bytes_sent)/sleep)
# Post Data
data = [
os_name,
str(uptime),
cpu_model,
str(cpu_speed),
str(cpu_cores),
str(cpu_usage),
str(cpu_iowait),
str(ram_size),
str(ram_usage),
str(swap_size),
str(swap_usage),
disks,
str(rx),
str(tx)
]
post_data = '{}|'.format('|'.join(data))
payload = {'v': VERSION, 'a': '2', 's': server_id, 'd': post_data}
# Post the collected data
requests.post(URL, data=payload, timeout=15, verify=False)