[项目] server.templates.fail.html(LOAMAW v1.0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fail</title>
</head>
<body>

    <p>fail</p>

    <table>
        <tr>
            <td>
            <form action="/server/server_list">
            <input type="submit" value="return"/> 
            </form>
            </td>
        </tr>
    </table>

</body>
</html>

[项目] server.templates.admin_register.html(LOAMAW v1.0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>admin_register</title>
</head>
<body>

   <table>
        <form action="/server/admin_register/" method="POST">
        {% csrf_token %}
            <tr>
                <label>admin_name:</label><input type="text" name="tadmin_name"/>
            </tr>
            </br>
            <tr>
                <label>admin_gid:</label><input type="text" name="tadmin_gid"/>
            </tr>
            </br>
            <tr>
                <label>admin_email:</label><input type="text" name="tadmin_email"/>
            </tr>
            <br/>
            <tr>
                <label>admin_phone:</label><input type="text" name="tadmin_phone"/>
            </tr>
            </br>
            <tr>
                <label>admin_level:</label><input type="text" name="tadmin_level"/>
            </tr>
            </br>
            <tr>
                <td>
                    <input type="submit" value="regsiter"/>
                </td>
            </form>
                <td>
                    <form action="/server/server_list">
                    <input type="submit" value="return"/> 
                    </form>
                </td>
            </tr>
        
</body>
</html>

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

from time import sleep
from django.shortcuts import render
from .models import *
from django.http import HttpResponse

# Create your views here.


def vserver_register(request):
    if request.method == 'GET':
        return render(request,'server_register.html')
    else:
        vserver_name = request.POST.get('tserver_name','')
        vserver_main_ip = request.POST.get('tserver_main_ip','')

    vnewserver = server_register(vserver_name,vserver_main_ip)
    if vnewserver:
        return render(request,'success.html')
    return render(request,'fail.html')


def vadmin_register(request):
    if request.method == 'GET':
        return render(request,'admin_register.html')
    else:
        vadmin_name = request.POST.get('tadmin_name','')
        vadmin_gid = request.POST.get('tadmin_gid','')
        vadmin_email = request.POST.get('tadmin_email','')
        vadmin_phone = request.POST.get('tadmin_phone','')
        vadmin_level = request.POST.get('tadmin_level','')

    vnewadmin = admin_register(vadmin_name,vadmin_gid,vadmin_email,vadmin_phone,vadmin_level)
    if vnewadmin:
        return render(request,'success.html')
    return render(request,'fail.html')


def vserver_bind_admin(request):
    if request.method == 'GET':
        return render(request,'server_bind_admin.html')
    else:
        vserver_bind = request.POST.get('server_bind_name','')
        vadmin_bind = request.POST.get('admin_bind_name','')
        dbind(vserver_bind,vadmin_bind)
        vnewbind = server.objects.filter(server_name = vserver_bind).filter(admin__admin_name = vadmin_bind)
        if vnewbind:
            return render(request,'success.html')
        return render(request,'fail.html')


def vserver_list(request):

    vserver_name_choice = request.POST.getlist('tserver_name_choice','')

    if vserver_name_choice != '':

        for vserver_name_choices in vserver_name_choice:
            os_check_thread = threading.Thread(target=os_check_all, args=[vserver_name_choices])
            cpu_check_thread = threading.Thread(target=cpu_check_all, args=[vserver_name_choices])
            mem_check_thread = threading.Thread(target=mem_check_all, args=[vserver_name_choices])
            space_check_thread = threading.Thread(target=space_check_all, args=[vserver_name_choices])
            nc_check_thread = threading.Thread(target=nc_check_all, args=[vserver_name_choices])

            os_check_thread.start()
            cpu_check_thread.start()
            mem_check_thread.start()
            space_check_thread.start()
            nc_check_thread.start()

            os_check_thread.join()
            cpu_check_thread.join()
            mem_check_thread.join()
            space_check_thread.join()
            nc_check_thread.join()

        for vserver_name_choices in vserver_name_choice:
            server_change_status(vserver_name_choices)


    vserver_all = server.objects.all()
    return render(request,'server_list.html',{'tserver_all':vserver_all})


import threading
def vserver_detail(request):
    vsdmserver = request.GET.get('server_name','')
    vrefresh = request.POST.get('trefresh','')


    if vrefresh == 'single thread':
        os_check_all(vsdmserver)
        cpu_check_all(vsdmserver)
        mem_check_all(vsdmserver)
        space_check_all(vsdmserver)
        nc_check_all(vsdmserver)
        server_change_status(vsdmserver)


    if vrefresh == 'multi thread':
        os_check_thread = threading.Thread(target=os_check_all, args=[vsdmserver])
        cpu_check_thread = threading.Thread(target=cpu_check_all, args=[vsdmserver])
        mem_check_thread = threading.Thread(target=mem_check_all, args=[vsdmserver])
        space_check_thread = threading.Thread(target=space_check_all, args=[vsdmserver])
        nc_check_thread = threading.Thread(target=nc_check_all, args=[vsdmserver])

        os_check_thread.start()
        cpu_check_thread.start()
        mem_check_thread.start()
        space_check_thread.start()
        nc_check_thread.start()

        os_check_thread.join()
        cpu_check_thread.join()
        mem_check_thread.join()
        space_check_thread.join()
        nc_check_thread.join()

        server_change_status(vsdmserver)

    vsdserver = server.objects.filter(server_name = vsdmserver)
    vsdos = server.objects.get(server_name = vsdmserver).os_set.all()
    vsdcpu = server.objects.get(server_name = vsdmserver).cpu_set.all()
    vsdmem = server.objects.get(server_name = vsdmserver).mem_set.all()
    vsdspace = server.objects.get(server_name = vsdmserver).space_set.all()
    vsdnc = server.objects.get(server_name = vsdmserver).nc_set.all()
    vsdadmin = server.objects.get(server_name = vsdmserver).admin_set.all()

    return render(request,'server_detail.html',{'tserver':vsdserver,'tos':vsdos,'tcpu':vsdcpu,'tmem':vsdmem,'tspace':vsdspace,'tnc':vsdnc,'tsdadmin':vsdadmin})

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

from django.conf.urls import url
from server.views import vserver_list, vserver_detail, vserver_register, vadmin_register, vserver_bind_admin

urlpatterns = [
     url(r'^server_register',vserver_register),
     url(r'^admin_register',vadmin_register),
     url(r'^server_bind_admin',vserver_bind_admin),
     url(r'^server_list',vserver_list),
     url(r'^server_detail',vserver_detail)
]

[项目] 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