File size: 6,720 Bytes
3a5cf48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python
# coding: utf-8
# -----------------------------
# 宝塔Linux面板网站日志切割脚本
# -----------------------------
import glob
import os
import re
import shutil
import sys
import time

os.chdir("/www/server/panel")
sys.path.append('class/')
import public

print('==================================================================')
print('★[' + time.strftime("%Y/%m/%d %H:%M:%S") + ']切割日志')
print('==================================================================')
print('|--当前保留最新的[' + sys.argv[2] + ']份')

logsPath = '/www/wwwlogs/'
is_gzip = not os.path.exists('/www/server/panel/data/log_not_gzip.pl')


def split_log(siteName, num, log_cut_path):
    global logsPath, is_gzip
    res = public.M('sites').where('name=?', (siteName,)).select()[0]['project_type'].lower()
    if res == 'php' or res == 'proxy':
        res = ''
    else:
        res = res + '_'
    # print(res)
    directory = '/etc/init.d/'
    files = os.listdir(directory)
    file_list = list(files)
    print('|-正在处理网站: {}'.format(siteName))
    if 'nginx' in file_list:
        config_path = '/www/server/panel/vhost/nginx/{}.conf'.format(res + siteName)
        config = public.readFile(config_path)
        if not config:
            print('|-正在处理网站:未检测到{}站点的日志'.format(siteName))
            return
        tmp, _ = nginx_get_log_path(config, is_error_log=False)
        if tmp is not None:
            logsPath = tmp
    elif 'httpd' in file_list:
        config_path = '/www/server/panel/vhost/apache/{}.conf'.format(res + siteName)
        config = public.readFile(config_path)
        if not config:
            print('|-正在处理网站:未检测到{}站点的日志'.format(siteName))
            return
        tmp, _ = apache_get_log_path(config, is_error_log=False)
        if tmp is not None:
            logsPath = tmp
    
    # print('|-正在处理网站: {}'.format(siteName))
    # print(logsPath)
    log_path = os.path.join(log_cut_path, 'history_backups', siteName)
    if not os.path.exists(log_path): os.makedirs(log_path, 384)
    logs = sorted(glob.glob(log_path + '/' + siteName + "_access_*"))
    old_logs = sorted(glob.glob(logsPath + '/' + siteName + "*log_*.log"))
    count = len(logs)
    remove_num = count +1 - num
    # print('|-当前日志数量: {}, 删除:{}'.format(count,remove_num))
    old_list = old_logs[:remove_num]
    for i in old_list:
        if os.path.exists(i):
            os.remove(old_logs[i])
            print('|---多余日志[' + i + ']已删除!')
    remove_num = remove_num - len(old_list)
    for i in logs[:remove_num]:
        if os.path.exists(i):
            os.remove(i)
            print('|---多余日志[' + i + ']已删除!')
        err_file = i.replace('access', 'error')
        if os.path.exists(err_file):
            os.remove(err_file)
            print('|---多余日志[' + err_file + ']已删除!')
    
    his_date = time.strftime("%Y-%m-%d_%H%M%S")
    ngx_access_log = os.path.join(logsPath, siteName + '.log')
    ngx_error_log = os.path.join(logsPath, siteName + '.error.log')
    if os.path.exists(ngx_access_log):
        history_log_file = log_path + '/' + siteName + '_access_' + his_date + '.log'
        shutil.move(ngx_access_log, history_log_file)
        if is_gzip:
            os.system('gzip {}'.format(history_log_file))
            print('|---已切割日志到:' + history_log_file + '.gz')
        else:
            print('|---已切割日志到:' + history_log_file + '.gz')
    if os.path.exists(ngx_error_log):
        history_log_file = log_path + '/' + siteName + '_error_' + his_date + '.log'
        shutil.move(ngx_error_log, history_log_file)
        if is_gzip:
            os.system('gzip {}'.format(history_log_file))
            print('|---已切割日志到:' + history_log_file + '.gz')
        else:
            print('|---已切割日志到:' + history_log_file + '.gz')
    
    httpd_access_log = os.path.join(logsPath, siteName + '-access_log')
    httpd_error_log = os.path.join(logsPath, siteName + '-error_log')
    if os.path.exists(httpd_access_log):
        history_log_file = log_path + '/' + siteName + '_access_' + his_date + '.log'
        if not os.path.exists(history_log_file):
            shutil.move(httpd_access_log, history_log_file)
            if is_gzip:
                os.system('gzip {}'.format(history_log_file))
                print('|---已切割日志到:' + history_log_file + '.gz')
            else:
                print('|---已切割日志到:' + history_log_file + '.gz')
    if os.path.exists(httpd_error_log):
        history_log_file = log_path + '/' + siteName + '_error_' + his_date + '.log'
        if not os.path.exists(history_log_file):
            shutil.move(httpd_error_log, history_log_file)
            if is_gzip:
                os.system('gzip {}'.format(history_log_file))
                print('|---已切割日志到:' + history_log_file + '.gz')
            else:
                print('|---已切割日志到:' + history_log_file + '.gz')
    print('|-网站: {} 完成处理'.format(siteName))


def split_all(save,log_cut_path):
    sites = public.M('sites').field('name').select()
    for site in sites:
        split_log(site['name'], save,log_cut_path)


def nginx_get_log_path(nginx_config: str, is_error_log: bool = False):
    if is_error_log:
        re_data = re.findall(r"error_log +(/(\S+/?)+) ?(.*?);", nginx_config)
    else:
        re_data = re.findall(r"access_log +(/(\S+/?)+) ?(.*?);", nginx_config)
    if re_data is None:
        return None, None
    for i in re_data:
        file_path = i[0].strip(";")
        if file_path != "/dev/null":
            return os.path.dirname(file_path), os.path.basename(file_path)
    return None, None


def apache_get_log_path(apache_config: str, is_error_log: bool = False):
    if is_error_log:
        re_data = re.findall(r'''ErrorLog +['"]?(/(\S+/?)+)['"]? ?(.*?)\n''', apache_config)
    else:
        re_data = re.findall(r'''CustomLog +['"]?(/(\S+/?)+)['"]? ?(.*?)\n''', apache_config)
    if re_data is None:
        return None
    for i in re_data:
        file_path = i[0].strip('"').strip("'")
        if file_path != "/dev/null":
            return os.path.dirname(file_path), os.path.basename(file_path)
    return None, None


if __name__ == '__main__':
    num = int(sys.argv[2])
    if len(sys.argv) > 3:
        log_cut_path = sys.argv[3]
    else:
        log_cut_path = '/www/wwwlogs/'
    if sys.argv[1].find('ALL') == 0:
        split_all(num, log_cut_path)
    else:
        siteName = sys.argv[1]
        split_log(sys.argv[1], num, log_cut_path)
    public.serviceReload()