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()

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