Use the Python Flask framework in combination with some MySQL memory monitoring programs, pythonflask

Source: Internet
Author: User

Use the Python Flask framework in combination with some MySQL memory monitoring programs, pythonflask

Here, we take monitoring memory usage as an example to write a simple demo program. The specific operation is as follows according to the tutorial provided by 51reboot.

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
<title>memory monitor</title><!DOCTYPE HTML>

Note: The JS Code here uses code on the Internet directly. If the host cannot connect to the Internet, you can retrieve the above three paragraphs and create a static directory in the same directory of templates, put the three downloaded files in this directory, delete the code that references javascript in the template, and use the three paragraphs in the current comment.

3. Set the agent to be monitored

The web display page is complete. Run python flask_web.py to listen on port 8888. We need to create an agent to collect data and request the flask_web page through the post method to upload the data to the database. The monitoring memory is used as an example. The specific monitoring code is as follows:

#! /Usr/bin/env python # coding = utf-8import inspectimport timeimport urllib, urllib2import jsonimport socketclass mon: def _ init _ (self): self. data = {} def getTime (self): return str (int (time. time () + 8*3600) def getHost (self): return socket. gethostname () def getLoadAvg (self): with open ('/proc/loadavg') as load_open: a = load_open.read (). split () [: 3] return ','. join (a) def getMemTotal (self): with open ('/proc/meminfo') as mem_open: a = int (mem_open.readline (). split () [1]) return a/1024 def getMemUsage (self, noBufferCache = True): if noBufferCache: with open ('/proc/meminfo') as mem_open: T = int (mem_open.readline (). split () [1]) F = int (mem_open.readline (). split () [1]) B = int (mem_open.readline (). split () [1]) C = int (mem_open.readline (). split () [1]) return (T-F-B-C)/1024 else: with open ('/proc/meminfo') as mem_open: a = int (mem_open.readline (). split () [1])-int (mem_open.readline (). split () [1]) return a/1024 def getMemFree (self, noBufferCache = True): if noBufferCache: with open ('/proc/meminfo') as mem_open: T = int (mem_open.readline (). split () [1]) F = int (mem_open.readline (). split () [1]) B = int (mem_open.readline (). split () [1]) C = int (mem_open.readline (). split () [1]) return (F + B + C)/1024 else: with open ('/proc/meminfo') as mem_open: mem_open.readline () a = int (mem_open.readline (). split () [1]) return a/1024 def runAllGet (self): # automatically obtain all the getXXX methods in the mon class, using XXX as the key, getXXX () and construct the dictionary for fun in inspect. getmembers (self, predicate = inspect. ismethod): if fun [0] [: 3] = 'get': self. data [fun [0] [3:] = fun [1] () return self. dataif _ name _ = "_ main _": while True: m = mon () data = m. runAllGet () print data req = urllib2.Request ("http://test.361way.com: 8888", json. dumps (data), {'content-type': 'application/json'}) f = urllib2.urlopen (req) response = f. read () print response f. close () time. sleep (60)

Nohup python moniItems. py>/dev/null 2> & 1 & run on the monitored host. If you want to see the effect as soon as possible for the purpose of the experiment, you can set the time. sleep (60) is changed to time. sleep (2), so that data is written to the database every two seconds.

Visit http://test.361way.com: 8888 to see our monitoring data:

Highcharts supports dragging by time and viewing by specified time period. You can save the images as png, jpg, pdf, and csv files.

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.