yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/cloudprovider/metrics.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cloudprovider 16 17 import ( 18 "strings" 19 "time" 20 ) 21 22 type TResourceType string 23 type TMetricType string 24 25 func (key TMetricType) Name() string { 26 if !strings.Contains(string(key), ".") { 27 return string(key) 28 } 29 return string(key)[0:strings.Index(string(key), ".")] 30 } 31 32 func (key TMetricType) Key() string { 33 if len(key) == 0 { 34 return "" 35 } 36 first, last := 0, len(key) 37 if strings.Contains(string(key), ",") { 38 last = strings.Index(string(key), ",") 39 } 40 if strings.Contains(string(key), ".") { 41 first = strings.LastIndex(string(key), ".") + 1 42 } 43 return string(key)[first:last] 44 } 45 46 const ( 47 METRIC_RESOURCE_TYPE_RDS TResourceType = "rds" 48 METRIC_RESOURCE_TYPE_SERVER TResourceType = "server" 49 METRIC_RESOURCE_TYPE_HOST TResourceType = "host" 50 METRIC_RESOURCE_TYPE_REDIS TResourceType = "redis" 51 METRIC_RESOURCE_TYPE_LB TResourceType = "lb" 52 METRIC_RESOURCE_TYPE_BUCKET TResourceType = "bucket" 53 METRIC_RESOURCE_TYPE_K8S TResourceType = "k8s" 54 METRIC_RESOURCE_TYPE_STORAGE TResourceType = "storage" 55 METRIC_RESOURCE_TYPE_CLOUD_ACCOUNT TResourceType = "cloudaccount_balance" 56 METRIC_RESOURCE_TYPE_MODELARTS_POOL TResourceType = "modelarts" 57 ) 58 59 const ( 60 // RDS监控指标 61 62 // RDS CPU利用率 63 // 支持的平台: huawei, aliyun, apsara, azure, jdcloud, qcloud, aws 64 // 仅azure的sqlserver支持group_by = database 65 RDS_METRIC_TYPE_CPU_USAGE TMetricType = "rds_cpu.usage_active" 66 // RDS 内存利用率 67 // 支持平台: huawei, aliyun, apsara, azure, jdcloud, qcloud 68 // 仅azure的sqlserver支持group_by = database 69 RDS_METRIC_TYPE_MEM_USAGE TMetricType = "rds_mem.used_percent" 70 // RDS 网络入流量 71 // 支持平台: huawei, aliyun, apsara, azure, aws, jdcloud, qcloud 72 // 仅azure的sqlserver支持group_by = database 73 RDS_METRIC_TYPE_NET_BPS_RX TMetricType = "rds_netio.bps_recv" 74 // RDS 网络出流量 75 // 支持平台: huawei, aliyun, apsara, azure, aws, jdcloud, qcloud 76 // 仅azure的sqlserver支持group_by = database 77 RDS_METRIC_TYPE_NET_BPS_TX TMetricType = "rds_netio.bps_sent" 78 79 // RDS磁盘使用率 80 // 支持平台: huawei, aliyun, apsara, azure, jdcloud, qcloud 81 // 仅azure的sqlserver支持group_by = database 82 RDS_METRIC_TYPE_DISK_USAGE TMetricType = "rds_disk.used_percent" 83 // RDS磁盘读取IO 84 // 支持平台: huawei 85 RDS_METRIC_TYPE_DISK_READ_BPS TMetricType = "rds_diskio.read_bps" 86 // RDS磁盘写IO 87 // 支持平台: huawei 88 RDS_METRIC_TYPE_DISK_WRITE_BPS TMetricType = "rds_diskio.write_bps" 89 // --- 90 // 支持平台: azure 91 RDS_METRIC_TYPE_DISK_IO_PERCENT TMetricType = "rds_diskio.used_percent" 92 93 // RDS 连接数 94 // 支持平台: huawei, aws, qcloud 95 RDS_METRIC_TYPE_CONN_COUNT TMetricType = "rds_conn.used_count" 96 // RDS 活跃连接数 97 // 支持平台: azure 98 RDS_METRIC_TYPE_CONN_ACTIVE TMetricType = "rds_conn.active_count" 99 // 连接数使用率 100 // 支持平台: aliyun, apsara, qcloud 101 RDS_METRIC_TYPE_CONN_USAGE TMetricType = "rds_conn.used_percent" 102 // 支持平台: azure 103 // 失败连接数 104 RDS_METRIC_TYPE_CONN_FAILED TMetricType = "rds_conn.failed_count" 105 106 METRIC_TAG_DATABASE = "database" 107 108 // RDS QPS 109 // 支持平台: huawei, qcloud 110 RDS_METRIC_TYPE_QPS TMetricType = "rds_qps.query_qps" 111 // RDS TPS 112 // 支持平台: huawei, qcloud 113 RDS_METRIC_TYPE_TPS TMetricType = "rds_tps.trans_qps" 114 // RDS innodb读IO 115 // 支持平台: huawei, qcloud 116 RDS_METRIC_TYPE_INNODB_READ_BPS TMetricType = "rds_innodb.read_bps" 117 // RDS innodb写IO 118 // 支持平台 huawei, qcloud 119 RDS_METRIC_TYPE_INNODB_WRITE_BPS TMetricType = "rds_innodb.write_bps" 120 121 // 虚拟机CPU使用率 122 // 支持平台: kvm, huawei, aliyun, apsara, azure, esxi, google, bingocloud, aws, jdcloud, ecloud, zstack, qcloud 123 VM_METRIC_TYPE_CPU_USAGE TMetricType = "vm_cpu.usage_active" 124 // 虚拟机内存使用率 125 // 支持平台: kvm, aliyun, apsara, azure, esxi, bingocloud, jdcloud, ecloud, qcloud 126 VM_METRIC_TYPE_MEM_USAGE TMetricType = "vm_mem.used_percent" 127 // 虚拟机磁盘使用率 128 // 支持平台: aliyun, apsara, jdcloud, azure 129 // 支持按盘符(group_by=device)平台: aliyun, apsara 130 VM_METRIC_TYPE_DISK_USAGE TMetricType = "vm_disk.used_percent" 131 132 // 虚拟机磁盘读速率 133 // 支持平台: huawei, aliyun, apsara, azure, esxi, google, bingocloud, aws, jdcloud, ecloud, zstack 134 VM_METRIC_TYPE_DISK_IO_READ_BPS TMetricType = "vm_diskio.read_bps" 135 // 虚拟机磁盘写速率 136 // 支持平台: huawei, aliyun, apsara, azure, esxi, google, bingocloud, aws, jdcloud, ecloud, zstack 137 VM_METRIC_TYPE_DISK_IO_WRITE_BPS TMetricType = "vm_diskio.write_bps" 138 // 虚拟机磁盘读IOPS 139 // 支持平台: huawei, aliyun, apsara, azure, google, bingocloud, aws, jdcloud, ecloud, zstack 140 VM_METRIC_TYPE_DISK_IO_READ_IOPS TMetricType = "vm_diskio.read_iops" 141 // 虚拟机磁盘写IOPS 142 // 支持平台: huawei, aliyun, apsara, azure, google, bingocloud, aws, jdcloud, ecloud, zstack 143 VM_METRIC_TYPE_DISK_IO_WRITE_IOPS TMetricType = "vm_diskio.write_iops" 144 145 // 虚拟机网络入速率 146 // 支持平台: huawei, aliyun, apsara, azure, esxi, google, bingocloud, aws, jdcloud, ecloud, zstack, qcloud 147 VM_METRIC_TYPE_NET_BPS_RX TMetricType = "vm_netio.bps_recv" 148 // 虚拟机网络出速率 149 // 支持平台: huawei, aliyun, apsara, azure, esxi, google, bingocloud, aws, jdcloud, ecloud, zstack, qcloud 150 VM_METRIC_TYPE_NET_BPS_TX TMetricType = "vm_netio.bps_sent" 151 152 // 宿主机CPU使用率 153 // 支持平台: esxi 154 HOST_METRIC_TYPE_CPU_USAGE TMetricType = "cpu.usage_active" 155 // 宿主机内存使用率 156 // 支持平台: esxi 157 HOST_METRIC_TYPE_MEM_USAGE TMetricType = "mem.used_percent" 158 // 宿主机磁盘读速率 159 // 支持平台: esxi 160 HOST_METRIC_TYPE_DISK_IO_READ_BPS TMetricType = "diskio.read_bps" 161 // 宿主机磁盘写速率 162 // 支持平台: esxi 163 HOST_METRIC_TYPE_DISK_IO_WRITE_BPS TMetricType = "diskio.write_bps" 164 // 宿主机网络入速率 165 // 支持平台: esxi 166 HOST_METRIC_TYPE_NET_BPS_RX TMetricType = "net.bps_recv" 167 // 宿主机网络出速率 168 // 支持平台: esxi 169 HOST_METRIC_TYPE_NET_BPS_TX TMetricType = "net.bps_sent" 170 171 // Redis CPU使用率 172 // 支持平台: huawei, aliyun, azure, apsara, aws, qcloud 173 REDIS_METRIC_TYPE_CPU_USAGE TMetricType = "dcs_cpu.usage_active" 174 // Redis 内存使用率 175 // 支持平台: huawei, aliyun, azure, apsara, qcloud 176 REDIS_METRIC_TYPE_MEM_USAGE TMetricType = "dcs_mem.used_percent" 177 // Redis 网络入速率 178 // 支持平台: huawei, aliyun, apsara, qcloud 179 REDIS_METRIC_TYPE_NET_BPS_RX TMetricType = "dcs_netio.bps_recv" 180 // Redis 网络出速率 181 // 支持平台: huawei, aliyun, apsara, qcloud 182 REDIS_METRIC_TYPE_NET_BPS_TX TMetricType = "dcs_netio.bps_sent" 183 // Redis 网络连接数 184 // 支持平台: huawei, aliyun, apsara, azure, aws, qcloud 185 REDIS_METRIC_TYPE_USED_CONN TMetricType = "dcs_conn.used_conn" 186 // 每秒处理指令数 187 // 支持平台: huawei, aliyun, apsara, azure, qcloud 188 REDIS_METRIC_TYPE_OPT_SES TMetricType = "dcs_instantopt.opt_sec" 189 // 命中key数量 190 // 支持平台: huawei, aliyun, apsara, azure, qcloud 191 REDIS_METRIC_TYPE_CACHE_KEYS TMetricType = "dcs_cachekeys.key_count" 192 // Expired Key数量 193 // 支持平台: huawei, aliyun, apsara, azure, aws, qcloud 194 REDIS_METRIC_TYPE_CACHE_EXP_KEYS TMetricType = "dcs_cachekeys.expire_key_count" 195 // 内存使用量 196 // 支持平台: huawei, aliyun, azure, apsara, qcloud 197 REDIS_METRIC_TYPE_DATA_MEM_USAGE TMetricType = "dcs_datamem.used_byte" 198 199 // 支持平台: azure 200 LB_METRIC_TYPE_SNAT_PORT TMetricType = "haproxy.used_snat_port" 201 // 支持平台: azure 202 LB_METRIC_TYPE_SNAT_CONN_COUNT TMetricType = "haproxy.snat_conn_count" 203 // 入速率 204 // 支持平台: huawei, aliyun, apsara 205 LB_METRIC_TYPE_NET_BPS_RX TMetricType = "haproxy.bin" 206 // 出速率 207 // 支持平台: huawei, aliyun, apsara 208 LB_METRIC_TYPE_NET_BPS_TX TMetricType = "haproxy.bout" 209 // 状态码统计 210 // 支持平台: huawei, aliyun, apsara 211 LB_METRIC_TYPE_HRSP_COUNT TMetricType = "haproxy.hrsp_Nxx" 212 213 // 对象存储出速率 214 // 支持平台: huawei, aliyun, apsara 215 BUCKET_METRIC_TYPE_NET_BPS_TX TMetricType = "oss_netio.bps_sent" 216 // 对象存储入速率 217 // 支持平台: huawei, aliyun, apsara 218 BUCKET_METRIC_TYPE_NET_BPS_RX TMetricType = "oss_netio.bps_recv" 219 // 请求延时 220 // 支持平台: huawei, aliyun, apsara 221 BUCKET_METRIC_TYPE_LATECY TMetricType = "oss_latency.req_late" 222 // 请求数量 223 // 支持平台: huawei, aliyun, apsara 224 BUCKET_METRYC_TYPE_REQ_COUNT TMetricType = "oss_req.req_count" 225 226 METRIC_TAG_REQUST = "request" 227 METRIC_TAG_REQUST_GET = "get" 228 METRIC_TAG_REQUST_POST = "post" 229 METRIC_TAG_REQUST_2XX = "2xx" 230 METRIC_TAG_REQUST_3XX = "3xx" 231 METRIC_TAG_REQUST_4XX = "4xx" 232 METRIC_TAG_REQUST_5XX = "5xx" 233 234 METRIC_TAG_NET_TYPE = "net_type" 235 METRIC_TAG_NET_TYPE_INTERNET = "internet" 236 METRIC_TAG_NET_TYPE_INTRANET = "intranet" 237 238 METRIC_TAG_TYPE_DISK_TYPE = "disk_type" 239 METRIC_TAG_TYPE_DISK_TYPE_EBS = "ebs" 240 241 // 磁盘利用率 242 METRIC_TAG_DEVICE = "device" 243 244 METRIC_TAG_NODE = "node" 245 246 // k8s节点CPU使用率 247 // 支持平台: aliyun, azure, qcloud 248 K8S_NODE_METRIC_TYPE_CPU_USAGE TMetricType = "k8s_node_cpu.usage_active" 249 // k8s节点内存使用率 250 // 支持平台: aliyun, azure, qcloud 251 K8S_NODE_METRIC_TYPE_MEM_USAGE TMetricType = "k8s_node_mem.used_percent" 252 253 K8S_NODE_METRIC_TYPE_DISK_USAGE TMetricType = "k8s_node_disk.used_percent" 254 K8S_NODE_METRIC_TYPE_NET_BPS_RX TMetricType = "k8s_node_netio.bps_recv" 255 K8S_NODE_METRIC_TYPE_NET_BPS_TX TMetricType = "k8s_node_netio.bps_sent" 256 257 // modelarts专属资源池监控数据 258 MODELARTS_POOL_METRIC_TYPE_CPU_USAGE TMetricType = "modelarts_pool_cpu.usage_percent" 259 MODELARTS_POOL_METRIC_TYPE_MEM_USAGE TMetricType = "modelarts_pool_mem.usage_percent" 260 MODELARTS_POOL_METRIC_TYPE_GPU_MEM_USAGE TMetricType = "modelarts_pool_gpu_mem.usage_percent" 261 MODELARTS_POOL_METRIC_TYPE_GPU_UTIL TMetricType = "modelarts_pool_gpu_util.percent" 262 MODELARTS_POOL_METRIC_TYPE_NPU_UTIL TMetricType = "modelarts_pool_npu_util.percent" 263 MODELARTS_POOL_METRIC_TYPE_NPU_MEM_USAGE TMetricType = "modelarts_pool_npu_mem.usage_percent" 264 265 //磁盘可用容量 266 MODELARTS_POOL_METRIC_TYPE_DISK_AVAILABLE_CAPACITY TMetricType = "modelarts_pool_disk.available_capacity" 267 MODELARTS_POOL_METRIC_TYPE_DISK_CAPACITY TMetricType = "modelarts_pool_disk.capacity" 268 MODELARTS_POOL_METRIC_TYPE_DISK_USAGE TMetricType = "modelarts_pool_disk.usage_percent" 269 ) 270 271 var ( 272 ALL_RDS_METRIC_TYPES = []TMetricType{ 273 RDS_METRIC_TYPE_CPU_USAGE, 274 RDS_METRIC_TYPE_MEM_USAGE, 275 RDS_METRIC_TYPE_NET_BPS_RX, 276 RDS_METRIC_TYPE_NET_BPS_TX, 277 278 RDS_METRIC_TYPE_DISK_USAGE, 279 RDS_METRIC_TYPE_DISK_READ_BPS, 280 RDS_METRIC_TYPE_DISK_WRITE_BPS, 281 RDS_METRIC_TYPE_DISK_IO_PERCENT, 282 283 RDS_METRIC_TYPE_CONN_COUNT, 284 RDS_METRIC_TYPE_CONN_ACTIVE, 285 RDS_METRIC_TYPE_CONN_USAGE, 286 RDS_METRIC_TYPE_CONN_FAILED, 287 288 RDS_METRIC_TYPE_QPS, 289 RDS_METRIC_TYPE_TPS, 290 RDS_METRIC_TYPE_INNODB_READ_BPS, 291 RDS_METRIC_TYPE_INNODB_WRITE_BPS, 292 } 293 294 ALL_HOST_METRIC_TYPES = []TMetricType{ 295 HOST_METRIC_TYPE_CPU_USAGE, 296 HOST_METRIC_TYPE_MEM_USAGE, 297 HOST_METRIC_TYPE_DISK_IO_READ_BPS, 298 HOST_METRIC_TYPE_DISK_IO_WRITE_BPS, 299 HOST_METRIC_TYPE_NET_BPS_RX, 300 HOST_METRIC_TYPE_NET_BPS_TX, 301 } 302 303 ALL_VM_METRIC_TYPES = []TMetricType{ 304 VM_METRIC_TYPE_CPU_USAGE, 305 VM_METRIC_TYPE_MEM_USAGE, 306 VM_METRIC_TYPE_DISK_USAGE, 307 308 VM_METRIC_TYPE_DISK_IO_READ_BPS, 309 VM_METRIC_TYPE_DISK_IO_WRITE_BPS, 310 VM_METRIC_TYPE_DISK_IO_READ_IOPS, 311 VM_METRIC_TYPE_DISK_IO_WRITE_IOPS, 312 313 VM_METRIC_TYPE_NET_BPS_RX, 314 VM_METRIC_TYPE_NET_BPS_TX, 315 } 316 317 ALL_REDIS_METRIC_TYPES = []TMetricType{ 318 REDIS_METRIC_TYPE_CPU_USAGE, 319 REDIS_METRIC_TYPE_MEM_USAGE, 320 REDIS_METRIC_TYPE_NET_BPS_RX, 321 REDIS_METRIC_TYPE_NET_BPS_TX, 322 REDIS_METRIC_TYPE_USED_CONN, 323 REDIS_METRIC_TYPE_OPT_SES, 324 REDIS_METRIC_TYPE_CACHE_KEYS, 325 REDIS_METRIC_TYPE_CACHE_EXP_KEYS, 326 REDIS_METRIC_TYPE_DATA_MEM_USAGE, 327 } 328 329 ALL_LB_METRIC_TYPES = []TMetricType{ 330 LB_METRIC_TYPE_SNAT_PORT, 331 LB_METRIC_TYPE_SNAT_CONN_COUNT, 332 LB_METRIC_TYPE_NET_BPS_RX, 333 LB_METRIC_TYPE_NET_BPS_TX, 334 LB_METRIC_TYPE_HRSP_COUNT, 335 } 336 337 ALL_BUCKET_TYPES = []TMetricType{ 338 BUCKET_METRIC_TYPE_NET_BPS_TX, 339 BUCKET_METRIC_TYPE_NET_BPS_RX, 340 BUCKET_METRIC_TYPE_LATECY, 341 BUCKET_METRYC_TYPE_REQ_COUNT, 342 } 343 344 ALL_K8S_NODE_TYPES = []TMetricType{ 345 K8S_NODE_METRIC_TYPE_CPU_USAGE, 346 K8S_NODE_METRIC_TYPE_MEM_USAGE, 347 } 348 ) 349 350 type MetricListOptions struct { 351 ResourceType TResourceType 352 MetricType TMetricType 353 354 ResourceId string 355 // batch metric pull for tencentcloud 356 ResourceIds []string 357 RegionExtId string 358 StartTime time.Time 359 EndTime time.Time 360 361 OsType string 362 Interval int 363 // rds 364 Engine string 365 } 366 367 type MetricValue struct { 368 Timestamp time.Time 369 Value float64 370 Tags map[string]string 371 } 372 373 type MetricValues struct { 374 Id string 375 Unit string 376 MetricType TMetricType 377 Values []MetricValue 378 }