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
[项目] mysite.urls.py(LOAMAW v1.0)
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^server/', include('server.urls')),
]
[项目] mysite.settings.py(LOAMAW v1.0)
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 3.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-ln+%tp_-5(i0cg8&62!tg)7ht)_n=t4ihz#7!&don5_ob_-9+9'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'server',
'HOST': '10.10.10.5',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'password',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
[项目] mysite.__init__.py(LOAMAW v1.0)
import pymysql
pymysql.install_as_MySQLdb()
[内容] Python splite 字符串切割
内容一:splite 的格式
<character string>.splite('<separator>', <split several times>)[<which number of word place>]
内容二:splite 的使用案例
2.1 案例一:以一行行的方式进行分割
# string = string.splite('\n')
(补充:这里以给 string 字符串以一行行的方式进行分割为例)
2.2 案例二:制作表格
# string = string.splite('\t')
(补充:这里以给 string 字符串制成表格为例)
2.3 案例三:以空格的方式进行分割
# string = string.splite()
或者:
# string = string.splite(, -1)
(补充:这里以给 string 字符串以空格的方式进行分割为例)
2.4 案例四:以空格的方式进行分割,并截取第 1 个单词
# string = string.splite()[0]
(补充:这里以给 string 字符串以空格的方式进行分割,并截取第 1 个单词为例)
2.5 案例五:以空格的方式进行分割,并截取最后 1 个单词
# string = string.splite()[-1]
(补充:这里以给 string 字符串以空格的方式进行分割,并截取最后 1 个单词为例)
2.6 案例六:以字母进行分割
# splite = re.split(r"[^A-Za-z]", line.strip())
(补充:这里以给 string 字符串以字母的方式进行分割为例)
(注意:要先导入 re 函数)
2.7 案例七:以冒号 “:” 进行分割
# splite = string.splite(':')
(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割为例)
2.8 案例八:以冒号 “:” 进行分割,只分割 1 次,并从开头开始计数
# splite = string.splite(':', 1)
(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割,只分割 1 次,并从开头开始计数为例)
2.9 案例九:以冒号 “:” 进行分割,只分割 1 次,并从末尾开始计数
# splite = string.rsplite(':', 1)
(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割,只分割 1 次,并从末尾开始计数为例)