阿里云 函數(shù)計(jì)算是一個(gè)事件驅(qū)動(dòng)的全托管計(jì)算服務(wù)。通過(guò)函數(shù)計(jì)算,您無(wú)需管理服務(wù)器等基礎(chǔ)設(shè)施,只需編寫(xiě)代碼并上傳。函數(shù)計(jì)算會(huì)為您準(zhǔn)備好計(jì)算資源,以彈性、可靠的方式運(yùn)行您的代碼,并提供日志查詢,性能監(jiān)控,報(bào)警等功能。借助于函數(shù)計(jì)算,您可以快速構(gòu)建任何類型的應(yīng)用和服務(wù),無(wú)需管理和運(yùn)維。更棒的是,您只需要為代碼實(shí)際運(yùn)行消耗的資源付費(fèi),而代碼未運(yùn)行則不產(chǎn)生費(fèi)用。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比防城網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式防城網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋防城地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
阿里云
云監(jiān)控為云上用戶提供開(kāi)箱即用的企業(yè)級(jí)開(kāi)放型一站式監(jiān)控解決方案。涵蓋IT設(shè)施基礎(chǔ)監(jiān)控,外網(wǎng)網(wǎng)絡(luò)質(zhì)量撥測(cè)監(jiān)控,基于事件、自定義指標(biāo)、日志的業(yè)務(wù)監(jiān)控。為您全方位提供更高效、更全面、更省錢(qián)的監(jiān)控服務(wù)。
云監(jiān)控提供了豐富事件,并且事件還在不斷豐富中(
云產(chǎn)品系統(tǒng)事件監(jiān)控), 豐富的事件觸發(fā)自定義處理的函數(shù),可以實(shí)現(xiàn)更加完美的的自動(dòng)化運(yùn)維。
在本文中,重點(diǎn)關(guān)注函數(shù)計(jì)算對(duì)ecs的重啟事件處理,因?yàn)檫@些ecs重啟事件是目前用戶需要很高優(yōu)先級(jí)用戶優(yōu)先級(jí)去響應(yīng)的;假設(shè)之前一臺(tái)ecs 發(fā)生因系統(tǒng)錯(cuò)誤而重啟,用戶可能會(huì)緊急起來(lái)做一些驗(yàn)證或者創(chuàng)建快照的處理, 在本示例中,我們對(duì)一臺(tái)因?yàn)橄到y(tǒng)錯(cuò)誤實(shí)例重啟或者因?qū)嵗e(cuò)誤而重啟的機(jī)器進(jìn)行自動(dòng)化處理,比如成功重啟后創(chuàng)建快照處理。
ecs 系統(tǒng)事件
cdn.com/31eeef9826151824881ec02ea99b81c2962a640c.png">
云產(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)
當(dāng)前名稱:函數(shù)計(jì)算自動(dòng)化運(yùn)維實(shí)戰(zhàn)3--事件觸發(fā)自動(dòng)創(chuàng)建快照
URL鏈接:http://aaarwkj.com/article14/pjdpge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、軟件開(kāi)發(fā)、做網(wǎng)站、網(wǎng)站內(nèi)鏈、全網(wǎng)營(yíng)銷推廣、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)