200706干活笔记(修智能日志)

python
python分割多空格字符串
长度不同的空格分割的字符串,如果直接用str.split(" ")只会分割一个空格
直接用split()就好,它会默认按空格分割并把结果中的空字符串删掉。

linux
复制文件
cp dir1/a dir2

SQL
查询某字段不为某值的行
select * from table+name where row not like “ABC”;

HTML
在控制台输出信息
console.log()
获取当前节点的html包括当前节点的方法
jQuery.html()是获取当前节点下的html代码,并不包含当前节点本身的代码
.prop(“outerHTML”)获取包含当前节点本身的代码以及当前节点下的html代码
jquery添加子元素
$(‘body’).append(‘html字符串’);
jquery改变html元素内容
$(‘body’).html(content)

js
从字符串中抽取从start下标开始的指定数目的字符
stringObject.substr(start,length)
是length,不是end

message出现转义和//“的转义
sb.append(""").append(rs.getString(4).replace("\","\\").replace(""","\"")).append(""");
tbody id没赋值

<tbody style="max-width:1260px" id="example_body">

读写日志代码(cron)

#! /usr/local/bin/python3
from pymysql import connect

conn_cx = connect(host='10.10.108.65', port=3306, user='root', passwd=str("123456"), db="ZY", charset='utf8')
def log_reader(row):
    cursor_insert = conn_cx.cursor()
    a=row.split()
    time=row[:15]
    hostname=a[3]
    user=a[4].strip(":")
    message=":".join(row.split(":")[3:])[1:]
    source="/var/log/message"
    tap="physical machine"
    #time,hostname,user,message,source,tap(data type,i.e. "Linux log system runtime state" )
    cursor_insert.execute("INSERT INTO log_91_cron (time,hostname,user,message,source,tap) VALUES (%s,%s,%s,%s,%s,%s);",
                          (time, hostname, user, message,source,tap))
    conn_cx.commit()

import fileinput
import time
import os

target_file = '/var/log/cron-20200705'
init_flag = True  # 初次加载程序
time_kick = 5
record_count = 0

while True:
    print('当前读到了', record_count)
    #没有日志文件,等待
    if not os.path.exists(target_file):
        print('target_file not exist')
        time.sleep(time_kick)
        continue

    try:
        if init_flag:
            #读取整个文件
            for eachline in fileinput.input(target_file):
                #print(eachline)
                log_reader(eachline)
                record_count += 1
            init_flag = False
        else:
            #如果总行数小于当前行,那么认为文件更新了,从第一行开始读。
            total_count = os.popen('wc -l %s' % target_file).read().split()[0]
            total_count = int(total_count)
            if total_count < record_count:
                record_count = 0

            for eachline in fileinput.input(target_file):
                line_no = fileinput.filelineno()
                #print(line_no)
                if line_no > record_count:
                    # print(eachline)
                    log_reader(eachline)
                    record_count += 1

    except:
        pass
    time.sleep(time_kick)      
conn_cx.close()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章