手机版
你好,游客 登录 注册
背景:
阅读新闻

监控Puppet日志的Python脚本

[日期:2014-08-15] 来源:Linux社区  作者:hh2o [字体:大 中 小]

监控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 学习一:安装及简单实例应用 http://www.linuxidc.com/Linux/2013-08/88710.htm

Puppet学习二:简单模块配置和应用 http://www.linuxidc.com/Linux/2013-08/88711.htm

相关阅读:

有关Puppet agent端三种备份恢复方案探讨研究 http://www.linuxidc.com/Linux/2013-07/87885.htm
选择更安全的方式注册你的Puppet节点 http://www.linuxidc.com/Linux/2013-07/87884.htm
通过配置SSH深刻理解Puppet的语法及工作机制 http://www.linuxidc.com/Linux/2013-07/87882.htm
Puppet利用Nginx多端口实现负载均衡 http://www.linuxidc.com/Linux/2013-02/79794.htm
CentOS(5和6)下Puppet的C/S模式实例 http://www.linuxidc.com/Linux/2011-12/50502.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105584.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款