首页 > 其他 > 详细

运维脚本

时间:2020-02-15 00:57:55      阅读:57      评论:0      收藏:0      [点我收藏+]

运维脚本


nginx日志切割


阿里云安装

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget https://nginx.org/download/nginx-1.16.0.tar.gz
tar -xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx-1.16.0 --with-http_stub_status_module --with-http_ssl_module
make && make install
/usr/local/nginx-1.16.0/sbin/nginx 


安装python3

tar xf Python-3.6.8.tgz 
cd Python-3.6.8
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3


日志切割脚本

import os, datetime, re, time

program_start = time.time()
date = datetime.datetime.now().strftime('%Y%m%d')
access_path = "/usr/local/nginx/logs/access.log"
error_path = "/usr/local/nginx/logs/error.log"
cut_dir = "/usr/local/nginx/logs/history"

# Get the size of the log
access_size = os.path.getsize(access_path)
error_size = os.path.getsize(error_path)

# convert the size from bytes to MB
def to_mb(size):
    to_kb = float(size)/1024
    to_mb = to_kb/1024
    
    # remain 1 bits
    return '%.1f'%(to_mb)


access_size_withmb = to_mb(access_size)
error_size_withmb = to_mb(error_size)



# get nginx_pid
def get_nginx_pid():
    nginx_conf_path = "/usr/local/nginx/conf/nginx.conf"
    with open(nginx_conf_path, 'r', encoding='utf-8') as f:
        nginx_conf = f.read()
        nginx_pid = re.search("/.*nginx.pid", nginx_conf).group(0)

    return nginx_pid
    

# cut logs
def main_process(size_withmb, path):
    if size_withmb > 500:
        new_path = '%s/%s-%s'%(cut_dir, path.rsplit('/', 1)[1], date)
        nginx_pid = get_nginx_pid()
        print('>>>>>>>>>>>>>>>>>>>>>>>>>>the log size is  %sMB, start to cut <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' %(size_withmb))
        
        if not os.path.isdir(cut_dir):
            os.system('mkdir {}'.format(cut_dir))
        
        os.system('mv {} {}'.format(path, new_path))
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> cut log finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
        
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> log is reloading <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
        os.system('kill -USR1 `cat {}`'.format(nginx_pid))
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> log reload finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')

    else:
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> log size is less than 500MB, not need to cut <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
        


# compress logs
def compress_log():
    print('>>>>>>>>>>>>>>>>>>>>>>>>>> start to compress logs <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
    res = os.system("find %s -mtime 3 -type f |xargs tar czPvf %s/log.tar-%s 2>/dev/null"%(cut_dir, cut_dir, date))
    
    if res == 0:
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> compress logs finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
    
    else:
        print('>>>>>>>>>>>>>>>>>>>>>>>>>> no logs need to compress <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
        


if __name__ == '__main__':
    main_process(access_size_withmb, access_path)
    main_process(error_size_withmb, error_path)
    compress_log()
    execute_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    program_end = time.time()
    execute_time = '%.1f'%(program_end - program_start)
    print('>>>>>>>>>>>>>>>>>>>>>>>>>> the program executed %s seconds, today is %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' %(execute_time, execute_date))
    print('\n')

运维脚本

原文:https://www.cnblogs.com/cjwnb/p/12310359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!