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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| import sys import os import logging import json import requests import smtplib import time import datetime import math
from qcloud_cos import CosConfig from qcloud_cos import CosS3Client
from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common import credential from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.monitor.v20180724 import monitor_client, models from datetime import timedelta
logger = logging.getLogger() logger.setLevel(logging.DEBUG)
data={ "secret_id":"", "secret_key":"", "region":"ap-guangzhou" }
def checkQuota(): secret_id = data['secret_id'] secret_key = data['secret_key'] region = data['region'] token = None scheme = 'https' bucket = 'cos-1317182407-sadf-1317182407'
localOffset = datetime.timedelta(hours = 8) S_delay = datetime.timedelta(minutes=15) E_delay = datetime.timedelta(minutes=10)
time_start = datetime.datetime.now() + localOffset - S_delay time_end = datetime.datetime.now() + localOffset - E_delay cred = credential.Credential(secret_id, secret_key) httpProfile = HttpProfile() httpProfile.endpoint = "monitor.tencentcloudapi.com"
clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = monitor_client.MonitorClient(cred, region, clientProfile)
req = models.GetMonitorDataRequest() params = { "Namespace": "QCE/COS", "MetricName": "InternetTraffic", "Period": 60, "StartTime": time_start.strftime("%Y-%m-%d %H:%M:%S"), "EndTime": time_end.strftime("%Y-%m-%d %H:%M:%S"), "Instances": [ { "Dimensions": [ { "Name": "bucket", "Value": bucket } ] } ] } req.from_json_string(json.dumps(params))
resp = client.GetMonitorData(req) _str = resp.to_json_string() print(_str) print("------------------------------------------------------") jsObj = json.loads(_str) for k,v in jsObj.items(): if k == "DataPoints": for k,v in v[0].items(): if k == "Values": _flow = sum(v) if _flow > 5000*1024*1024: config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) cosclient = CosS3Client(config) cosclient.put_bucket_acl( Bucket=bucket, ACL='private' ) print("判断阈值",_flow) print("达到阈值,已改为私有读写权限") else: ''' 如果需要在盗刷后自动恢复公共读权限,则去掉这段注释。 config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) cosclient = CosS3Client(config) cosclient.put_bucket_acl( Bucket=bucket, ACL='public-read' ) ''' print("判断阈值",_flow) print("未达到阈值,不做权限修改操作")
def main_handler(event, context): checkQuota()
if __name__ == '__main__': main_handler("", "")
|