sysinfo 项目是使用 Python Django 框架和 psutil 开发的一个中文版 Linux 服务器信息查看应用,可查看的信息包括系统、CPU、内存、硬盘、进程、网络、登录用户等,同时可查看并导出部分数据的图表(正在更新完成中)。
需要安装的 Python 包
- Django==3.x
- psutil
- 参考资料:
Github参考网址: https://github.com/hypersport/sysinfo
项目的基本流程
- 创建Django项目Sysinfo(手动或代码)
django-admin startproject Sysinfo - 创建子应用
python manage.py startapp host - 不需要后台管理
- 路由设置和视图函数的编写
- 模板的编写
要求1:基于psutil模块实现系统信息监控,监控详情如下表
psutil实现系统信息监控
1、安装psutil软件及依赖
依赖:
dnf install gcc python3-devel
安装psutil软件:
pip install -i https://pypi.douban.com/simple
#安装表格软件,可以绘制出好看的表格:
#pip install prettytable -i https://pypi.douban.com/simple
2、代码预实现
import psutil
import prettytable
#物理CPU
print("物理CPU",psutil.cpu_count(logical=False))
#逻辑CPU
print("逻辑CPU",psutil.cpu_count())
#CPU使用率
print(psutil.cpu.percent())
#datetime模块
#from datetime import date,datetime,timedelta
#import time
#start_time=datetime(year=2020,month=10,day=10,hour=10,minute=10,second=10)
#end_time=datetime(year=2021,month=10,day=11,hour=10,minute=10,second=10)
#用时间戳转成datetime对象
boot_time=psutil.boot_time()
boot_time =datetime.fromtimestamp(boot_time)
now_time = datetime.fromtimestamp(time.time())
print(boot_time,now_time)
print("开机时长:",now_time-boot_time)
3、结合Django实现
1)配置setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
+ 'host',
]
2)配置主路由Sysinfo/usrls.py和子路由host/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# 用户访问的网址没有特殊设置,读取host子应用里面的路由配置.
+ path('', include('host.urls'))
]
from django.urls import path, include
from . import views
urlpatterns = [
# 子路由配置,有对应的视图函数.
path('', views.index, name='index'),
]
- host/views.py
from django.shortcuts import render
from django.http import HttpResponse
import os
import platform
from datetime import datetime
import time
import psutil
# Create your views here.
# 需求: 用户访问http://127.0.0.1:8000,返回主机的详情信息
def index(request):
try:
# 如果是Linux系统,执行下面内容
# os.uname在windows系统中不能执行
system_info = os.uname()
node = system_info.nodename
system = system_info.sysname
except Exception as e:
# 如果是Windows系统,执行下面内容
system_info = platform.uname()
node = system_info.node
system = system_info.system
boot_time = psutil.boot_time()
boot_time = datetime.fromtimestamp(boot_time)
now_time = datetime.fromtimestamp(time.time())
info = {
'node': node,
'system': system,
"kernel_name": system,
'release': system_info.release,
'version': system_info.version,
'machine': system_info.machine,
'now_time': now_time,
'boot_time': boot_time,
'boot_delta': now_time - boot_time
}
# 默认情况下返回的是普通字符串,不美观, 需要模板
return render(request, 'host/index.html', {'info': info})
4)配置 templates/host/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页: 服务器信息查看应用</title>
{# 导入Bootstrap帮我们设置好的CSS样式和JS的动效 #}
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
{% include 'host/nav.html' %}
<h1>系统信息监控</h1>
{#设置table标签的类名称为table和table-striped,其实就是使用Bootstrap帮我们写好的CSS样式设置表格样式#}
<table class="table table-striped table-hover">
<tr>
<td>主机名</td>
{# 前端模板, 变量的使用{{ 变量名 }} 获取字典里面的key对应的value值: {{ info.keyname }} #}
<td>{{ info.node }}</td>
</tr>
<tr>
<td>操作系统</td>
<td>{{ info.system }}</td>
</tr>
<tr>
<td>内核名称</td>
<td>{{ info.kernel_name }}</td>
</tr>
<tr>
<td>发行版本号</td>
<td>{{ info.release }}</td>
</tr>
<tr>
<td>内核版本</td>
<td>{{ info.version }}</td>
</tr>
<tr>
<td>系统架构</td>
<td>{{ info.machine }}</td>
</tr>
<tr>
<td>当前时间</td>
<td>{{ info.now_time }}</td>
</tr>
<tr>
<td>开机时间</td>
<td>{{ info.boot_time }}</td>
</tr>
<tr>
<td>开机时长</td>
<td>{{ info.boot_delta }}</td>
</tr>
</table>
</body>
</html>
版权声明:本文为qq_27172205原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。