Zabbix uses Python to monitor Alibaba Cloud ECS

1. Install the required modules first

# pip install aliyunsdkcore
# pip install aliyunsdkecs
# pip install aliyunsdkcms

2. Obtain an SLB instance for automatic discovery on Zabbix

# Get detailed information of all ecs instances

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
import json

Id = 'xxx'
Secret = 'xxx'
RegionId = 'cn-shenzhen'

clt = client.AcsClient(Id,Secret,RegionId)
request = DescribeInstancesRequest.DescribeInstancesRequest();
response = json.loads(clt.do_action_with_exception(request), encoding='utf-8')
# print(response)
InstanceIdList = []
InstanceIdDict = {}
ZabbixDataDict = {}
for i in response['Instances']['Instance']:
InstanceIdDict['{#INSTANCEID}'] = i['InstanceId']
ZabbixDataDict["data"] = InstanceIdList

3. Collect various monitoring indicator data on the SLB instance

# ecs monitoring indicator information

import json
import sys
import time
import datetime
from aliyunsdkcore import client
from aliyunsdkcms.request.v20180308 import QueryMetricListRequest # query for a period of time
from aliyunsdkcms.request.v20180308 import QueryMetricLastRequest # Query the latest record

Id = 'xxx'
Secret = 'xxx'
RegionId = 'cn-shenzhen'

clt = client.AcsClient(Id,Secret,RegionId)

def getMetricData(instanceId, metric):
request = QueryMetricLastRequest.QueryMetricLastRequest()
request.set_Dimensions("{'instanceId':'%s'}" % instanceId)
request. set_Period('60')
response = clt.do_action_with_exception(request)
return json. loads(response)

instanceId = sys.argv[1]
metric = sys.argv[2]

if metric == "CPUUtilization":
CPUUtilization = getMetricData(instanceId,"CPUUtilization")
elif metric == "DiskReadIOPS": # system disk read IOPS
DiskReadIOPS = getMetricData(instanceId,"DiskReadIOPS")
elif metric == "DiskWriteIOPS": # system disk write IOPS
DiskReadIOPS = getMetricData(instanceId,"DiskWriteIOPS")
elif metric == "cpu_idle": # Current idle CPU percentage
DiskReadIOPS = getMetriceData(instanceId,"cpu_idle")

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us