[项目] server.models.py(LOAMAW v1.0)

from django.db import models

# Create your models here.

class server(models.Model):
    server_id = models.AutoField(primary_key=True)
    server_name = models.CharField(max_length=30,unique = True)
    server_main_ip = models.GenericIPAddressField(unique = True)
    server_status = models.CharField(max_length=30)

    def __str__(self):
        return u'server_id:%s,server_name:%s,server_main_ip:%s,server_status:%s'%(self.server_id, self.server_name, self.server_main_ip, self.server_status)


class admin(models.Model):
    admin_id = models.AutoField(primary_key=True)
    admin_name = models.CharField(max_length=30,unique = True)
    admin_gid = models.CharField(max_length=30,unique = True)
    admin_email = models.EmailField(unique = True)
    admin_phone = models.CharField(max_length=30,unique = True)
    admin_level = models.IntegerField()
    admin_status = models.CharField(max_length=30)
    admin_mid = models.ManyToManyField(server)

    def __str__(self):
        return u'admin_name:%s,admin_gid:%s,admin_email:%s,admin_phone:%s,admin_level:%s,admin_status:%s'%(self.admin_name, self.admin_gid, self.admin_email, self.admin_phone, self.admin_level, self.admin_status, self.admin_mid)


class os(models.Model):
    os_id = models.AutoField(primary_key=True)
    os_distributor = models.CharField(max_length=30)
    os_release = models.CharField(max_length=30)
    os_kernel = models.CharField(max_length=30)
    os_uptime = models.CharField(max_length=30)
    os_status = models.CharField(max_length=30)
    os_fid = models.ForeignKey(server,on_delete=models.CASCADE)

    def __str__(self):
        return u'os_distributor:%s,os_release:%s,os_kernel:%s,os_uptime:%s,os_status:%s'%(self.os_distributor, self.os_release, self.os_kernel, self.os_uptime, self.os_status)


class cpu(models.Model):
    cpu_id = models.AutoField(primary_key=True)
    cpu_qty = models.IntegerField()
    cpu_core = models.IntegerField()
    cpu_usage = models.CharField(max_length=255)
    cpu_idleness = models.CharField(max_length=255)
    cpu_status = models.CharField(max_length=30)
    cpu_fid = models.ForeignKey(server,on_delete=models.CASCADE)

    def __str__(self):
        return u'cpu_qty:%s,cpu_core:%s,cpu_usage:%s,cpu_idleness:%s,cpu_status:%s'%(self.cpu_qty, self.cpu_core, self.cpu_usage, self.cpu_idleness, self.cpu_status)


class mem(models.Model):
    mem_id = models.AutoField(primary_key=True)
    mem_size = models.CharField(max_length=30)
    mem_usage = models.CharField(max_length=255)
    mem_idleness = models.CharField(max_length=255)
    mem_status = models.CharField(max_length=30)
    mem_fid = models.ForeignKey(server,on_delete=models.CASCADE)

    def __str__(self):
        return u'mem_size:%s,mem_usage:%s,mem_idleness:%s,mem_status:%s'%(self.mem_size, self.mem_usage, self.mem_idleness, self.mem_status)


class space(models.Model):
    space_id = models.AutoField(primary_key=True)
    space_name = models.CharField(max_length=30)
    space_size = models.CharField(max_length=255)
    space_usage = models.CharField(max_length=255)
    space_idleness = models.CharField(max_length=255)
    space_mount = models.CharField(max_length=255)
    space_status = models.CharField(max_length=30)     
    space_fid = models.ForeignKey(server,on_delete=models.CASCADE)

    def __str__(self):
        return u'space_name:%s,space_size:%s,space_usage:,%s,space_idleness:%s,space_mount:%s,space_status:%s'%(self.space_name, self.space_size, self.space_usage, self.space_idleness,  self.space_mount, self.space_status)


class nc(models.Model):
    nc_id = models.AutoField(primary_key=True)
    nc_name = models.CharField(max_length=30)
    nc_ip = models.GenericIPAddressField()
    nc_status = models.CharField(max_length=30)
    nc_fid = models.ForeignKey(server,on_delete=models.CASCADE)

    def __str__(self):
        return u'nc_name:%s,nc_ip:%s,nc_status:%s'%(self.nc_name, self.nc_ip, self.nc_status)


def server_register(server_name,server_main_ip):
    try:
        register_server = server.objects.get(server_name = server_name)
        
    except server.DoesNotExist:
        register_server = server.objects.create(server_name = server_name,server_main_ip = server_main_ip,server_status = 'have not checked')
        return register_server


def server_change_status(status_check_server):
    status_check_server_id = server.objects.get(server_name = status_check_server).server_id

    check_os_status = os.objects.get(os_fid = status_check_server_id).os_status
    check_cpu_status = cpu.objects.get(cpu_fid = status_check_server_id).cpu_status
    check_mem_status = mem.objects.get(mem_fid = status_check_server_id).mem_status
    check_space_status = space.objects.get(space_fid = status_check_server_id).space_status

    check_space_status = 'normal'
    check_space_status_list = []
    check_space_status_list = space.objects.values('space_status').filter(space_fid = status_check_server_id)
    for check_space_statuss in check_space_status_list:
        if check_space_statuss == {'space_status': 'normal'}:
            check_space_status = 'normal'
        else:
            check_space_status = 'abnormal'

    check_nc_status = 'normal'
    check_nc_status_list = []
    check_nc_status_list = nc.objects.values('nc_status').filter(nc_fid = status_check_server_id)
    for check_nc_statuss in check_nc_status_list:

        if check_nc_statuss == {'nc_status': 'normal'}:
            check_nc_status = 'normal'
        else:
            check_nc_status = 'abnormal'

    mserver_status = 'abnormal'
    if check_os_status == 'normal':
        if check_cpu_status == 'normal':
            if check_mem_status == 'normal':
                if check_space_status == 'normal':
                    if check_nc_status == 'normal':
                        mserver_status = 'normal'

    server.objects.filter(server_name = status_check_server).update(server_status = mserver_status)


def admin_register(admin_name,admin_gid,admin_email,admin_phone,admin_level):
    try:
        register_admin = admin.objects.get(admin_name = admin_name)

    except admin.DoesNotExist:
        register_admin = admin.objects.create(admin_name = admin_name,admin_gid = admin_gid,admin_email = admin_email,admin_phone = admin_phone,admin_level = admin_level,admin_status = 'normal')
        return register_admin


def dbind(bind_server,bind_admin):
    bind_server_id = server.objects.get(server_name = bind_server).server_id
    bind = admin.objects.get(admin_name = bind_admin)
    bind.admin_mid.add(bind_server_id)


from server.scripts.os_sub import *
def os_check_all(os_check_server):
    os_distributor_out = script_os_distributor(os_check_server)
    os_distributor_out = os_distributor_out.decode().strip()

    os_release_out = script_os_release(os_check_server)
    os_release_out = os_release_out.decode().strip()

    os_kernel_out = script_os_kernel(os_check_server)
    os_kernel_out = os_kernel_out.decode().strip()

    os_uptime_out = script_os_uptime(os_check_server)
    os_uptime_out = os_uptime_out.decode().strip()

    os_status_out = 'abnormal'
    if os_distributor_out:
        if os_release_out:
            if os_kernel_out:
                if os_uptime_out:
                    os_status_out = 'normal'


    os_server = server.objects.get(server_name = os_check_server)

    try:
        chack_os_data = os.objects.get(os_fid = os_server)

    except os.DoesNotExist:
        os.objects.create(os_distributor = os_distributor_out,os_release = os_release_out,os_kernel = os_kernel_out,os_uptime = os_uptime_out,os_status = os_status_out,os_fid = os_server)

    os.objects.filter(os_fid = os_server).update(os_distributor = os_distributor_out,os_release = os_release_out,os_kernel = os_kernel_out,os_uptime = os_uptime_out,os_status = os_status_out,os_fid = os_server)


from server.scripts.cpu_sub import *
def cpu_check_all(cpu_check_server):
    cpu_qty_out = script_cpu_qty(cpu_check_server)
    cpu_qty_out = cpu_qty_out.decode().strip()

    cpu_core_out = script_cpu_core(cpu_check_server)
    cpu_core_out = cpu_core_out.decode().strip()

    cpu_usage_out = script_cpu_usage(cpu_check_server)
    cpu_usage_out = cpu_usage_out.decode().strip()

    cpu_idleness_out = script_cpu_idleness(cpu_check_server)
    cpu_idleness_out = cpu_idleness_out.decode().strip()


    cpu_status_out='abnormal'
    if cpu_core_out:
        if cpu_qty_out:
            if cpu_usage_out:
                if cpu_idleness_out:
                    cpu_status_out='normal'            

    cpu_server = server.objects.get(server_name = cpu_check_server)

    try: 
        check_cpu_data = cpu.objects.get(cpu_fid = cpu_server)

    except cpu.DoesNotExist:
        cpu.objects.create(cpu_qty = cpu_qty_out,cpu_core = cpu_core_out,cpu_usage = cpu_usage_out,cpu_idleness = cpu_idleness_out,cpu_status = cpu_status_out,cpu_fid = cpu_server)
    
    cpu.objects.filter(cpu_fid = cpu_server).update(cpu_qty = cpu_qty_out,cpu_core = cpu_core_out,cpu_usage = cpu_usage_out,cpu_idleness = cpu_idleness_out,cpu_status = cpu_status_out,cpu_fid = cpu_server)


from server.scripts.mem_sub import *
def mem_check_all(mem_check_server):
    mem_size_out = script_mem_size(mem_check_server)
    mem_size_out = mem_size_out.decode().strip()

    mem_usage_out = script_mem_usage(mem_check_server)
    mem_usage_out = mem_usage_out.decode().strip()

    mem_idleness_out = script_mem_idleness(mem_check_server)
    mem_idleness_out = mem_idleness_out.decode().strip()

    mem_status_out = 'abnormal'
    if mem_size_out:
        if mem_usage_out:
            if mem_idleness_out:
                    mem_status_out='normal'

    mem_server = server.objects.get(server_name = mem_check_server)

    try:
        check_mem_data = mem.objects.get(mem_fid = mem_server)

    except mem.DoesNotExist:
        mem.objects.create(mem_size = mem_size_out,mem_usage = mem_usage_out,mem_idleness = mem_idleness_out,mem_status = mem_status_out,mem_fid = mem_server)

    mem.objects.filter(mem_fid = mem_server).update(mem_size = mem_size_out,mem_usage = mem_usage_out,mem_idleness = mem_idleness_out,mem_status = mem_status_out,mem_fid = mem_server)


from server.scripts.space_sub import *
def space_check_all(space_check_server):
    space_all_out = script_space(space_check_server)
    space_all_out = space_all_out.decode().strip()
    space_all_out = space_all_out.split('\n')

    space_server = server.objects.get(server_name = space_check_server)

    for spaces in space_all_out:
        space_name_out = spaces.split()[0]
        space_size_out = spaces.split()[1]
        space_usage_out = spaces.split()[2]
        space_idleness_out = spaces.split()[3]
        space_mount_out = spaces.split()[-1]

        space_status_out='abnormal'
        if space_name_out:
            if space_size_out:
                if space_usage_out:
                    if space_idleness_out:
                        space_status_out='normal'

        try:
            check_space_data = space.objects.get(space_fid = space_server,space_name = space_name_out)

        except space.DoesNotExist:
            space.objects.create(space_name = space_name_out,space_size = space_size_out,space_usage = space_usage_out,space_idleness = space_idleness_out,space_mount = space_mount_out,space_status = space_status_out,space_fid = space_server)

        space.objects.filter(space_fid = space_server,space_name = space_name_out).update(space_name = space_name_out,space_size = space_size_out,space_usage = space_usage_out,space_idleness = space_idleness_out,space_mount = space_mount_out,space_status = space_status_out,space_fid = space_server)


from server.scripts.nc_sub import *
def nc_check_all(nc_check_server):
    nc_name_out = script_nc_name(nc_check_server)
    nc_name_out = nc_name_out.decode().strip()
    nc_name_out = nc_name_out.split('\n')

    nc_ip_out = script_nc_ip(nc_check_server)
    nc_ip_out = nc_ip_out.decode().strip()
    nc_ip_out = nc_ip_out.split('\n')

    nc_server = server.objects.get(server_name = nc_check_server)
    
    nc_name_line = 1
    nc_ip_line = 1
    for nc_name_outs in nc_name_out:
        nc_name_out_out = nc_name_outs.split()[0]

        for nc_ip_outs in nc_ip_out:
            nc_ip_out_out = nc_ip_outs.split()[0]

            nc_status_out = 'abnormal'
            if nc_name_out_out:
                if nc_ip_out_out:
                    nc_status_out = 'normal'
            
            if nc_name_line == nc_ip_line:
                try:
                    check_nc_data = nc.objects.get(nc_fid = nc_server,nc_name = nc_name_out_out)
            
                except nc.DoesNotExist:
                    nc.objects.create(nc_name = nc_name_out_out,nc_ip = nc_ip_out_out,nc_status = nc_status_out,nc_fid = nc_server)

                nc.objects.filter(nc_fid = nc_server,nc_name = nc_name_out_out).update(nc_name = nc_name_out_out,nc_ip = nc_ip_out_out,nc_status = nc_status_out,nc_fid = nc_server)
        
            nc_ip_line = nc_ip_line + 1
        
        nc_ip_line = 1
        nc_name_line = nc_name_line + 1