Skip to content

Commit

Permalink
feat(dbm-services): 资源池增加统计每日资源快照 TencentBlueKing#7556
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Oct 25, 2024
1 parent c4a9972 commit cd32483
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE tb_rp_daily_snap_shot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TABLE `tb_rp_daily_snap_shot` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`report_day` varchar(32) NOT NULL COMMENT '''上报日期''',
`bk_cloud_id` int(11) NOT NULL COMMENT '''云区域 ID''',
`bk_biz_id` int(11) NOT NULL COMMENT '机器当前所属业务',
`dedicated_biz` int(11) DEFAULT '0' COMMENT '专属业务',
`rs_type` varchar(64) DEFAULT 'PUBLIC' COMMENT '资源专用组件类型',
`bk_host_id` int(11) NOT NULL COMMENT '''bk主机ID''',
`ip` varchar(20) NOT NULL,
`device_class` varchar(64) NOT NULL,
`cpu_num` int(11) NOT NULL COMMENT '''cpu核数''',
`dram_cap` int(11) NOT NULL COMMENT '''内存大小''',
`storage_device` json DEFAULT NULL COMMENT '''磁盘设备''',
`total_storage_cap` int(11) DEFAULT NULL COMMENT '''磁盘总容量''',
`os_type` varchar(32) NOT NULL COMMENT '''操作系统类型''',
`os_bit` varchar(32) NOT NULL COMMENT '''操作系统位数''',
`os_version` varchar(64) NOT NULL COMMENT '''操作系统版本''',
`os_name` varchar(64) NOT NULL COMMENT '''操作系统名称''',
`city_id` varchar(64) NOT NULL,
`city` varchar(128) NOT NULL,
`sub_zone` varchar(32) NOT NULL,
`sub_zone_id` varchar(64) NOT NULL,
`label` json DEFAULT NULL,
`status` varchar(20) NOT NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_host_id` (`bk_host_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
7 changes: 7 additions & 0 deletions dbm-services/common/db-resource/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func init() {
logger.Info("tb_rp_operation_info columns %v", TbRpOperationInfoColumns)
}

// func migration() {
// err := DB.Self.AutoMigrate(&TbRpDailySnapShot{})
// if err != nil {
// logger.Error("auto migrate failed %v", err)
// }
// }

func createSysDb() {
user := config.AppConfig.Db.UserName
pwd := config.AppConfig.Db.PassWord
Expand Down
105 changes: 105 additions & 0 deletions dbm-services/common/db-resource/internal/model/tb_rp_daily_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package model

import (
"encoding/json"
"time"

"dbm-services/common/go-pubpkg/logger"
)

// TbRpDailySnapShot 机器资源快照表
type TbRpDailySnapShot struct {
ID int `gorm:"primary_key;auto_increment;not_null" json:"-"`
ReportDay string `gorm:"column:report_day;type:varchar(32);not null;comment:'上报日期'" json:"report_day"`
BkCloudID int `gorm:"uniqueIndex:ip;column:bk_cloud_id;type:int(11);not null;comment:'云区域 ID'"`
BkBizId int `gorm:"column:bk_biz_id;type:int(11);not null;comment:机器当前所属业务" json:"bk_biz_id"`
DedicatedBiz int `gorm:"column:dedicated_biz;type:int(11);default:0;comment:专属业务" json:"dedicated_biz"`
RsType string `gorm:"column:rs_type;type:varchar(64);default:'PUBLIC';comment:资源专用组件类型"`
BkHostID int `gorm:"index:idx_host_id;column:bk_host_id;type:int(11);not null;comment:'bk主机ID'"`
IP string `gorm:"uniqueIndex:ip;column:ip;type:varchar(20);not null" json:"ip"`
DeviceClass string `gorm:"column:device_class;type:varchar(64);not null" json:"device_class"`
CPUNum int `gorm:"column:cpu_num;type:int(11);not null;comment:'cpu核数'" json:"cpu_num"`
DramCap int `gorm:"column:dram_cap;type:int(11);not null;comment:'内存大小'" json:"dram_cap"`
StorageDevice json.RawMessage `gorm:"column:storage_device;type:json;comment:'磁盘设备'" json:"storage_device"`
TotalStorageCap int `gorm:"column:total_storage_cap;type:int(11);comment:'磁盘总容量'" json:"total_storage_cap"`
// 操作系统类型 Liunx,Windows
/*Linux(1) Windows(2) AIX(3) Unix(4) Solaris(5) FreeBSD(7)*/
OsType string `gorm:"column:os_type;type:varchar(32);not null;comment:'操作系统类型'" json:"os_type"`
OsBit string `gorm:"column:os_bit;type:varchar(32);not null;comment:'操作系统位数'" json:"os_bit"`
// 操作系统版本
OsVerion string `gorm:"column:os_version;type:varchar(64);not null;comment:'操作系统版本'" json:"os_version"`
// 操作系统名称
OsName string `gorm:"column:os_name;type:varchar(64);not null;comment:'操作系统名称'" json:"os_name"`
// 实际城市ID
CityID string `gorm:"column:city_id;type:varchar(64);not null" json:"city_id"`
// 实际城市名称
City string `gorm:"column:city;type:varchar(128);not null" json:"city"`
// 园区, 例如光明 cc_device_szone
SubZone string `gorm:"column:sub_zone;type:varchar(32);not null" json:"sub_zone"`
// 园区ID cc_device_szone_id
SubZoneID string `gorm:"column:sub_zone_id;type:varchar(64);not null" json:"sub_zone_id"`
// 标签
Label json.RawMessage `gorm:"column:label;type:json" json:"label"`
// Unused: 未使用 Used: 已经售卖被使用: Preselected:预占用
Status string `gorm:"column:status;type:varchar(20);not null" json:"status"`
// 最后修改时间
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"update_time"`
// 创建时间
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"create_time"`
}

// SyncDbRpDailySnapShot TODO
func SyncDbRpDailySnapShot() (err error) {
ql := `
insert into tb_rp_daily_snap_shot
select null,
date_format(now(), '%Y-%m-%d'),
bk_cloud_id,
bk_biz_id,
dedicated_biz,
rs_type,
bk_host_id,
ip,
device_class,
cpu_num,
dram_cap,
storage_device,
total_storage_cap,
os_type,
os_bit,
os_version,
os_name,
city_id,
city,
sub_zone,
sub_zone_id,
label,
status,
update_time,
create_time
from tb_rp_detail
where status = 'Unused';
`
res, err := DB.SelfSqlDB.Exec(ql)
if err != nil {
logger.Error("SyncDbRpDailySnapShot failed: %v", err)
return err
}
count, err := res.RowsAffected()
if err != nil {
logger.Error("SyncDbRpDailySnapShot failed: %v", err)
return err
}
logger.Info("SyncDbRpDailySnapShot affected rows: %d", count)
return err
}
10 changes: 10 additions & 0 deletions dbm-services/common/db-resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

"dbm-services/common/db-resource/internal/middleware"
"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/routers"
"dbm-services/common/db-resource/internal/svr/task"
"dbm-services/common/go-pubpkg/logger"
Expand Down Expand Up @@ -151,6 +152,15 @@ func registerCrontab(localcron *cron.Cron) {
}
},
},
{
Name: "生成每日资源快照",
Spec: " 0 3 * * *",
Func: func() {
if err := model.SyncDbRpDailySnapShot(); err != nil {
logger.Error("async machine softinfo failed:%s", err.Error())
}
},
},
}
for _, cron := range localCrontabs {
if _, err := localcron.AddFunc(cron.Spec, cron.Func); err != nil {
Expand Down

0 comments on commit cd32483

Please sign in to comment.