zabbix监控RDS

  sre, 开发


阿里提供了OpenAPI与SDK可用来监控RDS等常规资源。

新建/opt/BackupShell/api.py并配置5分钟执行一次。

内容为:

#!/usr/bin/python
# coding=utf-8

import json,datetime,sys,getopt
from aliyunsdkcore import client

def DescribeDBInstances(**INSTANCE_SET):
    #RDS实例列表
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancesRequest
    request = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
    request.set_accept_format(accept_format)
    response = clt.do_action_with_exception(request)
    json_response=json.loads(response)
    #KEY_VALUE_SET["DBInstances_total"] = json_response["TotalRecordCount"]
    for i in range(0,json_response["TotalRecordCount"]-1 +1):
        DBInstanceId=json_response["Items"]["DBInstance"][i]["DBInstanceId"]
        INSTANCE_SET[DBInstanceId]=json_response["Items"]["DBInstance"][i]["DBInstanceDescription"]
    return INSTANCE_SET

def DescribeAbnormalDBInstances(**KEY_VALUE_SET):
    #异常实例数量
    from aliyunsdkrds.request.v20140815 import DescribeAbnormalDBInstancesRequest
    request = DescribeAbnormalDBInstancesRequest.DescribeAbnormalDBInstancesRequest()
    request.set_accept_format('json')
    response = clt.do_action_with_exception(request)
    json_response=json.loads(response)
    KEY_VALUE_SET["DBInstances_abnormal"]=json_response["TotalRecordCount"]
    return KEY_VALUE_SET

def DescribeDBInstanceAttribute(**KEY_VALUE_SET):
    #查看RDS实例详情
    from aliyunsdkrds.request.v20140815 import DescribeDBInstanceAttributeRequest
    request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceDescription"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceDescription"]
    KEY_VALUE_SET["DBInstanceAttribute_Category"] = json_response["Items"]["DBInstanceAttribute"][0]["Category"]
    #KEY_VALUE_SET["DBInstanceAttribute_InsId"] = json_response["Items"]["DBInstanceAttribute"][0]["InsId"]
    KEY_VALUE_SET["DBInstanceAttribute_LockMode"] = json_response["Items"]["DBInstanceAttribute"][0]["LockMode"]
    KEY_VALUE_SET["DBInstanceAttribute_ConnectionString"] = json_response["Items"]["DBInstanceAttribute"][0]["ConnectionString"]
    KEY_VALUE_SET["DBInstanceAttribute_CreationTime"] = json_response["Items"]["DBInstanceAttribute"][0]["CreationTime"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceNetType"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceNetType"]
    #KEY_VALUE_SET["DBInstanceAttribute_ReadOnlyDBInstanceIds"] = json_response["Items"]["DBInstanceAttribute"][0]["ReadOnlyDBInstanceIds"]
    KEY_VALUE_SET["DBInstanceAttribute_MaxConnections"] = json_response["Items"]["DBInstanceAttribute"][0]["MaxConnections"]
    #KEY_VALUE_SET["DBInstanceAttribute_DBInstanceClassType"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceClassType"]
    KEY_VALUE_SET["DBInstanceAttribute_Engine"] = json_response["Items"]["DBInstanceAttribute"][0]["Engine"]
    KEY_VALUE_SET["DBInstanceAttribute_ExpireTime"] = json_response["Items"]["DBInstanceAttribute"][0]["ExpireTime"]
    #KEY_VALUE_SET["DBInstanceAttribute_VpcId"] = json_response["Items"]["DBInstanceAttribute"][0]["VpcId"]
    #KEY_VALUE_SET["DBInstanceAttribute_DBMaxQuantity"] = json_response["Items"]["DBInstanceAttribute"][0]["DBMaxQuantity"]
    #KEY_VALUE_SET["DBInstanceAttribute_ConnectionMode"] = json_response["Items"]["DBInstanceAttribute"][0]["ConnectionMode"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceMemory"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceMemory"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceCPU"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceCPU"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceType"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceType"]
    if json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceStatus"]=="Running":
        KEY_VALUE_SET["DBInstanceAttribute_DBInstanceStatus"]=1
    else:
        KEY_VALUE_SET["DBInstanceAttribute_DBInstanceStatus"] = 0
    #KEY_VALUE_SET["DBInstanceAttribute_DBInstanceStatus"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceStatus"]
    KEY_VALUE_SET["DBInstanceAttribute_AvailabilityValue"] = json_response["Items"]["DBInstanceAttribute"][0]["AvailabilityValue"]
    KEY_VALUE_SET["DBInstanceAttribute_ZoneId"] = json_response["Items"]["DBInstanceAttribute"][0]["ZoneId"]
    KEY_VALUE_SET["DBInstanceAttribute_EngineVersion"] = json_response["Items"]["DBInstanceAttribute"][0]["EngineVersion"]
    KEY_VALUE_SET["DBInstanceAttribute_InstanceNetworkType"] = json_response["Items"]["DBInstanceAttribute"][0]["InstanceNetworkType"]
    KEY_VALUE_SET["DBInstanceAttribute_MaxIOPS"] = json_response["Items"]["DBInstanceAttribute"][0]["MaxIOPS"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceStorage"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceStorage"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceId"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceId"]
    #KEY_VALUE_SET["DBInstanceAttribute_AccountMaxQuantity"] = json_response["Items"]["DBInstanceAttribute"][0]["AccountMaxQuantity"]
    KEY_VALUE_SET["DBInstanceAttribute_MaintainTime"] = json_response["Items"]["DBInstanceAttribute"][0]["MaintainTime"]
    #KEY_VALUE_SET["DBInstanceAttribute_SupportUpgradeAccountType"] = json_response["Items"]["DBInstanceAttribute"][0]["SupportUpgradeAccountType"]
    #KEY_VALUE_SET["DBInstanceAttribute_AccountType"] = json_response["Items"]["DBInstanceAttribute"][0]["AccountType"]
    KEY_VALUE_SET["DBInstanceAttribute_DBInstanceClass"] = json_response["Items"]["DBInstanceAttribute"][0]["DBInstanceClass"]
    #KEY_VALUE_SET["DBInstanceAttribute_SecurityIPList"] = json_response["Items"]["DBInstanceAttribute"][0]["SecurityIPList"]
    KEY_VALUE_SET["DBInstanceAttribute_Port"] = json_response["Items"]["DBInstanceAttribute"][0]["Port"]
    #KEY_VALUE_SET["DBInstanceAttribute_RequestId]=KEY_VALUE_SET[DBInstanceAttribute_"] = json_response["RequestId"]
    return KEY_VALUE_SET

def DescribeResourceUsage(**KEY_VALUE_SET):
    from aliyunsdkrds.request.v20140815 import DescribeResourceUsageRequest
    request = DescribeResourceUsageRequest.DescribeResourceUsageRequest()
    request.set_accept_format(accept_format)

    request.set_DBInstanceId(DBInstanceId);
    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)

    KEY_VALUE_SET["ResourceUsage_SQLSize"]=json_response["SQLSize"]
    #KEY_VALUE_SET["ResourceUsage_Engine"]=json_response["Engine"]
    KEY_VALUE_SET["ResourceUsage_DiskUsed"]=json_response["DiskUsed"]
    #KEY_VALUE_SET["ResourceUsage_DBInstanceId"]=json_response["DBInstanceId"]
    KEY_VALUE_SET["ResourceUsage_LogSize"]=json_response["LogSize"]
    KEY_VALUE_SET["ResourceUsage_BackupSize"]=json_response["BackupSize"]
    #KEY_VALUE_SET["ResourceUsage_RequestId"]=json_response["RequestId"]
    KEY_VALUE_SET["ResourceUsage_ColdBackupSize"]=json_response["ColdBackupSize"]
    KEY_VALUE_SET["ResourceUsage_DataSize"]=json_response["DataSize"]
    return KEY_VALUE_SET

def DescribeResourceDiagnosis(**KEY_VALUE_SET):
    #资源使用情况,可精确到300s
    from aliyunsdkrds.request.v20140815 import DescribeResourceDiagnosisRequest
    request = DescribeResourceDiagnosisRequest.DescribeResourceDiagnosisRequest()
    request.set_accept_format('json')

    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_DBInstanceId(DBInstanceId);

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)

    KEY_VALUE_SET["ResourceDiagnosis_StorageMax"]= json_response["Storage"]["Max"]
    KEY_VALUE_SET["ResourceDiagnosis_StorageAvg"]= json_response["Storage"]["Avg"]
    KEY_VALUE_SET["ResourceDiagnosis_StorageMin"]= json_response["Storage"]["Min"]
    KEY_VALUE_SET["ResourceDiagnosis_ConnectionMax"]= json_response["Connection"]["Max"]
    KEY_VALUE_SET["ResourceDiagnosis_ConnectionAvg"]= json_response["Connection"]["Avg"]
    KEY_VALUE_SET["ResourceDiagnosis_ConnectionMin"]= json_response["Connection"]["Min"]
    KEY_VALUE_SET["ResourceDiagnosis_IOPSMax"]= json_response["IOPS"]["Max"]
    KEY_VALUE_SET["ResourceDiagnosis_IOPSAvg"]= json_response["IOPS"]["Avg"]
    KEY_VALUE_SET["ResourceDiagnosis_IOPSMin"]= json_response["IOPS"]["Min"]
    #KEY_VALUE_SET["ResourceDiagnosis_"]=print json_response["RequestId"]
    #KEY_VALUE_SET["ResourceDiagnosis_"]=print json_response["StartTime"]
    KEY_VALUE_SET["ResourceDiagnosis_MemoryMax"]= json_response["Memory"]["Max"]
    KEY_VALUE_SET["ResourceDiagnosis_MemoryAvg"]= json_response["Memory"]["Avg"]
    KEY_VALUE_SET["ResourceDiagnosis_MemoryMin"]= json_response["Memory"]["Min"]
    #KEY_VALUE_SET["ResourceDiagnosis_"]=print json_response["EndTime"]
    KEY_VALUE_SET["ResourceDiagnosis_CPUMax"]= json_response["CPU"]["Max"]
    KEY_VALUE_SET["ResourceDiagnosis_CPUAvg"]= json_response["CPU"]["Avg"]
    KEY_VALUE_SET["ResourceDiagnosis_CPUMin"]= json_response["CPU"]["Min"]
    return KEY_VALUE_SET
    '''
MySQL_NetworkTraffic    MySQL实例平均每秒钟的输入流量,MySQL实例平均每秒钟的输出流量。单位为KB。
MySQL_QPSTPS    平均每秒SQL语句执行次数,平均每秒事务数
MySQL_Sessions  当前活跃连接数,当前总连接数
MySQL_InnoDBBufferRatio InnoDB缓冲池的读命中率,InnoDB缓冲池的利用率,InnoDB缓冲池脏块的百分率
MySQL_InnoDBDataReadWriten      InnoDB平均每秒钟读取的数据量,InnoDB平均每秒钟写入的数据量。单位为KB
MySQL_InnoDBLogRequests 平均每秒向InnoDB缓冲池的读次数,平均每秒向InnoDB缓冲池的写次数
MySQL_InnoDBLogWrites   平均每秒日志写请求数,平均每秒向日志文件的物理写次数,平均每秒向日志文件完成的fsync()写数量
MySQL_TempDiskTableCreates      MySQL执行语句时在硬盘上自动创建的临时表的数量
MySQL_MyISAMKeyBufferRatio      MyISAM平均每秒Key Buffer利用率,MyISAM平均每秒Key Buffer读命中率,MyISAM平均每秒Key Buffer写命中率
MySQL_MyISAMKeyReadWrites       MyISAM平均每秒钟从缓冲池中的读取次数,MyISAM平均每秒钟从缓冲池中的写入次数,MyISAM平均每秒钟从硬盘上读取的次数,MyISAM平均每秒钟从硬盘上写入的次数
MySQL_COMDML    平均每秒Delete语句执行次数,平均每秒Insert语句执行次数, 平均每秒Insert_Select语句执行次数,平均每秒Replace语句执行次数,平均每秒Replace_Select语句执行次数,平均每秒Select语句执行次数,平均每秒Update语句执行次数
MySQL_RowDML    平均每秒从InnoDB表读取的行数,平均每秒从InnoDB表更新的行数,平均每秒从InnoDB表删除的行数,平均每秒从InnoDB表插入的行数,平均每秒向日志文件的物理写次数
MySQL_MemCpuUsage       MySQL实例CPU使用率(占操作系统总数),MySQL实例内存使用率(占操作系统总数)
MySQL_IOPS      MySQL实例的IOPS(每秒IO请求次数)
MySQL_DetailedSpaceUsage        MySQL实例空间占用详情:ins_size实例总空间使用量;data_size数据空间;log_size日志空间;tmp_size临时空间;other_size系统空间
slavestat       只读实例延迟
    :return:
    '''

def DescribeDBInstancePerformance_NetworkTraffic(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_NetworkTraffic")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    recv_sent=json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart=recv_sent.index('&')
    KEY_VALUE_SET["DBInstancePerformance_NetworkTrafficRecv"] =recv_sent[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_NetworkTrafficSent"] =recv_sent[apart+1:]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_QPSTPS(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format('json')
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_QPSTPS")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    qps_tps = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart=qps_tps.index('&')
    KEY_VALUE_SET["DBInstancePerformance_QPS"] = qps_tps[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_TPS"] = qps_tps[apart + 1:]
    return KEY_VALUE_SET

    #print json_response["Engine"]
    #print json_response["DBInstanceId"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["ValueFormat"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Date"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Unit"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Key"]
    #print json_response["RequestId"]
    #print json_response["StartTime"]
    #print json_response["EndTime"]

def DescribeDBInstancePerformance_Sessions(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_Sessions")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)

    active_total = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = active_total.index('&')
    KEY_VALUE_SET["DBInstancePerformance_SessionsActive"] = active_total[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_SessionsTotal"] = active_total[apart + 1:]
    return KEY_VALUE_SET
    #print json_response["Engine"]
    #print json_response["DBInstanceId"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["ValueFormat"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Date"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Unit"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Key"]
    #print json_response["RequestId"]
    #print json_response["StartTime"]
    #print json_response["EndTime"]

def DescribeDBInstancePerformance_InnoDBDataReadWriten(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTimeMinute);
    request.set_EndTime(EndTimeMinute);
    request.set_Key("MySQL_InnoDBDataReadWriten")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    read_writen = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = read_writen.index('&')
    KEY_VALUE_SET["DBInstancePerformance_InnoDBDataRead"] = read_writen[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBDataWriten"] = read_writen[apart + 1:]
    return KEY_VALUE_SET
    #print json_response["Engine"]
    #print json_response["DBInstanceId"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["ValueFormat"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Date"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Unit"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Key"]
    #print json_response["RequestId"]
    #print json_response["StartTime"]
    #print json_response["EndTime"]

def DescribeDBInstancePerformance_InnoDBLogRequests(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTimeMinute);
    request.set_EndTime(EndTimeMinute);
    request.set_Key("MySQL_InnoDBLogRequests")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)

    read_writen = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = read_writen.index('&')
    KEY_VALUE_SET["DBInstancePerformance_InnoDBLogRequestsRead"] = read_writen[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBLogRequestsWriten"] = read_writen[apart + 1:]
    return KEY_VALUE_SET

    #print json_response["Engine"]
    #print json_response["DBInstanceId"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["ValueFormat"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Date"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Unit"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Key"]
    #print json_response["RequestId"]
    #print json_response["StartTime"]
    #print json_response["EndTime"]

def DescribeDBInstancePerformance_InnoDBLogWrites(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTimeMinute);
    request.set_EndTime(EndTimeMinute);
    request.set_Key("MySQL_InnoDBLogWrites")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    requests_writes_fsyncs = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = requests_writes_fsyncs.index('&')
    apart_next = requests_writes_fsyncs.index('&', apart + 1)
    KEY_VALUE_SET["DBInstancePerformance_InnoDBLogResquests"] = requests_writes_fsyncs[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBLogWrites"] = requests_writes_fsyncs[apart + 1:apart_next]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBLogFsyncs"] = requests_writes_fsyncs[apart_next + 1]
    return KEY_VALUE_SET
    #print json_response["Engine"]
    #print json_response["DBInstanceId"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["ValueFormat"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Date"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Unit"]
    #print json_response["PerformanceKeys"]["PerformanceKey"][0]["Key"]
    #print json_response["RequestId"]
    #print json_response["StartTime"]
    #print json_response["EndTime"]

def DescribeDBInstancePerformance_TempDiskTableCreates(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_TempDiskTableCreates")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    KEY_VALUE_SET["DBInstancePerformance_TempDiskTableCreates"]=json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_COMDML(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format('json')
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_COMDML")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)

    delete_insert_insertSelect_replace_replaceSelect_select_update = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart=delete_insert_insertSelect_replace_replaceSelect_select_update.index('&')
    apart_second=delete_insert_insertSelect_replace_replaceSelect_select_update.index('&',apart+1)
    apart_thrid = delete_insert_insertSelect_replace_replaceSelect_select_update.index('&', apart_second + 1)
    apart_fourth = delete_insert_insertSelect_replace_replaceSelect_select_update.index('&', apart_thrid + 1)
    apart_fifth = delete_insert_insertSelect_replace_replaceSelect_select_update.index('&', apart_fourth + 1)
    apart_sixth = delete_insert_insertSelect_replace_replaceSelect_select_update.index('&', apart_fifth + 1)
    KEY_VALUE_SET["DBInstancePerformance_COMDML__delete"]=delete_insert_insertSelect_replace_replaceSelect_select_update[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__insert"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart+1:apart_second]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__insertSelect"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart_second+1:apart_thrid]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__replace"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart_thrid+1:apart_fourth]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__replaceSelect"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart_fourth+1:apart_fifth]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__select"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart_fifth+1:apart_sixth]
    KEY_VALUE_SET["DBInstancePerformance_COMDML__update"]=delete_insert_insertSelect_replace_replaceSelect_select_update[apart_sixth+1:]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_RowDML(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_RowDML")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    readed_update_delete_insert_writes = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = readed_update_delete_insert_writes.index('&')
    apart_second = readed_update_delete_insert_writes.index('&', apart + 1)
    apart_thrid = readed_update_delete_insert_writes.index('&', apart_second + 1)
    apart_fourth = readed_update_delete_insert_writes.index('&', apart_thrid + 1)
    KEY_VALUE_SET["DBInstancePerformance_Row_readed"] = readed_update_delete_insert_writes[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_Row_update"] = readed_update_delete_insert_writes[apart + 1:apart_second]
    KEY_VALUE_SET["DBInstancePerformance_Row_delete"] = readed_update_delete_insert_writes[apart_second + 1:apart_thrid]
    KEY_VALUE_SET["DBInstancePerformance_Row_insert"] = readed_update_delete_insert_writes[apart_thrid + 1:apart_fourth]
    KEY_VALUE_SET["DBInstancePerformance_Row_writes"] = readed_update_delete_insert_writes[apart_fourth + 1:]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_MemCpuUsage(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_MemCpuUsage")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    mem_cpu = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = mem_cpu.index('&')
    KEY_VALUE_SET["DBInstancePerformance_MemUsage"] = 100.00-float(mem_cpu[0:apart])
    KEY_VALUE_SET["DBInstancePerformance_CpuUsage"] = 100.00-float(mem_cpu[apart + 1:])
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_IOPS(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_IOPS")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    KEY_VALUE_SET["DBInstancePerformance_IOPS"] = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_DetailedSpaceUsage(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟MB
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format('json')
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_DetailedSpaceUsage")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    ins_data_log_tmp_other = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = ins_data_log_tmp_other.index('&')
    apart_second = ins_data_log_tmp_other.index('&', apart + 1)
    apart_thrid = ins_data_log_tmp_other.index('&', apart_second + 1)
    apart_fourth = ins_data_log_tmp_other.index('&', apart_thrid + 1)
    KEY_VALUE_SET["DBInstancePerformance_DetailedSpaceUsage_ins"] = ins_data_log_tmp_other[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_DetailedSpaceUsage_data"] = ins_data_log_tmp_other[apart + 1:apart_second]
    KEY_VALUE_SET["DBInstancePerformance_DetailedSpaceUsage_log"] = ins_data_log_tmp_other[apart_second + 1:apart_thrid]
    KEY_VALUE_SET["DBInstancePerformance_DetailedSpaceUsage_tmp"] = ins_data_log_tmp_other[apart_thrid + 1:apart_fourth]
    KEY_VALUE_SET["DBInstancePerformance_DetailedSpaceUsage_other"] = ins_data_log_tmp_other[apart_fourth + 1:]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_slavestat(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("slavestat")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    iothread_sqlthread_slaveLag = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = iothread_sqlthread_slaveLag.index('&')
    apart_second = iothread_sqlthread_slaveLag.index('&', apart + 1)
    KEY_VALUE_SET["DBInstancePerformance_iothread"] = iothread_sqlthread_slaveLag[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_sqlthread"] = iothread_sqlthread_slaveLag[apart + 1:apart_second]
    KEY_VALUE_SET["DBInstancePerformance_slaveLag"] = iothread_sqlthread_slaveLag[apart_second + 1:]
    return KEY_VALUE_SET

def DescribeDBInstancePerformance_InnoDBBufferRatio(**KEY_VALUE_SET):
    #查看实例性能数据,时间戳精确到分钟
    from aliyunsdkrds.request.v20140815 import DescribeDBInstancePerformanceRequest
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    request.set_accept_format(accept_format)
    request.set_DBInstanceId(DBInstanceId);
    request.set_StartTime(StartTime);
    request.set_EndTime(EndTime);
    request.set_Key("MySQL_InnoDBBufferRatio")

    response = clt.do_action_with_exception(request)
    json_response = json.loads(response)
    read_use_dirty = json_response["PerformanceKeys"]["PerformanceKey"][0]["Values"]["PerformanceValue"][0]["Value"]
    apart = read_use_dirty.index('&')
    apart_second = read_use_dirty.index('&', apart + 1)
    KEY_VALUE_SET["DBInstancePerformance_InnoDBReadHit"] = read_use_dirty[0:apart]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBUseRatio"] = read_use_dirty[apart + 1:apart_second]
    KEY_VALUE_SET["DBInstancePerformance_InnoDBDirtyRatio"] = read_use_dirty[apart_second + 1:]
    return KEY_VALUE_SET

def getValue(DBInstanceId):
    KEY_VALUE_SET={}
    #KEY_VALUE_SET=DescribeAbnormalDBInstances(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstanceAttribute(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeResourceUsage(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeResourceDiagnosis(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_TempDiskTableCreates(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_NetworkTraffic(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_COMDML(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_RowDML(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_MemCpuUsage(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_IOPS(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_DetailedSpaceUsage(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_slavestat(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_InnoDBBufferRatio(**KEY_VALUE_SET);
    KEY_VALUE_SET=DescribeDBInstancePerformance_Sessions(**KEY_VALUE_SET);

    DataFile = "/tmp/%s" % DBInstanceId
    fp=open(DataFile,'w')
    fp.write(json.dumps(KEY_VALUE_SET))
    fp.close()

#main##############################################
accept_format = 'json'
clt = client.AcsClient('ID12345', 'KEY12345', 'cn-qingdao')
INSTANCE_SET={}
INSTANCE_SET=DescribeDBInstances(**INSTANCE_SET);
for DBInstanceId in INSTANCE_SET.keys():
    #GMT to UTC
    now = datetime.datetime.now()-datetime.timedelta(hours=8)
    now_before = now - datetime.timedelta(seconds=300)
    EndTime = now.strftime('%Y-%m-%dT%H:%MZ')
    StartTime = now_before.strftime('%Y-%m-%dT%H:%MZ')
    getValue(DBInstanceId);

/opt/BackupShell/rds.py

#!/usr/bin/python
# coding=utf-8

#from api import *
import sys,getopt,json
KEY_SET={"DBInstancePerformance_iothread":"IO进程","DBInstancePerformance_sqlthread":"SQL进程","DBInstancePerformance_slaveLag":"从库延迟","DBInstancePerformance_InnoDBUseRatio":"利用率","DBInstancePerformance_InnoDBDirtyRatio":"脏块率","DBInstancePerformance_InnoDBReadHit":"读命中率","DBInstancePerformance_DetailedSpaceUsage_ins":"磁盘空间总体使用量MB","DBInstancePerformance_DetailedSpaceUsage_other":"系统文件空间使用量MB","DBInstancePerformance_DetailedSpaceUsage_log":"日志空间使用量MB","DBInstancePerformance_DetailedSpaceUsage_data":"数据空间使用量MB","DBInstancePerformance_DetailedSpaceUsage_tmp":"临时文件空间使用量MB","DBInstancePerformance_IOPS":"当前实例IOPS使用量","DBInstancePerformance_MemUsage":"内存使用率","DBInstancePerformance_CpuUsage":"CPU使用率","DBInstancePerformance_Row_writes":"平均每秒向日志文件的物理写次数","DBInstancePerformance_Row_update":"平均每秒从InnoDB表更新的行数","DBInstancePerformance_Row_delete":"平均每秒从InnoDB表删除的行数","DBInstancePerformance_Row_insert":"平均每秒从InnoDB表的插入行数","DBInstancePerformance_Row_readed":"平均每秒从InnoDB表读取的行数","DBInstancePerformance_COMDML__select":"平均每秒Select语句执行次数","DBInstancePerformance_COMDML__insert":"平均每秒Insert语句执行次数","DBInstancePerformance_COMDML__insertSelect":"平均每秒Insert_Select语句执行次数","DBInstancePerformance_COMDML__replace":"平均每秒Replace语句执行次数语句执行次数","DBInstancePerformance_COMDML__replaceSelect":"平均每秒Replace_Insert语句执行次数","DBInstancePerformance_COMDML__update":"平均每秒Update语句执行次数","DBInstancePerformance_COMDML__delete":"平均每秒Delete语句执行次数","DBInstancePerformance_NetworkTrafficRecv":"入网流量KBPS","DBInstancePerformance_NetworkTrafficSent":"出网流量KBPS","DBInstancePerformance_TempDiskTableCreates":"临时表创建数量","ResourceDiagnosis_CPUMax":"CPU使用率最大值","ResourceDiagnosis_CPUAvg":"CPU使用率平均值","ResourceDiagnosis_CPUMin":"CPU使用率最小值","ResourceDiagnosis_StorageMax":"硬盘使用率最大值","ResourceDiagnosis_StorageMin":"硬盘使用率平均值","ResourceDiagnosis_StorageAvg":"硬盘使用率最小值","ResourceDiagnosis_MemoryMax":"内存使用率最大值","ResourceDiagnosis_MemoryAvg":"内存使用率平均值","ResourceDiagnosis_MemoryMin":"内存使用率最小值","ResourceDiagnosis_ConnectionMax":"连接数使用率最大值","ResourceDiagnosis_ConnectionAvg":"连接数使用率平均值","ResourceDiagnosis_ConnectionMin":"连接数使用率最小值","ResourceDiagnosis_IOPSMax":"IOPS使用率最大值","ResourceDiagnosis_IOPSAvg":"IOPS使用率平均值","ResourceDiagnosis_IOPSMin":"IOPS使用率最小值","ResourceUsage_LogSize":"日志大小B","ResourceUsage_SQLSize":"SQL大小B","ResourceUsage_DiskUsed":"已使用磁盘B","ResourceUsage_ColdBackupSize":"冷备份大小B","ResourceUsage_BackupSize":"备份大小B","ResourceUsage_DataSize":"数据大小B","DBInstances_total":"RDS实例数量","DBInstances_abnormal":"异常实例数量","DBInstanceAttribute_DBInstanceId":"实例ID","DBInstanceAttribute_ZoneId":"实例区域","DBInstanceAttribute_Category":"实例类别","DBInstanceAttribute_DBInstanceType":"实例类型","DBInstanceAttribute_DBInstanceClass":"实例型号","DBInstanceAttribute_Engine":"引擎类型","DBInstanceAttribute_EngineVersion":"引擎版本","DBInstanceAttribute_ConnectionString":"连接地址","DBInstanceAttribute_Port":"连接端口","DBInstanceAttribute_CreationTime":"实例创建时间","DBInstanceAttribute_MaintainTime":"实例维护时间","DBInstanceAttribute_ExpireTime":"实例到期时间","DBInstanceAttribute_DBInstanceCPU":"实例CPU数量","DBInstanceAttribute_DBInstanceMemory":"实例总内存MB","DBInstanceAttribute_DBInstanceStorage":"实例总硬盘GB","DBInstanceAttribute_MaxConnections":"最大连接数","DBInstanceAttribute_MaxIOPS":"最大IOPS","DBInstanceAttribute_AccountType":"账号类型","DBInstanceAttribute_LockMode":"锁定状态","DBInstanceAttribute_DBInstanceNetType":"网络类型","DBInstanceAttribute_DBInstanceStatus":"实例状态","DBInstanceAttribute_AvailabilityValue":"实例可用性"}
def usage():
  print "-i DBInstanceId RDS实例ID,可省略"
  print "-k Key 监控项 "
  print "支持的监控项 说明:"
  for key in KEY_SET:
    print key,KEY_SET[key]
opts, args = getopt.getopt(sys.argv[1:], "hi:k:")
for op, value in opts:
  if op == "-i":
    DBInstanceId = value
  elif op == "-k":
    Key = value
  elif op == "-h":
    usage()
    sys.exit()
'''
if (Key not in KEY_SET.keys()):
  print "监控项未传入或未定义"
  print KEY_SET
elif
'''
DataFile = "/tmp/%s" % DBInstanceId
fp=open(DataFile)
KEY_VALUE_SET_json = fp.read()
fp.close()

KEY_VALUE_SET=json.loads(KEY_VALUE_SET_json)
print KEY_VALUE_SET[Key]

因为RDS是接口形式获取的数据,并不能安装客户端,因此需要新建host用于监控数据:

host.name:rdsxxx
host.dns  :rdsxxx

并且在zabbix_server上配置dns解析 127.0.0.1 rdsxxx

rds模板

LEAVE A COMMENT

Captcha Code