Zabbix 5.0:監控阿里雲RDS

Blog:部落格園 個人
由於近期壓測,需要頻繁登錄阿里雲查看RDS監控,每次登錄查看監控步驟較為繁瑣,故將監控接入到zabbix。

概述

由於阿里雲已做了RDS的監控,我們只需要通過阿里雲SDK把這些監控數據傳輸到zabbix即可。

前提

子帳號

使用阿里雲SDK需要一個AK,申請方式如下:

image-20211028172353976

  • 得到AccessKey IDAccessKey Secret;

image-20211028172500213

  • 添加AliyunCloudMonitorReadOnlyAccess許可權;

環境

  • Python版本:Python3
  • SDK:aliyun-python-sdk-core、aliyun-python-sdk-cms

調用Api示例

Api:DescribeMetricTop

  • Namespaceacs_rds_dashboard
  • Period默認為60秒,也可以為60的整數倍。

當前雲產品的MetricNameDimensions的取值如下表所示。

監控項 單位 MetricName Dimensions Statistics
連接數使用率 % ConnectionUsage userId、instanceId Maximum、Minimum、Average
CPU使用率 % CpuUsage userId、instanceId Maximum、Minimum、Average
只讀實例延遲 Second DataDelay userId、instanceId Maximum、Minimum、Average
磁碟使用率 % DiskUsage userId、instanceId Maximum、Minimum、Average
IOPS使用率 % IOPSUsage userId、instanceId Maximum、Minimum、Average
記憶體使用率 % MemoryUsage userId、instanceId Maximum、Minimum、Average
MySQL_ActiveSessions Count MySQL_ActiveSessions userId、instanceId Maximum、Minimum、Average
MySQL每秒Delete量 CountSecond MySQL_ComDelete userId、instanceId Maximum、Minimum、Average
MySQL每秒Insert量 CountSecond MySQL_ComInsert userId、instanceId Maximum、Minimum、Average
MySQL每秒InsertSelect量 CountSecond MySQL_ComInsertSelect userId、instanceId Maximum、Minimum、Average
MySQL每秒Replace量 CountSecond MySQL_ComReplace userId、instanceId Maximum、Minimum、Average
MySQL每秒ReplaceSelect量 CountSecond MySQL_ComReplaceSelect userId、instanceId Maximum、Minimum、Average
MySQL每秒Select量 CountSecond MySQL_ComSelect userId、instanceId Maximum、Minimum、Average
MySQL每秒Update量 CountSecond MySQL_ComUpdate userId、instanceId Maximum、Minimum、Average
MySQL每秒邏輯讀次數 CountSecond MySQL_IbufRequestR userId、instanceId Maximum、Minimum、Average
MySQL每秒邏輯寫次數 CountSecond MySQL_IbufRequestW userId、instanceId Maximum、Minimum、Average
MySQL每秒查詢量 CountSecond MySQL_QPS userId、instanceId Maximum、Minimum、Average
MySQL每秒事務數 CountSecond MySQL_TPS userId、instanceId Maximum、Minimum、Average
SQLServer網路流入頻寬 bit/s SQLServer_NetworkInNew userId、instanceId Maximum、Minimum、Average
SQLServer網路流出頻寬 bit/s SQLServer_NetworkOutNew userId、instanceId Maximum、Minimum、Average
MySQL網路流入頻寬 bit/s MySQL_NetworkInNew userId、instanceId Average、Minimum、Maximum
MySQL網路流出頻寬 bit/s MySQL_NetworkOutNew userId、instanceId Average、Minimum、Maximum
MySQL_BP臟頁百分率 % MySQL_IbufDirtyRatio userId、instanceId Average、Maximum、Minimum
MySQL_BP利用率 % MySQL_IbufUseRatio userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒讀取數據量 KByte MySQL_InnoDBDataRead userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒寫入數據量 KByte MySQL_InnoDBDataWritten userId、instanceId Average、Maximum、Minimum
MySQL每秒創建臨時表數量 CountSecond MySQL_TempDiskTableCreates userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒更新行數 CountSecond MySQL_InnoDBRowUpdate userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒插入行數 CountSecond MySQL_InnoDBRowInsert userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒刪除行數 CountSecond MySQL_InnoDBRowDelete userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒讀取行數 CountSecond MySQL_InnoDBRowRead userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒日誌fsync量 CountSecond MySQL_InnoDBLogFsync userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒日誌物理寫次數 CountSecond MySQL_InnoDBLogWrites userId、instanceId Average、Maximum、Minimum
MySQL_InnoDB每秒日誌寫請求次數 CountSecond MySQL_InnoDBLogWriteRequests userId、instanceId Average、Maximum、Minimum
MySQL每秒慢查詢量 CountSecond MySQL_SlowQueries userId、instanceId Average、Maximum、Minimum
MySQL_BP讀命中率 % MySQL_IbufReadHit userId、instanceId Average、Maximum、Minimum
MySQL每秒物理讀次數 CountSecond MySQL_ibufPoolReads userId、instanceId Average、Maximum、Minimum
MySQL_數據磁碟使用量 MB MySQL_DataDiskSize userId、instanceId Average、Maximum、Minimum
MySQL_實例磁碟使用量 MB MySQL_InstanceDiskSize userId、instanceId Average、Maximum、Minimum
MySQL_日誌磁碟使用量 MB MySQL_LogDiskSize userId、instanceId Average、Maximum、Minimum
MySQL_其他磁碟使用量 MB MySQL_OtherDiskSize userId、instanceId Average、Maximum、Minimum
MySQL_臨時磁碟使用量 MB MySQL_TmpDiskSize userId、instanceId Average、Maximum、Minimum
MySQL_ProxyCpu使用率 % MySQL_ProxyCpuUsage userId、instanceId Average、Maximum、Minimum
只讀實例IO執行緒狀態 Value MySQL_SlaveIORunning userId、instanceId Average、Maximum、Minimum
只讀實例SQL執行緒狀態 Value MySQL_SlaveSQLRunning userId、instanceId Average、Maximum、Minimum
MySQL_執行緒連接數 Count MySQL_ThreadsConnected userId、instanceId Average、Maximum、Minimum
MySQL_活躍執行緒數 Count MySQL_ThreadsRunning userId、instanceId Average、Maximum、Minimum

腳本示例:

#!/usr/bin/env python3
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkcms.request.v20190101.DescribeMetricTopRequest import DescribeMetricTopRequest

credentials = AccessKeyCredential('<your-access-key-id>', '<your-access-key-secret>')
# use STS Token
# credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
client = AcsClient(region_id='cn-hangzhou', credential=credentials)

request = DescribeMetricTopRequest()
request.set_accept_format('json')

request.set_Namespace("acs_rds_dashboard")
request.set_MetricName("CpuUsage")
request.set_Orderby("Maximum")
request.set_Dimensions("[{\"instanceId\":\"rm-b*****qc\"}]")

response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))

輸出結果如下:

{
  "RequestId": "D0237455-0978-532A-AG95-C1DG8CD83346",
  "Period": "60",
  "Datapoints": "[{\"order\":1,\"timestamp\":1635413400000,\"userId\":\"1862599732974435\",\"instanceId\":\"rm-b*****c\",\"Maximum\":14.95,\"Minimum\":14.95,\"Average\":14.95,\"_count\":1.0}]",
  "Code": "200"
}

配置模板

通過以上示例,很容易通過腳本得到相關監控數據。

腳本完成後,再導入模板,監控項如下:

image-20211028173859926

查看監控數據:

image-20211028174021744

監控圖如下:

image-20211028174828895