zabbix通过api监控阿里云RDS

一.说明

监控阿里云的RDS可以写脚本去用mysql命令登陆获取,也可以用API获取(API还按次数收费,前100万次免费)。

安装核心sdk和云监控、RDS的sdk
pip3 install aliyun-python-sdk-core
pip3 install aliyun-python-sdk-cms
pip3 install aliyun-python-sdk-rds

二.脚本

1.根据api获取rds的实例列表,生成json格式的内容
file

auto_rds.sh

#!/usr/bin/python3

import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkrds.request.v20140815.DescribeDBInstancesRequest import DescribeDBInstancesRequest

#秘钥
aliyun_user_ak = 'xxxxxxxxxxxxxxxxx'
aliyun_user_sk = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
region_id = 'cn-beijing'
client = AcsClient(ak=aliyun_user_ak, secret=aliyun_user_sk, region_id=region_id, timeout=300)

rds_list = []

request = DescribeDBInstancesRequest()
request.set_accept_format('json')
request.set_InstanceNetworkType("VPC")
request.set_VpcId("vpc-2ze0w06g138ax9rh7aj2e")
response = client.do_action_with_exception(request)
rds_info = json.loads(response)

for i in range(len(rds_info["Items"]["DBInstance"])):
    rds_id = rds_info["Items"]["DBInstance"][i]["DBInstanceId"]
    rds_list += [{'{#RDSID}':rds_id}]

print(json.dumps({'data':rds_list},sort_keys=True,indent=4,separators=(',',':')))

2.根据api传入RDS的id和键值来获取信息
测试:./rds_info.py rm-2zem2w8dlem15e49s ConnectionUsage
file

rds_info.py

#!/usr/bin/python3

import datetime, json, sys, ast
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

#秘钥
aliyun_user_ak = 'xxxxxxxxxxxxxxxxxxxxxxx'
aliyun_user_sk = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
region_id = 'cn-beijing'
client = AcsClient(ak=aliyun_user_ak, secret=aliyun_user_sk, region_id=region_id, timeout=300)

nowtime = datetime.datetime.now()
stmp_time = nowtime - datetime.timedelta(minutes=4)
etmp_time = nowtime - datetime.timedelta(minutes=3)
start_time = stmp_time.strftime('%Y-%m-%d %H:%M:%S') #开始时间
end_time = etmp_time.strftime('%Y-%m-%d %H:%M:%S') #结束时

request = DescribeMetricLastRequest()
request.set_accept_format('json')
request.set_MetricName(sys.argv[2])
request.set_Period("60")
request.set_StartTime(start_time)
request.set_EndTime(end_time)
request.set_Dimensions("[{\"instanceId\":%s}]" %(sys.argv[1]))
request.set_Namespace("acs_rds_dashboard")

response = client.do_action_with_exception(request)
item_info = json.loads(response)
item_str = item_info["Datapoints"]
item_str = item_str.lstrip('[') #字符串去掉两边[]
item_str = item_str.rstrip(']')
item_dict = json.loads(item_str)
#item_dict = ast.literal_eval(item_str)

if item_info["Code"] == "200":
    print(item_dict["Average"])
else:
    print(item_info)

三.zabbix配置

1.修改配置文件,添加脚本位置
vim /etc/zabbix/zabbix_agentd.d/userparams.conf

#rds-api
UserParameter=auto_rds,/usr/bin/python3 /etc/zabbix/script/rdsapi/auto_rds.py
UserParameter=rds_info[*],/usr/bin/python3 /etc/zabbix/script/rdsapi/rds-info.py $1 $2

重启
service zabbix-agent restart

2.添加模板,增加自动发现规则
file

3.监控项原型,{#RDSID}是json里定义的,rds_info[{#RDSID},MySQL_IbufUseRatio]
file

0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论