Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import smtplib
- import zipfile
- from email.header import Header
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import math
- import paramiko
- import datetime
- import re
- def send_email(_from, from_name, from_password, recipient, recipient_name, message):
- mail = MIMEMultipart()
- mail['From'] = Header(from_name + '\n' + _from, 'utf-8').encode()
- mail['To'] = Header(recipient_name + '\n' + recipient, 'utf-8').encode()
- mail['Subject'] = Header('{} Log迁移报告'.format(datetime.date.today()), 'utf-8').encode()
- mail.attach(MIMEText(message, 'html', 'utf-8'))
- server = smtplib.SMTP('smtp.ym.163.com', 25) # smtp默认端口25
- server.set_debuglevel(5)
- server.login(from_name, from_password)
- server.sendmail(recipient_name, [recipient, ], mail.as_string())
- server.quit()
- def get_dir_size(dir):
- size = 0
- for root, dirs, files in os.walk(dir):
- size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
- # 单位kb
- size = math.ceil(size / 1024)
- return size
- class Disk(object):
- __path = '/'
- __disk = ''
- ssh = None
- __sftp = None
- server_info = {}
- def __init__(self, *args, **kwargs):
- if args:
- self.__path = args[0]
- self.__disk = os.statvfs(self.__path)
- def capacity(self):
- # 磁盘容量
- return self.__disk.f_blocks * self.__disk.f_bsize
- def remaining_capacity(self):
- # 磁盘剩余容量
- return self.__disk.f_bavail * self.__disk.f_bsize
- def remaining_capacity_ratio(self):
- # 磁盘剩余容量比例
- return self.remaining_capacity() / self.capacity()
- def get_server_info(self):
- # df 命令单位kb
- stdin, stdout, stderr = disk.ssh.exec_command('df ' + REMOTELY_PATH)
- info = stdout.readlines()[1]
- info = info.split(' ')
- for _ in info:
- info.remove('')
- self.server_info['used'] = int(info[1])
- self.server_info['available'] = int(info[2])
- self.server_info['percentage'] = float(info[4][0:-1:1]) / 100
- return self.server_info
- def gets_the_servers_remaining_capacity(self):
- self.get_server_info()
- return self.server_info['available']
- def get_file_size(self, path, file):
- return os.path.getsize(path + file)
- def zip_file(self, path, file):
- zip_file = zipfile.ZipFile(path + file + '.zip', 'w')
- zip_file.write(path + file, file)
- zip_file.close()
- os.remove(path + file)
- def connect_to_the_server(self, host, port, username, password):
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(host, port, username, password)
- self.ssh = ssh
- self.__sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
- return True
- def close_connect(self):
- if self.__sftp is not None:
- self.__sftp.close()
- self.__sftp = None
- if self.ssh is not None:
- self.ssh.close()
- self.ssh = None
- def upload_file(self, path, file, **kwargs):
- try:
- remotely_path = kwargs['remotely_path']
- except KeyError:
- remotely_path = '/'
- try:
- remotely_name = kwargs['remotely_name']
- except KeyError:
- remotely_name = file
- if os.path.isfile(path + file):
- if self.__sftp is None:
- print('请先调用 connect_to_the_server 方法')
- return False
- else:
- # print('upload {} to {}'.format(path + file, remotely_path + remotely_name))
- # self.__sftp.put(path + file, remotely_path + remotely_name)
- return True
- else:
- print('请检查路径 -> \'{}\''.format(path + file))
- return False
- def get_file_list(self, path):
- result = []
- list = os.listdir(path)
- for i in list:
- print('filename : {}, match : {}'.format(i, MATCH.match(i)))
- if i[0] == '.' or MATCH.match(i):
- pass
- else:
- result.append(i)
- return result
- def upload_the_file_within_the_path(self, path, remotely_path, file):
- if not os.path.isdir(path + file):
- self.zip_file(path, file)
- file = file + '.zip'
- self.upload_file(path, file, remotely_path=remotely_path)
- os.remove(path + file)
- return None
- else:
- path = path + file + '/'
- remotely_path = remotely_path + file + '/'
- file_list = self.get_file_list(path)
- for file in file_list:
- stdin, stdout, stderr = self.ssh.exec_command('mkdir ' + remotely_path)
- if stdout.readlines() == []:
- self.upload_the_file_within_the_path(path, remotely_path, file)
- if __name__ == '__main__':
- # ---------------------------目标服务器账号---------------------------
- HOST = '120.26.100.13'
- PORT = 1105
- USER = 'buhao'
- PASSWORD = 'buhao@123!'
- # ---------------------------目标服务器账号---------------------------
- THRESHOLD = 0 # 剩余磁盘空间小于这个值时会进行传递文件操作
- PATH = '/Users/xuzhaoning/Documents/' # 本地目录
- FILE = 'test'
- REMOTELY_PATH = '/home/buhao/xzn_test/' # 上传服务器目录(要有这个目录)
- # 正则匹配规则 今日log不同步
- # MATCH = re.compile('[^\d]*' + str(datetime.date.today()) + '[^\d]*')
- MATCH = re.compile('.*' + str(datetime.date.today()) + '.*')
- disk = Disk()
- if disk.remaining_capacity_ratio() < 1.0:
- disk.connect_to_the_server(HOST, PORT, USER, PASSWORD)
- if get_dir_size(PATH + FILE) < disk.gets_the_servers_remaining_capacity() and disk.get_server_info()[
- 'percentage'] > 0.1:
- disk.upload_the_file_within_the_path(PATH, REMOTELY_PATH, FILE)
- else:
- # TODO 其他通知处理
- pass
- disk.close_connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement