Using python to call pinpoint interface to realize pin alarm

Use python to call the pinpoint interface to implement the pin alarm.

1. When the application monitored on the pinpoint is called incorrectly and the number of errors is more than 5 (you can set the threshold value by yourself, which is 5 by default), the alarm will be triggered.

2. Just change the webhook and PPURL in the script and put them into the scheduled task, such as once every three minutes.

3. When the script is running, it will check the number of wrong calls applied in the last five minutes, and an alarm will be given if the threshold value is exceeded.

4. Environment: python3.6 (python2 can also be supported)

 

#!/usr/local/bin/python
#Author:zzx
#Function: call the pinpoint interface, monitor the number of calling errors of each application, and send the alarm information to the pin.
import sys
import os
import requests
import time
import datetime
import json

from dingtalkchatbot.chatbot import DingtalkChatbot #pip install dingtalkchatbot



webhook = "Your nails webhook"
PPURL = "http://your_pinpoint_ip:port"

'''Get timestamp in last five minutes'''
From_Time = datetime.datetime.now() + datetime.timedelta(seconds=-300)
To_Time = datetime.datetime.now()
From_TimeStamp = int(time.mktime(From_Time.timetuple()))*1000
To_TimeStamp = int(time.mktime(datetime.datetime.now().timetuple()))*1000


"""Obtain pinpoint Basic information of all services in,Including service name, service type, etc"""
def get_applications():
    '''return application dict
    '''
    applicationListUrl = PPURL + "/applications.pinpoint"
    res = requests.get(applicationListUrl)
    if res.status_code != 200:
        print("Request exception,Please check.")
        return
    return res.json()
    #print(res.json()[0])


'''Pass in the service name, and return the number of nodes of the service and the node names of each node'''
def getAgentList(appname):
    AgentListUrl = PPURL + "/getAgentList.pinpoint"
    param = {
        'application':appname
    }
    res = requests.get(AgentListUrl, params=param)
    if res.status_code != 200:
        print("Request exception,Please check.")
        return
    return len(res.json().keys()),json.dumps(list(res.json().keys()))



'''Get call failures'''
def update_servermap(appname , from_time=From_TimeStamp,to_time=To_TimeStamp, serviceType='TOMCAT'):
    '''To update app Upstream downstream relationship
    :param appname: apply name
    :param serviceType: Application type
    :param from_time: Starting time
    :param to_time: Termination time
    :
    '''
    #https://pinpoint.*****.com/getServerMapData.pinpoint?applicationName=test-app&from=1547721493000&to=1547721553000&callerRange=1&calleeRange=1&serviceTypeName=TOMCAT&_=1547720614229
    param = {
        'applicationName':appname,
        'from':from_time,
        'to':to_time,
        'callerRange':1,
        'calleeRange':1,
        'serviceTypeName':serviceType
    }

    # serverMapUrl = PPURL + "/getServerMapData.pinpoint"
    serverMapUrl = "{}{}".format(PPURL, "/getServerMapData.pinpoint")
    res = requests.get(serverMapUrl, params=param)
    if res.status_code != 200:
        print("Request exception,Please check.")
        return
    update_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    links = res.json()["applicationMapData"]["linkDataArray"]
    #links contains the upstream and downstream call chain of the app, as well as the number of calls and failures between them.
    #print(links)
    #print(len(links))
#    totalCount=0
    errorCount=0
#    slowCount=0
    for link in links :
        ###Exclude the application of test
        if link['sourceInfo']['applicationName'].startswith('test'):
            continue
        #Application name, application type, downstream application name, downstream application type, number of application nodes, number of downstream application nodes, total requests, number of error requests, number of slow requests (the number of this application to the next application)
#        application = link['sourceInfo']['applicationName']
#        serviceType = link['sourceInfo']['serviceType']
#        to_application = link['targetInfo']['applicationName']
#        to_serviceType = link['targetInfo']['serviceType']
#        agents = len(link.get('fromAgent',' '))
#        to_agents =  len(link.get('toAgent',' '))
        '''Total errors accumulated'''
#        totalCount += link['totalCount']
        errorCount += link['errorCount']
#        slowCount  += link['slowCount']

#    return totalCount
    return errorCount



'''Native nail alarm, not used in this script'''
def messages(application_name,service_type,error_count):  # Defining information functions
    headers = {'Content-Type': 'application/json;charset=utf-8'}  # Head information, official document writing method of ZABBIX, you can view official document of ZABBIX
    text="""
//Alarm policy: ERROR COUNT
//Alarm content: ERROR COUNT value is {error_count} during the past 5 mins
//Service type: {service Utype}
   //Service Name: {application uname}
""".format(error_count=error_count,service_type=service_type,application_name=application_name)
    text_info = {  # You can check the Zabbix Api in the official document of Zabbix for rules
        "msgtype": "text",
        "at": {
            "atMobiles": [
                "13128841321", "13728978429"
            ],
            "isAtAll": False
        },
        "text": {
            "content": text
        }
    }

    print(requests.post(webhook, json.dumps(text_info), headers=headers).content)  # Encode the returned data into JSON string
    requests.post(webhook, json.dumps(text_info), headers=headers).content  # Encode the returned data into JSON string




 
     

if __name__ == "__main__":
    '''Initialize pin object'''
    xiaoding = DingtalkChatbot(webhook)
    at_mobiles=['13728978429','wangwei']

    '''Get all services app Name and service type, coexisting in dictionary'''
    applicationLists=get_applications()
    #print(applicationLists)

    '''debugging update_servermap Function, the return value of the function needs to be changed:totalCount,errotCount,slowCount'''
    #count=update_servermap('push-base', from_time=From_TimeStamp,to_time=To_TimeStamp,serviceType='TOMCAT')
    #print(count)

    '''polling application,Query each application The total number of errors in the past five minutes, and alarm by nailing'''
    for app in applicationLists:
        application_name = app['applicationName']
        service_type = app['serviceType']
        pid = os.fork() #Using fork to achieve concurrency
        if pid == 0:
            error_count = update_servermap(application_name, from_time=From_TimeStamp,to_time=To_TimeStamp, serviceType=service_type)
            text = """
<font color=#Ff0000 > pinpoint alarm < / font > \ n \ n
> <font color=#0a0a0a > alarm policy: < / font > error count \ n \ n
> <font color=#0A0A0A>Alarm content:</font>ERROR COUNT value is <font color=#FF0000>{error_count}</font> during the past 5 mins.\n\n
> <font color=#0a0a0a > service type: < / font > {service_type} \ n \ n
> &emsp;<font color=#0a0a0a > Service Name: < / font > {application_name}
""".format(error_count=error_count, service_type=service_type, application_name=application_name)

            '''If the total number of call errors exceeds the threshold of 5(Set according to actual needs),Alarm'''
            if error_count >5:
                #messages(application_name,service_type,error_count)
                xiaoding.send_markdown(title='pp Call the police', text=text,at_mobiles=at_mobiles)
            exit(0)

I hope to help you. What do you need to communicate with me qq: 1074060710

Reference article:

https://yq.aliyun.com/articles/690351

Tags: JSON Zabbix Tomcat Python

Posted on Tue, 05 Nov 2019 11:08:54 -0500 by cmanhatton