Chinaunix首页 | 论坛 | 博客
  • 博客访问: 352697
  • 博文数量: 26
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 13:50
文章分类

全部博文(26)

文章存档

2015年(9)

2014年(6)

2013年(7)

2012年(2)

2011年(2)

分类: 系统运维

2015-10-15 13:43:25

公司部分业务放在阿里云上面,虽然阿里云管理平台本身有自己的监控后台,但是公司也有自己独立的zabbix监控系统,目前要做的就是把阿里云的监控转移到自己公司的监控系统,需要使用到阿里云的API,API的使用分以下2种方式

1.使用阿里云的Python SDK(python2.7),具体参考http://develop.aliyun.com/sdk/python?spm=5176.100054.0.0.tcU93U,示例代码:

点击(此处)折叠或打开

  1. #!/usr/local/bin/bin/python2.7
  2. #coding:utf-8

  3. from aliyunsdkcore import client
  4. from aliyunsdkrds.request.v20140815 import DescribeRegionsRequest

  5. clt = client.AcsClient('access_key','access_secret','cn-hangzhou')
  6. request=DescribeRegionsRequest.DescribeRegionsRequest()
  7. request.set_accept_format('json')
  8. #request.set_action_name("DescribeDBInstancePerformance")
  9. request.set_action_name("DescribeResourceUsage")
  10. #request.set_query_params(dict(DBInstanceId="rdsid",key="MySQL_NetworkTraffic",StartTime="2015-10-13T15:00Z",EndTime="2015-10-14T15:00Z"))
  11. request.set_query_params(dict(DBInstanceId="rdsid",StartTime="2015-10-13T15:00Z",EndTime="2015-10-14T15:00Z"))
  12. print(clt.do_action(request))
2.使用原生的API,示例代码:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding:utf-8

  3. try: import httplib
  4. except ImportError:
  5.     import http.client as httplib
  6. import sys
  7. import urllib
  8. import urllib2
  9. import time
  10. import json
  11. import itertools
  12. import mimetypes
  13. import base64
  14. import hmac
  15. import uuid
  16. from hashlib import sha1




  17. class AliyunMonitor:


  18.     def __init__(self,url):
  19.         self.access_id = 'access_id'
  20.         self.access_secret = 'access_secret'
  21.         self.url = url
  22.     ##签名
  23.     def sign(self,accessKeySecret, parameters):
  24.         sortedParameters = sorted(parameters.items(), key=lambda parameters: parameters[0])
  25.         canonicalizedQueryString = ''
  26.         for (k,v) in sortedParameters:
  27.             canonicalizedQueryString += '&' + self.percent_encode(k) + '=' + self.percent_encode(v)

  28.         stringToSign = 'GET&%2F&' + self.percent_encode(canonicalizedQueryString[1:]) #使用get请求方法

  29.         h = hmac.new(accessKeySecret + "&", stringToSign, sha1)
  30.         signature = base64.encodestring(h.digest()).strip()
  31.         return signature

  32.     def percent_encode(self,encodeStr):
  33.         encodeStr = str(encodeStr)
  34.         res = urllib.quote(encodeStr.decode(sys.stdin.encoding).encode('utf8'), '')
  35.         res = res.replace('+', '%20')
  36.         res = res.replace('*', '%2A')
  37.         res = res.replace('%7E', '~')
  38.         return res

  39.     def make_url(self,params):
  40.         timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  41.         parameters = {
  42.             'Format' : 'JSON',
  43.             'Version' : '2014-08-15',
  44.             'AccessKeyId' : self.access_id,
  45.             'SignatureVersion' : '1.0',
  46.             'SignatureMethod' : 'HMAC-SHA1',
  47.             'SignatureNonce' : str(uuid.uuid1()),
  48.             'TimeStamp' : timestamp,
  49.         }
  50.         for key in params.keys():
  51.             parameters[key] = params[key]

  52.         signature = self.sign(self.access_secret,parameters)
  53.         parameters['Signature'] = signature
  54.         url = self.url + "/?" + urllib.urlencode(parameters)
  55.         return url

  56.     def do_request(self,params):
  57.         url = self.make_url(params)
  58.         print(url)
  59.         request = urllib2.Request(url)
  60.         try:
  61.             conn = urllib2.urlopen(request)
  62.             response = conn.read()
  63.         except urllib2.HTTPError, e:
  64.             print(e.read().strip())
  65.             raise SystemExit(e)
  66.         try:
  67.             obj = json.loads(response)
  68.         except ValueError, e:
  69.             raise SystemExit(e)
  70.         print obj

  71. if __name__ == "__main__":
  72.     T = AliyunMonitor(" style="color:#0000CC;">)
  73.     T.do_request({"key":"MySQL_NetworkTraffic","Action":"DescribeDBInstancePerformance","DBInstanceId":"rds_id","StartTime":"2015-10-13T15:00Z","EndTime":"2015-10-14T15:00Z"})

注意事项:确保运行脚本的主机时区时间无误(yum install ntpdate  && ntpdate  time.windows.com) 



阅读(12937) | 评论(2) | 转发(0) |
0

上一篇:基于zabbix API的命令行工具

下一篇:没有了

给主人留下些什么吧!~~

stupid_lee2017-01-16 16:46:22

感谢楼主分享,对我很有帮助,阿里云虽然给了sdk,但是并没有对sdk的各个功能块做详细介绍,还得去看python源码,烦

howge2015-11-04 13:39:02

监控数据获取需要取格林时间,也就是北京时间要减去8个小时,不然获取不到实时数据