A simple monitoring system written in Python

Source: Internet
Author: User
Tags json readline socket split in python

This article mainly introduces the python written a simple monitoring system, this article explains the detailed coding steps, and give the corresponding implementation code, need friends can refer to the following

There are many open source monitoring systems on the market: Cacti, Nagios, Zabbix. Doesn't feel like I need it, why don't you do it yourself?

Using Python's two-hour manual to masturbate a simple monitoring system, to share with you, hoping to inspire you

First Database Building table

Set up a database "Falcon", the table statement is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 CREATE TABLE ' stat ' (' id ' int ' unsigned NOT NULL auto_increment, ' host ' varchar (256) DEFAULT null, ' mem_free ' int (11) Default NULL, ' Mem_usage ' int (one) default null, ' Mem_total ' int (one) default null, ' LOAD_AVG ' varchar (128) default NULL, ' t IME ' bigint (one) default NULL, PRIMARY key (' ID '), key ' host ' (' Host ' (255)) Engine=innodb auto_increment=0 default Charse T=utf8;

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

1. Complete the Monitoring page display

2. Accept the data submitted by post

3. Provide JSON data get interface

The directory structure is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

This template page is an example of the Highstock I copied, mon.html

For simplicity, we'll just show mem_usage information to the page.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the ; > <title>jb51.net</title> <! DOCTYPE html>

The Web presentation page is complete and running:

Python flask_web.py is listening on port 8888.

We need to do an agent to collect the data and upload the database.

moniitems.py

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74-75 #!/usr/bin/env python import inspect import time import urllib, URLLIB2 import JSON imp ORT 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 L Oad_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 no Buffercache: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.readl INE (). Split () [1]) return a/1024   def runallget (self): #自动获取mon类里的所有getXXX方法, use XXX as value for key,getxxx (), Construct dictionary 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://3lian.net:8888", Json.dumps (data), {' Content-type ': ' Application/json '}) F = Urllib2.urlopen (req) Response = F.read () print response F.close () Time.sleep (m)     nohup python moniitems.py >/dev/null P;1 & run up

Visit the http://jb51.net:8888 to see our monitoring data: The effect is as follows

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.