Example #1
0
def get_groupscore_by_cluster(cluster):
    ret = {
        "ok": False,
        "msg": "",
        "data": [],
    }

    endpoints = Endpoint.search_agent_endpoint_by_cluster(cluster)
    score = 0
    for endpoint in endpoints:
	p = []
	q = {
	    "endpoint": endpoint.endpoint,
	    "counter": "net.port.listen/port=%s"%endpoint.id
	}
	p.append(q)
        method = "POST"
        handler = urllib2.HTTPHandler()
        opener = urllib2.build_opener(handler)
        url = config.QUERY_ADDR + "/graph/last"
        # print ("post data==>>>%s"%json.dumps(p))
        request = urllib2.Request(url, data=json.dumps(p))
        request.add_header("Content-Type", "application/json")
        request.get_method = lambda: method
        try:
            connection = opener.open(request)
        except urllib2.HTTPError,e:
            connection = e

        # check. Substitute with appropriate HTTP code.
        if connection.code == 200:
            msg = connection.read()
	    jsonmsg = json.loads(msg)
	    value = jsonmsg[0]["value"]["value"]
	    score = score + int(value)
	    #j = {
	    #	"endpoint": endpoint.endpoint,
	    #	"value": score
	    #}
	    ret['ok'] = True
        else:
            print '{"err":1,"msg":"%s"}' % connection
            ret['ok'] = False