這篇文章將為大家詳細講解有關(guān)python如何獲取到預(yù)留實例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、雁峰網(wǎng)站維護、網(wǎng)站推廣。
boto3是aws提供給python開發(fā)者的SDK,我們通過boto3可以獲取到預(yù)留實例的相關(guān)信息。
boto3部署按照方法:
可以參照官方網(wǎng)站https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html。
值得注意的是,因為我們使用zabbix進行監(jiān)控,所以.aws目錄需要在zabbix用戶的根目錄下創(chuàng)建。
監(jiān)控腳本:
rinViewer.py:
import datetime import json import time import sys from pytz import timezone class rinViewer: def __init__(self,botoclient): self.client = botoclient; self.info = self.client.describe_reserved_instances()["ReservedInstances"]; def simList(self): #return a simple list of reservedinstance and days left. ans = [] for i in self.info: if i["State"] == "active": rins = {"instanceType":i["InstanceType"]} dayleft = self.dayLeft(i["End"]); rins["leftday"] = dayleft; ans.append(rins) return ans; def dayLeft(self,time): #input datetime,return the days left. cst_tz = timezone('Asia/Shanghai'); now = datetime.datetime.now(cst_tz); return (time-now).days; def leftDaysCheck(self,days=30): #if all reinstance left days over 30(default) it will return true,else return false and reason info = self.simList(); for i in info: if i["leftday"] < days: return {"status":False,"reason":i["instanceType"]+" will be expired in "+str(i["leftday"])+"."} return {"status":True}; def typeSum(self,type,count=-1): # return sum of ec2 with the type if count is not set.Or return bool which the sum of ec2 is correct. siminfo = self.simList(); n = 0; for i in siminfo: if i["instanceType"] == type: n = n + 1; if count == -1: return n; elif count == n: return {"status":True} else: return {"status":False,"reason":"Only "+str(n)+" "+type+" reserved instances.Expect "+str(count)} def typeCheck(self,typelist): # return bool that the ec2 reinstances is match the type list or not. for i in typelist: ans = self.typeSum(i["type"],i["sum"]); if ans["status"] == False: return ans; return {"status":True} if __name__ == "__main__": client = boto3.client('ec2'); rinsList=[{"type":"c5.large","sum":2}},{"type":"t2.medium","sum":1},{"type":"m4.large","sum":1}] reqStr = sys.argv[1]; riv = rinViewer(client); if reqStr == "leftdays": if len(sys.argv) > 2: res = riv.leftDaysCheck(int(sys.argv[2]))["status"]; else: res = riv.leftDaysCheck()["status"]; print res; elif reqStr == "typecount": res = riv.typeCheck(rinsList)["status"]; print res;
使用說明
支持兩個參數(shù):leftdays和typecount
python rinViewer.py leftdays [days]
會檢索你所有的預(yù)留實例的到期時間,如果有預(yù)留實例到期天數(shù)小于days(默認是30),返回false,如果沒有臨近到期返回true。
python rinViewer.py typecount
在函數(shù)中我們定義了rinsList=[{"type":"c5.large","sum":2}},{"type":"t2.medium","sum":1},這里記錄了所在region所需要的預(yù)留實例數(shù)量,當執(zhí)行時,會比較預(yù)留實例數(shù)量和rinsList中指定的數(shù)量是否存在出入,如果滿足了指定的數(shù)量則返回true,反之返回false.
當然如果有配置中心,或者其他入口能夠獲得所需預(yù)留實例信息,只要提供給rinViewer.typeCheck對應(yīng)的list即可。
添加到監(jiān)控系統(tǒng):
怎么添加到zabbix,網(wǎng)上文檔很多,這里就不介紹了,給個效果圖:
關(guān)于“python如何獲取到預(yù)留實例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。