jason接口解析监控--Python实例
本例从配置文件中读取监控接口#!/usr/bin/python#-*- coding: UTF-8 -*-# wirte by luotianhui 2016.6.21import typesimport urllib2import jsonimport ConfigParserimport string, os, sysclass urlData:name = "
·
本例从配置文件中读取监控接口
#!/usr/bin/python
#-*- coding: UTF-8 -*-
# wirte by 2016.6.21
import types
import urllib2
import json
import ConfigParser
import string, os, sys
class urlData:
name = ""
cluster = ""
def __init__(self, name, cluster):
self.name = name
self.cluster = cluster
def readConfig():
try:
cf = ConfigParser.ConfigParser()
path = "C:/Users/luotianhui/Desktop/config.conf"
cf.read(path)
# 获取文件中所有类别
category = cf.sections()
print "sections =", category
opts1 = cf.options("name")
print 'options:', opts1
opts2 = cf.options("cluster")
print 'options:', opts2
urlList = []
# 从配置文件读取queue1
tuan_sc_name = cf.get("name", "name_tuan_sc")
tuan_sc_cluster = cf.get("cluster", "cluster_tuan_sc")
print tuan_sc_name,tuan_sc_cluster
tuan_sc = urlData(tuan_sc_name, tuan_sc_cluster)
#print "queue1"
#print tuan_sc.name,tuan_sc.cluster
# 从配置文件读取queue2
tuan_financial_name = cf.get("name", "name_tuan_financial")
tuan_financial_cluster = cf.get("cluster", "cluster_tuan_financial")
print tuan_financial_name,tuan_financial_cluster
tuan_financial = urlData(tuan_financial_name, tuan_financial_cluster)
# 添加queue
urlList.append(tuan_sc)
urlList.append(tuan_financial)
print len(urlList)
return urlList
except Exception, e:
print e
# 获取要解析的网络地址的jason数据
def registerUrl(name, cluster):
try:
url = "http://bigpipe.baidu.com/monitor/queue.php?name=" + name + "&cluster=" + cluster
print url
data = urllib2.urlopen(url).read()
print data
return data
except Exception, e:
print e
except urllib2.HTTPError, e:
print 'Error code: ', e.code
except urllib2.URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
def praserJsonFile(jsonData):
value = json.loads(jsonData)
if value["code"] == 0 and value["queue"]["number_of_messages"] < 5000:
print "True"
return True
else:
return False
print "False"
if __name__ == "__main__":
config_data = []
config_data = readConfig()
#print config_data
# 保存不符合条件的queue
failed_queue = []
for obj in config_data:
print "config_data"
if obj is None:
break
print obj.name,obj.cluster
data = registerUrl(obj.name, obj.cluster)
print data
flag = praserJsonFile(data)
if (flag == False):
failed_queue.append(obj.name, obj.cluster)
更多推荐
已为社区贡献3条内容
所有评论(0)