周期性监控某一文件并写入数据库脚本

import time
import datetime
import pymysql
import os
from apscheduler.schedulers.blocking import BlockingScheduler


def insert_log():
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='servicetb', charset='utf8')
    cursor = conn.cursor()
    dt = ''
    today = datetime.date.today()
    tomorrow = '%s 00:00:00' % str(today + datetime.timedelta(days=1))  # 取后一天的时间
    while dt < tomorrow:
        time_local = time.localtime(int(time.time()))
        dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
        year = time_local[0]
        if time_local[1]/10 < 1:
            month = '0%s' % time_local[1]
        else:
            month = time_local[1]
        if time_local[2]/10 < 1:
            day = '0%s' % time_local[2]
        else:
            day = time_local[2]
        file_path = 'C:\\Users\\admin-x\Desktop\\tb_audit\proxy\\%s%s\\proxy_%s.log' % (year, month, day)
        print file_path
        if os.path.exists(file_path):
            print 'file exits,start loading file'
            with open(file_path, 'r') as f:
                while dt < tomorrow:
                    line = f.readline()
                    if line:
                        print line
                        if 'start hop' in line:
                            IP = line.split('start hop')[1].split(':')[0].strip()
                            Time = line.split(' ')[5] + ' ' + line.split(' ')[6][:-7]
                            cursor.execute('SELECT VmId, ImageId FROM t_vmmaptable WHERE VmIp="%s"' % IP)
                            data = cursor.fetchone()
                            VmId = data[0]
                            ImageId = data[1]
                            cursor.execute('INSERT INTO t_hoplogs(dtime,ImageId,VmId) VALUES ("%s","%s","%s")' % (Time, ImageId, VmId))
                            conn.commit()
                    else:
                        time.sleep(5)
                    time_local = time.localtime(int(time.time()))
                    dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)

        time.sleep(5)
    cursor.close()
    conn.close()

if __name__ == '__main__':
    insert_log()
    sched = BlockingScheduler()
    sched.daemonic = False
    sched.add_job(insert_log, 'cron', day_of_week='0-6', hour=01, minute=00, second=00)
    sched.start()

你可能感兴趣的:(Python,MySQL,Linux)