开发者社区> 问答> 正文

云监控中上报事件数据

反向一觉 2017-10-31 10:40:34 1268
事件监控功能为您提供上报事件的接口,方便您将业务产生的异常事件采集上报到云监控,通过对上报的事件配置报警规则来接收报警通知。
云监控为您提供 OpenAPI、Java SDK 和阿里云命令行工具(CLI) 三种方式上报数据。
使用限制
  • 单云账号QPS限制为20
  • 单次最多上报100个事件
  • 单次最多上报500KB数据

OpenAPI上报数据
服务地址
https://metrichub-cms-cn-hangzhou.aliyuncs.com
请求语法
  1. [backcolor=transparent]POST [backcolor=transparent]/[backcolor=transparent]event[backcolor=transparent]/[backcolor=transparent]custom[backcolor=transparent]/[backcolor=transparent]upload HTTP[backcolor=transparent]/[backcolor=transparent]1.1[backcolor=transparent]
  2. [backcolor=transparent]Authorization[backcolor=transparent]:<[backcolor=transparent]AuthorizationString[backcolor=transparent]>
  3. [backcolor=transparent]Content[backcolor=transparent]-[backcolor=transparent]Length[backcolor=transparent]:<[backcolor=transparent]Content[backcolor=transparent] [backcolor=transparent]Length[backcolor=transparent]>
  4. [backcolor=transparent]Content[backcolor=transparent]-[backcolor=transparent]MD5[backcolor=transparent]:<[backcolor=transparent]Content[backcolor=transparent] MD5[backcolor=transparent]>
  5. [backcolor=transparent]Content[backcolor=transparent]-[backcolor=transparent]Type[backcolor=transparent]:[backcolor=transparent]application[backcolor=transparent]/[backcolor=transparent]json
  6. [backcolor=transparent]Date[backcolor=transparent]:<[backcolor=transparent]GMT [backcolor=transparent]Date[backcolor=transparent]>
  7. [backcolor=transparent]Host[backcolor=transparent]:[backcolor=transparent] metrichub[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]cn[backcolor=transparent]-[backcolor=transparent]hangzhou[backcolor=transparent].[backcolor=transparent]aliyuncs[backcolor=transparent].[backcolor=transparent]com
  8. [backcolor=transparent]x[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]signature[backcolor=transparent]:[backcolor=transparent]hmac[backcolor=transparent]-[backcolor=transparent]sha1
  9. [backcolor=transparent]x[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]api[backcolor=transparent]-[backcolor=transparent]version[backcolor=transparent]:[backcolor=transparent]1.0
  10. [backcolor=transparent]x[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]ip[backcolor=transparent]:[backcolor=transparent]30.27[backcolor=transparent].[backcolor=transparent]84.196
  11. [backcolor=transparent]User[backcolor=transparent]-[backcolor=transparent]Agent[backcolor=transparent]:[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]java[backcolor=transparent]-[backcolor=transparent]sdk[backcolor=transparent]-[backcolor=transparent]v[backcolor=transparent]-[backcolor=transparent]1.0
  12. [backcolor=transparent][{[backcolor=transparent]"content"[backcolor=transparent]:[backcolor=transparent]"EventContent"[backcolor=transparent],[backcolor=transparent]"groupId"[backcolor=transparent]:[backcolor=transparent]GroupId[backcolor=transparent],[backcolor=transparent]"name"[backcolor=transparent]:[backcolor=transparent]"EventName"[backcolor=transparent],[backcolor=transparent]"time"[backcolor=transparent]:[backcolor=transparent]"20171023T144439.948+0800"[backcolor=transparent]}]

请求参数
名称类型必选描述
name字符串事件名称
groupId数值事件所属的应用分组Id
time字符串事件发生时间
content字符串事件详情

关于API的请求头,请参考 请求头定义
响应元素
HTTP 状态码返回 200。
示例
请求示例
  1. [backcolor=transparent]POST [backcolor=transparent]/[backcolor=transparent]event[backcolor=transparent]/[backcolor=transparent]custom[backcolor=transparent]/[backcolor=transparent]upload HTTP[backcolor=transparent]/[backcolor=transparent]1.1[backcolor=transparent]
  2. [backcolor=transparent]Host[backcolor=transparent]:[backcolor=transparent] metrichub[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]cn[backcolor=transparent]-[backcolor=transparent]hangzhou[backcolor=transparent].[backcolor=transparent]aliyuncs[backcolor=transparent].[backcolor=transparent]com
  3. [backcolor=transparent]x[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]api[backcolor=transparent]-[backcolor=transparent]version[backcolor=transparent]:[backcolor=transparent]1.0
  4. [backcolor=transparent]Authorization[backcolor=transparent]:[backcolor=transparent]YourAccKey[backcolor=transparent]:[backcolor=transparent]YourAccSecret[backcolor=transparent]
  5. [backcolor=transparent]Host[backcolor=transparent]:[backcolor=transparent]metrichub[backcolor=transparent]-[backcolor=transparent]cms[backcolor=transparent]-[backcolor=transparent]cn[backcolor=transparent]-[backcolor=transparent]hangzhou[backcolor=transparent].[backcolor=transparent]aliyuncs[backcolor=transparent].[backcolor=transparent]com[backcolor=transparent]"
  6. [backcolor=transparent]Date:Mon, 23 Oct 2017 06:51:11 GMT
  7. [backcolor=transparent]Content-Length:180
  8. [backcolor=transparent]x-cms-signature:hmac-sha1
  9. [backcolor=transparent]Content-MD5:E9EF574D1AEAAA370860FE37856995CD
  10. [backcolor=transparent]x-cms-ip:30.27.84.196
  11. [backcolor=transparent]User-Agent:cms-java-sdk-v-1.0
  12. [backcolor=transparent]Content-Type:application/json
  13. [backcolor=transparent][{"[backcolor=transparent]content[backcolor=transparent]":"[backcolor=transparent]123[backcolor=transparent],[backcolor=transparent]abc[backcolor=transparent]","[backcolor=transparent]groupId[backcolor=transparent]":100,"[backcolor=transparent]name[backcolor=transparent]":"[backcolor=transparent]Event_0[backcolor=transparent]","[backcolor=transparent]time[backcolor=transparent]":"[backcolor=transparent]20171023T144439.948[backcolor=transparent]+[backcolor=transparent]0800[backcolor=transparent]"}]


返回示例

  1. [backcolor=transparent]{
  2. [backcolor=transparent]   [backcolor=transparent]"code"[backcolor=transparent]:[backcolor=transparent]"200"[backcolor=transparent],
  3. [backcolor=transparent]   [backcolor=transparent]"msg"[backcolor=transparent]:[backcolor=transparent]""[backcolor=transparent]//正常上报时返回msg为空
  4. [backcolor=transparent]}

Java SDK上报数据
maven依赖
  1. [backcolor=transparent]<dependency>
  2. [backcolor=transparent]    [backcolor=transparent]<groupId>[backcolor=transparent]com.aliyun.openservices[backcolor=transparent]</groupId>
  3. [backcolor=transparent]    [backcolor=transparent]<artifactId>[backcolor=transparent]aliyun-cms[backcolor=transparent]</artifactId>
  4. [backcolor=transparent]    [backcolor=transparent]<version>[backcolor=transparent]0.1.2[backcolor=transparent]</version>
  5. [backcolor=transparent]</dependency>

示例代码
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] uploadEvent[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]CMSException[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]InterruptedException[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]        [backcolor=transparent]//初始化客户端
  3. [backcolor=transparent]        [backcolor=transparent]CMSClient[backcolor=transparent] cmsClient [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]CMSClient[backcolor=transparent]([backcolor=transparent]endpoint[backcolor=transparent],[backcolor=transparent] accKey[backcolor=transparent],[backcolor=transparent] secret[backcolor=transparent]);
  4. [backcolor=transparent]       [backcolor=transparent]//构建2个事件上报
  5. [backcolor=transparent]         [backcolor=transparent]CustomEventUploadRequest[backcolor=transparent] request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]CustomEventUploadRequest[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  6. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]CustomEvent[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  7. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setContent[backcolor=transparent]([backcolor=transparent]"abc,123"[backcolor=transparent])
  8. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setGroupId[backcolor=transparent]([backcolor=transparent]101l[backcolor=transparent])
  9. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setName[backcolor=transparent]([backcolor=transparent]"Event001"[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]())
  10. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]CustomEvent[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  11. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setContent[backcolor=transparent]([backcolor=transparent]"abc,123"[backcolor=transparent])
  12. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setGroupId[backcolor=transparent]([backcolor=transparent]101l[backcolor=transparent])
  13. [backcolor=transparent]                            [backcolor=transparent].[backcolor=transparent]setName[backcolor=transparent]([backcolor=transparent]"Event002"[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]())
  14. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]build[backcolor=transparent]();
  15. [backcolor=transparent]            [backcolor=transparent]CustomEventUploadResponse[backcolor=transparent] response [backcolor=transparent]=[backcolor=transparent] cmsClient[backcolor=transparent].[backcolor=transparent]putCustomEvent[backcolor=transparent]([backcolor=transparent]request[backcolor=transparent]);
  16. [backcolor=transparent]            [backcolor=transparent]List[backcolor=transparent]<[backcolor=transparent]CustomEvent[backcolor=transparent]>[backcolor=transparent] eventList [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ArrayList[backcolor=transparent]<[backcolor=transparent]CustomEvent[backcolor=transparent]>();
  17. [backcolor=transparent]            eventList[backcolor=transparent].[backcolor=transparent]add[backcolor=transparent]([backcolor=transparent]CustomEvent[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  18. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setContent[backcolor=transparent]([backcolor=transparent]"abcd,1234"[backcolor=transparent])
  19. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setGroupId[backcolor=transparent]([backcolor=transparent]101l[backcolor=transparent])
  20. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setName[backcolor=transparent]([backcolor=transparent]"Event001"[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]());
  21. [backcolor=transparent]            eventList[backcolor=transparent].[backcolor=transparent]add[backcolor=transparent]([backcolor=transparent]CustomEvent[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  22. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setContent[backcolor=transparent]([backcolor=transparent]"abcd,1234"[backcolor=transparent])
  23. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setGroupId[backcolor=transparent]([backcolor=transparent]101l[backcolor=transparent])
  24. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setName[backcolor=transparent]([backcolor=transparent]"Event002"[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]());
  25. [backcolor=transparent]            request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]CustomEventUploadRequest[backcolor=transparent].[backcolor=transparent]builder[backcolor=transparent]()
  26. [backcolor=transparent]                    [backcolor=transparent].[backcolor=transparent]setEventList[backcolor=transparent]([backcolor=transparent]eventList[backcolor=transparent]).[backcolor=transparent]build[backcolor=transparent]();
  27. [backcolor=transparent]            response [backcolor=transparent]=[backcolor=transparent] cmsClient[backcolor=transparent].[backcolor=transparent]putCustomEvent[backcolor=transparent]([backcolor=transparent]request[backcolor=transparent]);
  28. [backcolor=transparent]    [backcolor=transparent]}

阿里云命令行(CLI)方式上报数据
第一步: 安装阿里云CLI, 安装文档
第二步:安装CmsSDK
Windows安装方式:在命令行窗口输入如下命令
  1. [backcolor=transparent]cd C[backcolor=transparent]:[backcolor=transparent]\Python27\Scripts
  2. [backcolor=transparent]pip install aliyun[backcolor=transparent]-[backcolor=transparent]python[backcolor=transparent]-[backcolor=transparent]sdk[backcolor=transparent]-[backcolor=transparent]cms

如果需要更新SDK,则使用如下命令:
  1. [backcolor=transparent]pip install [backcolor=transparent]--[backcolor=transparent]upgrade aliyun[backcolor=transparent]-[backcolor=transparent]python[backcolor=transparent]-[backcolor=transparent]sdk[backcolor=transparent]-[backcolor=transparent]cms

Linux 安装方式:
  1. [backcolor=transparent]sudo pip install aliyun[backcolor=transparent]-[backcolor=transparent]python[backcolor=transparent]-[backcolor=transparent]sdk[backcolor=transparent]-[backcolor=transparent]cms

如果需要升级SDK,则使用如下命令:
  1. [backcolor=transparent]sudo pip install [backcolor=transparent][backcolor=transparent]upgrade aliyun[backcolor=transparent]-[backcolor=transparent]python[backcolor=transparent]-[backcolor=transparent]sdk[backcolor=transparent]-[backcolor=transparent]rds

第三步:上报监控数据
使用PutEvent接口。
Windows上报示例:
  1. [backcolor=transparent]aliyuncli[backcolor=transparent].[backcolor=transparent]exe cms [backcolor=transparent]PutEvent[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]EventInfo[backcolor=transparent] [backcolor=transparent]"[{'content':'helloworld','time':'20171013T170923.456+0800','name':'ErrorEvent','groupId':'27147'}]"

Linux 上报示例:
  1. [backcolor=transparent]aliyuncli cms [backcolor=transparent]PutEvent[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]EventInfo[backcolor=transparent] [backcolor=transparent]"[{'content':'helloworld','time':'20171023T180923.456+0800','name':'ErrorEvent','groupId':'27147'}]"

上报成功后,返回200状态码
  1. [backcolor=transparent]{
  2. [backcolor=transparent]"Code"[backcolor=transparent]:[backcolor=transparent]"200"
  3. [backcolor=transparent]}

错误编码
错误代码含义
200正常
[tr=rgb(239, 251, 255)][td]400 客户端请求中的语法错误 403校验失败、限速、没有授权 500服务器内部错误
JSON 监控 Java 关系型数据库 Linux API 开发工具 数据格式 Python Windows
分享到
取消 提交回答
全部回答(0)

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题