Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Serverless function to calculate and realize HTTP Health check + Fault SMS Notification

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

What this article shares with you is about how to use the Serverless function to calculate and realize HTTP health check + fault SMS notification. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Using Serverless function to calculate and realize the application scenario of HTTP health check + fault SMS notification

Request the website / API regularly, judge whether the service is available and whether the website has downtime according to the request response, and send SMS to notify the administrator when the downtime occurs.

Use of technology

Running platform: Ali cloud function computing

Development language: Python3 (small features, streamlined, fast development, online code editing on Aliyun)

Other: Aliyun SMS interface

Why choose function calculation?

There is no need to pay attention to the operation and maintenance, only need to write the core code, a python script is enough (the code can be edited online on Aliyun, and the local development environment does not need to be built)

For timing detection, you only need to choose the "timing trigger" calculated by the function.

Billed according to the number of calls and run time of the code (the relative price should be very low)

Structure diagram process

Function computing service is activated on Ali Cloud.

Create service: function calculation-create service: httpchk

Create function: language Python- blank function

Create function: trigger-timing trigger: httpchk-trigger- interval 1 minute

Create function: function name: httpchk-fc

Create function: code mode: online editing

Create function: function execution memory: 128MB (sufficient)

Function code:

#-*-coding: utf-8-*-import loggingimport requestsfrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.request import CommonRequestfrom requests.adapters import HTTPAdapter# should be retried 2 times for a total of 3 times request = requests.Session () request.mount ('http://', HTTPAdapter (max_retries=2)) request.mount (' https://', HTTPAdapter (max_retries=2)) # URL to be tested. Only GET request urls = ["https://www.baidu.com"," is supported "the mobile number of http://www.mtain.top"]# receiving SMS notification phone =" 180000000 "# related information about Aliyun SMS interface accessKeyId = 'xxxx'accessSecret =' xxxx'signName = 'xxxxx'templateCode =' SMS_xxxx'logger = logging.getLogger () def handler (event) Context): for url in urls: do_httpchk (url) def do_httpchk (url): logger.info ('Detection website: {}' .format (url)) try: req = request.get (url, timeout=5) logger.info ('website: {} response is normal, return data length: {}' .format (url,len (req.text)) except Exception as e: logger.error ('website: {} service exception, {}' .format (url) E) send_sms (url) def send_sms (url): client = AcsClient (accessKeyId, accessSecret) 'default') request = CommonRequest () request.set_accept_format (' json') request.set_domain ('dysmsapi.aliyuncs.com') request.set_method (' POST') request.set_protocol_type ('https') # https | http request.set_version (' 2017-05-25') request.set_action_name ('SendSms') request.add_query_param (' PhoneNumbers', phone) request.add_query_param ('SignName' SignName) request.add_query_param ('TemplateCode', templateCode) # Aliyun SMS variable [a-zA-Z0-9] and length less than 20 web_name = url.replace (' https://','').replace('http://','').replace('.','-')[0:18] request.add_query_param ('TemplateParam') '{"code": "' + web_name+'"}') response = client.do_action (request) logger.info ('Send SMS Response:'+str (response, encoding =' utf-8')) above is how to use the Serverless function to calculate and implement HTTP health check + fault SMS notification The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report