本次监控方式通过Zabbix外部检查方式自动发现实例,监控项数据通过Zabbix采集器方式生成。将下面脚本上传至外部检查路径下。
注:部署路径有差异,根据实际环境的外部检查路径放至脚本。
#!/usr/bin/python3 import json from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.vpc.v20170312 import vpc_client, models import sys from os import popen from zbxsend import ZabbixMetric, ZabbixSender akey = sys.argv[1] ckey = sys.argv[2] region = sys.argv[3] ZABBIX_SEND_ADDR = '/itops/zabbix/bin/zabbix_sender' ZABBIX_SERVER_IP = '127.0.0.1' ZABBIX_PORT = '10051' def get_data(offset): params = { "Limit": str(100), "Offset": str(offset) } req.from_json_string(json.dumps(params)) # 返回的resp是一个DescribeVpcsResponse的实例,与请求对象对应 resp = client.DescribeVpcs(req) # 输出json格式的字符串回包 res = resp.to_json_string() data = json.loads(res) disklist = data['VpcSet'] # 获取记录总条目数 totalcount = int(data['TotalCount']) return totalcount,disklist try: # 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey # 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 cred = credential.Credential(akey,ckey) # 实例化一个http选项,可选的,没有特殊需求可以跳过 httpProfile = HttpProfile() httpProfile.endpoint = "vpc.tencentcloudapi.com" # 实例化一个client选项,可选的,没有特殊需求可以跳过 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品的client对象,clientProfile是可选的 client = vpc_client.VpcClient(cred, region, clientProfile) # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.DescribeVpcsRequest() totalcount,disklist = get_data(0) ## 获取100条以内的disk数据 if totalcount > 100: ## 如果disk记录大于100,则将100往后的数据拼接至list if totalcount % 100 > 0: num = totalcount // 100 + 1 else: num = totalcount // 100 for i in range(num): if i == 0: continue else: disklist = disklist + get_data(i*100)[1] list_disk = [] for i in disklist: vpc_id = i['VpcId'] vpc_name = i['VpcName'] vpc_name = vpc_name+vpc_id list_disk.append({"{#VPCID}":vpc_id,"{#VPCNAME}":vpc_name}) keys_list = list(i.keys()) for key in keys_list: result = ZabbixSender(ZABBIX_SERVER_IP).send([ZabbixMetric(vpc_id,key,str(i[key]))]) print(json.dumps(list_disk)) except TencentCloudSDKException as err: print(err)
注意以下三处的值根据实际环境调整
ZABBIX_SEND_ADDR:zabbix_sender路径
ZABBIX_SERVER_IP:server服务器IP地址
ZABBIX_PORT:server服务器端口
键值解释:
tencent.VPC.py:脚本名称,可自定义脚本名称,需要与外部检查路径下的脚本名称一致
{$ACCESS_KEY}: SecretId
{$SECRET_KEY}:SecretKey
可前往官网控制台 https://console.cloud.tencent.com/cam/capi 获取SecretId和SecretKey
{$REGION}:资源地域,表示操作的资源所属的地域
监控项名称入口:
设置完成后,等待实例自动发现即可。
以上就是本期的Zabbix技术分享。
错误信息