monitoring.py
4.3 KB
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
from app.models import *
from app.util.component.ApiTemplate import ApiTemplate
import paramiko
import time
class Api(ApiTemplate):
api_name = "远程监控"
def process(self):
# 返回结果
res = {}
res["data"] = {}
try:
# 业务逻辑
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect(hostname='172.26.60.100',
username='root', password='DMap@123')
# cpu
order = "top -b -n1 | sed -n '3p' | awk '{print $2}'"
stdin, stdout, stderr = client.exec_command(order)
cpu_usage = stdout.read().decode().split("\n")[0] # cpu使用率
# 内存
order = "free -h | sed -n '2p' | awk '{print $2}'"
stdin, stdout, stderr = client.exec_command(order)
totalMem = stdout.read().decode().split("\n")[0] # 总内存
order = "free -h | sed -n '3p' | awk '{print $3}'"
stdin, stdout, stderr = client.exec_command(order)
freeMem = stdout.read().decode().split("\n")[0] # 空余内存
# disk
order = "df -m | grep -v 'overlay\|Filesystem' | awk '{print $1,$2,$3}' | grep /dev | awk '{print $2}' | awk -v total=0 '{total+=$1}END{print total}'"
stdin, stdout, stderr = client.exec_command(order)
totalDisk = int(stdout.read().decode().split("\n")
[0]) # 总磁盘空间,单位Mb
order = "df -m | grep -v 'overlay\|Filesystem' | awk '{print $1,$2,$3}' | grep /dev | awk '{print $3}' | awk -v total=0 '{total+=$1}END{print total}'"
stdin, stdout, stderr = client.exec_command(order)
usedDisk = int(stdout.read().decode().split("\n")
[0]) # 已使用磁盘空间,单位Mb
# network
# 接收的字节数
rx_time = []
rx_bytes = []
tx_time = []
tx_bytes = []
# 接收的字节数
order = "ifconfig | grep RX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
i = 0
while i < 2:
i = i+1
stdin, stdout, stderr = client.exec_command(order)
rx_time.append(time.time())
rx_bytes.append(int(stdout.read().decode().split("\n")[0]))
# 发送的字节数
order = "ifconfig | grep TX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
i = 0
while i < 2:
i = i+1
stdin, stdout, stderr = client.exec_command(order)
tx_time.append(time.time())
tx_bytes.append(int(stdout.read().decode().split("\n")[0]))
res["data"] = {
"cpuUsage": "{}%".format(cpu_usage),
"totalMemory": "{}".format(totalMem),
"freeMemory": "{}".format(freeMem),
"totalDisk": "{}".format(self.format_value(totalDisk*1024**2)),
"usedDisk": "{}".format(self.format_value(usedDisk*1024**2)),
"networkRecv": "{}".format(self.format_value((rx_bytes[1] - rx_bytes[0])/(rx_time[1]-rx_time[0]))),
"networkSend": "{}".format(self.format_value((tx_bytes[1] - tx_bytes[0])/(tx_time[1]-tx_time[0])))
}
res["result"] = True
except Exception as e:
raise e
finally:
client.close()
return res
api_doc = {
"tags": ["监控接口"],
"parameters": [
],
"responses": {
200: {
"schema": {
"properties": {
}
}
}
}
}
def format_value(self, value):
if value > 1024**3:
value = "{}GB".format(format(value/1024.0**3, '.1f'))
elif value > 1024**2:
value = "{}MB".format(format(value / 1024.0 ** 2, '.1f'))
elif value > 1024:
value = "{}KB".format(format(value / 1024.0, '.1f'))
else:
value = "{}B".format(format(value, '.1f'))
return value
if __name__ == '__main__':
api = Api()
api.process()