-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_monitor_gpu.py
149 lines (133 loc) · 4.38 KB
/
os_monitor_gpu.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import psutil, time, argparse, pynvml
from flask import Flask, jsonify, request
net_counter = psutil.net_io_counters(pernic=True)
for interface, stats in net_counter.items():
print(f"网卡:{interface} ,网卡信息:{stats}")
pynvml.nvmlInit()
def get_cpu(interval):
cpu = psutil.cpu_percent(interval)
return cpu
def get_mem():
mem = psutil.virtual_memory()
total = mem.total
available = mem.available
used = mem.used
percent = mem.percent
return total, available, used, percent
def get_swap():
swap = psutil.swap_memory()
total = swap.total
free = swap.free
used = swap.used
percent = swap.percent
return total, free, used, percent
def get_speed(nic):
s1 = psutil.net_io_counters(pernic=True)[nic]
time.sleep(1)
s2 = psutil.net_io_counters(pernic=True)[nic]
up = s2.bytes_sent - s1.bytes_sent
dn = s2.bytes_recv - s1.bytes_recv
sent = s1.bytes_sent
recv = s1.bytes_recv
return up, dn, sent, recv
def get_gpu(gpu):
handle = pynvml.nvmlDeviceGetHandleByIndex(gpu)
memory = pynvml.nvmlDeviceGetMemoryInfo(handle)
usage = pynvml.nvmlDeviceGetUtilizationRates(handle)
total = int(memory.total)
free = int(memory.free)
used = int(memory.used)
gpu_usage = float(usage.gpu)
mem_usage = used / total * 100
return total, free, used, gpu_usage, mem_usage
def get_disk(disk):
disk = psutil.disk_usage(disk)
total = disk.total
free = disk.free
used = disk.used
percent = disk.percent
return total, free, used, percent
app = Flask(__name__)
@app.route('/hardware', methods=["GET","POST"])
def hardware_status():
if request.method == "POST":
data = request.get_json()
interval = data['interval']
cpu = get_cpu(interval)
total_mem, available_mem, used_mem, mem_percent = get_mem()
total_swap, free_swap, used_swap, swap_percent = get_swap()
data_dict = {
"cpu": {
"usage": cpu
},
"mem": {
"total": total_mem,
"available": available_mem,
"used": used_mem,
"percent": mem_percent
},
"swap": {
"total": total_swap,
"free": free_swap,
"used": used_swap,
"percent": swap_percent
}
}
return jsonify(data_dict), 200
else:
dic = {"msg": "不支持的请求方式!"}
return jsonify(dic), 405
@app.route('/network', methods=["GET","POST"])
def network_status():
if request.method == "POST":
data = request.get_json()
nic = data['nic']
up, dn, sent, recv = get_speed(nic)
data_dict = {
"upload": up,
"download": dn,
"sent": sent,
"recv": recv
}
return jsonify(data_dict), 200
else:
dic = {"msg": "不支持的请求方式!"}
return jsonify(dic), 405
@app.route('/disk', methods=["GET","POST"])
def disk_status():
if request.method == "POST":
data = request.get_json()
disk = data['disk']
total, free, used, percent = get_disk(disk)
data_dict = {
"total": total,
"free": free,
"used": used,
"percent": percent
}
return jsonify(data_dict), 200
else:
dic = {"msg": "不支持的请求方式!"}
return jsonify(dic), 405
@app.route('/gpu', methods=["GET","POST"])
def gpu_status():
if request.method == "POST":
data = request.get_json()
gpu_num = int(data['gpu'])
total, free, used, gpu_usage, mem_usage = get_gpu(gpu_num)
data_dict = {
"total": total,
"free": free,
"used": used,
"gpu": gpu_usage,
"mem": mem_usage
}
return jsonify(data_dict), 200
else:
dic = {"msg": "不支持的请求方式!"}
return jsonify(dic), 405
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-p','--port',type=int,help='端口号',default=2333)
args = parser.parse_args()
app.run(host='0.0.0.0',debug=False,port=args.port)