# coding=utf8 import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import sys import pymysql sender = '' # 发件人邮箱登录账号 sender_pass = '' # 发件人邮箱登录密码 fail_receiver = [] def send_mail(sender, sender_pass, receiver, content): SMTP_SERVER = 'mail.broada.com' message = MIMEMultipart() message.attach(MIMEText(content, "plain", "utf-8")) message['from'] = sender message['to'] = receiver message['subject'] = '虚拟机使用情况检查结果'.decode('utf8') server = smtplib.SMTP_SSL(SMTP_SERVER, 465) server.set_debuglevel(1) server.login(sender, sender_pass) server.sendmail(from_addr=sender, to_addrs=[receiver], msg=message.as_string()) server.quit() print("邮件发送结束") def get_vms(): global excludes,raw_num,add_data,update_data,upper_data db = pymysql.connect('', port=3306, user='root', passwd='', db='vm_resource') vm_infos={} vm_users={} sql = "select t.vmip,t.owner from (select a.vmip,a.vmname,a.owner,a.vmcreatetime,a.vmreleasetime,a.lastpowertime," "(select ROUND(sum(b.value)/count(b.value),2) from vm_memory b where a.vmip=b.ip) value," "IFNULL((SELECT status from vm_status where ip=a.vmip), '停机') status " "from vminfo a where a.status='used' and a.owner not in('unknown','',' ' ) ) t where t.value<=10" with db.cursor() as cursor: cursor.execute(sql) datas = cursor.fetchall() for i in datas: if i[1] in excludes: continue #数据库记录数修改 if raw_num.get(i[0]) and int(raw_num.get(i[0]))>=3: upper_data.update({i[0]:int(raw_num.get(i[0]))+1}) continue elif not raw_num.get(i[0]): add_data.update({i[0]:1}) elif raw_num.get(i[0]) and int(raw_num.get(i[0]))<3: update_data.update({i[0]:int(raw_num.get(i[0]))+1}) if not vm_infos.get(i[1]): vm_infos.update({i[1]: []}) vm_infos[i[1]].append(i[0]) user = i[1].split('@')[0] if not vm_users.get(user): vm_users.update({user: []}) vm_users[user].append(i[0]) return vm_infos,vm_users def change_db_datas(): global add_data,update_data,upper_data print update_data db = pymysql.connect('', port=3306, user='root', passwd='', db='') if add_data: insert_data=[] for ip,num in add_data.items(): insert_data.append((ip,num)) with db.cursor() as cursor: sql = "insert into vm_report_nums(ip,num) values(%s,%s)" cursor.executemany(sql, insert_data) db.commit() if update_data: insert_data = [] for ip, num in update_data.items(): insert_data.append((num,ip)) with db.cursor() as cursor: sql = "UPDATE vm_report_nums SET num=%s WHERE ip=%s;" cursor.executemany(sql, insert_data) db.commit() if upper_data: insert_data = [] for ip, num in upper_data.items(): insert_data.append((num, ip)) with db.cursor() as cursor: sql = "UPDATE vm_report_nums SET num=%s WHERE ip=%s;" cursor.executemany(sql, insert_data) db.commit() def get_db_datas(): ips={} db = pymysql.connect('', port=3306, user='root', passwd='', db='') sql = "select ip,num from vm_report_nums" with db.cursor() as cursor: cursor.execute(sql) datas = cursor.fetchall() for i in datas: ips.update({i[0]:i[1]}) return ips if __name__ == '__main__': excludes='' excludes=excludes.split(',') add_data={} update_data={} upper_data = {} raw_num = get_db_datas() datas,vm_users=get_vms() # print datas ips=[] try: for user,vms in vm_users.items(): for i in vms: ips.append(i) data_res='' if datas.get(user+'@.'): data_res+='' if datas.get(user+'@.'): data_res += ' 您的申请清单如下: {}""".format(','.join(vms),data_res) send_mail(sender, sender_pass, user+'@.com', result.decode('utf8')) change_db_datas() except Exception as e: print e sys.exit(1)