按照http://51reboot.com/python36-falcon-mon-video/ 一步步操作实现一个简单的系统监控
我自己安装的是Python 3.5 和博客里面的版本不太一样
而且博主是用于监控Linux系统,我修改了些代码可以检测到Windows的信息
还有个地方是博主用的是Mysql,我也懒得装,我就用Sqlit3。
先把flask环境搭建好
建个数据库
from sqlite3 import dbapi2 as sqlite3
conn = sqlite3.connect('stat')
print("创建数据库成功")
conn.execute("""create table stat(
id integer primary key autoincrement,
host string,
mem_free integer,
mem_usage integer,
mem_total integer,
time integer
);""")
print("数据表创建成功")
建好后别忘了执行一下。
Server 部分基本与51reboot里一样
from sqlite3 import dbapi2 as mysql
import json
from flask import Flask, request, render_template
app = Flask(__name__)
db = mysql.connect("stat",check_same_thread=False)
db.commit()
c = db.cursor()
@app.route("/", methods=["GET", "POST"])
def hello():
sql = ""
if request.method == "POST":
data = request.json
try:
sql = "INSERT INTO stat (host,mem_free,mem_usage,mem_total,time) VALUES('%s', '%s', '%s', '%s', '%d')" % (data[&#