Use the Python Flask framework and MySQL to write a memory monitoring program

Source: Internet
Author: User
This article mainly introduces some examples of a memory monitoring program using the Flask framework of Python combined with MySQL, and can display the results in a simple graphical way, for more information about how to monitor memory usage, see the following demo program.

I. create databases and tables

Create a falcon database:

mysql> create database falcon character set utf8;Query OK, 1 row affected (0.00 sec)

Create a table stat for memory monitoring. The table structure is as follows:

CREATE TABLE `stat` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `host` varchar(256) DEFAULT NULL, `mem_free` int(11) DEFAULT NULL, `mem_usage` int(11) DEFAULT NULL, `mem_total` int(11) DEFAULT NULL, `load_avg` varchar(128) DEFAULT NULL, `time` bigint(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `host` (`host`(255))) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

Ii. flask web terminal settings

First, we design a web service to implement the following functions:

Complete monitoring page display
Accept data submitted by POST
Json data GET Interface
The framework structure is as follows:

The directory structure is as follows:

web├── flask_web.py└── templates └── mon.html

The flask_web code is as follows:

import MySQLdb as mysqlimport jsonfrom flask import Flask, request, render_templateapp = Flask(__name__)db = mysql.connect(user="361way", passwd="123456", \  db="falcon", charset="utf8")db.autocommit(True)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`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % (data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], int(data['Time']))   ret = c.execute(sql)  except mysql.IntegrityError:   pass  return "OK" else:  return render_template("mon.html")@app.route("/data", methods=["GET"])def getdata(): c.execute("SELECT `time`,`mem_usage` FROM `stat`") ones = [[i[0]*1000, i[1]] for i in c.fetchall()] return "%s(%s);" % (request.args.get('callback'), json.dumps(ones))if __name__ == "__main__": app.run(host="0.0.0.0", port=8888, debug=True)

The graph JS used here is highcharts and highstock. The specific template content is as follows:

[root@91it templates]# cat mon.html

Memory monitor   
   Highstock Example  
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.