-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogdatatar.py
More file actions
56 lines (52 loc) · 1.91 KB
/
logdatatar.py
File metadata and controls
56 lines (52 loc) · 1.91 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
import datetime
from logTools.config import *
from logTools.tools import getFileSize, sendTheMsgToDingtalk
try:
# 初始化服务器报文 【x.x.x.x】:
text = headerText
# 昨天日期文件名
today = datetime.date.today()
yesterdayFileName = LOG_FILE_PREFIX + str(today - datetime.timedelta(days=1)) + LOG_FILE_EXTENSION
# 删除日期文件名
deleteFileName = LOG_FILE_PREFIX + str(today - datetime.timedelta(days=deleteDays)) + LOG_FILE_EXTENSION
# print("=====",logFileList)
# 获取文件名及文件大小
for f in logFileList:
fileSize = getFileSize('./' + f)
if fileSize >= warnsize:
text += '【文件过大】'
text += str(f) + ' ' + str(fileSize) + 'MB \n\n'
# 压缩日志
try:
os.system('tar -czvf ./%s %s' % (yesterdayFileName, ' '.join(logFileList)))
tarSize = str(getFileSize('./' + yesterdayFileName))
text += yesterdayFileName + ' ' + tarSize + 'MB \n\n'
except:
print('tar error, please check you fileType config')
# 删除所有log,重新创建文件
os.system('rm -rf ./*%s && touch %s' % (fileType, ' '.join(logFileList)))
# 删除过期日志压缩包
if os.path.exists('./' + deleteFileName):
os.system('rm -rf ./' + deleteFileName)
# 报文增加删除文件
text += '删除文件有:' + deleteFileName + '\n\n'
else:
pass
# 告知nginx重读日志文件
if isNginx:
os.system('nginx -s reopen')
# 告知UWSGI重写日志
if isUwsgi:
uwsgiLogrotate = logFilePath + '/touchforlog'
os.system('touch ' + uwsgiLogrotate)
# 增加服务器剩余空间告警
df = os.popen('df -lh').read().strip().split('\n')
for l in df:
if l[-1] in '/':
text += l + '\n\n'
except Exception as e:
text = e
# 发送消息给钉钉
# print(text)
if isDingtalkMsg:
sendTheMsgToDingtalk(text=text)