监控Puppet日志的Python脚本


监控Puppet日志的Python脚本,供参考


# -*- coding: utf-8 -*-

__author__ = 'River'

import timeit,os

import re,time

'''

'''

#被监控的文件

log_file="/var/log/messages"

#记录读取的行数

line_file="/var/log/line.log"

##计算文件行数

def linecount(log_file):

  count = 0

  with open (log_file,'rb') as thefile:

      while 1:

          buffer = thefile.read(65536)

          if not buffer:

              break

          count += buffer.count('\n')#通过读取换行符计算

      return count

try:

  ##宁愿重复报警,也不能遗漏报警,上次读取的结尾初始化为50%文件行数

  end_line=int(linecount(log_file)*0.5)

  while 1:

      with open (line_file,'r') as fileHandle_line:

          try:

              #获取上次记录的读取位置

              start_line_record=fileHandle_line.readlines()[0:1][0].strip()

              start_line = int(start_line_record)

          except:

              #start_line=int(end_line*0.5)

              start_line=int(linecount(log_file)*0.5)

      with open (log_file,'r') as fileHandle_log:

          try:

              fileList = fileHandle_log.readlines()[start_line:]

              end_line=start_line

              for line in fileList:

                  end_line=end_line+1

                  #匹配上正则规则

                  if re.match(r".*puppet-agent.*\(\/Stage",line):

                      #过滤出需要的文字:时间、主机名、变更内容

                      tmp_list=line.strip().split(" ")

                      action_time=tmp_list[0]+tmp_list[1]+"  "+tmp_list[2]+" "+tmp_list[3]

                      hostname=tmp_list[4]

                      change=line.strip().split("(")[1]

                      print  action_time +"  "+hostname+"  "+change

                  else:

                      continue

              fileList=[]

              with open (line_file,'w') as fileHandle_line_w:

                  #保存本次读到的文件位置

                  fileHandle_line_w.write(str(end_line))

          except:

              #start_line=int(end_line*0.5)

              fileHandle_line_w.write(str(end_line))

              raise "ERROR 读取文件失败%s" %(log_file)

      #以5s为间隔,再次从上次记录的位置读取文件

      time.sleep(5)

except:

  with open (line_file,'w') as fileHandle_line_w:

      #异常时保存本次读到的文件位置

      fileHandle_line_w.write(str(end_line))

Puppet 学习系列:

Puppet 学习一:安装及简单实例应用

Puppet学习二:简单模块配置和应用

有关Puppet agent端三种备份恢复方案探讨研究
选择更安全的方式注册你的Puppet节点
通过配置SSH深刻理解Puppet的语法及工作机制
Puppet利用Nginx多端口实现负载均衡
CentOS(5和6)下Puppet的C/S模式实例

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]

Python脚本获取Linux系统信息

在Ubuntu下用Python搭建桌面算法交易研究环境

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

本文永久更新链接地址:

相关内容