Example #1
0
def get_endpoint_detail_charts():
    
    counters = []
    endpoints = []
    counters0 = request.form.getlist("counters[]") or []
    graph_type = request.form.get("graph_type") or GRAPH_TYPE_HOST
    endpoint0 = request.form.get("endpoint") or ""
    qtype = request.form.get("type") or ""    
    
    endpointlist = Endpoint.search_agent_and_httpapi_by_endpoint(endpoint0)
    for endpoint in endpointlist:
	endpoints.append(endpoint.endpoint)
	for counter in counters0:
            if counter == "port":
                counters.append("net.port.listen/port=" + endpoint.id)
	    elif counter == "memory":
		counters.append("mem.memfree.percent")
	    elif counter == "df":
		counters.append("df.statistics.used.percent")
	    else:
		if (qtype == "mysql" or qtype == "redis"):
		    counters.append(counter + endpoint.id)
		else:
		    counters.append(counter)

    endpoints.append(endpoint0)
    print endpoints
    print counters
    id_ = TmpGraph.add(endpoints, counters)

    ret = {
            "ok": False,
            "id": id_,
            "params": {
                "graph_type": graph_type,
            },
    }
    if id_:
        ret['ok'] = True

    return json.dumps(ret)
Example #2
0
def get_endpoint_detail():
    ret = {
        "ok": False,
        "msg": "",
        "data": [],
    }

    endpoint0 = request.form.get("endpoint") or ""
    qparam = request.form.getlist("q[]") or []
    print qparam
    qtype = request.form.get("type") or ""
    endpoints = Endpoint.search_agent_and_httpapi_by_endpoint(endpoint0)
    for endpoint in endpoints:
	if 'port' in qparam:
            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"
            request1 = urllib2.Request(url, data=json.dumps(p))
            request1.add_header("Content-Type", "application/json")
            request1.get_method = lambda: method
            try:
                connection = opener.open(request1)
            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 = int(value)
                j = []
	        j.append('port')
	        j.append(score)
                ret['data'].append(j)
	        ret['ok'] = True
		qparam.remove('port')
            else:
                print '{"err":1,"msg":"%s"}' % connection
                ret['ok'] = False
	
	# memory
	if (ret['ok'] and 'memory' in qparam):
            http_api = endpoint.ts
            method = "GET"
            handler = urllib2.HTTPHandler()
            opener = urllib2.build_opener(handler)
            request2 = urllib2.Request(http_api + "/page/memory")
            request2.add_header("Content-Type",'application/json')
            request2.get_method = lambda: method
            try:
                connection = opener.open(request2)
            except urllib2.HTTPError,e:
                connection = e

            # check. Substitute with appropriate HTTP code.
            if connection.code == 200:
                msg = connection.read()
                jsonmsg = json.loads(msg)
                if jsonmsg["msg"]=="success":
                    if jsonmsg["data"]:
                        x = jsonmsg["data"][1]
                        y = jsonmsg["data"][0]
                        a = "%.2f"%(x*100/float(y))
                        j = []
                        j.append('memory')
                        j.append(a + '%')
			
                        ret['data'].append(j)
			qparam.remove('memory')
                else:
                    ret['ok'] = False
                    break
            else:
                print '{"err":1,"msg":"%s"}' % connection
                ret['ok'] = False