阿里云 函數(shù)計(jì)算是一個(gè)事件驅(qū)動的全托管計(jì)算服務(wù)。通過函數(shù)計(jì)算,您無需管理服務(wù)器等基礎(chǔ)設(shè)施,只需編寫代碼并上傳。函數(shù)計(jì)算會為您準(zhǔn)備好計(jì)算資源,以彈性、可靠的方式運(yùn)行您的代碼,并提供日志查詢,性能監(jiān)控,報(bào)警等功能。借助于函數(shù)計(jì)算,您可以快速構(gòu)建任何類型的應(yīng)用和服務(wù),無需管理和運(yùn)維。更棒的是,您只需要為代碼實(shí)際運(yùn)行消耗的資源付費(fèi),而代碼未運(yùn)行則不產(chǎn)生費(fèi)用。
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),井陘網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:井陘等地區(qū)。井陘做網(wǎng)站價(jià)格咨詢:13518219792阿里云
云監(jiān)控為云上用戶提供開箱即用的企業(yè)級開放型一站式監(jiān)控解決方案。涵蓋IT設(shè)施基礎(chǔ)監(jiān)控,外網(wǎng)網(wǎng)絡(luò)質(zhì)量撥測監(jiān)控,基于事件、自定義指標(biāo)、日志的業(yè)務(wù)監(jiān)控。為您全方位提供更高效、更全面、更省錢的監(jiān)控服務(wù)。
云監(jiān)控提供了豐富事件,并且事件還在不斷豐富中(
云產(chǎn)品系統(tǒng)事件監(jiān)控), 豐富的事件觸發(fā)自定義處理的函數(shù),可以實(shí)現(xiàn)更加完美的的自動化運(yùn)維。
在本文中,重點(diǎn)關(guān)注函數(shù)計(jì)算對ecs的重啟事件處理,因?yàn)檫@些ecs重啟事件是目前用戶需要很高優(yōu)先級用戶優(yōu)先級去響應(yīng)的;假設(shè)之前一臺ecs 發(fā)生因系統(tǒng)錯誤而重啟,用戶可能會緊急起來做一些驗(yàn)證或者創(chuàng)建快照的處理, 在本示例中,我們對一臺因?yàn)橄到y(tǒng)錯誤實(shí)例重啟或者因?qū)嵗e誤而重啟的機(jī)器進(jìn)行自動化處理,比如成功重啟后創(chuàng)建快照處理。
ecs 系統(tǒng)事件
云產(chǎn)品系統(tǒng)事件監(jiān)控
注:記得給函數(shù)的service的role設(shè)置操作ecs的權(quán)限
# -*- coding: utf-8 -*-
import logging
import json, random, string, time
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DeleteSnapshotRequest import DeleteSnapshotRequest
from aliyunsdkecs.request.v20140526.CreateSnapshotRequest import CreateSnapshotRequest
from aliyunsdkecs.request.v20140526.DescribeDisksRequest import DescribeDisksRequest
from aliyunsdkcore.auth.credentials import StsTokenCredential
LOGGER = logging.getLogger()
clt = None
def handler(event, context):
creds = context.credentials
sts_token_credential = StsTokenCredential(creds.access_key_id, creds.access_key_secret, creds.security_token)
'''
{
"product": "ECS",
"content": {
"executeFinishTime": "2018-06-08T01:25:37Z",
"executeStartTime": "2018-06-08T01:23:37Z",
"ecsInstanceName": "timewarp",
"eventId": "e-t4nhcpqcu8fqushpn3mm",
"eventType": "InstanceFailure.Reboot",
"ecsInstanceId": "i-bp18l0uopocfc98xxxx"
},
"resourceId": "acs:ecs:cn-hangzhou:123456789:instance/i-bp18l0uopocfc98xxxx",
"level": "CRITICAL",
"instanceName": "instanceName",
"status": "Executing",
"name": "Instance:SystemFailure.Reboot:Executing",
"regionId": "cn-hangzhou"
}
'''
evt = json.loads(event)
content = evt.get("content");
ecsInstanceId = content.get("ecsInstanceId");
regionId = evt.get("regionId");
global clt
clt = client.AcsClient(region_id=regionId, credential=sts_token_credential)
name = evt.get("name");
name = name.lower()
if name in ['Instance:SystemFailure.Reboot:Executing'.lower(), "Instance:InstanceFailure.Reboot:Executing".lower()]:
pass
# do other things
if name in ['Instance:SystemFailure.Reboot:Executed'.lower(), "Instance:InstanceFailure.Reboot:Executed".lower()]:
request = DescribeDisksRequest()
request.add_query_param("RegionId", "cn-shenzhen")
request.set_InstanceId(ecsInstanceId)
response = _send_request(request)
disks = response.get('Disks').get('Disk', [])
for disk in disks:
diskId = disk["DiskId"]
SnapshotId = create_ecs_snap_by_id(diskId)
LOGGER.info("Create ecs snap sucess, ecs id = %s , disk id = %s ", ecsInstanceId, diskId)
def create_ecs_snap_by_id(disk_id):
LOGGER.info("Create ecs snap, disk id is %s ", disk_id)
request = CreateSnapshotRequest()
request.set_DiskId(disk_id)
request.set_SnapshotName("reboot_" + ''.join(random.choice(string.ascii_lowercase) for _ in range(6)))
response = _send_request(request)
return response.get("SnapshotId")
# send open api request
def _send_request(request):
request.set_accept_format('json')
try:
response_str = clt.do_action_with_exception(request)
LOGGER.info(response_str)
response_detail = json.loads(response_str)
return response_detail
except Exception as e:
LOGGER.error(e)