• 幕客老师召集小伙伴
  • 运维高手36项修炼
  • python自动化运维项目实战
  • nginx从入门到实战
  • 阿里云与Centos7实战

实现尾行文件监听并实现服务管理py脚本

一、本脚本特色

1、支持查找大文件,并只取出最后一行,提高索引文件性能

2、支持批量的管理

如:当出现问题后,系统的日志急剧增长,需要循环先进行日志清理。

进程需要执行重启,提前是需要进行多次的进程存活判断。

 

二、准备进程重启进程操作脚本

如下链接:

http://www.iaskjob.com/admin/zinnia/entry/

 

三、python2.7脚本如下

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os,sys,time
import re
__author__ = 'Jeson@imoocc.com'

def search_info(line_content,search_word):
    if re.search(search_word,line_content):
        return True
    else:
        return False

def get_last_line(inputfile):
  filesize = os.path.getsize(inputfile)
  blocksize = 1024
  dat_file = open(inputfile, 'rb')
  last_line = ""
  if filesize > blocksize :
      maxseekpoint = (filesize // blocksize)
      dat_file.seek((maxseekpoint-1)*blocksize)
  elif filesize :
      dat_file.seek(0, 0)
  lines =  dat_file.readlines()
  if lines :
      last_line = lines[-1].strip()
  #print "last line : ", last_line
  dat_file.close()
  return last_line

def killprocess():
    pass

def main():
    log_file_path = '/usr/local/amoeba/logs/net.log'
    clear_log_file = ['/usr/local/amoeba/logs/net.log','/usr/local/amoeba/logs/root.log']
    key_word = 'listening on 0.0.0.'
    restart_process_shell = '/usr/local/amoeba/bin/restartameba.sh'
    for i in [2,3,5]:
        line_con = get_last_line(inputfile=log_file_path)
        print line_con
        if search_info(line_content=line_con,search_word=key_word):
            for logfile in clear_log_file:
                print "Clear log %s"%logfile
                os.system("> %s"%logfile)
            time.sleep(i)
            print "Find Key:%s"%key_word
    line_con = get_last_line(inputfile=log_file_path)
    if search_info(line_content=line_con,search_word=key_word):
        print "Restart Master process!"
        os.system('/bin/sh %s'%restart_process_shell)
        print "All Done!"

if __name__ == '__main__':
    main()

 

实现尾行文件监听并实现服务管理py脚本

Pingbacks已打开。

引用地址

暂无评论

发表评论