最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)監(jiān)控阿里云賬戶余額功能

 更新時(shí)間:2019年12月16日 11:37:47   作者:憤怒的蘋果ext  
這篇文章主要介紹了python實(shí)現(xiàn)監(jiān)控阿里云賬戶余額功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

背景

由于阿里云oss,cdn消耗錢的速度比較快,在不知道的情況下,服務(wù)就被停了,影響比較大。所以想做個(gè)監(jiān)控。百度一下阿里云賬戶余額 api 還真有;于是開啟了踩坑之路。

查閱資料創(chuàng)建accessKeyId和accessKeySecret

  • 官方文檔(感覺并不細(xì)致) 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
  • 看了官方文檔后還是有點(diǎn)懵逼,后面Google了這個(gè)關(guān)鍵字QueryAccountBalanceRequest才看到真正的樣例代碼https://developer.aliyun.com/ask/132002(感覺這塊資料很少呀,aliyun-python-sdk-bssopenapi居然沒寫在sdk安裝列表里面,在社區(qū)找到的)。
  • 創(chuàng)建accessKeyId,鼠標(biāo)懸停到右上角


在這里插入圖片描述
在這里插入圖片描述

擼碼階段

要安裝的依賴

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)

下面是我封裝的檢查賬戶余額,如果低于閥值就給要通知的人發(fā)郵件。 monitor_balance.py

# -*-coding: UTF-8 -*-
'''
監(jiān)控阿里云賬戶余額
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/
https://developer.aliyun.com/ask/132002
'''
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服務(wù)器
host_server = 'smtp.qq.com'
# sender_qq為發(fā)件人的qq號(hào)碼
sender_qq = '1030907690@qq.com'
# pwd為qq郵箱的授權(quán)碼
pwd = 'xxxxxx'
# 發(fā)件人的郵箱
sender_qq_mail = '1030907690@qq.com'
# 第一個(gè)參數(shù)確定任務(wù)的時(shí)間,返回從某個(gè)特定的時(shí)間到現(xiàn)在經(jīng)歷的秒數(shù)
# 第二個(gè)參數(shù)以某種人為的方式衡量時(shí)間
schedule = sched.scheduler(time.time, time.sleep);
def send_mail(receiver, name, balance, valve):
  # 收件人郵箱
  # receiver = '1030907690@qq.com'
  # 郵件的正文內(nèi)容
  mail_content = '您好,目前賬戶%s,余額為%s,低于閥值%s,請(qǐng)知悉!' % (name, balance, valve)
  # 郵件標(biāo)題
  mail_title = '%s余額監(jiān)控通知郵件' % (name)
  # ssl登錄
  smtp = smtplib.SMTP_SSL(host_server)
  # set_debuglevel()是用來調(diào)試的。參數(shù)值為1表示開啟調(diào)試模式,參數(shù)值為0關(guān)閉調(diào)試模式
  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秒后再次運(yùn)行自己,即周期運(yùn)行
  schedule.enter(inc, 0, perform_command, (cmd, inc));
  os.system(cmd);
  start_check();
def timming_exe(cmd, inc=60):
  # enter用來安排某事件的發(fā)生時(shí)間,從現(xiàn)在起第n秒開始啟動(dòng)
  schedule.enter(inc, 0, perform_command, (cmd, inc))
  # 持續(xù)運(yùn)行,直到計(jì)劃時(shí)間隊(duì)列變成空為止
  schedule.run()
if __name__ == '__main__':
  print("start")
  print("show time after 60 seconds:");
  #timming_exe("echo %time%", 60); # 每間隔多少秒執(zhí)行
  timming_exe("date", 60); # 每間隔多少秒執(zhí)行
  print("end")
'''
AvailableAmount	String	可用額度
MybankCreditAmount	String	網(wǎng)商銀行信用額度
AvailableCashAmount	String	現(xiàn)金余額
Currency	String	幣種。取值范圍:CNY:人民幣,USD:美元,JPY:日元
CreditAmount	String	信控余額
'''
  • 還有個(gè)json文件配置monitor.json
  • 里面分別代表的是名稱,發(fā)起郵件通知賬戶余額閥值,id,密鑰,通知的郵箱(可以多個(gè),逗號(hào),分割)。
[{"name":"恭喜你888","valve": "100","accessKeyId":"xxx","accessKeySecret":"xxx","notifyEmail":1030907690@qq.com}]

運(yùn)行效果


在這里插入圖片描述
在這里插入圖片描述

如果是正式環(huán)境部署的話可以用這個(gè)命令,可以后臺(tái)運(yùn)行,日志輸出到 nohup.out:

nohup python -u monitor_balance.py > nohup.out 2>&1 &

 總結(jié)

以上所述是小編給大家介紹的python實(shí)現(xiàn)監(jiān)控阿里云賬戶余額功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

天台县| 梁河县| 百色市| 定兴县| 兴宁市| 平南县| 江达县| 怀来县| 河间市| 满城县| 湘西| 二连浩特市| 湖北省| 丹寨县| 弥勒县| 晋中市| 磐石市| 娄烦县| 融水| 县级市| 玉屏| 织金县| 义马市| 临邑县| 星座| 延长县| 株洲县| 锦州市| 德格县| 白城市| 渝北区| 阿拉善右旗| 曲麻莱县| 元朗区| 城口县| 家居| 枣庄市| 明星| 丹东市| 黄石市| 贡觉县|