#!/usr/bin/python
# -*- coding: gbk -*-
#@PyName :
#@author : whx
#date : 2012-1-8
#描述 :
#version : 1.0
#-------------------------------------------------------------------------------
import MySQLdb
import re
import ConfigParser
import os
import MyLog
Def_Mark = "请尽快处理(您可以点击这里参考告警类型说明及处理建议)。 本邮件为系统推送,请不要直接回复。如有任何问题,请联系我们"
Def_DateCompile = re.compile('告警发生时间:(.+)')
Def_IPCompile = re.compile('告警内容:(.+) cpu')
Def_ValueCompile = re.compile('使用率持续高负载 当前值:(\d+)')
##文件名
#Def_FileName = r"D:\Documents and Settings\Administrator\桌面\11\4.txt"
#log初始化
Log = MyLog.Log()
g_cursor = None
g_conn = None
#----------------- 读配置---------------
configInfo = ConfigParser.RawConfigParser()
fobj = open("dbConfig.ini")
configInfo.readfp(fobj)
Def_DBIp = configInfo.get('Database', 'dbIp')
Def_DBName = configInfo.get('Database', 'database')
Def_TableName = configInfo.get('Database', 'tableName')
Def_DBPort = int(configInfo.get('Database', 'port'))
Def_DBUser = configInfo.get('Database', 'user')
Def_DBPwd = configInfo.get('Database', 'pwd')
Def_FileName = configInfo.get('Database', 'filePath')
fobj.close()
#----------------------------------------
## 连接数据库
# @parma None None
## return None
def ConMysql():
global g_cursor
global g_conn
try:
g_conn = MySQLdb.connect(host = Def_DBIp, port = Def_DBPort, db = Def_DBName,
user = Def_DBUser, passwd = Def_DBPwd)
except MySQLdb._mysql.OperationalError, e:
raise MySQLdb._mysql.OperationalError, str(e) + '\nConnect MySQL error'
g_cursor = g_conn.cursor()
return
## 提交信息
# @parma None None
## return None
def Commit():
try:
g_conn.commit()
except Exception, e:
raise e + '\nCommit SQL error'
return
## 执行sql语句
# @parma None None
## return None
def Execute(SQL):
'''
`查询语句 (开头是select\Select\SELECT)返回结果列表( 空 返回() )
`执行语句 返回影响条数
'''
try:
g_cursor.nextset()
count = g_cursor.execute(SQL)
except Exception, e:
raise str(e) + '\nExecute SQL error:' + SQL
if re.match('[select|Select|SELECT|show]', SQL):
return g_cursor.fetchall()
else:
Commit()
return count
## 关闭数据库
# @parma None None
## return None
def CloseDB():
g_cursor.close()
g_conn.close()
return
## 获取文件信息
# @parma None None
## return 每封邮件信息
def GetFileInfo():
try:
tagFile = open(Def_FileName, 'r')
info = tagFile.read()
tagFile.close()
except Exception, e:
print e
Log.Error("GetFileInfo: fileName = %s, e=%s"%(Def_FileName, str(e)))
return None
return info
## 获取每个邮件片段
# @parma None None
## return 每封邮件信息列表
def GetTagInfoList():
fileInfo = GetFileInfo()
if not fileInfo:
return
tagInfoList = fileInfo.split(Def_Mark)
if len(tagInfoList) <= 0:
return
return tagInfoList
## 获取需要的信息列表
# @parma tagInfo 每条记录的信息
## return 信息列表
def GetInfoList(tagInfo):
dateInfo = Def_DateCompile.findall(tagInfo)
ip = Def_IPCompile.findall(tagInfo)
value = Def_ValueCompile.findall(tagInfo)
if len(dateInfo) <= 0 or len(ip) <= 0 or len(value) <= 0:
Log.Error("GetInfoList: info = %s"%tagInfo)
return []
return [dateInfo[0], ip[0], value[0]]
## 主函数
# @parma None None
## return None
def Main():
print "begin runing......"
tagInfoList = GetTagInfoList()
if len(tagInfoList) <= 0:
Log.Error("the file is null")
return
print "connect db...."
#连接数据库
ConMysql()
print "add data, please wainting...."
#解析每条邮件
for info in tagInfoList:
tagList = GetInfoList(info)
if len(tagList) <= 0:
continue
#添加到数据库
sqlCmd = "insert into %s (%s,%s,%s) values('%s', '%s', %s)"% \
(Def_TableName, 'ip_addr', 'alarm_time', 'cpu_top', tagList[0], tagList[1], tagList[2])
#插入新表
Execute(sqlCmd)
CloseDB()
print "OVER....."
return
if __name__ == "__main__":
Main()