欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

背景

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

查閱資料創(chuàng)建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安裝列表里面,在社區(qū)找到的)。
  • 創(chuàng)建accessKeyId,鼠標懸停到右上角


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

擼碼階段

要安裝的依賴

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服務器
host_server = 'smtp.qq.com'
# sender_qq為發(fā)件人的qq號碼
sender_qq = '1030907690@qq.com'
# pwd為qq郵箱的授權(quán)碼
pwd = 'xxxxxx'
# 發(fā)件人的郵箱
sender_qq_mail = '1030907690@qq.com'
# 第一個參數(shù)確定任務的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(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,請知悉!' % (name, balance, valve)
  # 郵件標題
  mail_title = '%s余額監(jiān)控通知郵件' % (name)
  # ssl登錄
  smtp = smtplib.SMTP_SSL(host_server)
  # set_debuglevel()是用來調(diào)試的。參數(shù)值為1表示開啟調(diào)試模式,參數(shù)值為0關閉調(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秒后再次運行自己,即周期運行
  schedule.enter(inc, 0, perform_command, (cmd, inc));
  os.system(cmd);
  start_check();
def timming_exe(cmd, inc=60):
  # enter用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動
  schedule.enter(inc, 0, perform_command, (cmd, inc))
  # 持續(xù)運行,直到計劃時間隊列變成空為止
  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	信控余額
'''
  • 還有個json文件配置monitor.json
  • 里面分別代表的是名稱,發(fā)起郵件通知賬戶余額閥值,id,密鑰,通知的郵箱(可以多個,逗號,分割)。
[{"name":"恭喜你888","valve": "100","accessKeyId":"xxx","accessKeySecret":"xxx","notifyEmail":1030907690@qq.com}]

運行效果


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

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

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

 總結(jié)

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

相關文章

  • Python連接Mssql基礎教程之Python庫pymssql

    Python連接Mssql基礎教程之Python庫pymssql

    這篇文章主要給大家介紹了關于Python連接Mssql基礎教程之Python庫pymssql的相關資料,文中分別介紹了連接數(shù)據(jù)庫、游標使用注意事項、游標返回行為字典變量、使用with語句(上下文管理器)以及調(diào)用存儲過程等的實現(xiàn),需要的朋友可以參考下
    2018-09-09
  • python語言是免費還是收費的?

    python語言是免費還是收費的?

    在本篇文章里小編給大家分享的是關于python語言是否免費的相關知識點,需要的朋友們可以學習下。
    2020-06-06
  • python實現(xiàn)顏色rgb和hex相互轉(zhuǎn)換的函數(shù)

    python實現(xiàn)顏色rgb和hex相互轉(zhuǎn)換的函數(shù)

    這篇文章主要介紹了python實現(xiàn)顏色rgb和hex相互轉(zhuǎn)換的函數(shù),可實現(xiàn)將rgb表示的顏色轉(zhuǎn)換成hex值的功能,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • 使用pandas讀取表格數(shù)據(jù)并進行單行數(shù)據(jù)拼接的詳細教程

    使用pandas讀取表格數(shù)據(jù)并進行單行數(shù)據(jù)拼接的詳細教程

    這篇文章主要介紹了使用pandas讀取表格數(shù)據(jù)并進行單行數(shù)據(jù)拼接的詳細教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • Python for循環(huán)中的陷阱詳解

    Python for循環(huán)中的陷阱詳解

    這篇文章主要給大家介紹了關于Python for循環(huán)中陷阱的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • Python學習之sys模塊使用教程詳解

    Python學習之sys模塊使用教程詳解

    sys模塊?與?os包一樣,也是對系統(tǒng)資源進行調(diào)用。功能同樣也是非常豐富。本文將對sys模塊的一些簡單且常用的函數(shù)進行介紹,感興趣的可以學習一下
    2022-03-03
  • Python面向?qū)ο笾^承和多態(tài)用法分析

    Python面向?qū)ο笾^承和多態(tài)用法分析

    這篇文章主要介紹了Python面向?qū)ο笾^承和多態(tài)用法,結(jié)合實例形式分析了Python面向?qū)ο蟪绦蛟O計中繼承與多態(tài)的原理及相關操作技巧,需要的朋友可以參考下
    2019-06-06
  • python 簡單的繪圖工具turtle使用詳解

    python 簡單的繪圖工具turtle使用詳解

    這篇文章主要介紹了python 簡單的繪圖工具turtle使用詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • 詳解python中eval函數(shù)的作用

    詳解python中eval函數(shù)的作用

    在本篇文章里小編給大家整理的是關于python中eval函數(shù)作用以及實例代碼,需要的朋友們參考下吧。
    2019-10-10
  • 對python中dict和json的區(qū)別詳解

    對python中dict和json的區(qū)別詳解

    今天小編就為大家分享一篇對python中dict和json的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論