1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#脚本功能:
#1. 实现自定义key
#2. 将文件同步检查结果insert into MySQL数据库
import os
import re
import time
import pymysql
import datetime
import configparser
def check_sync(path,string):
check_sync_result=0
read_log=open(path,mode='r',encoding='UTF-8')
conent=read_log.readlines()
for index in range(len(conent)):
if string in conent[index]:
check_sync_result=1
read_log.close()
return(check_sync_result)
def get_sync_date(path):
info = os.stat(path)
time_local=time.localtime(info.st_mtime)
file_change_date=time.strftime("%Y-%m-%d",time_local)
return(file_change_date)
def check_sync_result():
config=configparser.ConfigParser()
config.read(r"C:\Zabbix\windows\server_info.ini")
items=config.items("Sync_Path")
sync=[]
for index in range(len(items)):
path_list=re.split(r'[,]',items[index][1])
#skip empty path
if str(items[index][1])=="":
pass
#if not exist sync result log, sync result is 0
elif os.path.exists(path_list[0].strip())==False:
sync.append(0)
#if sync log is not today, sync result is 0
elif str(get_sync_date(path_list[0].strip()))!=str(datetime.date.today()):
sync.append(0)
#if sync log not contains pass result, sync result is 0
elif check_sync(path_list[0].strip(),path_list[1].strip())==0:
sync.append(0)
else:
sync.append(1)
return(sync)
if __name__ == '__main__':
#check sync result
sync=check_sync_result()
sync_total=0
for index in range(len(sync)):
sync_total=sync_total+sync[index]
if sync_total<len(sync):
sync_result=0
else:
sync_result=1
#get now time
now_time=time.strftime("%Y-%m-%d %H:%M:%S"),time.localtime()
now_time_string="\'"+str(now_time[0])+"\'"
#get server ip
config=configparser.ConfigParser()
config.read(r"C:\Zabbix\windows\server_info.ini")
IP=config.get("Server_Info","Back_IP")
IP_string="\'"+str(IP)+"\'"
#insert into db
sql="INSERT INTO SYNCTABLE (IP, TIME, SYNC_STATUS) VALUES (%s,%s,%s)" % (IP_string,now_time_string,sync_result)
db=pymysql.connect("192.168.123.211","root","password","SYNCFILES")
cursor=db.cursor()
cursor.execute(sql)
db.commit()
db.close()
#print sync result
print(sync_result)
|