文章目录
前言
sysinfo项目能够查看系统、CPU、内存、硬盘、进程、网络、登录用户等信息,同时可将部分数据以图表形式展示,最后创建了定时任务,能够定时获取系统资源信息。
一、准备工作
创建Django项目:
创建子应用并设置本地化:
python manage.py startapp host
编辑settings.py在app列表中加入host
创建数据库表和后台管理用户:
python manage.py migrate
python manage.py createsuperuser
git管理项目(requirements.txt, README.md, .gitignore):
pip freeze > requirements.txt
编辑 README.md文档
生成.ignore文件
设置语言时区等基本信息:
编辑settings.py文件:
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
# USE_TZ = True
USE_TZ = False
二、git仓库
git init %初始化本地仓库
git add * %添加所有更改到暂存区
git commit -m "" %提交更改到本地仓库
git remote add remote_urls %与远程仓库关联
git push %push到远程仓库
三、项目整体设计思路梳理
1.路由设计:
主路由sysinfo/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('host.urls')),
]
子路由host/urls.py:
from django.urls import path, include
from .views import *
urlpatterns = [
path('', index, name='index'),
path('user/', user, name='user'),
path('cpu/', cpu, name='cpu'),
path('cpu/<str:chart>/', cpu, name='cpu'),
path('memory/', memory, name='memory'),
path('disk/', disk, name='disk'),
path('network/', network, name='network'),
path('process/', process, name='process'),
]
2.视图函数设计:
from django.shortcuts import render
# Create your views here.
def index(request):
pass
return render(request, 'host/index.html', locals())
def user(request):
pass
return render(request, 'host/user.html', locals())
def cpu(request):
pass
return render(request, 'host/cpu.html', locals())
def memory(request):
pass
return render(request, 'host/memory.html', locals())
def disk(request):
pass
return render(request, 'host/disk.html', locals())
def network(request):
pass
return render(request, 'host/network.html', locals())
def process(request):
pass
return render(request, 'host/process.html', locals())
3.前端展示页面设计:
创建以下几个前端模板文件并逐步进行改进
templates/host/cpu.html
templates/host/disk.html
templates/host/index.html
templates/host/memory.html
templates/host/network.html
templates/host/process.html
templates/host/user.html
四、创建前端页面基模板(base.html)
创建base模板是为了在编写每一个前端展示页面相关html文件时更加便捷易写,这里我们需要提前导入一些静态文件来使前端页面能有更加优雅的展示
添加静态文件到主配置:
sysinfo/settings.py:
STATICFILES_DIRS = [
BASE_DIR / "static",
]
编辑基模板文件:
templates/host/base.html:
<!DOCTYPE html>
<html {
% block html_attribs %}{
% endblock html_attribs %}>
<head>
{
% block head %}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{
% block title %} {
% endblock title %}</title>
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css"> %在静态文件中查找
<link rel="stylesheet" type="text/css" href="/static/css/my-style.css"> %在静态文件中查找
<script src="/static/js/jquery-3.1.1.min.js"></script>
{
% endblock head %}
</head>
<body>
<div class="sysinfo">
<div class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Sys Info</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">系统</a></li>
<li><a href="/cpu/">CPU</a></li>
<li><a href="/memory/">内存</a></li>
<li><a href="/disk/">硬盘</a></li>
<li><a href="/network/">网络</a></li>
<li><a href="/process/">进程</a></li>
<li><a href="/user/">用户</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
{
% block content %}{
% endblock %}
</div>
</div>
</body>
</html>
注意:用block包起来的部分是每个页面不同的部分
模板使用示例:
{
% include 'host/base.html' %} %使用模板
{
% block title %}
磁盘信息
{
% endblock %}
五、模块使用以及前端首页展示信息的返回
这里我们需要用到psutil模块:它是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统.
host/views.py:
import psutil
import os, platform
# Create your views here.
from host.models import UserCpuPercent
def index(request):
"""
sys_name
kernel_name
kernel_no
kernel_version
sys_framework
now_time
boot_time
up_time
"""
try:
info = os.uname()
except Exception as e:
info = platform.uname()
sys_name = info.node
kernel_name = info.system
kernel_no = info.release
kernel_version = info.version
sys_framework = info.machine
boot_time = datetime.fromtimestamp(psutil.boot_time())
now_time = datetime.now()
print(boot_time, now_time)
up_time = now_time - boot_time
return render(request, 'host/index.html', locals())