pinpoint默認是從web界面設置應用告警規則的。pinpoint官方文檔中也並未有相關api接口的說明,但操做pinpoint web界面時,按F12打開開發者工具,可觀察到其api接口。python
假設當前pinpoint web的地址爲http://172.31.2.5:8079/,上面有個應用爲xmgate:web
獲取應用列表:json
GET請求,http://172.31.2.5:8079/applications.pinpointapi
其返回json數據,相似於:服務器
[{"applicationName":"xmgate","serviceType":"SPRING_BOOT","code":1210}] |
獲取指定應用的告警規則:
app
GET請求,http://172.31.2.5:8079/application/alarmRule.pinpoint?applicationId=xmgateide
其返回json數據,相似於:函數
[{"ruleId":"1","applicationId":"xmgate","serviceType":"SPRING_BOOT","checkerName":"HEAP USAGE RATE","threshold":80,"userGroupId":"DevOpsEngineers","smsSend":false,"emailSend":true,"notes":""}] |
設置告警規則:工具
POST請求,http://172.31.2.5:8079/application/alarmRule.pinpoint,需攜帶請求頭「Content-Type: application/json」,根據所需設置的告警規則需攜帶相應載荷,相似於(不一樣的監控指標可能會有所差別):性能
{"applicationId":"xmgate","serviceType":"SPRING_BOOT","checkerName":"HEAP USAGE RATE","userGroupId":"DevOpsEngineers","threshold":80,"emailSend":true,"smsSend":false,"notes":""} |
其返回json數據,相似於:
{'result': 'SUCCESS', 'ruleId': '35'} |
根據上述分析,可編寫以下python腳本:
[root@gw5 ~]# cat setAlarm.py #!/usr/bin/env python3 # -*- coding: utf-8 -*-
import sys, json, urllib.request, re
# pinpoint web地址 ppWeb = 'http://172.31.2.5:8079' # pinpoint web中接收告警的用戶組 userGroup = 'DevOpsEngineers' # 需設置告警的性能指標名稱(mtc)及閾值(tsd)的列表,可按需增長 metricList = [{'mtc':'SLOW RATE','tsd':30}, {'mtc':'ERROR RATE','tsd':30}, {'mtc':'HEAP USAGE RATE','tsd':80}, {'mtc':'JVM CPU USAGE RATE','tsd':80}, {'mtc':'DATASOURCE CONNECTION USAGE RATE','tsd':80}, {'mtc':'FILE DESCRIPTOR COUNT','tsd':10000}]
# 訪問pinpoint的函數 def accessPP(Url, Header, Data): url, header, data = Url, Header, Data if not data: request = urllib.request.Request(url) else: request = urllib.request.Request(url, json.dumps(data).encode("utf-8")) if header: for key in header: request.add_header(key, header[key]) try: response = urllib.request.urlopen(request) except Exception as e: print('[ERROR] %s' % e) sys.exit(1) else: return json.loads(response.read( ).decode("utf-8")) finally: if 'response' in vars( ): response.close( )
# 主函數 def main(): # 獲取應用列表 url = '%s/applications.pinpoint' % ppWeb header = {} data = {} appList = accessPP(url, header, data) if not appList: print(u'[INFO] pinpoint中未發現有應用!') sys.exit(0) for app in appList: # 獲取應用告警規則列表 url = '%s/application/alarmRule.pinpoint?applicationId=%s' % (ppWeb, app['applicationName']) header = {} data = {} alarmRuleList = accessPP(url, header, data) # 若告警規則已存在則跳過,若不存在則進行設置 url = '%s/application/alarmRule.pinpoint' % ppWeb header = {'Content-Type': 'application/json'} for metric in metricList: if re.findall(metric['mtc'], str(alarmRuleList)): print(u'[INFO] 應用程序 "%s" 跳過設置告警規則 "%s"' % (app['applicationName'], metric['mtc'])) continue data = { "applicationId": app['applicationName'], "serviceType": app['serviceType'], "checkerName": metric['mtc'], "userGroupId": userGroup, "threshold": metric['tsd'], "emailSend": "true", "smsSend": "false", "notes": "" } state = accessPP(url, header, data) # 因爲pinpoint對傳入的參數未作校驗,因此基本上返回的都是'SUCCESS',因此下面的判斷沒啥太大意義,但仍是留着備用吧 if state['result'] == 'SUCCESS': print(u'[INFO] 應用程序 "%s" 告警規則設置成功 "%s"' % (app['applicationName'], metric['mtc'])) else: print(u'[ERROR] 應用程序 "%s" 告警規則設置失敗 "%s"' % (app['applicationName'], metric['mtc'])) print(u'[INFO] 返回信息 %s' % state) main( ) |
執行腳本:
[root@gw5 ~]# ./setAlarm.py |
該腳本爲以下性能指標設置了告警規則(如需增刪性能指標或調整閾值,可自行修改腳本中的metricList變量):
SLOW RATE
ERROR RATE
HEAP USAGE RATE
JVM CPU USAGE RATE
DATASOURCE CONNECTION USAGE RATE
FILE DESCRIPTOR COUNT
該腳本對於已設置告警規則的性能指標會跳過設置,如果未設置的則會新增告警規則。也可將該腳本放在Linux服務器的crontab中定時運行,以實現對新增應用自動設置告警規則。