Grafana使用阿里雲短信的報警實現

 

一、阿里雲短信服務API
二、Jenkins-plugin:Generic Webhook Trigger Pluginpython

  • 阿里雲

    • 下載阿里雲短信服務SDK
    • 建立短信服務ACCESS_KEY_ID
  • Grafana

    • 產生報警信息
    • 將報警信息發送給Jenkins的webhook接口
  • Jenkins

    • 建立JOB,執行python腳本
    • 設置JOB的webhook地址

1、首先根據實際狀況修改阿里雲短信接口的python demoweb

# -*- coding: utf-8 -*-
import sys
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
from aliyunsdkcore.client import AcsClient
import uuid
from aliyunsdkcore.profile import region_provider
from aliyunsdkcore.http import method_type as MT
from aliyunsdkcore.http import format_type as FT
#import const
import json

"""
短信業務調用接口示例,版本號:v20170525

Created on 2017-06-12

"""
try:
    reload(sys)
    sys.setdefaultencoding('utf8')
except NameError:
    pass
except Exception as err:
    raise err

# 注意:不要更改
ACCESS_KEY_ID = "LTAIqEab3HRpRmk6"
ACCESS_KEY_SECRET = "TrnLKl5Qdd7aiAxsUF9v57635y07oK"

REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"

#Jenkins webhook變量
PhoneNumbers = sys.argv[1].split(",")
Contents = sys.argv[2]
State = sys.argv[3]

print "PhoneNumbers: %s" % PhoneNumbers
print "Contents: %s" % Contents

acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)

def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
    smsRequest = SendSmsRequest.SendSmsRequest()
    # 申請的短信模板編碼,必填
    smsRequest.set_TemplateCode(template_code)

    # 短信模板變量參數
    if template_param is not None:
        smsRequest.set_TemplateParam(template_param)

    # 設置業務請求流水號,必填。
    smsRequest.set_OutId(business_id)

    # 短信簽名
    smsRequest.set_SignName(sign_name)
    
    # 數據提交方式
    # smsRequest.set_method(MT.POST)
    
    # 數據提交格式
    # smsRequest.set_accept_format(FT.JSON)
    
    # 短信發送的號碼列表,必填。
    smsRequest.set_PhoneNumbers(phone_numbers)

    # 調用短信發送接口,返回json
    smsResponse = acs_client.do_action_with_exception(smsRequest)

    # TODO 業務處理

    return smsResponse


if __name__ == '__main__':
    __business_id = uuid.uuid1()
    #print(__business_id)
    #params = "{\"code\":\"11113\"}"
    #params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}'
    params = {"name":Contents,"state":State}
    params = json.dumps(params)
    print params
    for PhoneNumber in PhoneNumbers:
        print(send_sms(__business_id, PhoneNumber, "wjoyxt", "SMS_155355123", params))
View Code

附:阿里雲短信模板詳情json

模版類型:短信通知
模版名稱:Grafana短信模板
模版CODE:SMS_155355123
模版內容:Grafana告警名稱:${name} ,如今狀態變爲:${state}
申請說明:公司內部運維人員專用告警短信

2、Jenkins插件Generic Webhook Trigger Plugin配置api

 

3、建立Grafana Notification channels,Url:http://172.17.102.200:8080/generic-webhook-trigger/invoke?token=smsapp

完成!至此在Grafana觸發報警後會經過這個Channel 發送一個POST請求到Jenkins,Jenkins便會執行對應的JOB從而發送報警短信。運維

構建過程日誌以下圖所示:curl

回調參數示例:ide

{
  "title": "My alert",
  "ruleId": 1,
  "ruleName": "Load peaking!",
  "ruleUrl": "http://url.to.grafana/db/dashboard/my_dashboard?panelId=2",
  "state": "alerting",
  "imageUrl": "http://s3.image.url",
  "message": "Load is peaking. Make sure the traffic is real and spin up more webfronts",
  "evalMatches": [
    {
      "metric": "requests",
      "tags": {},
      "value": 122
    }
  ]
}

手動測試:測試

curl -H "Content-Type: application/json" -X POST -d '{"ruleName":"test","state":"OK"}' "http://172.17.102.200:18080/generic-webhook-trigger/invoke?token=sms"
相關文章
相關標籤/搜索