导航

< 2025年1月 >
29 30 31 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 1
2 3 4 5 6 7 8
统计
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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 += '    您的申请清单如下:\n{}""".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)

 

posted on   slqt  阅读(237)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
 
点击右上角即可分享
微信分享提示