一、环境配置
下载python3及pip3
yum install python3*
下载阿里云sdk
pip3 install requests
pip3 install aliyunsdkcms
pip3 install aliyun-python-sdk-cms
pip3 install aliyun-python-sdk-ecs
参考
https://www.alibabacloud.com/help/zh/doc-detail/28182.htm
接口参考
https://help.aliyun.com/document_detail/28619.html?spm=a2c4g.11186623.4.6.7a6a5e5dVfFqGX
https://help.aliyun.com/document_detail/51939.html?spm=a2c4g.11186623.2.25.1f74750egVzpq1
二、脚本
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcms.request.v20190101.DescribeMetricLastRequest import DescribeMetricLastRequest
import time,json,ast,requests
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
date = time.strftime("%Y-%m-%d-%H:%M:%S")
class PUSH_info(object):
def __init__(self,key,value,where):
self.key = key
self.value = value
self.where = where
self.client = AcsClient(key,value,where)
def check_cpu(self):
request = DescribeMetricLastRequest()
request.set_accept_format('json')
request.set_MetricName("cpu_system")
request.set_Period("60")
request.set_Namespace("acs_ecs_dashboard")
response = self.client.do_action_with_exception(request)
data = str(response,encoding='utf-8')
datas = json.loads(data)
Datapoints = ast.literal_eval(datas['Datapoints'][1:-1])
#print(Datapoints)
#print(type(Datapoints))
for k in Datapoints:
if k['Average'] > 90:
sms_push('cpu',k['instanceId'],k['Average'])
def check_disk(self):
request = DescribeMetricLastRequest()
request.set_accept_format('json')
request.set_MetricName("diskusage_utilization")
request.set_Period("60")
request.set_Namespace("acs_ecs_dashboard")
response = self.client.do_action_with_exception(request)
data = str(response,encoding='utf-8')
datas = json.loads(data)
Datapoints = ast.literal_eval(datas['Datapoints'][1:-1])
#print(Datapoints)
#print(type(Datapoints))
for k in Datapoints:
if k['Average'] > 90:
sms_push('磁盘',k['instanceId'],k['Average'],k['device'])
def check_mem(self):
request = DescribeMetricLastRequest()
request.set_accept_format('json')
request.set_MetricName("memory_usedutilization")
request.set_Period("60")
request.set_Namespace("acs_ecs_dashboard")
response = self.client.do_action_with_exception(request)
data = str(response,encoding='utf-8')
datas = json.loads(data)
Datapoints = ast.literal_eval(datas['Datapoints'][1:-1])
#print(Datapoints)
#print(type(Datapoints))
for k in Datapoints:
if k['Average'] > 90:
sms_push('内存',k['instanceId'],k['Average'])
def get_ecs_info():
request = DescribeInstancesRequest()
request.set_accept_format('json')
request.add_query_param('RegionId', 'cn-shenzhen')
request.set_PageSize('100')
response = client.do_action_with_exception(request)
data = str(response,encoding='utf-8')
datas = json.loads(data)
info = datas['Instances']['Instance']
lists = []
for k in info:
lists.append({"InstanceId":k['InstanceId'],"InstanceName":k['InstanceName'],"PublicIpAddress":k['PublicIpAddress']['IpAddress']})
return lists
def sms_push(pro,ids,value,disk=''):
for i in get_ecs_info():
if ids in filters:
pass
elif ids in i['InstanceId'] :
strs = "服务器:%s ip:%s %s %s当前使用已达%s 请注意!!!"%(i['InstanceName'],i['PublicIpAddress'],pro,disk,value)
#get_sms(strs)
print(i['InstanceName'],i['PublicIpAddress'],pro,disk,value)
def get_sms(sms):
headers = {"Content-Type": "text/plain"}
send_url = ""
send_data = {
"msgtype": "markdown", # 消息类型,此时固定为markdown
"markdown": {
"content": "# **提醒:阿里云系统监控处理**\n" +
"#### **请相关系统同事注意,及时跟进!**\n" +
"> %s \n"%sms
}
}
res = requests.post(url=send_url, headers=headers, json=send_data)
print(res.text)
if __name__=="__main__":
filters = ['']
key = ''
value = ''
where = ''
push = PUSH_info(key,value,where)
push.check_cpu()
push.check_mem()
push.check_disk()