公司部分业务放在阿里云上面,虽然阿里云管理平台本身有自己的监控后台,但是公司也有自己独立的zabbix监控系统,目前要做的就是把阿里云的监控转移到自己公司的监控系统,需要使用到阿里云的API,API的使用分以下2种方式
1.使用阿里云的Python SDK(python2.7),具体参考http://develop.aliyun.com/sdk/python?spm=5176.100054.0.0.tcU93U,示例代码:
-
#!/usr/local/bin/bin/python2.7
-
#coding:utf-8
-
-
from aliyunsdkcore import client
-
from aliyunsdkrds.request.v20140815 import DescribeRegionsRequest
-
-
clt = client.AcsClient('access_key','access_secret','cn-hangzhou')
-
request=DescribeRegionsRequest.DescribeRegionsRequest()
-
request.set_accept_format('json')
-
#request.set_action_name("DescribeDBInstancePerformance")
-
request.set_action_name("DescribeResourceUsage")
-
#request.set_query_params(dict(DBInstanceId="rdsid",key="MySQL_NetworkTraffic",StartTime="2015-10-13T15:00Z",EndTime="2015-10-14T15:00Z"))
-
request.set_query_params(dict(DBInstanceId="rdsid",StartTime="2015-10-13T15:00Z",EndTime="2015-10-14T15:00Z"))
-
print(clt.do_action(request))
2.使用原生的API,示例代码:
-
#!/usr/bin/python
-
#coding:utf-8
-
-
try: import httplib
-
except ImportError:
-
import http.client as httplib
-
import sys
-
import urllib
-
import urllib2
-
import time
-
import json
-
import itertools
-
import mimetypes
-
import base64
-
import hmac
-
import uuid
-
from hashlib import sha1
-
-
-
-
-
class AliyunMonitor:
-
-
-
def __init__(self,url):
-
self.access_id = 'access_id'
-
self.access_secret = 'access_secret'
-
self.url = url
-
##签名
-
def sign(self,accessKeySecret, parameters):
-
sortedParameters = sorted(parameters.items(), key=lambda parameters: parameters[0])
-
canonicalizedQueryString = ''
-
for (k,v) in sortedParameters:
-
canonicalizedQueryString += '&' + self.percent_encode(k) + '=' + self.percent_encode(v)
-
-
stringToSign = 'GET&%2F&' + self.percent_encode(canonicalizedQueryString[1:]) #使用get请求方法
-
-
h = hmac.new(accessKeySecret + "&", stringToSign, sha1)
-
signature = base64.encodestring(h.digest()).strip()
-
return signature
-
-
def percent_encode(self,encodeStr):
-
encodeStr = str(encodeStr)
-
res = urllib.quote(encodeStr.decode(sys.stdin.encoding).encode('utf8'), '')
-
res = res.replace('+', '%20')
-
res = res.replace('*', '%2A')
-
res = res.replace('%7E', '~')
-
return res
-
-
def make_url(self,params):
-
timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
-
parameters = {
-
'Format' : 'JSON',
-
'Version' : '2014-08-15',
-
'AccessKeyId' : self.access_id,
-
'SignatureVersion' : '1.0',
-
'SignatureMethod' : 'HMAC-SHA1',
-
'SignatureNonce' : str(uuid.uuid1()),
-
'TimeStamp' : timestamp,
-
}
-
for key in params.keys():
-
parameters[key] = params[key]
-
-
signature = self.sign(self.access_secret,parameters)
-
parameters['Signature'] = signature
-
url = self.url + "/?" + urllib.urlencode(parameters)
-
return url
-
-
def do_request(self,params):
-
url = self.make_url(params)
-
print(url)
-
request = urllib2.Request(url)
-
try:
-
conn = urllib2.urlopen(request)
-
response = conn.read()
-
except urllib2.HTTPError, e:
-
print(e.read().strip())
-
raise SystemExit(e)
-
try:
-
obj = json.loads(response)
-
except ValueError, e:
-
raise SystemExit(e)
-
print obj
-
-
if __name__ == "__main__":
-
T = AliyunMonitor(" style="color:#0000CC;">)
-
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)
阅读(12918) | 评论(2) | 转发(0) |