python获取阿里云,腾讯云账户余额,并发送钉钉告警
python获取阿里云,腾讯云账户余额阿里云下载阿里云sdkpip install aliyun-python-sdk-corepip install aliyun-python-sdk-ecspip install yaml创建一个文件存放ak信息cat ak.yamlAccessKeyId: "aaaaaaaaaaaa"AccessKeySecret: "bbbbbbbbbbbbbb"#Get
·
python获取阿里云,腾讯云账户余额
阿里云
下载阿里云sdk
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-ecs
pip install aliyun-python-sdk-bssopenapi
pip install pyyaml
创建一个文件存放ak信息
cat ak.yaml
AccessKeyId: "aaaaaaaaaaaa"
AccessKeySecret: "bbbbbbbbbbbbbb"
#GetAliAaccout.py
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkbssopenapi.request.v20171214.QueryAccountBalanceRequest import QueryAccountBalanceRequest
import json
import urllib2
import yaml
def GetAk(f_obj):
with open(f_obj) as f:
ak = yaml.load(f,Loader=yaml.SafeLoader)
return ak
def AvailableAmount(Akid,AkSecret):
client = AcsClient(Akid, AkSecret, 'cn-hangzhou')
request = QueryAccountBalanceRequest()
request.set_accept_format('json')
response = client.do_action_with_exception(request)
return (json.loads(response))
def SendDingDingMsg(msg):
#钉钉机器人url
url = "https://oapi.dingtalk.com/robot/send?access_token=dfasfwewrqweergfs56362345dfgst232342"
header = {
"Content-Type": "application/json",
"charset": "utf-8"
}
data = {
"msgtype": "text",
"text": {
"content": msg
}
}
sendData = json.dumps(data)
request = urllib2.Request(url,data = sendData,headers = header)
urlopen = urllib2.urlopen(request)
return urlopen.read()
if __name__ == "__main__":
AK = GetAk('ak.yaml')
Mon = AvailableAmount(AK['AccessKeyId'],AK['AccessKeySecret'])['Data']['AvailableAmount'].replace(',','')
SendDingDingMsg('阿里云账户余额: '+str(Mon))
python GetAliAaccout.py
腾讯云
pip install tencentcloud-sdk-python
#腾讯ak
cat tencentAk.yaml
"SecretId": "adfasdfsadfasdf"
"SecretKey": "234efwe5dfetrgadf"
#GetTencentAccout.py
#!/usr/bin/env python
#coding=utf-8
#author:suzhuchang
import json
import yaml
import urllib2
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.billing.v20180709 import billing_client, models
def GetAk(f_obj):
with open(f_obj) as f:
ak = yaml.load(f)
return ak
def GetAccout(ak,aks):
try:
cred = credential.Credential(ak, aks)
httpProfile = HttpProfile()
httpProfile.endpoint = "billing.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = billing_client.BillingClient(cred, "", clientProfile)
req = models.DescribeAccountBalanceRequest()
params = {
}
req.from_json_string(json.dumps(params))
resp = client.DescribeAccountBalance(req)
return resp.__dict__
except TencentCloudSDKException as err:
print(err)
def SendDingDingMsg(msg):
url = "https://oapi.dingtalk.com/robot/send?access_token=9278a173cb856c59570e2b2076db4a066f90796f4"
header = {
"Content-Type": "application/json",
"charset": "utf-8"
}
data = {
"msgtype": "text",
"text": {
"content": msg
}
}
sendData = json.dumps(data)
request = urllib2.Request(url,data = sendData,headers = header)
urlopen = urllib2.urlopen(request)
return urlopen.read()
if __name__ == "__main__":
AK = GetAk('tencentAk.yaml')
Mon =('%.2f' %(float(GetAccout(AK['SecretId'],AK['SecretKey'])['Balance'])/float(100)))
SendDingDingMsg('腾讯云账户余额: '+str(Mon))
python GetTencentAccout.py
更多推荐
已为社区贡献1条内容
所有评论(0)