jason接口解析监控--Python实例

本例从配置文件中读取监控接口
#!/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)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fullstack_lth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值