背景
由于阿里云oss,cdn消耗錢的速度比較快,在不知道的情況下,服務就被停了,影響比較大。所以想做個監控。百度一下阿里云賬戶余額 api
還真有;于是開啟了踩坑之路。
查閱資料創建accessKeyId和accessKeySecret
- 官方文檔(感覺并不細致) https://help.aliyun.com/document_detail/87997.html?spm=a2c6h.13066369.0.0.59e4581eaxXH1O
- sdk https://developer.aliyun.com/sdk?spm=5176.12818093.resource-links.dsdk_platform.488716d022QXo0
-
看了官方文檔后還是有點懵逼,后面Google了這個關鍵字
QueryAccountBalanceRequest
才看到真正的樣例代碼https://developer.aliyun.com/ask/132002(感覺這塊資料很少呀,aliyun-python-sdk-bssopenapi
居然沒寫在sdk安裝列表里面,在社區找到的)。 - 創建accessKeyId,鼠標懸停到右上角
擼碼階段
要安裝的依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
sudo pip install aliyun - python - sdk - core - i https: / / mirrors.aliyun.com / pypi / simple / sudo pip install aliyun - python - sdk - bssopenapi - i https: / / mirrors.aliyun.com / pypi / simple / from aliyunsdkcore import client from aliyunsdkbssopenapi.request.v20171214 import QueryAccountBalanceRequest from aliyunsdkcore.profile import region_provider # 檢查賬戶余額 def check_account(name, accessKeyId, accessKeySecret, valve, notify_emails): region_provider.add_endpoint( 'BssOpenApi' , 'cn-hangzhou' , 'business.aliyuncs.com' ) clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-hangzhou' ) request = QueryAccountBalanceRequest.QueryAccountBalanceRequest() request.set_accept_format( "JSON" ) result = clt.do_action_with_exception(request) print (result) |
下面是我封裝的檢查賬戶余額,如果低于閥值就給要通知的人發郵件。 monitor_balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# -*-coding: UTF-8 -*- ''' 監控阿里云賬戶余額 zhouzhongqing 2019年12月14日20:21:11 sudo pip install aliyun-python-sdk-core -i https://mirrors.aliyun.com/pypi/simple/ sudo pip install aliyun-python-sdk-bssopenapi -i https://mirrors.aliyun.com/pypi/simple/ ''' import os import time import sched import smtplib from email.mime.text import MIMEText from email.header import Header from aliyunsdkcore import client from aliyunsdkbssopenapi.request.v20171214 import QueryAccountBalanceRequest from aliyunsdkcore.profile import region_provider import json from decimal import Decimal # qq郵箱smtp服務器 host_server = 'smtp.qq.com' # sender_qq為發件人的qq號碼 sender_qq = '1030907690@qq.com' # pwd為qq郵箱的授權碼 pwd = 'xxxxxx' # 發件人的郵箱 sender_qq_mail = '1030907690@qq.com' # 第一個參數確定任務的時間,返回從某個特定的時間到現在經歷的秒數 # 第二個參數以某種人為的方式衡量時間 schedule = sched.scheduler(time.time, time.sleep); def send_mail(receiver, name, balance, valve): # 收件人郵箱 # receiver = '1030907690@qq.com' # 郵件的正文內容 mail_content = '您好,目前賬戶%s,余額為%s,低于閥值%s,請知悉!' % (name, balance, valve) # 郵件標題 mail_title = '%s余額監控通知郵件' % (name) # ssl登錄 smtp = smtplib.SMTP_SSL(host_server) # set_debuglevel()是用來調試的。參數值為1表示開啟調試模式,參數值為0關閉調試模式 smtp.set_debuglevel( 0 ) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "plain" , 'utf-8' ) msg[ "Subject" ] = Header(mail_title, 'utf-8' ) msg[ "From" ] = sender_qq_mail msg[ "To" ] = receiver smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit() #解析配置 def parse_account(): f = open ( "monitor.json" ) lines = f.read() data = json.loads(lines) f.close() return data # 檢查賬戶余額 def check_account(name, accessKeyId, accessKeySecret, valve, notify_emails): region_provider.add_endpoint( 'BssOpenApi' , 'cn-hangzhou' , 'business.aliyuncs.com' ) clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-hangzhou' ) request = QueryAccountBalanceRequest.QueryAccountBalanceRequest() request.set_accept_format( "JSON" ) result = clt.do_action_with_exception(request) # print(result) res_json = json.loads( str (result, encoding = "utf-8" )) print (res_json) if res_json is not None and res_json[ "Code" ] = = "200" : availableAmount = res_json[ "Data" ][ "AvailableAmount" ] if Decimal(availableAmount) < Decimal(valve): print ( "%s低于閥值 " % name) notify_email_arr = notify_emails.split( "," ) for email in notify_email_arr: send_mail(email, name, availableAmount, valve) def start_check(): try : data = parse_account(); for item in data: print ( "檢查%s" % item[ "name" ]) check_account(item[ "name" ], item[ "accessKeyId" ], item[ 'accessKeySecret' ], item[ 'valve' ], item[ 'notifyEmail' ]) # send_mail("1030907690@qq.com","恭喜你888","50","100") except Exception as e: print ( "program error %s " % e) finally : print ( "finally print!" ) def perform_command(cmd, inc): # 安排inc秒后再次運行自己,即周期運行 schedule.enter(inc, 0 , perform_command, (cmd, inc)); os.system(cmd); start_check(); def timming_exe(cmd, inc = 60 ): # enter用來安排某事件的發生時間,從現在起第n秒開始啟動 schedule.enter(inc, 0 , perform_command, (cmd, inc)) # 持續運行,直到計劃時間隊列變成空為止 schedule.run() if __name__ = = '__main__' : print ( "start" ) print ( "show time after 60 seconds:" ); #timming_exe("echo %time%", 60); # 每間隔多少秒執行 timming_exe( "date" , 60 ); # 每間隔多少秒執行 print ( "end" ) ''' AvailableAmount String 可用額度 MybankCreditAmount String 網商銀行信用額度 AvailableCashAmount String 現金余額 Currency String 幣種。取值范圍:CNY:人民幣,USD:美元,JPY:日元 CreditAmount String 信控余額 ''' |
-
還有個json文件配置
monitor.json
-
里面分別代表的是名稱,發起郵件通知賬戶余額閥值,id,密鑰,通知的郵箱(可以多個,逗號
,
分割)。
1
|
[{ "name" : "恭喜你888" , "valve" : "100" , "accessKeyId" : "xxx" , "accessKeySecret" : "xxx" , "notifyEmail" :<a href = "mailto:1030907690@qq.com" > 1030907690 @qq.com< / a>}] |
運行效果
如果是正式環境部署的話可以用這個命令,可以后臺運行,日志輸出到 nohup.out:
1
|
nohup python - u monitor_balance.py > nohup.out 2 >& 1 & |
總結
以上所述是小編給大家介紹的python實現監控阿里云賬戶余額功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
原文鏈接:https://blog.csdn.net/baidu_19473529/article/details/103544403