蒙珣的博客

活好当下,做好今天该做的事情。

0%

监控docker容器状态脚本

使用前先要修改的内容

  1. 更改server_log_path 存储路径,该路径是存储监控内容的总路径

  2. 修改server_array 数组值,修改成要监控的docker容器名称

  3. 同步修改calc.sh文件第13行 printf '0.FreeSwitch\t1.dialer\t2.PNLP\t3.MRCP\t4.VCG_TTS\t5.ptts_cloud\t 6.VCG\t7.PSTT\n'

使用方法

  1. 执行 calc.sh 文件

  2. 输入要监控的容器名称,**数字间空格分开。如: 0 2 3 **

  3. 输入监控的时长只能输入数字

    注意1: calc.py 和 calc.sh 要在同一个目录下

    注意2: 请使用管理员用户执行该脚本,否则需要修改脚本中docker status,给其添加权限

第一版

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
#!/bin/bash

#----------------------------------------------------------------------------------------------------
date_time=`date "+%Y_%m_%d_%H-%M-%S"`
server_log_path=/home/dengyuanjing/docker/test

# 初始服务数组
server_array=("Freeswitch" "dialer" "PNLP" "MRCP" "VCG_TTS" "ptts_cloud" "VCG" "PSTT")

echo "-----------------------------------------------------------------------------------------------"
echo "生成的目录名称是:$date_time"
printf '\n'
printf '0.FreeSwitch\t1.dialer\t2.PNLP\t3.MRCP\t4.VCG_TTS\t5.ptts_cloud\t 6.VCG\t7.PSTT\n'
printf '\n'
read -p "请输入要监控的服务序号(空格分隔):" number
read -p "请输入要监控的时长(分钟):" server_time

# 获取用户输入的长度(不包括空格)
# num=`echo $number | awk -F " " '{print NF}'`

# 用户指定要监控的数组
select_array=()

for i in ${number[@]}
do
select_array[${#select_array[@]}]=${server_array[$i]}
done

echo "您所选择的监控有:${select_array[@]}"

#----------------------------------------------------------------------------------------------------
# 使用下面命令生成1.txt
# docker stats dialer > dialer.txt

# 存放log的路径
#LOG_PATH=/home/dengyuanjing/docker/test/$date_time/$server/
PID_array=()
for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
mkdir -p $log_path
log_result_path=$log_path/result
mkdir -p $log_result_path
# 后台监控docker进程
nohup docker stats $server > $log_path/$server.txt 2>& 1 &
# 将各个服务的进程号记录的数组中
PID_array[${#PID_array[@]}]=$!
done

# 输出PID_array的值
# ps -ef | grep "docker stats dialer"
for i in ${PID_array[@]}
do
echo "所监控的进程ID为:$i"
done

sleep ${server_time}m

# kill 掉监控进程
for server_PID in ${PID_array[@]}
do
kill -9 $server_PID
done

sleep 1m

#----------------------------------------------------------------------------------------------------
# 过滤docker监控的文本

for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
log_result_path=$log_path/result/
#echo "现在要过滤的server名称为:$server"
#echo "路径为:$log_path/$server.txt"
# 1.过滤出Container ID
cat $log_path/$server.txt |awk 'NR==2 {print $1}' | grep -v CONTAINER > CONTAINER.log
# 2.过滤出容器名
cat $log_path/$server.txt |awk 'NR==2 {print $2}' > CONTAINER_NAME.log
# 3.过滤出CPU
cat $log_path/$server.txt |awk '{print $3}' | grep -v NAME > CPU_Usage.log
# 4.过滤出使用内存
cat $log_path/$server.txt |awk '{print $4}' | grep -v CPU > MEM_Usage.log
# 5.过滤出内存占比
cat $log_path/$server.txt |awk '{print $7}' | grep -v USAGE > MEM_Rate.log
# 6.过滤出发送数据量Net I
cat $log_path/$server.txt |awk '{print $8}' | grep -v USAGE |grep -v / > NET_input.log
# 7.过滤出接受数据量Net O
cat $log_path/$server.txt |awk '{print $10}' | grep -v USAGE |grep -v 'MEM' > NET_output.log
# 8.过滤出块读取数据量Block
cat $log_path/$server.txt |awk '{print $11}' | grep -v "%" > Block_input.log
# 9.过滤出块写入数据量Block O
cat $log_path/$server.txt |awk '{print $13}' | grep -v "I/O" > Block_output.log
# 10.过滤出容器线程数Pids
# PIDS=cat $log_path/$server.txt |awk '{print $14}' | grep -v "BLOCK"

# 将日志移入LOG_PATH下
mv CONTAINER.log $log_result_path
mv CONTAINER_NAME.log $log_result_path
mv CPU_Usage.log $log_result_path
mv MEM_Usage.log $log_result_path
mv MEM_Rate.log $log_result_path
mv NET_input.log $log_result_path
mv NET_output.log $log_result_path
mv Block_input.log $log_result_path
mv Block_output.log $log_result_path

done

#----------------------------------------------------------------------------------------------------
# 计算

for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
log_result_path=$log_path/result/
python3 calc.py $log_result_path $server

# 打印
sleep 5
log_result_path=$log_path/result
cat $log_result_path/calc.txt
echo ""
done

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
import math
import re
import sys

"""
设置读取的文本路径
1.cpuUsagePath
2.memUsagePath
3.memRatePath
4.netInputPath
5.netOutputPath
6.blockInputPath
7.blockOutputPath
"""

PATH = sys.argv[1]
server = sys.argv[2]
#print("============================================================")
#print(PATH)
cpuUsagePath = PATH+'CPU_Usage.log'
memUsagePath = PATH+'MEM_Usage.log'
memRatePath = PATH+'MEM_Rate.log'
netInputPath = PATH+'NET_input.log'
netOutputPath = PATH+'NET_output.log'
blockInputPath = PATH+'Block_input.log'
blockOutputPath = PATH+'Block_output.log'
arrayPath = [cpuUsagePath, memUsagePath, memRatePath, netInputPath, netOutputPath, blockInputPath, blockOutputPath]

def write_calc():
txtPath = PATH + 'calc.txt'
#print("路径为:"+txtPath)
with open(txtPath,'a+',encoding='utf-8') as f:
f.write(server+'\n')
f.write("======================================================\n")
f.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %("type","Max","Min","Avg","PCT90","PCT95","PCT99"))

"""
1.calc 函数接收两个参数,一个是shell过滤后的值的单位,一个是shell过滤后的数组
2.calc 仅接受排序后的数组
"""
def calc(unit,array,server,listName):

# 90/95/99百分率计算
percent1 = 99
percent2 = 95
percent3 = 90
a1 = percent1/100
a2 = percent2/100
a3 = percent3/100

num_count = len(array)

# 最大值、最小值、平均值
maxValue = array[-1]
mixValue = array[0]
averageValue = sum(array)/len(array)

# 90、95、99%
if math.ceil(num_count*a1) == num_count:
percent_99 = array[num_count-1]
else:
percent_99 = array[math.ceil(num_count*a1)]
if math.ceil(num_count*a2) == num_count:
percent_95 = array[num_count-1]
else:
percent_95 = array[math.ceil(num_count*a2)]
if math.ceil(num_count*a3) == num_count:
percent_90 = array[num_count-1]
else:
percent_90 = array[math.ceil(num_count*a3)]

txtPath = PATH + 'calc.txt'
with open(txtPath,'a+',encoding='utf-8') as f:
f.write("%s%s%s%s\t %.2f %.2f %.2f %.2f %.2f %.2f\n" %(listName,"(",unit,")",maxValue,mixValue,averageValue,percent_90,percent_95,percent_99))

"""
用于分离出unit

用于读取数据,并创建列表,将其送入calc计算最大、最小值
1.循环列表
2.打印要计算的列表的名称,如:CPU_Usage、MEM_Usage
3.读取该路径文件
4.按照read_file()分离出来的unit,添加数组元素
5.送入calc()计算
"""
def read_file():
# 分割出每个log中的单位
for i in arrayPath:
with open(i,'r+',encoding='utf-8') as f:
firstLine = f.readline()
# 正则匹配非数字的最后一位,即单位
pattern_value = re.findall(r'\D+',firstLine)
unit = pattern_value[-1]
unit = unit.split()[0]
#print('-------------------------------------单位为:'+unit)

# 用于读取数据,并创建列表,将其送入calc计算最大、最小值
listName = i.split('/')[-1]
listName = listName.split('.')[0]
#print()
#print(listName)
#print(type(listName))
array = []
with open(i,'r+',encoding='utf-8') as f:
for line in f.readlines():
lineValue = line.split(unit)[0]
try:
array.append(float(lineValue))
except ValueError:
print("加入列表进行计算的值格式有误!")
array.sort()
calc(unit,array,server,listName)

if __name__ == '__main__':
write_calc()
read_file()

第二版

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash

#----------------------------------------------------------------------------------------------------
date_time=`date "+%Y_%m_%d_%H-%M-%S"`
server_log_path=/home/liuyue/cpu/tool

# 初始服务数组
server_array=("freeswitch" "dialer" "piod" "mrcp" "vcg_3.3.2_hisense_docker" "nlp_management" "dm_service" "nlp_management_web" "nlu_service" "content_service" "classify_service")

echo "-----------------------------------------------------------------------------------------------"
echo "生成的目录名称是:$date_time"
#printf '\n'
#printf '0.dialer\t1.piod\t2.mrcp\t3.vcg_3.3.2_hisense_docker\t4.nlp_management\t5.dm_service\t6.nlp_management_web\t7.nlu_service\t8.content_service\t9.classify_service\n'
printf '\n'
#read -p "是否监控freeSwitch(y/n):" freeSwitchJudgment
#read -p "请输入要监控的服务序号(空格分隔):" number
#read -p "请输入要监控的时长(分钟):" server_time

freeSwitchJudgment=n
number=(0 1 2 3 4 5 6 7 8 9 10)
server_time=30

# 获取用户输入的长度(不包括空格)
# num=`echo $number | awk -F " " '{print NF}'`

# 用户指定要监控的数组
select_array=()

if [ $freeSwitchJudgment == "y" ]; then
echo "您选择了监控freeSwitch"
fi

for i in ${number[@]}
do
select_array[${#select_array[@]}]=${server_array[$i]}
done

echo "您所选择的监控有:${select_array[@]}"


#----------------------------------------------------------------------------------------------------
# 使用下面命令生成1.txt
# docker stats dialer > dialer.txt

# 存放log的路径
#LOG_PATH=/home/dengyuanjing/docker/test/$date_time/$server/
PID_array=()

if [ $freeSwitchJudgment == "y" ]; then
log_path=$server_log_path/$date_time/freeSwitch
mkdir -p $log_path
log_result_path=$log_path/result
mkdir -p $log_result_path

# 获取fs进程号
fs_PID=`ps -ef |grep freeswitch | grep -v grep | awk '{print $2}'`
fi


for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
mkdir -p $log_path
log_result_path=$log_path/result
mkdir -p $log_result_path
# 后台监控docker进程
nohup docker stats $server > $log_path/$server.txt 2>& 1 &
# 将各个服务的进程号记录的数组中
PID_array[${#PID_array[@]}]=$!
done

# 输出PID_array的值
# ps -ef | grep "docker stats dialer"
for i in ${PID_array[@]}
do
echo "所监控的进程ID为:$i"
done

if [ $freeSwitchJudgment == "y" ]; then
# fs监控时间 == sleep ${server_time}m
allTime=$(($server_time*60))
fsTime=0
log_path=$server_log_path/$date_time/freeSwitch
log_result_path=$log_path/result
while [ $fsTime -lt ${allTime} ]
do
# 监控内存
top -n 1 -b | grep $fs_PID | awk '{print $10}' >> $log_result_path/fsmem.txt
# 监控cpu
top -n 1 -b | grep $fs_PID | awk '{print $9}' >> $log_result_path/fscpu.txt

sleep 5s
fsTime=`expr $fsTime + 5`

done
else
sleep ${server_time}m

fi

# kill 掉监控进程
for server_PID in ${PID_array[@]}
do
kill -9 $server_PID
done

sleep 1m

#----------------------------------------------------------------------------------------------------
# 过滤docker监控的文本

for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
log_result_path=$log_path/result/
#echo "现在要过滤的server名称为:$server"
#echo "路径为:$log_path/$server.txt"
# 1.过滤出Container ID
cat $log_path/$server.txt |awk 'NR==2 {print $1}' | grep -v CONTAINER > CONTAINER.log
# 2.过滤出容器名
cat $log_path/$server.txt |awk 'NR==2 {print $2}' > CONTAINER_NAME.log
# 3.过滤出CPU
cat $log_path/$server.txt |awk '{print $3}' | grep -v NAME > CPU_Usage.log
# 4.过滤出使用内存
cat $log_path/$server.txt |awk '{print $4}' | grep -v CPU > MEM_Usage.log
# 5.过滤出内存占比
cat $log_path/$server.txt |awk '{print $7}' | grep -v USAGE > MEM_Rate.log
# 6.过滤出发送数据量Net I
cat $log_path/$server.txt |awk '{print $8}' | grep -v USAGE |grep -v / > NET_input.log
# 7.过滤出接受数据量Net O
cat $log_path/$server.txt |awk '{print $10}' | grep -v USAGE |grep -v 'MEM' > NET_output.log
# 8.过滤出块读取数据量Block
cat $log_path/$server.txt |awk '{print $11}' | grep -v "%" > Block_input.log
# 9.过滤出块写入数据量Block O
cat $log_path/$server.txt |awk '{print $13}' | grep -v "I/O" > Block_output.log
# 10.过滤出容器线程数Pids
# PIDS=cat $log_path/$server.txt |awk '{print $14}' | grep -v "BLOCK"

# 将日志移入LOG_PATH下
mv CONTAINER.log $log_result_path
mv CONTAINER_NAME.log $log_result_path
mv CPU_Usage.log $log_result_path
mv MEM_Usage.log $log_result_path
mv MEM_Rate.log $log_result_path
mv NET_input.log $log_result_path
mv NET_output.log $log_result_path
mv Block_input.log $log_result_path
mv Block_output.log $log_result_path

done

#----------------------------------------------------------------------------------------------------
# 计算

echo $date_time >> $server_log_path/$date_time/calc.log

for server in ${select_array[@]}
do
log_path=$server_log_path/$date_time/$server
log_result_path=$log_path/result/
python3 calc.py $log_result_path $server

# 打印
sleep 5
log_result_path=$log_path/result
cat $log_result_path/calc.txt
cat $log_result_path/calc.txt >> $server_log_path/$date_time/calc.log
echo ""
done

# 计算fs
if [ $freeSwitchJudgment == "y" ]; then
log_path=$server_log_path/$date_time/freeSwitch
log_result_path=$log_path/result/
python3 calc.py $log_result_path freeSwitch
cat $log_result_path/calc.txt
cat $log_result_path/calc.txt >> $server_log_path/$date_time/calc.log
echo ""
fi

echo "===========================" >> $server_log_path/$date_time/calc.log
echo "" >> $server_log_path/$date_time/calc.log

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from curses.ascii import isspace
import math
import re,os
import sys


def write_calc():
txtPath = PATH + 'calc.txt'
#print("路径为:"+txtPath)
with open(txtPath,'a+',encoding='utf-8') as f:
f.write(server+'\n')
f.write("======================================================\n")
f.write("%s\t%s\t%s\t%s\t%5s\t%5s\t%5s\n" %("type","Max","Min","Avg","PCT90","PCT95","PCT99"))
#f.write("type\tMax\tMin\tAvg\tPCT90\tPCT95\tPCT99\n")


"""
1.calc 函数接收两个参数,一个是shell过滤后的值的单位,一个是shell过滤后的数组
2.calc 仅接受排序后的数组
"""
def calc(unit,array,server,listName):

# 90/95/99百分率计算
percent1 = 99
percent2 = 95
percent3 = 90
a1 = percent1/100
a2 = percent2/100
a3 = percent3/100

num_count = len(array)

# 最大值、最小值、平均值
maxValue = array[-1]
mixValue = array[0]
averageValue = sum(array)/len(array)

# 90、95、99%
if math.ceil(num_count*a1) == num_count:
percent_99 = array[num_count-1]
else:
percent_99 = array[math.ceil(num_count*a1)]
if math.ceil(num_count*a2) == num_count:
percent_95 = array[num_count-1]
else:
percent_95 = array[math.ceil(num_count*a2)]
if math.ceil(num_count*a3) == num_count:
percent_90 = array[num_count-1]
else:
percent_90 = array[math.ceil(num_count*a3)]

#print("PATH:",PATH)
txtPath = PATH + 'calc.txt'
#print("路径为:"+txtPath)
with open(txtPath,'a+',encoding='utf-8') as f:
f.write("%s%s%s%s\t %.2f %.2f %.2f %.2f %.2f %.2f\n" %(listName,"(",unit,")",maxValue,mixValue,averageValue,percent_90,percent_95,percent_99))

# 将两个列表的值改写到的文件中
def save_txt(list1, list2, save_path):
if os.path.isfile(save_path):
save_path_new = save_path+'_old'
#print(save_path_new)
os.rename(save_path,save_path_new)
with open(save_path, "a") as f:
for i in range(len(list1)):
f.write('{}{}\n'.format(list1[i], list2[i]))

# docker中网络IO/读写IO单位不同转换,未涉及内存单位问题
def transformation(i,unitList):
temporaryList = []
with open(i,'r+',encoding='utf-8') as f:
for line in f.readlines():
if line.isspace():
continue
else:
pattern_value = re.findall(r'\d+',line)
num = pattern_value[0]
temporaryList.append(float(num))
# print(temporaryList)
# 逐一比对单位,将不同的单位统一修改成相同单位
# print("unitList",unitList)
for k in range(0,len(unitList)):
for j in range(k+1,len(unitList)):
if unitList[k] == unitList[j]:
continue
else:
# B kB MB GB
if unitList[k] == "MB" and unitList[j] == "GB":
temporaryList[j] = temporaryList[j] * 1000
unitList[j] = "MB"
elif unitList[k] == "MB" and unitList[j] == "kB":
temporaryList[j] = temporaryList[j] / 1000
unitList[j] = "MB"
elif unitList[k] == "MB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1000 / 1000
unitList[j] = "MB"
elif unitList[k] == "kB" and unitList[j] == "MB":
temporaryList[j] = temporaryList[j] * 1000
unitList[j] = "kB"
elif unitList[k] == "kB" and unitList[j] == "GB":
temporaryList[j] = temporaryList[j] * 1000 * 1000
unitList[j] = "kB"
elif unitList[k] == "kB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1000
unitList[j] = "kB"
elif unitList[k] == "GB" and unitList[j] == "MB":
temporaryList[j] = temporaryList[j] / 1000
unitList[j] = "GB"
elif unitList[k] == "GB" and unitList[j] == "kB":
temporaryList[j] = temporaryList[j] / 1000 / 1000
unitList[j] = "GB"
elif unitList[k] == "GB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1000 / 1000 / 1000
unitList[j] = "GB"
elif unitList[k] == "B" and unitList[j] == "kB":
temporaryList[j] = temporaryList[j] * 1000
unitList[j] = "B"
elif unitList[k] == "B" and unitList[j] == "MB":
temporaryList[j] = temporaryList[j] * 1000 * 1000
unitList[j] = "B"
elif unitList[k] == "B" and unitList[j] == "GB":
temporaryList[j] = temporaryList[j] * 1000 * 1000 * 1000
unitList[j] = "B"

# B KiB MiB GiB
elif unitList[k] == "MiB" and unitList[j] == "GiB":
temporaryList[j] = temporaryList[j] * 1024
unitList[j] = "MiB"
elif unitList[k] == "MiB" and unitList[j] == "KiB":
temporaryList[j] = temporaryList[j] / 1024
unitList[j] = "MiB"
elif unitList[k] == "MiB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1024 / 1024
unitList[j] = "MiB"
elif unitList[k] == "KiB" and unitList[j] == "MiB":
temporaryList[j] = temporaryList[j] * 1024
unitList[j] = "KiB"
elif unitList[k] == "KiB" and unitList[j] == "GiB":
temporaryList[j] = temporaryList[j] * 1024 * 1024
unitList[j] = "KiB"
elif unitList[k] == "KiB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1024
unitList[j] = "KiB"
elif unitList[k] == "GiB" and unitList[j] == "MiB":
temporaryList[j] = temporaryList[j] / 1024
unitList[j] = "GiB"
elif unitList[k] == "GiB" and unitList[j] == "kiB":
temporaryList[j] = temporaryList[j] / 1024 / 1024
unitList[j] = "GiB"
elif unitList[k] == "GiB" and unitList[j] == "B":
temporaryList[j] = temporaryList[j] / 1024 / 1024 / 1024
unitList[j] = "GiB"
elif unitList[k] == "B" and unitList[j] == "KiB":
temporaryList[j] = temporaryList[j] * 1024
unitList[j] = "B"
elif unitList[k] == "B" and unitList[j] == "MiB":
temporaryList[j] = temporaryList[j] * 1024 * 1024
unitList[j] = "B"
elif unitList[k] == "B" and unitList[j] == "GiB":
temporaryList[j] = temporaryList[j] * 1024 * 1024 * 1024
unitList[j] = "B"
break
save_txt(temporaryList,unitList,i)

"""
用于分离出unit

用于读取数据,并创建列表,将其送入calc计算最大、最小值
1.循环列表
2.打印要计算的列表的名称,如:CPU_Usage、MEM_Usage
3.读取该路径文件
4.按照read_file()分离出来的unit,添加数组元素
5.送入calc()计算
"""
def read_file():
# 分割出每个log中的单位
for i in arrayPath:
with open(i,'r+',encoding='utf-8') as f:
# 读取第一行
firstLine = f.readline()
# 正则匹配非数字的最后一位,即单位
pattern_value = re.findall(r'\D+',firstLine)
unit = pattern_value[-1]
unit = unit.split()[0]
#print('-------------------------------------单位为:'+unit)
# 读取每一行,将所有单位分离出来,并写入unitList中
unitList = []
with open(i,'r+',encoding='utf-8') as f:
# 读取每一行
for line in f.readlines():
if line.isspace():
continue
else:
pattern_value = re.findall(r'\D+',line)
unit = pattern_value[-1]
unit = unit.split()[0]
unitList.append(unit)

# 检查列表中所有元素是否相同
isSame = unitList.count(unitList[0]) == len(unitList)
if(isSame):
#print("列表中所有元素相同")
pass
else:
print(i+"列表中有元素不相同")
print("不相同的单位有",set(unitList))
# 读取文件,将所有的值都写入列表
transformation(i,unitList)

# 用于读取数据,并创建列表,将其送入calc计算最大、最小值
listName = i.split('/')[-1]
listName = listName.split('.')[0]
#print()
#print(listName)
#print(type(listName))
array = []
unit = unitList[0]
with open(i,'r+',encoding='utf-8') as f:
for line in f.readlines():
if line.isspace():
continue
else:
lineValue = line.split(unit)[0]
try:
array.append(float(lineValue))
except ValueError:
print(server+" "+i+"加入列表进行计算的值格式有误! line为:"+str(line)+"lineValue为:"+str(lineValue)+"unit为:"+str(unit))
array.sort()
calc(unit,array,server,listName)

if __name__ == '__main__':
"""
设置读取的文本路径
1.cpuUsagePath
2.memUsagePath
3.memRatePath
4.netInputPath
5.netOutputPath
6.blockInputPath
7.blockOutputPath
"""

PATH = sys.argv[1]
server = sys.argv[2]
#print("============================================================")
#print(PATH)

write_calc()
if server == "freeSwitch":
cpuUsagePath = PATH+'fscpu.txt'
memUsagePath = PATH+'fsmem.txt'
arrayPath = [cpuUsagePath, memUsagePath]
unit="%"
for i in arrayPath:
array = []
listName = i.split('/')[-1]
listName = listName.split('.')[0]
with open(i,'r+',encoding='utf-8') as f:
for line in f.readlines():
try:
array.append(float(line))
except ValueError:
print(i,"中有错误数据,请查看")
array.sort()
calc(unit,array,server,listName)
else:
cpuUsagePath = PATH+'CPU_Usage.log'
memUsagePath = PATH+'MEM_Usage.log'
memRatePath = PATH+'MEM_Rate.log'
netInputPath = PATH+'NET_input.log'
netOutputPath = PATH+'NET_output.log'
blockInputPath = PATH+'Block_input.log'
blockOutputPath = PATH+'Block_output.log'
arrayPath = [cpuUsagePath, memUsagePath, memRatePath, netInputPath, netOutputPath, blockInputPath, blockOutputPath]
# 函数调用
read_file()