├── README.md ├── check_rds.py ├── discovery_rds.py └── zbx_AliyunRds_templates.xml /README.md: -------------------------------------------------------------------------------- 1 | # zabbix-RDS-monitor 2 | Aliyun RDS-mysql status monitor with zabbix 3 | 4 | zabbix通过阿里云api 自动发现、监控阿里云RDS-Mysql数据库   5 | 本版本数据的图表展示,是以**监控项进行分组**,后期会再发布**以rds实例分组**的版本。 6 | ## 使用方法 7 | ### 注意事项 8 | 1. 脚本会收集RDS别名, 9 | 2. 不要默认别名 10 | 3. 不要使用中文别名(zabbix不识别) 11 | 4. 切记aliyun-python-sdk-core==2.3.5,新版本的sdk有bug 12 | ### 环境要求 13 | python = 2.7 14 | ### 模块安装 15 | ```shell 16 | /usr/bin/env pip2.7 install aliyun-python-sdk-core==2.3.5 aliyun-python-sdk-rds==2.1.4 datetime 17 | ``` 18 | ### 使用方法 19 | 1. 从阿里云控制台获取 **AccessKey** ,并修改脚本中的 **ID** 与 **Secret** 20 | 2. 修改区域 **RegionId** 21 | 3. 将两个脚本放置于以下目录 22 | ```conf 23 | /etc/zabbix/script 24 | ``` 25 | ```shell 26 | chmod +x /etc/zabbix/script/* 27 | ``` 28 | 4. 修改zabbix-agentd.conf,添加以下内容 29 | ```conf 30 | #rds 31 | UserParameter=rds.discovery,/usr/bin/env python2.7 /etc/zabbix/script/discovery_rds.py 32 | UserParameter=check.rds[*],/usr/bin/env python2.7 /etc/zabbix/script/check_rds.py $1 $2 $3 33 | ``` 34 | 5. 重启zabbix-agent 35 | 6. zabbix控制台导入模板,并关联主机 36 | -------------------------------------------------------------------------------- /check_rds.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | #Auther:xwjr.com 3 | from aliyunsdkcore import client 4 | from aliyunsdkrds.request.v20140815 import DescribeResourceUsageRequest,DescribeDBInstancePerformanceRequest 5 | import json,sys,datetime 6 | 7 | ID = 'ID' 8 | Secret = 'Secret' 9 | RegionId = 'cn-shenzhen' 10 | 11 | clt = client.AcsClient(ID,Secret,RegionId) 12 | 13 | Type = sys.argv[1] 14 | DBInstanceId = sys.argv[2] 15 | Key = sys.argv[3] 16 | 17 | # 阿里云返回的数据为UTC时间,因此要转换为东八区时间。其他时区同理。 18 | UTC_End = datetime.datetime.today() - datetime.timedelta(hours=8) 19 | UTC_Start = UTC_End - datetime.timedelta(minutes=25) 20 | 21 | StartTime = datetime.datetime.strftime(UTC_Start, '%Y-%m-%dT%H:%MZ') 22 | EndTime = datetime.datetime.strftime(UTC_End, '%Y-%m-%dT%H:%MZ') 23 | 24 | def GetResourceUsage(DBInstanceId,Key): 25 | ResourceUsage = DescribeResourceUsageRequest.DescribeResourceUsageRequest() 26 | ResourceUsage.set_accept_format('json') 27 | ResourceUsage.set_DBInstanceId(DBInstanceId) 28 | ResourceUsageInfo = clt.do_action_with_exception(ResourceUsage) 29 | #print ResourceUsageInfo 30 | Info = (json.loads(ResourceUsageInfo))[Key] 31 | print(Info) 32 | 33 | def GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime): 34 | Performance = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest() 35 | Performance.set_accept_format('json') 36 | Performance.set_DBInstanceId(DBInstanceId) 37 | Performance.set_Key(MasterKey) 38 | Performance.set_StartTime(StartTime) 39 | Performance.set_EndTime(EndTime) 40 | PerformanceInfo = clt.do_action_with_exception(Performance) 41 | #print(PerformanceInfo 42 | Info = (json.loads(PerformanceInfo)) 43 | Value = Info['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue'][-1]['Value'] 44 | print(str(Value).split('&')[IndexNum]) 45 | 46 | 47 | if (Type == "Disk"): 48 | GetResourceUsage(DBInstanceId, Key) 49 | 50 | elif (Type == "Performance"): 51 | 52 | #平均每秒钟的输入流量 53 | if (Key == "MySQL_NetworkTraffic_In"): 54 | IndexNum = 0 55 | MasterKey = "MySQL_NetworkTraffic" 56 | GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime) 57 | 58 | #平均每秒钟的输出流量 59 | elif (Key == "MySQL_NetworkTraffic_Out"): 60 | IndexNum = 1 61 | MasterKey = "MySQL_NetworkTraffic" 62 | GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime) 63 | 64 | #每秒SQL语句执行次数 65 | elif (Key == "MySQL_QPS"): 66 | IndexNum = 0 67 | MasterKey = "MySQL_QPSTPS" 68 | GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime) 69 | 70 | #平均每秒事务数 71 | elif (Key == "MySQL_TPS"): 72 | IndexNum = 1 73 | MasterKey = "MySQL_QPSTPS" 74 | GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime) 75 | 76 | #当前活跃连接数 77 | elif (Key == "MySQL_Sessions_Active"): 78 | MasterKey = "MySQL_Sessions" 79 | IndexNum = 0 80 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 81 | 82 | #当前总连接数 83 | elif (Key == "MySQL_Sessions_Totle"): 84 | MasterKey = "MySQL_Sessions" 85 | IndexNum = 1 86 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 87 | 88 | #InnoDB缓冲池的读命中率 89 | elif (Key == "ibuf_read_hit"): 90 | MasterKey = "MySQL_InnoDBBufferRatio" 91 | IndexNum = 0 92 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 93 | 94 | #InnoDB缓冲池的利用率 95 | elif (Key == "ibuf_use_ratio"): 96 | MasterKey = "MySQL_InnoDBBufferRatio" 97 | IndexNum = 1 98 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 99 | 100 | #InnoDB缓冲池脏块的百分率 101 | elif (Key == "ibuf_dirty_ratio"): 102 | MasterKey = "MySQL_InnoDBBufferRatio" 103 | IndexNum = 2 104 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 105 | 106 | #InnoDB平均每秒钟读取的数据量 107 | elif (Key == "inno_data_read"): 108 | MasterKey = "MySQL_InnoDBDataReadWriten" 109 | IndexNum = 0 110 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 111 | 112 | #InnoDB平均每秒钟写入的数据量 113 | elif (Key == "inno_data_written"): 114 | MasterKey = "MySQL_InnoDBDataReadWriten" 115 | IndexNum = 1 116 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 117 | 118 | #平均每秒向InnoDB缓冲池的读次数 119 | elif (Key == "ibuf_request_r"): 120 | MasterKey = "MySQL_InnoDBLogRequests" 121 | IndexNum = 0 122 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 123 | 124 | #平均每秒向InnoDB缓冲池的写次数 125 | elif (Key == "ibuf_request_w"): 126 | MasterKey = "MySQL_InnoDBLogRequests" 127 | IndexNum = 1 128 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 129 | 130 | #平均每秒日志写请求数 131 | elif (Key == "Innodb_log_write_requests"): 132 | MasterKey = "MySQL_InnoDBLogWrites" 133 | IndexNum = 0 134 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 135 | 136 | #平均每秒向日志文件的物理写次数 137 | elif (Key == "Innodb_log_writes"): 138 | MasterKey = "MySQL_InnoDBLogWrites" 139 | IndexNum = 1 140 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 141 | 142 | #平均每秒向日志文件完成的fsync()写数量 143 | elif (Key == "Innodb_os_log_fsyncs"): 144 | MasterKey = "MySQL_InnoDBLogWrites" 145 | IndexNum = 2 146 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 147 | 148 | #MySQL执行语句时在硬盘上自动创建的临时表的数量 149 | elif (Key == "tb_tmp_disk"): 150 | MasterKey = "MySQL_TempDiskTableCreates" 151 | IndexNum = 0 152 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 153 | 154 | #MyISAM平均每秒Key Buffer利用率 155 | elif (Key == "Key_usage_ratio"): 156 | MasterKey = "MySQL_MyISAMKeyBufferRatio" 157 | IndexNum = 0 158 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 159 | 160 | #MyISAM平均每秒Key Buffer读命中率 161 | elif (Key == "Key_read_hit_ratio"): 162 | MasterKey = "MySQL_MyISAMKeyBufferRatio" 163 | IndexNum = 1 164 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 165 | 166 | #MyISAM平均每秒Key Buffer写命中率 167 | elif (Key == "Key_write_hit_ratio"): 168 | MasterKey = "MySQL_MyISAMKeyBufferRatio" 169 | IndexNum = 2 170 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 171 | 172 | #MyISAM平均每秒钟从缓冲池中的读取次数 173 | elif (Key == "myisam_keyr_r"): 174 | MasterKey = "MySQL_MyISAMKeyReadWrites" 175 | IndexNum = 0 176 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 177 | 178 | #MyISAM平均每秒钟从缓冲池中的写入次数 179 | elif (Key == "myisam_keyr_w"): 180 | MasterKey = "MySQL_MyISAMKeyReadWrites" 181 | IndexNum = 1 182 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 183 | 184 | #MyISAM平均每秒钟从硬盘上读取的次数 185 | elif (Key == "myisam_keyr"): 186 | MasterKey = "MySQL_MyISAMKeyReadWrites" 187 | IndexNum = 2 188 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 189 | 190 | #MyISAM平均每秒钟从硬盘上写入的次数 191 | elif (Key == "myisam_keyw"): 192 | MasterKey = "MySQL_MyISAMKeyReadWrites" 193 | IndexNum = 3 194 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 195 | 196 | #平均每秒Delete语句执行次数 197 | elif (Key == "com_delete"): 198 | MasterKey = "MySQL_COMDML" 199 | IndexNum = 0 200 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 201 | 202 | #平均每秒Insert语句执行次数 203 | elif (Key == "com_insert"): 204 | MasterKey = "MySQL_COMDML" 205 | IndexNum = 1 206 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 207 | 208 | #平均每秒Insert_Select语句执行次数 209 | elif (Key == "com_insert_select"): 210 | MasterKey = "MySQL_COMDML" 211 | IndexNum = 2 212 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 213 | 214 | #平均每秒Replace语句执行次数 215 | elif (Key == "com_replace"): 216 | MasterKey = "MySQL_COMDML" 217 | IndexNum = 3 218 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 219 | 220 | #平均每秒Replace_Select语句执行次数 221 | elif (Key == "com_replace_select"): 222 | MasterKey = "MySQL_COMDML" 223 | IndexNum = 4 224 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 225 | 226 | #平均每秒Select语句执行次数 227 | elif (Key == "com_select"): 228 | MasterKey = "MySQL_COMDML" 229 | IndexNum = 5 230 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 231 | 232 | #平均每秒Update语句执行次数 233 | elif (Key == "com_update"): 234 | MasterKey = "MySQL_COMDML" 235 | IndexNum = 6 236 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 237 | 238 | #平均每秒从InnoDB表读取的行数 239 | elif (Key == "inno_row_readed"): 240 | MasterKey = "MySQL_RowDML" 241 | IndexNum = 0 242 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 243 | 244 | #平均每秒从InnoDB表更新的行数 245 | elif (Key == "inno_row_update"): 246 | MasterKey = "MySQL_RowDML" 247 | IndexNum = 1 248 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 249 | 250 | #平均每秒从InnoDB表删除的行数 251 | elif (Key == "inno_row_delete"): 252 | MasterKey = "MySQL_RowDML" 253 | IndexNum = 2 254 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 255 | 256 | #平均每秒从InnoDB表插入的行数 257 | elif (Key == "inno_row_insert"): 258 | MasterKey = "MySQL_RowDML" 259 | IndexNum = 3 260 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 261 | 262 | #平均每秒向日志文件的物理写次数 263 | elif (Key == "Inno_log_writes"): 264 | MasterKey = "MySQL_RowDML" 265 | IndexNum = 4 266 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 267 | 268 | #MySQL实例CPU使用率(占操作系统总数) 269 | elif (Key == "cpuusage"): 270 | MasterKey = "MySQL_MemCpuUsage" 271 | IndexNum = 0 272 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 273 | 274 | #MySQL实例内存使用率(占操作系统总数) 275 | elif (Key == "memusage"): 276 | MasterKey = "MySQL_MemCpuUsage" 277 | IndexNum = 1 278 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 279 | 280 | #MySQL实例的IOPS(每秒IO请求次数) 281 | elif (Key == "io"): 282 | MasterKey = "MySQL_IOPS" 283 | IndexNum = 0 284 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 285 | 286 | #ins_size实例总空间使用量 287 | elif (Key == "ins_size"): 288 | MasterKey = "MySQL_DetailedSpaceUsage" 289 | IndexNum = 0 290 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 291 | 292 | #data_size数据空间 293 | elif (Key == "data_size"): 294 | MasterKey = "MySQL_DetailedSpaceUsage" 295 | IndexNum = 1 296 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 297 | 298 | #log_size日志空间 299 | elif (Key == "log_size"): 300 | MasterKey = "MySQL_DetailedSpaceUsage" 301 | IndexNum = 2 302 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 303 | 304 | #tmp_size临时空间 305 | elif (Key == "tmp_size"): 306 | MasterKey = "MySQL_DetailedSpaceUsage" 307 | IndexNum = 3 308 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 309 | 310 | #other_size系统空间 311 | elif (Key == "other_size"): 312 | MasterKey = "MySQL_DetailedSpaceUsage" 313 | IndexNum = 4 314 | GetPerformance(DBInstanceId, MasterKey, IndexNum, StartTime, EndTime) 315 | -------------------------------------------------------------------------------- /discovery_rds.py: -------------------------------------------------------------------------------- 1 | #coding=UTF-8 2 | #Auther:xwjr.com 3 | from aliyunsdkcore import client 4 | from aliyunsdkrds.request.v20140815 import DescribeDBInstancesRequest 5 | import json 6 | 7 | ID = 'ID' 8 | Secret = 'Secret' 9 | RegionId = 'cn-shenzhen' 10 | 11 | clt = client.AcsClient(ID,Secret,RegionId) 12 | 13 | DBInstanceIdList = [] 14 | DBInstanceIdDict = {} 15 | ZabbixDataDict = {} 16 | def GetRdsList(): 17 | RdsRequest = DescribeDBInstancesRequest.DescribeDBInstancesRequest() 18 | RdsRequest.set_accept_format('json') 19 | #RdsInfo = clt.do_action(RdsRequest) 20 | RdsInfo = clt.do_action_with_exception(RdsRequest) 21 | for RdsInfoJson in (json.loads(RdsInfo))['Items']['DBInstance']: 22 | DBInstanceIdDict = {} 23 | try: 24 | DBInstanceIdDict["{#DBINSTANCEID}"] = RdsInfoJson['DBInstanceId'] 25 | DBInstanceIdDict["{#DBINSTANCEDESCRIPTION}"] = RdsInfoJson['DBInstanceDescription'] 26 | DBInstanceIdList.append(DBInstanceIdDict) 27 | except Exception, e: 28 | print(Exception, ":", e) 29 | print("Please check the RDS alias !Alias must not be the same as DBInstanceId!!!") 30 | 31 | 32 | 33 | GetRdsList() 34 | ZabbixDataDict['data'] = DBInstanceIdList 35 | print(json.dumps(ZabbixDataDict)) 36 | -------------------------------------------------------------------------------- /zbx_AliyunRds_templates.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 3.0 4 | 2017-08-04T08:50:48Z 5 | 6 | 7 | 模板汇总(201707更新) 8 | 9 | 10 | 11 | 2802 | 2803 | 2804 | --------------------------------------------------------------------------------