打赏

星辰大海ゞ

That which does not kill us makes us stronger!

导航

Grafana使用阿里云短信的报警实现

 

1、阿里云短信服务API
2、Jenkins-plugin:Generic Webhook Trigger Plugin

  • 阿里云

  • Grafana

    • 产生报警信息
    • 将报警信息发送给Jenkins的webhook接口
  • Jenkins

    • 创建JOB,执行python脚本
    • 设置JOB的webhook地址

一、首先根据实际情况修改阿里云短信接口的python demo

# -*- 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 = "ABSIqEab378pRmk9"
ACCESS_KEY_SECRET = "PrnNAVl5Qdd7aiAxsUF9v33213yoK"

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))

 

附:阿里云短信模板详情

模版类型:短信通知
模版名称:Grafana短信模板
模版CODE:SMS_155355123
模版内容:Grafana告警名称:${name} ,现在状态变为:${state}
申请说明:公司内部运维人员专用告警短信

二、Jenkins插件Generic Webhook Trigger Plugin配置

 

三、创建Grafana Notification channels,Url:http://172.17.102.200:8080/generic-webhook-trigger/invoke?token=sms

完成!至此在Grafana触发报警后会通过这个Channel 发送一个POST请求到Jenkins,Jenkins便会执行对应的JOB从而发送报警短信。

构建过程日志如下图所示:

回调参数示例:

{
  "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"

 

posted on 2019-01-11 15:02  星辰大海ゞ  阅读(2637)  评论(0编辑  收藏  举报