编程语言
首页 > 编程语言> > python使用AliyunOpenApi获取arms数据

python使用AliyunOpenApi获取arms数据

作者:互联网

python使用AliyunOpenApi获取arms数据

arms_class.py

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
# 该库获取监控app基本信息
from aliyunsdkarms.request.v20190808.ListTraceAppsRequest import ListTraceAppsRequest
# 该库获取调用链路基本信息
from aliyunsdkarms.request.v20190808.SearchTracesRequest import SearchTracesRequest
from aliyunsdkarms.request.v20190808.QueryMetricByPageRequest import QueryMetricByPageRequest
import json
import time
import sys

class ArmsInfo():
    def __init__(self, starttime, endtime):
        self.starttime = starttime
        self.endtime = endtime
        self.client = AcsClient('AKID', 'AK_SRT', 'cn-beijing')
        pass
    def time_to_mktime(self,dt):
        ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
        return int(round(ts * 1000))

    def get_app_trace(self):
        pass
    def get_app_exception(self):
        pass

arms_py.py

#!/usr/bin/env python
#coding=utf-8
import json
import re
import time
from aliyunsdkcore.client import AcsClient
# 该库获取监控app基本信息
from aliyunsdkarms.request.v20190808.ListTraceAppsRequest import ListTraceAppsRequest
# 该库获取调用链路基本信息
from aliyunsdkarms.request.v20190808.SearchTracesRequest import SearchTracesRequest
from aliyunsdkarms.request.v20190808.SearchTracesByPageRequest import SearchTracesByPageRequest
# 获取接口名称
from aliyunsdkarms.request.v20190808.GetAppApiByPageRequest import GetAppApiByPageRequest

from aliyunsdkarms.request.v20190808.QueryMetricByPageRequest import QueryMetricByPageRequest

client = AcsClient('AK_ID', 'AK_SRT', 'cn-beijing', timeout=300)
##################################################################################################
# 时间戳转换
def time_to_mktime(dt):
    ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    return int(round(ts * 1000))

##################################################################################################
# 获取jvm host 接口错误 异常数据 mq sql执行情况
# https://help.aliyun.com/document_detail/186100.html?spm=a2c4g.11186623.6.980.407814031ejB7j
def get_app_exception(pid,startime,endtime):
    request = QueryMetricByPageRequest()
    request.set_accept_format('json')
    request.set_StartTime(startime)
    request.set_PageSize(5000)
    request.set_EndTime(endtime)
    request.set_Metric("appstat.exception")
    request.set_Measuress(["rt","count"])
    request.set_IntervalInSec(60000)
    request.set_Filterss([
    {
        "Key": "pid",
        "Value": pid
    },
    {
        "Key": "regionId",
        "Value": "cn-beijing"
    }
    ])
    request.set_Order("ASC")

    response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    err_num=0
    for ss in response["Data"]["Items"]:
        # print(ss)
        err_num += ss["count"]

    # print(err_num)
    return err_num

##################################################################################################
# 获取app所有接口调用的次数,无法获取
def get_interface_count(pid,starttime,endtime):
    request = QueryMetricByPageRequest()
    request.set_accept_format('json')
    request.set_EndTime(endtime)
    request.set_Metric("appstat.txn")
    request.set_Measuress(["rt","count","error"])
    request.set_IntervalInSec(600000)
    # request.set_IntervalInSec(2147483647)
    request.set_Dimensionss(["rpc"])
    request.set_OrderBy("rpc")
    request.set_Filterss([
    {
    "Key": "pid",
    "Value": pid
    },
    {
    "Key": "regionId",
    "Value": "cn-beijing"
    }
    ])
    request.set_StartTime(starttime)
    request.set_PageSize(5000)
    request.set_Order("ASC")
    response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    
    total = response['Data']['Total']
    pagenum = int(total/5000) +1
    print('total=',total,'pagenum=',pagenum,5000)
    count = 0
    for page in range(1,pagenum+1):
        # print('page=',page)
        request.set_CurrentPage(page)
        response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
        for interfaces in response["Data"]["Items"]:
            count = count + interfaces["count"]
            # print(interfaces["rpc"])
    print('接口请求数:',count)
    return count

##################################################################################################
# 获取app所有接口请求时间并筛选>200ms的接口数据;应用请求错误数
def get_interface_time(pid,starttime,endtime):
    request = QueryMetricByPageRequest()
    request.set_accept_format('json')
    request.set_EndTime(endtime)
    request.set_Metric("appstat.incall")
    request.set_Measuress(["rt","count","error"])
    request.set_IntervalInSec(2147483647)
    request.set_Dimensionss(["rpc"])
    request.set_OrderBy("rpc")
    request.set_Filterss([
    {
    "Key": "pid",
    "Value": pid
    },
    {
    "Key": "regionId",
    "Value": "cn-beijing"
    }
    ])
    request.set_StartTime(starttime)
    request.set_PageSize(5000)
    request.set_Order("ASC")
    response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    
    total = response['Data']['Total']
    pagenum = int(total/5000) +1
    ss = {}
    err = 0
    for page in range(1,pagenum+1):
        # print('page=',page)
        request.set_CurrentPage(page)
        response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
        for interfaces in response["Data"]["Items"]:
            err = err + interfaces["error"]
            # if interfaces["error"] != 0:
            #     # print(interfaces["rpc"])
            if interfaces["rpc"] not in ss.keys() and interfaces["rt"] >200.0: 
                ss[interfaces["rpc"]]=interfaces["rt"]
            elif interfaces["rt"] >200.0 and interfaces["rt"]> ss[interfaces["rpc"]] :
                ss[interfaces["rpc"]] = interfaces["rt"]
            else:
                pass
    # print(err,ss)
    return ss,err

##################################################################################################
# 获取app监控sql调用信息
def get_app_sqlinfo(pid,starttime,endtime):
    # print(starttime,endtime,pid)
    request = QueryMetricByPageRequest()
    request.set_accept_format('json')
    request.set_EndTime(endtime)
    request.set_Metric("appstat.sql")
    request.set_Measuress(["rt","count","slow"])
    request.set_IntervalInSec(60000)
    request.set_Dimensionss(["pid","rpc","sqlId"])
    request.set_Filterss([
    {
    "Key": "pid",
    "Value": pid
    },
    {
    "Key": "regionId",
    "Value": "cn-beijing"
    }
    ])
    request.set_StartTime(starttime)
    request.set_PageSize(5000)
    request.set_Order("ASC")
    response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    ss = {}
    for interfaces in response["Data"]["Items"]:
        data = []
        if (interfaces["rpc"] not in ss.keys() )and interfaces["rt"] >50.0 :
            data.append(interfaces["sqlId"])
            data.append(interfaces["rt"])
            ss[interfaces["rpc"]] = data
            # print(ss)
        elif interfaces["rt"] >50.0 and interfaces["rpc"] in ss.keys():
            ss[interfaces["rpc"]][1]=interfaces["rt"] 
        else:
            pass
    # print(set(data))
    return ss
##################################################################################################
# 获取app接口名称
def get_app_interface(pid,starttime,endtime):
    request = GetAppApiByPageRequest()
    request.set_accept_format('json')
    request.set_EndTime(endtime)
    request.set_StartTime(starttime)
    request.set_PId(pid)
    request.set_CurrentPage(1)
    request.set_PageSize(100000)
    request.set_IntervalMills(2147483647)
    response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    interfaces = []
    for interface in response["Data"]["Items"]:
        # print(interface)
        if "actuator" not in interface['rpc']:
            interfaces.append(interface['rpc'])
    print(len(set(interfaces)),set(interfaces))
    return set(interfaces)

##################################################################################################
# 获取app接口链路调用信息
# def get_interface_trace(appname,OperationName,starttime,endtime):
    # request = SearchTracesByPageRequest()
    # request.set_accept_format('json')
    # request.set_EndTime(endtime)
    # request.set_StartTime(starttime)
    # request.set_ServiceName(appname)
    # request.set_OperationName(OperationName)
    # request.set_PageNumber(1)
    # request.set_PageSize(100)
    # response = json.loads(str(client.do_action_with_exception(request),encoding='utf-8'))
    # print(response)   
    # print(len(response["TraceInfos"]))


##################################################################################################
# 获取app监控基本信息
def get_app_armsinfo(starttime,endtime):
    data = {}
    
    appinfos = ListTraceAppsRequest()
    appinfos.set_accept_format('json')
    appinfos = json.loads(str(client.do_action_with_exception(appinfos),encoding='utf-8'))
    # ss = get_interface_time("d166d4acnt@e5e51171a92099b",starttime,endtime)
    
    for apps in appinfos["TraceApps"]:
        ss = []
        # 接口请求数据
        print(apps["AppName"],apps["Pid"])
        ss1,ss2 = get_interface_time(apps["Pid"],starttime,endtime)
        print("执行结束")
        # count = get_interface_count(apps["Pid"],starttime,endtime)
        # print(count)
        # time.sleep(200000)
        # 接口sql信息
        ss3 = get_app_sqlinfo(apps["Pid"],starttime,endtime)
        ss.append(ss1)
        ss.append(ss2)
        ss.append(ss3)
        data[apps["AppName"]]=ss
        # time.sleep(200)
    # print(data,len(data))
    return data
    
##################################################################################################
# 数据写入excel

if __name__ == '__main__':
    

    starttime = time_to_mktime('2021-06-21 00:00:00')
    endtime = time_to_mktime('2021-06-25 23:50:59')
    print(starttime,endtime)
    # 获取应用基本信息
    data = get_app_armsinfo(starttime,endtime)
    print(data)
    # save_xls(data,'arms_info')
    # toMail = ['zhouping13@xdf.cn','libo28@xdf.cn']
    # s = SendMail("smtp.163.com", "465", "xdfkidsplus@163.com", toMail, "xdfkidsplus@163.com", "BGWVTSNTSJNCKYUG", filename)
    # s.sendMail()

标签:set,python,interfaces,request,arms,ss,AliyunOpenApi,print,endtime
来源: https://www.cnblogs.com/pgy674477230/p/16351499.html