import os import datetime import time import boto3 import json import hmac import base64 import base64 import hmac import json import time from hashlib import sha256 import json import requests import boto3 def get_tidbcluster_id(instance): if "Tags" in instance: for tag in instance['Tags']: if tag["Key"] == 'tidbcluster': return tag['Value'] if tag['Key'] == 'gardener.wg.name': seps = str.split(tag['Value'], '-') return seps[0] return '' def get_sign_timestamp(): timestamp = str(int(time.time())) secret = os.getenv("feishu_secret") key = f'{timestamp}\n{secret}' key_enc = key.encode('utf-8') msg = "" msg_enc = msg.encode('utf-8') hmac_code = hmac.new(key_enc, msg_enc, digestmod=sha256).digest() sign = base64.b64encode(hmac_code).decode('utf-8') return {'timestamp': timestamp, 'sign': sign} def post_bot_webhook(): ec2client = boto3.client('ec2') response = ec2client.describe_regions() instanceType = ['c5.xlarge', 'r5.2xlarge', 'c5.2xlarge', 'c5.9xlarge', 'i3.2xlarge'] regions = [] for rs in response['Regions']: regions.append(rs['RegionName']) print(regions) allTidbCluster = {} result = [] for region in regions: ec2client = boto3.client('ec2', region_name=region) response = ec2client.describe_instances() for reservation in response['Reservations']: for instance in reservation['Instances']: if instance['InstanceType'] in instanceType: if instance['State']['Name'] != 'running': continue clusterId = get_tidbcluster_id(instance) if clusterId == '': continue lt_datetime = instance['LaunchTime'] lt_delta = datetime.datetime.now(lt_datetime.tzinfo) - lt_datetime if lt_delta.seconds > 60 * 60 * 8: if clusterId not in allTidbCluster: allTidbCluster[clusterId] = [] allTidbCluster[clusterId].append( { 'InstanceId': instance['InstanceId'], "LaunchTime": instance['LaunchTime'], "InstanceType": instance['InstanceType'] } ) detailMsg = [] clusterMsg = [] for cluster in allTidbCluster: clusterMsg.append({"is_short": False, "text": { "tag": "lark_md", "content": "tidbCluster {}".format(cluster), } }) for cluster in allTidbCluster: for instance in allTidbCluster[cluster]: msg = "Cluster: {} , Region: {} , InstanceId: {} , LaunchTime: {} , InstanceType: {}" \ .format(cluster, region, instance['InstanceId'], instance['LaunchTime'], instance['InstanceType']) detailMsg.append( { "is_short": False, "text": { "tag": "lark_md", "content": msg, } } ) url = os.getenv("feishu_webhook") sign_timestamp = get_sign_timestamp() payload_message = { "timestamp": sign_timestamp['timestamp'], "sign": sign_timestamp['sign'], "msg_type": "interactive", "card": { "i18n_elements": { "en_us": [ { "tag": "div", "text": { "tag": "lark_md", "content": "**使用超过8小时的测试环境TiDB集群**" }, "fields": clusterMsg }, { "tag": "div", "text": { "tag": "lark_md", "content": "**使用超过8小时的测试环境TiDB集群VM**" }, "fields": detailMsg }, ] } } } headers = { 'Content-Type': 'application/json' } response = requests.request( "POST", url, headers=headers, data=json.dumps(payload_message)) print(response.text) def handler(event, context): post_bot_webhook() print("post to webhook success!") return { 'statusCode': 200, 'body': json.dumps(200) }