环境准备
安装python的aws 开发工具包
pip install boto3
配置aws账号
[[email protected] service]# aws configureAWS Access Key ID [None]: xxxxxxAWS Secret Access Key [None]: xxxxxDefault region name [None]:Default output format [None]:
监控脚本:
#!/usr/bin/env python# -*- Coding:utf-8 -*-import boto3,osimport datetimefrom ReservedInstancesMonitor_message import *def reserved_instances_info(): data=response[‘ReservedInstances‘] #send_message="" reserved_instances_number=0 for info in data: #购买时长 purchase_time=info[‘Duration‘] #购买起始时间 purchase_start_time=info[‘Start‘] #print(purchase_time,purchase_start_time) #获取当前时间 current_time=datetime.datetime.Now() #print(current_time) #获取过期时间 retired_time=datetime.datetime.strptime( str(purchase_start_time).split(‘+‘)[0],"%Y-%m-%d %H:%M:%s.%f") + datetime.timedelta(seconds=purchase_time) #剩余过期时间 retired_left_time=(retired_time -current_time).days if retired_left_time <= 7: #send_message +=info[‘InstanceType‘] +" "+str(info[‘InstanceCount‘])+"\n" reserved_instances_number+=info[‘InstanceCount‘] #print("预留实例类型: " + info[‘InstanceType‘]," 过期数量: ",info[‘InstanceCount‘]," 多少天后过期: ",retired_left_time) return reserved_instances_numberif __name__==‘__main__‘: region_List=[‘ap-northeast-2‘,‘eu-central-1‘] for region in region_List: print("-"*100) clIEnt=boto3.clIEnt(‘ec2‘,region_name=region) response = clIEnt.describe_reserved_instances(Filters=[{‘name‘:‘state‘,‘Values‘:[‘active‘,]},]) instances_number=reserved_instances_info() if instances_number != 0: message="【xx告警】区域: " +region+ " " + str(instances_number) +" "+ "台预留实例将于7天后到期,请及时购置!" print (message) send_sms(APIkey,message,mobile)
发短信脚本:
#!/usr/bin/python#-*- Coding:utf-8 -*-# APIkey:成功注册后登录云片官网,进入后台可查看# text:需要使用已审核通过的模板或者默认模板# mobile:接收的手机号,仅支持单号码发送import osimport urllib,urllib2,httplib,timefrom subprocess import Popen,PIPEAPIkey=‘xxxx‘mobile=‘xxxx‘def send_sms(APIkey,text,mobile): #服务地址 sms_host = "sms.yunpian.com" #端口号 port = 443 #版本号 version = "v2" #智能匹配模板短信接口的URI sms_send_uri = "/" + version + "/sms/single_send.Json" params = urllib.urlencode({‘APIkey‘: APIkey,‘text‘: text,‘mobile‘:mobile}) headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = httplib.httpSConnection(sms_host,port=port,timeout=30) conn.request("POST",sms_send_uri,params,headers) response = conn.getresponse() response_str = response.read() conn.close() return response_strif __name__==‘__main__‘: pass #message="【小慕告警】区域: %s %s 台预留实例将于7天后到期,请及时购置!" %(region,instances_number) #print send_message #send_sms(APIkey,mobile)总结
以上是内存溢出为你收集整理的aws 预留实例到期监控全部内容,希望文章能够帮你解决aws 预留实例到期监控所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)