# vim:tabstop=4 shiftwidth=4 softtabstop=4# Copyright states government as represented by the# Administrator of the National Aeronautics and Space administration.# Copyright, Justin Santa barbara# All rights reserved.# Copyrigh T (c) Citrix Systems, inc.## Licensed under the Apache License, Version 2.0 (the "License"); You may# don't use this file except in compliance with the License. obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## unless required by a Pplicable law or agreed to in writing, software# distributed under the License are distributed on a "as is" BASIS, with out# warranties or CONDITIONS of any KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.import Smtplibim Port Os,sys,time,fcntl,structimport configparserimport commandsimport tracebackimport socketimport shutilimport Sysimport Retry:from Email.mime. multipart import mimemultipartexcept:from email. Mimemultipart Import mimemultiparttry:from email.mime.text import mimetextexcept:from email. Mimetext Import mimetexttry:from hashlib import md5except:from MD5 import Md5config_path = "/etc/rsyncd.conf" def _ Load_cfg (section = None): "' Load config section from Config_path '" global config_path config = Configpa Rser. Configparser () if Os.path.isfile (Config_path): Config.read (config_path) sections = Config.sections () If sections and sections in Sections:return dict (Config.items (sections)) Elif not section:r st = Dict () for sec in sections:rst[sec] = Dict (Config.items (SEC)) return rst Else:return dict () def load_cfg (sections = []): ' From CFG format to dict:p arams sections The sect Ion list in CFG return a dict with all options ' "rst = Dict () if not Sections:return _loAd_cfg () for sec in sections:rst[sec] = _load_cfg (sec) return rstdef logging (item,level,mes): LogPath = ' /var/log/kxtools/' If not os.path.exists (LogPath): Os.makedirs (logpath) fp = open ('%s/kxbackup.log '%logpath, ' A ') Fp.write ('%s-%s-%s-%s \ n '% (Time.ctime (), item,level,mes)) Fp.close () "" "Access File MD5 value" "Def MD5 (Fnam E): Filemd5 = "" Try:file = Open (fname, "RB") md5f = MD5 () STRs = "" While True: STRs = File.read (8096) if not strs:break md5f.update (strs) FILEMD5 = MD5 F.hexdigest () File.close () return filemd5 except:logging (' MySQL Check ', ' ERROR ', traceback.format_e XC ()) def get_em_ipaddr (dev): s = socket.socket (socket.af_inet, socket. SOCK_DGRAM) IP = Socket.inet_ntoa (Fcntl.ioctl (S.fileno (), 0x8915, # siocgifaddr struct. Pack (' 24s ', dev)) [20:24]) return ipdef ipaddr (): "" "Get host Name, IP return (hostname, IP) "" "Def _get_ipaddr (): Try:s = Socket.socket (socket.af_inet, so Cket. SOCK_DGRAM) S.connect (("8.8.8.8", 8000)) return s.getsockname () [0] Except:logging (' MySQL Check ', ' ERROR ', Traceback.format_exc ()) S.close () return (Socket.gethostname (), _get_ipaddr ()) def tracert (IP): count = 0 for T in range: Sock=socket.socket (socket.af_inet,socket. Sock_stream) Sock.settimeout (Ten) Try:sock.connect ((ip,873)) Count + = 1 except Socket.error,e:pass Time.sleep (1) if Count >= 6:return 0 Else:return 1class S Endmail (object): Def __init__ (self): Self.smtpserver = ' smtp.163.com ' Self.fro = ' [email protected] ' Self.c_code = ' UTF-8 ' self.port = Self.auth = ' [email protected] ' self.pawd = ' [Emai L protected] "Send Email" def sendMail (self,to,subject,content,cc= ", files=[]): msg = Mimemultipart () msg[' subject '] = subject txt = MI Metext (content) msg.attach (TXT) HD = smtplib. SMTP (Self.smtpserver,self.port) Hd.ehlo () Hd.starttls () Hd.ehlo () if Self.auth and self.pawd: Hd.login (SELF.AUTH,SELF.PAWD) for T in To.split (","): Hd.sendmail (Self.fro,t, msg.as_string () If Cc:for T in Cc.split (","): Hd.sendmail (Self.fro,t, msg.as_string ()) Hd.close () class Checkmysql (object): Def __init__ (self): Version = 1.0 def checkrsyncconfig (self): # Rsync conf Iguration Check fp = open ('/etc/rsyncd.conf ', ' R '). ReadLines () for n,s in Enumerate (FP): if n = = 0 : if s[0]! = ' [': Fp.insert (0, ' [global]\n ') f = open ('/etc/rsyncd.conf ', ' W ') F.writelines (FP) f.close () def checkfile (self): # Directory TraversAL, find today compared with yesterday ' s file Backuppath = load_cfg () [' Yttx '] [' path '] Os.chdir (backuppath) CTIME = Time.strftime ('%y%m%d ') files= dict () # Define The keys for F in Os.listdir (backuppath ): stamp_time, File_kyes = Self._get_file_ctime (f) if (Time.time ()-Stamp_time) <= 86400: Files[file_kyes] = list () # According to the date of classified documents for F in Os.listdir (Backu Ppath): stamp_time, file_ctime = Self._get_file_ctime (f) stamp_ctime = Time.time () for T M in Files.keys (): #print file_ctime,f If TM = = File_ctime and (stamp_ctime-stamp_time) &L T;= 86400:files[tm].append (f) # Check the file MD5 today_file =dict () ystday_file = Dict () ttime = Time.strftime ("%y%m%d") Ytime = Time.strftime ("%y%m%d", Time.localtime ((Time.time ()-86400 )))) for the F in OS.Listdir (backuppath): If Ttime ==f.split ('_') [0]: IP = f.split ('_') [1] today_file[ Ip]=f elif Ytime = = F.split ('_') [0]: IP = f.split ('_') [1] ystday_file[ip] = f Else:pass # Read The latest IP list backup_list = '/data0/backup_mysql.txt ' I F not os.path.exists (backup_list): Utils. COMM ("Touch%s"%backup_list) fp = open ('/data0/backup_mysql.txt ', ' R '). ReadLines () ips = [I.strip () for I in FP] fail_ip = list () Done_ip = list () # Call Func MD5 for IP in ips:if IP in Tod Ay_file:if not SELF._CHECKMD5 (Backuppath,today_file[ip]): Fail_ip.append (IP) Else:done_ip.append (IP) else:fail_ip.append (IP) # determine whe Ther is empty, # If not empty, calls the SendMail class done_mes = str ()If Done_ip:for IP in Done_ip:done_mes + = "Host%s MySQL backup is successful \ n"%ip do Ne_info = "\nbackup successful: \ n" + done_mes ret = str () if fail_ip:for IP in fail_ip: RET + = "Host%s MySQL backup is failure \ n"%ip mess = "\nplease check the specific reason for the error \ n "+" e-mail: [email protected], [email protected],[email protected] \ n "+" Q q:11111,2222222,33333 " Back_time = "\ncheck time:%s \ n"%time.ctime () mes = Done_info + "\nbackup failure:\n" + ret + back_time + Mess send = SendMail () # Subject Subject = "MySQL Backup%s Information"%ipaddr () [1] cc = ' [E Mail protected],[email protected],[email protected] ' Send.sendmail (' [email protected] ', Subject,mes,cc= ") def _checkmd5 (self,backuppath,files): File = backuppath + '/' + files try: Md5sun = File.split ("_") [ -1].splIt ('. ') [0] except:return False fmd5sun = MD5 (file) if Fmd5sun = = Md5sun:return True Else:return False def _get_file_ctime (self,f): # return time stamp, Yy-mm-dd st_ctime = Os.stat (f). St_mtime return St_ctime, Time.strftime ('%y%m%d ', Time.localtime (st_ctime)) def deletefile (self): # Delete file from 8 day ago Reserve = '/data0/reserve/' if not os.path.exists (reserve): O S.makedirs (Reserve) Backupath = Load_cfg () [' Yttx '] [' path '] keep_file = list () Delete_file = List () Os.chdir (backupath) ctimes = Time.time () Keep_time = 691200 for f in Os.listdir (Backupath): Create_time = Os.stat (f). st_ctime if Ctimes-create_time >= keep_time:try: Shutil.move (F,reserve) except:shutil.move (F, '/dev/null ') log Ging (' MySQL Check', ' INFO ', "Delete file%s"%f) if __name__ = = "__main__": sc = checkmysql () sc.checkrsyncconfig () Sc.checkfile () Sc. DeleteFile ()
This article from "Welcome comments, Welcome to like" blog, please be sure to keep this source http://swq499809608.blog.51cto.com/797714/1412673