log:
2018/11/04 02:09:58 [11212] connect from joomla-site-yuanxi (19
2018/11/04 02:09:58 [11212] rsync to clxy_web/web/Sunday/ from rsync_user@joomla-site-yuanxi (1
2018/11/04 02:40:06 [12864] sent 10185 bytes received 2895278 bytes total size 2939121
2018/11/04 02:09:58 [11212] sent 23 bytes received 49 bytes total size 0
2018/11/04 02:09:58 [11214] connect from joomla-site-yuanxi (192.1
2018/11/04 02:09:58 [11214] rsync to clxy_web/web/current from rsync_user@joomla-site-yuanxi (19
2018/11/04 02:09:58 [11214] receiving file list
2018/11/04 02:09:59 [11214] sent 3719 bytes received 329620 bytes total size 217691468
2018/11/04 02:14:58 [11482] connect from joomla-site-yuanxi (192
2018/11/04 02:14:58 [11482] rsync to office_web/web/Sunday/ from rsync_user@joomla-site-yuanxi (192.1
2018/11/04 02:14:58 [11482] receiving file list
2018/11/04 02:14:58 [11482] sent 23 bytes received 49 bytes total size 0
2018/11/04 02:14:58 [11484] connect from joomla-site-yuanxi (192.168.
2018/11/04 02:14:58 [11484] rsync to office_web/web/current from rsync_user@joomla-site-yuanxi (192.16
2018/11/04 02:14:58 [11484] receiving file list
2018/11/04 02:14:58 [11484] sent 2620 bytes received 254522 bytes total size 69138548
2018/11/04 02:24:58 [12031] connect from joomla-site-yuanxi (192.16
2018/11/04 02:24:58 [12032] connect from joomla-site-yuanxi (192.1
2018/11/04 02:24:58 [12033] connect from campus-wwww (192.16
2018/11/04 02:24:58 [12031] rsync to jgxy_web/web/Sunday/ from rsync_user@joomla-site-yuanxi (
2018/11/04 02:24:58 [12032] rsync to wfxy_web/web/Sunday/ from rsync_user@joomla-site-yuanxi (
2018/11/04 02:24:58 [12033] rsync to www_web/web/Sunday/ from rsync_user@campus-wwww (192
2018/11/04 02:24:58 [12031] receiving file list
2018/11/04 02:24:58 [12032] receiving file list
2018/11/04 02:24:58 [12033] receiving file list
2018/11/04 02:24:58 [12031] sent 23 bytes received 49 bytes total size 0
2018/11/04 02:24:58 [12032] sent 23 bytes received 49 bytes total size 0
2018/11/04 02:24:58 [12037] connect from joomla-site-yuanxi (1)
2018/11/04 02:24:58 [12038] connect from joomla-site-yuanxi ()
2018/11/04 02:24:58 [12033] sent 23 bytes received 49 bytes total size 0
2018/11/04 02:24:58 [12039] connect from campus-wwww )
2018/11/04 02:24:58 [12037] rsync to jgxy_web/web/current from rsync_user@joomla-site-yuanxi (.............)
2018/11/04 02:24:58 [12038] rsync to wfxy_web/web/current from rsync_user@joomla-site-yuanxi ()
2018/11/04 02:24:58 [12039] rsync to www_web/web/current from rsync_user@campus-wwww ()
2018/11/04 02:24:58 [12037] receiving file list
2018/11/04 02:24:58 [12038] receiving file list
2018/11/04 02:24:58 [12039] receiving file list
2018/11/04 02:24:59 [12038] sent 4116 bytes received 361511 bytes total size 249585747
2018/11/04 02:24:59 [12037] sent 4100 bytes received 364106 bytes total size 219381539
2018/11/04 02:24:59 [12039] sent 42690 bytes received 668352 bytes total size 237763956
2018/11/04 02:40:00 [12851] connect from joomla-site-yuanxi (..........................)
2018/11/04 02:40:00 [12851] rsync to clxy_web/db/ from rsync_user@joomla-site-yuanxi (.......................)
2018/11/04 02:40:00 [12851] receiving file list
2018/11/04 02:40:00 [12851] sent 12861 bytes received 4317598 bytes total size 4544221
2018/11/04 02:40:02 [12858] connect from joomla-site-yuanxi (192
2018/11/04 02:40:02 [12858] rsync to office_web/db/ from rsync_user@joomla-site-yuanxi (................)
2018/11/04 02:40:02 [12858] receiving file list
2018/11/04 02:40:02 [12858] sent 7065 bytes received 48468 bytes total size 1367105
2018/11/04 02:40:02 [12860] connect from campus-wwww (......................
2018/11/04 02:40:02 [12860] rsync to www_web/db/ from rsync_user@campus-wwww (.................)
2018/11/04 02:40:02 [12860] receiving file list
2018/11/04 02:40:03 [12860] sent 18381 bytes received 8569894 bytes total size 9427853
2018/11/04 02:40:04 [12862] connect from joomla-site-yuanxi (.....................)
2018/11/04 02:40:04 [12862] rsync to jgxy_web/db/ from rsync_user@joomla-site-yuanxi (.............)
2018/11/04 02:40:04 [12862] receiving file list
2018/11/04 02:40:05 [12862] sent 14463 bytes received 3321543 bytes total size 5763078
2018/11/04 02:40:06 [12864] connect from joomla-site-yuanxi (..........1)
2018/11/04 02:40:06 [12864] rsync to wfxy_web/db/ from rsync_user@joomla-site-yuanxi (........)
2018/11/04 02:40:06 [12864] receiving file list
2018/11/04 02:40:06 [12864] sent 10185 bytes received 2895278 bytes total size 2939121
结果:
import csv
import re
import sys
import os
ZABBIX_CONFIG = 'zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k'
#全局变量声明:主要是正则表达式匹配格式:
# pattern1的作用是匹配网站名称,如果害怕此处不精确可以\w*_web/进行定位
pattern1 = re.compile(r' \w*/')
# pattern2的作用是将网站名称进行优化去除空格和'/'
pattern2 = re.compile(r'\w+')
# 找出线程名称
pattern3 = re.compile(r'\[\d+\]')
#得到传输数字,去除字母与空格,考虑输入值五匹配情况用-1表示
pattern4 = re.compile(r'\d+|-\d+')
# 声明两个list分别放web db 三种数据
quantity_web = []
quantity_db = []
webAndDb = []
# 用csv读文件
def readCsv():
csvFile = open('rsyncd.log', 'r')
csvData = csv.reader(csvFile)
return csvData
# 找到所需数据的线程名称,放在list中
def findThread(input_value):
data = readCsv()
thread = []
for item in data:
item1 = ''.join(item)
result = pattern1.findall(item1)
web_name = pattern2.findall(''.join(result))
if result and ''.join(web_name) == input_value:
# print(item1)
thread.append(pattern3.findall(item1))
return thread
# 匹配线程,根据条件将相同的线程log句子放在一起(thread[1]代表web,thread[2]代表db)
# 分别放至全局变量quantity_web,quantity_db中
def dataQuantity(thread):
data = readCsv()
num = 0
if len(thread):
for item in data:
item1 = ''.join(item)
if thread[0] == pattern3.findall(item1):
continue
# 若担心隐患,则用-1,-2表示
elif thread[1] == pattern3.findall(item1):
quantity_web.append(item)
elif thread[2] == pattern3.findall(item1):
quantity_db.append(item)
return 1
# name[-1]指的是,每个线程最后一句对应的是传输数据情况
def getResult(name):
if name:
data_SR = name[-1]
# print((data_SR))
result = re.findall("[a-z]+ \d+",''.join(data_SR))
else:
result = ['-1','-1','-1']
return result
# 显示list,如需要返回值,则直接return
def show(list):
for item in list:
print(item)
# 拼接基础字符串格式:send -o "5689" web拼接
def joinStr1(result):
# 需要字符串的共同串,定义后进行拼接
str = ZABBIX_CONFIG + ' rsync_statu.web.'
str3 = ['send.','receive.','total.']
#拼接
for index in range(len(result)):
webAndDb.append(str + str3[index] + '[' + input_value + '] -o '+ '"' + result[index] + '"')
# 拼接基础字符串格式: send -o "5689" db拼接
def joinStr2(result):
# 需要字符串的共同串,定义后进行拼接
str = ZABBIX_CONFIG + ' rsync_statu.db.'
str3 = ['send.', 'receive.', 'total.']
#拼接
for index in range(len(result)):
webAndDb.append(str + str3[index] + '[' + input_value + '] -o ' + '"' + result[index] + '"')
if __name__ == '__main__':
# 命令行参数
# input_value = input("输入网站名称(如:clxy_web):")
if len(sys.argv)>=2:
input_value = str(sys.argv[1])
else:
exit(0)
dataQuantity(findThread(input_value))
web_result = pattern4.findall(''.join(getResult(quantity_web)))
db_result = pattern4.findall(''.join(getResult(quantity_db)))
joinStr1(web_result)
joinStr2(db_result)
show(webAndDb)
for item in webAndDb:
os.system(item)