In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to obtain reserved examples for python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Boto3 is a SDK provided by aws to python developers. We can obtain information about reserved instances through boto3.
Boto3 deployment is as follows:
Please refer to the official website https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html.
It is worth noting that because we use zabbix for monitoring, the .aws directory needs to be created under the root directory of the zabbix user.
Monitoring script:
RinViewer.py:
Import datetimeimport jsonimport timeimport sysfrom pytz import timezoneclass 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) >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
instructions
Two parameters are supported: leftdays and typecount
Python rinViewer.py leftdays [days]
The expiration time of all your reserved instances will be retrieved. If the expiration date of any reserved instance is less than days (default is 30), return false, and return true if it is not about to expire.
Python rinViewer.py typecount
In the function, we define rinsList= [{"type": "c5.large", "sum": 2}}, {"type": "t2.medium", "sum": 1}, where the number of reserved instances required by the region is recorded. When executed, the number of reserved instances is compared with the number specified in rinsList. If the specified number is met, true is returned, otherwise, false is returned.
Of course, if a configuration center or other entry can obtain the required reserved instance information, just provide it to the list corresponding to the rinViewer.typeCheck.
Add to the monitoring system:
How to add to zabbix, there are a lot of online documents, so I won't introduce them here. Give me an effect picture:
This is the end of the article on "how to get reserved examples for python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.