open falcon 监控php,Python写的一个简单监控系统

web

├── flask_web.py

└── templates

└── mon.html

[/code]

flask_web.py

[code]

import MySQLdb as mysql

import json

from flask import Flask, request, render_template

app = Flask(__name__)

db = mysql.connect(user="reboot", passwd="reboot123", \

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)

这个template页面是我抄的highstock的示例,mon.html

简单起见我们只展示mem_usage信息到页面上

jb51.net

Highstock Example

web展示页面完成了,运行起来:

Python flask_web.py 监听在8888端口上

我们需要做一个agent来采集数据,并上传数据库

moniItems.py

#!/usr/bin/env python

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方法,用XXX作为key,getXXX()的返回值作为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://jb51.net: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 & 运行起来

访问 http://jb51.net:8888 就可以看到我们的监控数据了:效果图如下

41bd1cd16b839a4578296503ac0ed820.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

47d507a036d4dd65488c445c0974b649.png

立即提交

专题推荐064df72cb40df78e80e61b7041ee044f.png独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

7dafe36c040e31d783922649aefe0be1.png玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

04246fdfe8958426b043c89ded0857f1.png天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值