项目名称:LinuxMM
监控平台:Linux
使用模块:psutil、Paramiko
前端框架:Bootstrap
后端框架:DJango
远程读取信息的方式一般分两种
python3 setup.py install
。简单起见,我们这里使用拉式。
代码仓库:https://gitee.com/luhuadong/Python_Learning/tree/master/13th_week/homework/linuxmm
创建数据库
CREATE DATABASE IF NOT EXISTS linuxmm DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
选择数据库
use linuxmm
创建数据表
CREATE TABLE host(
id int unsigned NOT NULL auto_increment PRIMARY KEY COMMENT 'ID号',
tag varchar(32) NOT NULL COMMENT '标签',
ip varchar(32) NOT NULL COMMENT 'IP地址',
cpu smallint unsigned DEFAULT NULL COMMENT 'CPU',
mem bigint unsigned DEFAULT NULL COMMENT '内存',
disk bigint unsigned DEFAULT NULL COMMENT '硬盘',
stat tinyint unsigned default 0 COMMENT '状态',
cdate datetime COMMENT '登记时间'
);
查看表结构
mysql> desc host;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| tag | varchar(32) | NO | | NULL | |
| ip | varchar(32) | NO | | NULL | |
| cpu | smallint(5) unsigned | YES | | NULL | |
| mem | bigint(20) unsigned | YES | | NULL | |
| disk | bigint(20) unsigned | YES | | NULL | |
| stat | tinyint(3) unsigned | YES | | 0 | |
| cdate | datetime | YES | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
添加两条数据
INSERT INTO host(tag,ip,cdate)
VALUES('test01','192.168.3.100',curtime()),
('test02','192.168.3.101',curtime());
查看数据
mysql> select * from host;
+----+--------+---------------+------+------+------+------+---------------------+
| id | tag | ip | cpu | mem | disk | stat | cdate |
+----+--------+---------------+------+------+------+------+---------------------+
| 1 | test01 | 192.168.3.100 | NULL | NULL | NULL | 0 | 2018-10-23 17:50:54 |
| 2 | test02 | 192.168.3.101 | NULL | NULL | NULL | 0 | 2018-10-23 17:50:54 |
+----+--------+---------------+------+------+------+------+---------------------+
2 rows in set (0.00 sec)
$ django-admin startproject linuxmm # 创建linuxmm项目
$ cd linuxmm
$ python3 manage.py startapp monitor # 监控中心应用
$ python3 manage.py startapp api # Web API应用
$ python3 manage.py runserver
Django 配置 settings.py
import os
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'monitor',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
'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',
],
},
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'linuxmm',
'USER': 'root',
'PASSWORD': 'lu1010',
'HOST': 'localhost',
'PORT': '3306',
}
}
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
在 linuxmm/__init__.py 文件中添加
import pymysql
pymysql.install_as_MySQLdb()
Bootstrap 3.3.7 + jQuery 3.3.1
static/
├── css
│ ├── bootstrap.css
│ ├── bootstrap.css.map
│ ├── bootstrap.min.css
│ ├── bootstrap.min.css.map
│ ├── bootstrap-theme.css
│ ├── bootstrap-theme.css.map
│ ├── bootstrap-theme.min.css
│ └── bootstrap-theme.min.css.map
├── fonts
│ ├── glyphicons-halflings-regular.eot
│ ├── glyphicons-halflings-regular.svg
│ ├── glyphicons-halflings-regular.ttf
│ ├── glyphicons-halflings-regular.woff
│ └── glyphicons-halflings-regular.woff2
└── js
├── bootstrap.js
├── bootstrap.min.js
├── jquery-3.3.1.min.js
└── npm.js
Web API 返回的数据
# json格式
{
"mem_total": 1040789504,
"mem_available": 432156672,
"mem_percent": 58.5,
"mem_used": 449925120,
"mem_free": 76451840
}
# 字典格式
{
'mem_total': 1040789504,
'mem_available': 432386048,
'mem_percent': 58.5,
'mem_used': 449667072,
'mem_free': 74817536
}
# 看起来是一样的呀 >_<
数据迁移
python3 manage.py migrate
CREATE TABLE user(
id int unsigned NOT NULL auto_increment PRIMARY KEY COMMENT 'ID号',
username varchar(32) NOT NULL COMMENT '用户名',
password varchar(32) NOT NULL COMMENT '密码',
email varchar(32) NOT NULL COMMENT '邮箱',
cdate datetime COMMENT '注册时间'
);
添加
INSERT INTO user(username,password,email,cdate)
VALUE('admin','061c1da6af11de1d84374ce61d17ceaf','[email protected]',curtime());
数据表结构
mysql> desc host;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| tag | varchar(32) | NO | | NULL | |
| ip | varchar(32) | NO | | NULL | |
| cpu | smallint(5) unsigned | YES | | NULL | |
| mem | bigint(20) unsigned | YES | | NULL | |
| disk | bigint(20) unsigned | YES | | NULL | |
| stat | tinyint(3) unsigned | YES | | 0 | |
| cdate | datetime | YES | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
mysql> desc user;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(32) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| email | varchar(32) | NO | | NULL | |
| cdate | datetime | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
导出数据库
mysqldump -u root -p linuxmm > linuxmmdb.sql
$ python3 manage.py ruerver
服务启动后,在浏览器输入 URL http://127.0.0.1:8000/monitor/
注意:运行 LinuxMM 项目前,请导入 linuxmm.sql 数据库文件!
注意:远程主机需安装 Python3 以及 psutil 模块,并且需要将监控服务器的 ssh 公钥上传到受监控的远程主机!
例如:ssh-copy-id [email protected]