Example of simple monitoring system written by flask and MySQL

Source: Internet
Author: User
Tags memory usage readline sleep create database

Here to monitor memory usage as an example, write a simple demo program, the specific operation according to the 51reboot tutorials written below.

First, the establishment of a database to build tables

To create a Falcon database:

Mysql> CREATE DATABASE Falcon character set UTF8;
Query OK, 1 row Affected (0.00 sec)
To create the table stat used by memory monitoring, the table structure is as follows:

CREATE TABLE ' stat ' (
' id ' int (one) unsigned not NULL auto_increment,
' Host ' varchar (256) DEFAULT NULL,
' Mem_free ' int (one) DEFAULT NULL,
' Mem_usage ' int (one) DEFAULT NULL,
' Mem_total ' int (one) DEFAULT NULL,
' Load_avg ' varchar (128) DEFAULT NULL,
' Time ' bigint (one) DEFAULT NULL,
PRIMARY KEY (' id '),
KEY ' Host ' (' Host ' (255))
) Engine=innodb auto_increment=0 DEFAULT Charset=utf8;
Second, flask web-side settings

First we design a Web service that implements the following functions:

Complete the Monitoring page display
Accept the data submitted by post
Provides a JSON data get interface
The concrete frame chart 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 MySQL
Import JSON
From flask import flask, request, render_template
App = 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 use of the map JS here is Highcharts, Highstock, the specific template page content as follows:

[Root@91it templates]# Cat mon.html
<title>memory monitor</title>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>highstock example</title>
<!--<script type= "Text/javascript" src= "{{url_for (' static ', filename= ' Jquery.min.js ')}}" ></script> -->
<script type= "Text/javascript" src= "Http://ajax.useso.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></ Script>
<style type= "Text/css" >
${DEMO.CSS}
</style>
<script type= "Text/javascript" >
$ (function () {
$.getjson ('/data?callback=? ', function (data) {
Create the chart
$ (' #container '). Highcharts (' Stockchart '), {
Rangeselector: {
Inputenabled: $ (' #container '). Width () > 480,
Selected:1
},
Title: {
Text: ' Memory monitor '
},
Series: [{
Name: ' Memory Monitor ',
Data:data,
Type: ' Spline ',
ToolTip: {
Valuedecimals:2
}
}]
});
});
});
</script>
<body>
<!--<script src= "{{url_for (' static ', filename= ' Highstock.js ')}}" ></script>-->
<script src= "Http://cdnjs.cloudflare.com/ajax/libs/highstock/2.0.4/highstock.js" ></script>
<!--<script src= "{{url_for (' static ', filename= ' Exporting.js ')}}" ></script>-->
<script src= "Http://code.highcharts.com/modules/exporting.js" ></script>
<div id= "Container" style= "height:400px" ></div>
</body>
Note: The JS code here is directly using the Internet code, if the host can not connect the Internet, the above three paragraphs to take down, in the templates of the same level directory to create a static directory, will download the three files to the directory, Delete the three code in the template that references JavaScript, using the three paragraphs of the current annotation.

Third, the agent is monitored by the end settings

The Web presentation page is complete, running: The Python flask_web.py is listening on port 8888. We need to do an agent to collect data, and through the Post method request Flask_web page, upload data to the database. Here, for example, monitoring memory, the specific monitoring code is as follows:

#!/usr/bin/env python
#coding =utf-8
Import Inspect
Import time
Import Urllib, Urllib2
Import JSON
Import socket
Class 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):
#自动获取mon类里的所有getXXX方法, the dictionary is constructed using XXX as the return value of key,getxxx () as value.
For fun in inspect.getmembers (self, Predicate=inspect.ismethod):
If fun[0][:3] = = ' Get ':
Self.data[fun[0][3:]] = fun[1] ()
Return Self.data
if __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 & running on the monitored host, if for experimental purposes, want to see the display effect as soon as possible, can be time.sleep (60) Change to Time.sleep (2) so that data is written to the database every 2 seconds.

Visit the http://test.361way.com:8888 to see our monitoring data: The effect is as follows

Highcharts support is dragged over time and is also supported for viewing for a specified period of time. And see the picture can be directly saved as PNG, JPG or PDF, CSV and other formats to view.


This is just a simple monitoring system is actually very simple, of course, to do and high accuracy or need other professional tools to achieve.

Related Article

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.