阿里云
阿里云多端小程序中小企业获客首选
发表主题 回复主题
  • 1304阅读
  • 0回复

[云安全技术和产品专区 ]云监控监控数据上报

级别: 论坛粉丝
发帖
1227
云币
2325
您可以使用API或SDK上报监控数据 nn:pf1  
注意事项 B [ ka@z7  
  • SDK支持python(2.6 +)与bash
&X,)+ b=  
SDK上报数据 )\Q|}JV  
参数说明 ss8v4@C  
  • userid,是用户的账号ID
  • 监控项名称:创建监控项时填写的“监控项名称”。
  • 监控项值:监控项对应的监控数据。
  • 字段信息:创建监控项时填写的“字段信息”和它的值。这个参数用于对数据区分维度。
86F+N_>Z  
使用步骤 2(/ /slP  
  1. 下载SDK -yGm^EwP  
    a.自定义监控SDK(python版) :cms_post.py z.HNb$;  
    b. 定义监控SDK(bash版) :cms_post.sh
  2. 编写脚本。 '|N4fbZd  
    a. 在用户的脚本中,import cms_post,调用cms_post的post方法推送数据到云监控。 M~F2cX W  
    b. post方法中传入4个参数,分别为userid, 监控项名称,监控项值,字段信息,您只需添加定时任务即可。
  3. 定时调度脚本,按创建监控项时定义的上报周期上报数据。Linux环境可以使用Crontab,Windows环境可以使用quartz.net。
  4. 补充说明:请在您的脚本开始处添加对应的解释器,一般默认#!/usr/bin/python,在test.py中,import cms_post,然后调用cms_post.post()上报您的监控数据。请将您的test.py文件和cms_post.py文件放在同一文件夹下,避免import时发生错误
/ i2-h  
脚本示例(Python+linux环境) 7h/Q;P5  
  1. [backcolor=transparent]#!/usr/bin/python
  2. [backcolor=transparent]import[backcolor=transparent] cms_post
  3. [backcolor=transparent]def[backcolor=transparent] get_mem_usage_percent[backcolor=transparent]():
  4. [backcolor=transparent]    [backcolor=transparent]try[backcolor=transparent]:
  5. [backcolor=transparent]        f [backcolor=transparent]=[backcolor=transparent] open[backcolor=transparent]([backcolor=transparent]'/proc/meminfo'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'r'[backcolor=transparent])
  6. [backcolor=transparent]        [backcolor=transparent]for[backcolor=transparent] line [backcolor=transparent]in[backcolor=transparent] f[backcolor=transparent]:
  7. [backcolor=transparent]            [backcolor=transparent]if[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'MemTotal:'[backcolor=transparent]):
  8. [backcolor=transparent]                mem_total [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  9. [backcolor=transparent]            [backcolor=transparent]elif[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'MemFree:'[backcolor=transparent]):
  10. [backcolor=transparent]                mem_free [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  11. [backcolor=transparent]            [backcolor=transparent]elif[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'Buffers:'[backcolor=transparent]):
  12. [backcolor=transparent]                mem_buffer [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  13. [backcolor=transparent]            [backcolor=transparent]elif[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'Cached:'[backcolor=transparent]):
  14. [backcolor=transparent]                mem_cache [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  15. [backcolor=transparent]            [backcolor=transparent]elif[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'SwapTotal:'[backcolor=transparent]):
  16. [backcolor=transparent]                vmem_total [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  17. [backcolor=transparent]            [backcolor=transparent]elif[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]startswith[backcolor=transparent]([backcolor=transparent]'SwapFree:'[backcolor=transparent]):
  18. [backcolor=transparent]                vmem_free [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent]([backcolor=transparent]line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]()[[backcolor=transparent]1[backcolor=transparent]])
  19. [backcolor=transparent]            [backcolor=transparent]else[backcolor=transparent]:
  20. [backcolor=transparent]                [backcolor=transparent]continue
  21. [backcolor=transparent]        f[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]()
  22. [backcolor=transparent]    [backcolor=transparent]except[backcolor=transparent]:
  23. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]None
  24. [backcolor=transparent]    physical_percent [backcolor=transparent]=[backcolor=transparent] usage_percent[backcolor=transparent]([backcolor=transparent]mem_total [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]mem_free [backcolor=transparent]+[backcolor=transparent] mem_buffer [backcolor=transparent]+[backcolor=transparent] mem_cache[backcolor=transparent]),[backcolor=transparent] mem_total[backcolor=transparent])
  25. [backcolor=transparent]    virtual_percent [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0
  26. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] vmem_total [backcolor=transparent]>[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent]:
  27. [backcolor=transparent]        virtual_percent [backcolor=transparent]=[backcolor=transparent] usage_percent[backcolor=transparent](([backcolor=transparent]vmem_total [backcolor=transparent]-[backcolor=transparent] vmem_free[backcolor=transparent]),[backcolor=transparent] vmem_total[backcolor=transparent])
  28. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] physical_percent
  29. [backcolor=transparent]def[backcolor=transparent] usage_percent[backcolor=transparent]([backcolor=transparent]use[backcolor=transparent],[backcolor=transparent] total[backcolor=transparent]):
  30. [backcolor=transparent]    [backcolor=transparent]try[backcolor=transparent]:
  31. [backcolor=transparent]        ret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]float[backcolor=transparent]([backcolor=transparent]use[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]/[backcolor=transparent] total[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]100
  32. [backcolor=transparent]    [backcolor=transparent]except[backcolor=transparent] [backcolor=transparent]ZeroDivisionError[backcolor=transparent]:
  33. [backcolor=transparent]        [backcolor=transparent]raise[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent]([backcolor=transparent]"ERROR - zero division error"[backcolor=transparent])
  34. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] ret
  35. [backcolor=transparent]MEMS_usage[backcolor=transparent]=[backcolor=transparent]get_mem_usage_percent[backcolor=transparent]()
  36. [backcolor=transparent]if[backcolor=transparent] __name__ [backcolor=transparent]==[backcolor=transparent] [backcolor=transparent]'__main__'[backcolor=transparent]:
  37. [backcolor=transparent] cms_post[backcolor=transparent].[backcolor=transparent]post[backcolor=transparent]([backcolor=transparent]"1058019241820815"[backcolor=transparent],[backcolor=transparent]"MEMS_usage"[backcolor=transparent],[backcolor=transparent]MEMS_usage[backcolor=transparent],[backcolor=transparent]"Percent"[backcolor=transparent],[backcolor=transparent]"ecsinstanceId=i-28zdkoobp"[backcolor=transparent])
QfWu~[  
API上报数据 v^_]W3K  
用户可以使用云监控接口 http://open.cms.aliyun.com/metrics/put 上报监控数据。 To%*)a  
参数说明 @SG"t,5s  
  • metricName:创建监控项时填写的“监控项名称”。
  • unit:创建监控项时填写的“单位”。
  • dimensions:创建监控项时填写的“字段信息”,允许多个,使用半角逗号分隔。
  • namespace:格式为“ACS/CUSTOM/userId”,将userid替换为您的账号ID。
  • userId:即您的账号ID
  • timestamp:数据上传的时间,支持两种表示方式:一种方式取时间的长整型表示方式1395556197448,另一种方式按照 ISO8601 标准表示,并使用 UTC 时间,例如 2014-9-11T10:00:00Z,注意它对应的北京时间是 2014-09-11 18:00:00。
R bc2g"]  
Post方式 .x?zky^  
@/NZ>.  
]J=)pD rk  
用户可以一次提交多条统计数据,统计数据放在消息体中,遵循 JSON 格式。如消息体内容:
  1. [backcolor=transparent]userId[backcolor=transparent]=[backcolor=transparent]123456[backcolor=transparent]&[backcolor=transparent]namespace[backcolor=transparent]=[backcolor=transparent]acs[backcolor=transparent]/[backcolor=transparent]custom[backcolor=transparent]/[backcolor=transparent]123456[backcolor=transparent]&[backcolor=transparent]metrics [backcolor=transparent]=
  2. [backcolor=transparent][{[backcolor=transparent]"metricName"[backcolor=transparent]:[backcolor=transparent]"vm.cpu"[backcolor=transparent],[backcolor=transparent]"timestamp"[backcolor=transparent]:[backcolor=transparent]"1395556197448"[backcolor=transparent],[backcolor=transparent]"value"[backcolor=transparent]:[backcolor=transparent]80.0[backcolor=transparent],[backcolor=transparent]"unit"[backcolor=transparent]:[backcolor=transparent]"Percen
  3. [backcolor=transparent]t"[backcolor=transparent],[backcolor=transparent]"dimensions"[backcolor=transparent]:{[backcolor=transparent]"instanceId"[backcolor=transparent]:[backcolor=transparent]"vm_001"[backcolor=transparent]}}]
R&_\&:4f  
GET方式 gJ8 c]2c  
用户可以一次提交多条统计数据,统计数据存放在 metrics 字段中,遵循 JSON 格式。
  1. [backcolor=transparent]http[backcolor=transparent]:[backcolor=transparent]//open.cms.aliyun.com
  2. [backcolor=transparent]/[backcolor=transparent]metrics[backcolor=transparent]/[backcolor=transparent]put[backcolor=transparent]?[backcolor=transparent]userId[backcolor=transparent]=[backcolor=transparent]123456[backcolor=transparent]&[backcolor=transparent]namespace[backcolor=transparent]=[backcolor=transparent]acs[backcolor=transparent]/[backcolor=transparent]custom[backcolor=transparent]/[backcolor=transparent]123456[backcolor=transparent]&[backcolor=transparent]metrics[backcolor=transparent]=[{[backcolor=transparent]"metricName"[backcolor=transparent]:
  3. [backcolor=transparent]"vm.cpu"[backcolor=transparent],[backcolor=transparent]"timestamp"[backcolor=transparent]:[backcolor=transparent]"1395556197448"[backcolor=transparent],[backcolor=transparent]"value"[backcolor=transparent]:[backcolor=transparent]80.0[backcolor=transparent],[backcolor=transparent]"unit"[backcolor=transparent]:[backcolor=transparent]"Percent"[backcolor=transparent],[backcolor=transparent]"dimensions"
  4. [backcolor=transparent]:{[backcolor=transparent]"instanceId"[backcolor=transparent]:[backcolor=transparent]"vm_001"[backcolor=transparent]}},{[backcolor=transparent]"metricName"[backcolor=transparent]:[backcolor=transparent]"vm.mem"[backcolor=transparent],[backcolor=transparent]"timestamp"[backcolor=transparent]:[backcolor=transparent]"1395556197448"[backcolor=transparent],[backcolor=transparent]"v
  5. [backcolor=transparent]alue"[backcolor=transparent]:[backcolor=transparent]1280.0[backcolor=transparent],[backcolor=transparent]"unit"[backcolor=transparent]:[backcolor=transparent]"Megabytes"[backcolor=transparent],[backcolor=transparent]"dimensions"[backcolor=transparent]:{[backcolor=transparent]"instanceId"[backcolor=transparent]:[backcolor=transparent]"vm_002"[backcolor=transparent]}}]
)u/ ^aK53^  
响应消息 )r,R!8  
云监控收到HTTP请求后,若处理成功则直接返回状态码为200的HTTP响应,消息体中不携带内容,用户也不需要处理响应消息。若处理失败,返回 JSON 格式的消息体,同时HTTP响应的状态码不为200。
错误错误描述HTTP Status Code
InternalServerError由于内部错误或不确定的异常导致的请求失败500
InvalidParameterCombination参数组合错误400
InvalidParameterValue参数无效或超出范围400
MissingRequiredParameter缺少必须的参数400
J K]tcP  
例如:
  1. [backcolor=transparent]{[backcolor=transparent]"code"[backcolor=transparent]:[backcolor=transparent]"InvalidParameterValue"[backcolor=transparent],[backcolor=transparent]"msg"[backcolor=transparent]:[backcolor=transparent]"the metricName is empty."[backcolor=transparent]}
TIWLp  
[ 此帖被反向一觉在2017-10-31 11:52重新编辑 ]
发表主题 回复主题
« 返回列表上一主题下一主题

限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
验证问题: ECS是阿里云提供的什么服务? 正确答案:云服务器
上一个 下一个
      ×
      全新阿里云开发者社区, 去探索开发者的新世界吧!
      一站式的体验,更多的精彩!
      通过下面领域大门,一起探索新的技术世界吧~ (点击图标进入)