-
Notifications
You must be signed in to change notification settings - Fork 960
/
task.py
370 lines (333 loc) · 11.9 KB
/
task.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/python
#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2016 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: hwliang <[email protected]>
# +-------------------------------------------------------------------
# ------------------------------
# 计划任务
# ------------------------------
import sys
import os
import logging
from json import dumps, loads
from psutil import Process, pids, cpu_count
os.environ['BT_TASK'] = '1'
sys.path.insert(0, "/www/server/panel/class/")
import time
import public
import db
logPath = '/tmp/panelExec.log'
isTask = '/tmp/panelTask.pl'
python_bin = None
def get_python_bin():
global python_bin
if python_bin: return python_bin
bin_file = '/www/server/panel/pyenv/bin/python'
bin_file2 = '/usr/bin/python'
if os.path.exists(bin_file):
python_bin = bin_file
return bin_file
python_bin = bin_file2
return bin_file2
def WriteFile(filename,s_body,mode='w+'):
"""
写入文件内容
@filename 文件名
@s_body 欲写入的内容
return bool 若文件不存在则尝试自动创建
"""
try:
fp = open(filename, mode)
fp.write(s_body)
fp.close()
return True
except:
try:
fp = open(filename, mode,encoding="utf-8")
fp.write(s_body)
fp.close()
return True
except:
return False
def ReadFile(filename, mode='r'):
"""
读取文件内容
@filename 文件名
return string(bin) 若文件不存在,则返回None
"""
if not os.path.exists(filename):
return False
f_body = None
with open(filename, mode) as fp:
f_body = fp.read()
return f_body
# 写输出日志
def WriteLogs(logMsg):
try:
global logPath
with open(logPath, 'w+') as fp:
fp.write(logMsg)
fp.close()
except:
pass
# 日报监控任务
def systemTask():
process_object = process_task()
while 1:
time.sleep(60)
process_object.get_monitor_list()
# 日报数据收集
if os.path.exists("/www/server/panel/data/start_daily.pl"):
try:
from panelDaily import panelDaily
pd = panelDaily()
t_now = time.localtime()
yesterday = time.localtime(time.mktime((
t_now.tm_year, t_now.tm_mon, t_now.tm_mday-1,
0,0,0,0,0,0
)))
yes_time_key = pd.get_time_key(yesterday)
con = ReadFile("/www/server/panel/data/store_app_usage.pl")
store = False
if con:
if con != str(yes_time_key):
store = True
else:
store = True
if store:
date_str = str(yes_time_key)
daily_data = pd.get_daily_data_local(date_str)
if "status" in daily_data.keys():
if daily_data["status"]:
score = daily_data["score"]
if public.M("system").dbfile("system").table("daily").where("time_key=?", (yes_time_key,)).count() == 0:
public.M("system").dbfile("system").table("daily").add("time_key,evaluate,addtime", (yes_time_key, score, time.time()))
pd.store_app_usage(yes_time_key)
WriteFile("/www/server/panel/data/store_app_usage.pl", str(yes_time_key), "w")
pd.check_server()
except Exception as e:
logging.info("存储应用空间信息错误:"+str(e))
# 检查502错误
def check502():
try:
phpversions = public.get_php_versions()
for version in phpversions:
if version in ['52','5.2']: continue
php_path = '/www/server/php/' + version + '/sbin/php-fpm'
if not os.path.exists(php_path):
continue
if checkPHPVersion(version):
continue
if startPHPVersion(version):
public.WriteLog('PHP守护程序', '检测到PHP-' + version + '处理异常,已自动修复!', not_web=True)
except Exception as ex:
logging.info(ex)
# 处理指定PHP版本
def startPHPVersion(version):
try:
fpm = '/etc/init.d/php-fpm-'+version
php_path = '/www/server/php/' + version + '/sbin/php-fpm'
if not os.path.exists(php_path):
if os.path.exists(fpm):
os.remove(fpm)
return False
# 尝试重载服务
os.system(fpm + ' start')
os.system(fpm + ' reload')
if checkPHPVersion(version):
return True
# 尝试重启服务
cgi = '/tmp/php-cgi-'+version + '.sock'
pid = '/www/server/php/'+version+'/var/run/php-fpm.pid'
os.system('pkill -9 php-fpm-'+version)
time.sleep(0.5)
if os.path.exists(cgi):
os.remove(cgi)
if os.path.exists(pid):
os.remove(pid)
os.system(fpm + ' start')
if checkPHPVersion(version):
return True
# 检查是否正确启动
if os.path.exists(cgi):
return True
return False
except Exception as ex:
logging.info(ex)
return True
# 检查指定PHP版本
def checkPHPVersion(version):
try:
cgi_file = '/tmp/php-cgi-{}.sock'.format(version)
if os.path.exists(cgi_file):
init_file = '/etc/init.d/php-fpm-{}'.format(version)
if os.path.exists(init_file):
init_body = public.ReadFile(init_file)
if not init_body: return True
uri = "/phpfpm_"+version+"_status?json"
result = public.request_php(version, uri, '')
loads(result)
return True
except:
logging.info("检测到PHP-{}无法访问".format(version))
return False
# 502错误检查线程
def check502Task():
try:
while True:
public.auto_backup_panel()
check502()
time.sleep(600)
except Exception as ex:
logging.info(ex)
time.sleep(600)
check502Task()
#5个小时更新一次更新软件列表
def update_software_list():
while True:
try:
import panelPlugin
panelPlugin.panelPlugin().get_cloud_list_status(None)
time.sleep(18000)
except:
time.sleep(1800)
update_software_list()
class process_task:
__pids = []
__last_times = {}
__last_dates = {}
__cpu_count = cpu_count()
__sql = None
def __init__(self):
self.__sql = db.Sql().dbfile('system')
csql = '''CREATE TABLE IF NOT EXISTS `process_tops` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`process_list` REAL,
`addtime` INTEGER
)'''
self.__sql.execute(csql, ())
csql = '''CREATE TABLE IF NOT EXISTS `process_high_percent` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` REAL,
`pid` REAL,
`cmdline` REAL,
`cpu_percent` FLOAT,
`memory` FLOAT,
`cpu_time_total` INTEGER,
`addtime` INTEGER
)'''
self.__sql.execute(csql, ())
self.__sql.close()
def get_pids(self):
'''
@name 获取pid列表
@author hwliang<2021-09-04>
@return None
'''
self.__pids = pids()
def get_cpu_percent(self,pid,cpu_time_total):
'''
@name 获取pid的cpu占用率
@author hwliang<2021-09-04>
@param pid 进程id
@param cpu_time_total 进程总cpu时间
@return 占用cpu百分比
'''
stime = time.time()
if pid in self.__last_times:
old_time = self.__last_times[pid]
else:
self.__last_times[pid] = cpu_time_total
self.__last_dates[pid] = stime
return 0
cpu_percent = round(100.00 * float(cpu_time_total - old_time) / (stime - self.__last_dates[pid]) / self.__cpu_count,2)
return cpu_percent
def read_file(self,filename):
f = open(filename,'rb')
result = f.read()
f.close()
return result.decode().replace("\u0000"," ").strip()
def get_monitor_list(self):
'''
@name 获取监控列表
@author hwliang<2021-09-04>
@return list
'''
self.get_pids()
monitor_list = {}
cpu_src_limit = 30
cpu_pre_limit = 80 / self.__cpu_count
addtime = int(time.time())
for pid in self.__pids:
try:
process_proc_comm = '/proc/{}/comm'.format(pid)
process_proc_cmdline = '/proc/{}/cmdline'.format(pid)
if pid < 100: continue
p = Process(pid)
cpu_times = p.cpu_times()
cpu_time_total = int(sum(cpu_times))
pname = self.read_file(process_proc_comm)
cmdline = self.read_file(process_proc_cmdline)
rss = p.memory_info().rss
cpu_percent = self.get_cpu_percent(pid,cpu_time_total)
pid = str(pid)
ppid = str(p.ppid())
if ppid in monitor_list:
monitor_list[ppid]['pid'].append(pid)
monitor_list[ppid]['cpu_time_total'] += cpu_time_total
monitor_list[ppid]['memory'] += rss
monitor_list[ppid]['cpu_percent'] += cpu_percent
else:
if pname == 'php-fpm':
if cmdline.find('/www/server/php/') != -1:
pname += '(' + '.'.join(cmdline.split('/')[-3]) + ')'
elif pname in ['mysqld','mysqld_safe']:
pname = 'mysqld,mysql_safe(MySQL)'
elif pname in ['BT-Task','BT-Panel']:
continue
monitor_list[pid] = {
'pid':[pid],
'name':pname,
'cmdline': cmdline,
'memory': rss,
'cpu_time_total': cpu_time_total,
'cpu_percent': cpu_percent
}
except:
continue
process_info_list = []
cpu_high_list = []
for i in monitor_list:
pid_count = len(monitor_list[i]['pid'])
monitor_list[i]['pid'] = ','.join(monitor_list[i]['pid'])
process_info_list.append(monitor_list[i])
# 记录CPU占用超过 cpu_pre_limit 的进程
if pid_count > 1:
if monitor_list[i]['cpu_percent'] >= cpu_src_limit:
cpu_high_list.append(monitor_list[i])
else:
if monitor_list[i]['cpu_percent'] >= cpu_pre_limit:
cpu_high_list.append(monitor_list[i])
self.__sql = db.Sql().dbfile('system')
process_info_list = sorted(process_info_list,key=lambda x:x['cpu_time_total'],reverse=True)
self.__sql.table('process_tops').insert({"process_list": dumps(process_info_list[:10]),'addtime':addtime})
if cpu_high_list:
for cpu_high in cpu_high_list:
self.__sql.table('process_high_percent').addAll(
'pid,name,cmdline,memory,cpu_time_total,cpu_percent,addtime',
(
cpu_high['pid'],
cpu_high['name'],
cpu_high['cmdline'],
cpu_high['memory'],
cpu_high['cpu_time_total'],
cpu_high['cpu_percent'],
addtime
)
)
self.__sql.commit()
self.__sql.close()