#!/usr/bin/env python
#coding:utf-8
#create by 、礦泉水 2015/8/4
import os,sys,hashlib
#檢測輸入參數是否滿足執行條件
def Value():
if len(sys.argv) == 2:
pass
else:
print 'Usage: %s <monitor file path>'%(sys.argv[0])
exit(2)
Value()
CURRENT_FILE_NAME = os.path.basename(sys.argv[1])
#當前文件的MD5值
FILE_NAME = sys.argv[1]
FILE_NAME = open(FILE_NAME)
FILE_NAME_READ = FILE_NAME.read()
NOW_FILE_NAME = hashlib.md5(FILE_NAME_READ)
CURRENT_FILE_MD5 = NOW_FILE_NAME.hexdigest()
FILE_NAME.close()
#追加當前md5值至/tmp/check_file_notupdate_current_file_md5_xxx文件
def AddMd5():
CURRENT_MD5 = open('/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME,'a+')
CURRENT_MD5.write(CURRENT_FILE_MD5)
CURRENT_MD5.write('\n')
CURRENT_MD5.close()
#打開/tmp/check_file_notupdate_current_file_md5_xxx文件
def OpenMd5():
CURRENT_MD5 = open('/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME)
CURRENT_MD5_READ = CURRENT_MD5.read()
CURRENT_MD5_READ = CURRENT_MD5_READ.split()
CURRENT_MD5.close()
CURRENT_MD5 = open('/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME)
NOW_CURRENT_MD5 = CURRENT_MD5.read()
NOW_CURRENT_MD5 = NOW_CURRENT_MD5.split()
if len(NOW_CURRENT_MD5) < 3:
AddMd5()
print 'WARNING: file has been updated'
exit(2)
else:
#比對文件最後三個值的內容是否一致,如果一致則清空文件並返回正常狀態,如果不一致則返回異常狀態
VALUE = NOW_CURRENT_MD5[-1] in NOW_CURRENT_MD5[-2] in NOW_CURRENT_MD5[-3]
if VALUE == True:
CURRENT_MD5 = open('/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME,'w')
CURRENT_MD5.write(NOW_CURRENT_MD5[-1])
CURRENT_MD5.write('\n')
CURRENT_MD5.close()
CURRENT_MD5 = open('/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME,'w')
CURRENT_MD5.write(CURRENT_FILE_MD5)
CURRENT_MD5.write('\n')
CURRENT_MD5.close()
if NOW_CURRENT_MD5[-1] == CURRENT_FILE_MD5:
print 'OK: File is not updated'
exit(0)
else:
print 'WARNING: file has been updated'
exit(2)
elif VALUE == False:
AddMd5()
print 'WARNING: file has been updated'
exit(2)
#判斷/tmp/check_file_notupdate_md5_xxx文件是否存在,不存在則新建
def OpenFile():
FILE_PATH = os.path.exists(r'/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME)
if FILE_PATH == True:
pass
elif FILE_PATH == False:
print 'ERROR: /tmp/check_file_notupdate_md5_%s File does not exist, have to help you create, the next operation to read data, please run again'%CURRENT_FILE_NAME
FILE_MD5 = open('/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME,'w')
FILE_MD5.close()
exit(2)
OpenFile()
#判斷/tmp/check_file_notupdate_md5_xxx文件是否有數據,如果沒有數據追加當前文件的MD5值至文件
def CurrentMd5():
FILE_MD5_OPEN = open('/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME)
FILE_MD5 = FILE_MD5_OPEN.read()
FILE_MD5_OPEN.close()
if len(FILE_MD5) != 0:
pass
else:
FILE_MD5_OPEN = open('/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME,'w')
FILE_MD5_OPEN.write(CURRENT_FILE_MD5)
FILE_MD5_OPEN.write('\n')
FILE_MD5_OPEN.close()
print 'ERROR: /tmp/check_file_notupdate_md5_%s The file is empty, has upped the MD5 value of the current file into, please run again'%CURRENT_FILE_NAME
exit(2)
CurrentMd5()
#判斷/tmp/check_file_notupdate_current_file_md5_xxx文件是否存在,不存在則新建
def CurrentMd5File():
FILE_PATH = os.path.exists(r'/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME)
if FILE_PATH == True:
pass
elif FILE_PATH == False:
print 'ERROR: /tmp/check_file_notupdate_current_file_md5_%s File does not exist, have to help you create, the next operation to read data, please run again'%CURRENT_FILE_NAME
CURRENT_MD5 = open('/tmp/check_file_notupdate_current_file_md5_%s'%CURRENT_FILE_NAME,'w')
CURRENT_MD5.close()
exit(2)
CurrentMd5File()
#文件存儲的上一次MD5值
FILE_MD5_OPEN = open('/tmp/check_file_notupdate_md5_%s'%CURRENT_FILE_NAME)
FILE_MD5 = FILE_MD5_OPEN.read()
FILE_MD5 = FILE_MD5.split()[0]
FILE_MD5_OPEN.close()
if FILE_MD5 == CURRENT_FILE_MD5:
print 'OK: File is not updated'
exit(0)
else:
OpenMd5()