From d8b1a9244b4f1e2ba874929a2002dd8f5f4bcbc0 Mon Sep 17 00:00:00 2001 From: xfwduke Date: Fri, 20 Dec 2024 13:00:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(mysql):=20mysql-monitor=E7=A7=BB=E9=99=A4co?= =?UTF-8?q?ntext=20timeout=20#8730?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-services/common/reverse-api/.gitignore | 2 + .../apis/common/list_nginx_addrs.go | 23 ++++ .../apis/mysql/list_instance_info.go | 56 ++++++++++ .../common/reverse-api/config/apis.go | 6 + .../common/reverse-api/config/init.go | 11 ++ dbm-services/common/reverse-api/go.mod | 3 + dbm-services/common/reverse-api/init.go | 1 + .../common/reverse-api/internal/init.go | 11 ++ .../reverse-api/internal/reverse_call.go | 103 ++++++++++++++++++ dbm-services/go.work | 1 + .../mysql-crond/api/create_or_replace.go | 4 + .../mysql-crond/pkg/config/job_config.go | 45 +++++--- .../db-tools/mysql-crond/pkg/crond/crond.go | 6 + .../mysql-crond/pkg/crond/job_replace.go | 4 + .../mysql-crond/pkg/nginx_updater/init.go | 53 --------- .../mysql-crond/pkg/service/service.go | 6 +- .../mysql-crond/pkg/third_party/init.go | 15 +++ .../pkg/third_party/nginx_updater/init.go | 64 +++++++++++ .../mysql-monitor/cmd/reschedule_items.go | 25 +++-- .../character_consistency.go | 2 - .../characterconsistency/checker.go | 7 +- .../pkg/itemscollect/dbhaheartbeat/checker.go | 8 +- .../pkg/itemscollect/definer/routine.go | 6 +- .../pkg/itemscollect/definer/trigger.go | 6 +- .../definer/user_list_snap_shot.go | 8 +- .../pkg/itemscollect/definer/view.go | 6 +- .../pkg/itemscollect/engine/init.go | 6 +- .../ext3check/query_mysql_dirs.go | 12 +- .../ibdstatistic/ibd_statistic.go | 15 +-- .../master_slave_heartbeat.go | 11 +- .../mysqlconnlog/connlog_rotate.go | 18 +-- .../itemscollect/mysqlconnlog/connlog_size.go | 9 +- .../mysqlerrlog/errlog_snapshot.go | 8 +- .../itemscollect/mysqlerrlog/mysql_errlog.go | 2 - .../mysqlprocesslist/mysql_lock.go | 32 +----- .../mysqlprocesslist/processlist_snapshot.go | 7 +- .../proxybackend/proxy_backend.go | 7 +- .../pkg/itemscollect/proxyuserlist/backup.go | 10 +- .../itemscollect/proxyuserlist/load_users.go | 6 +- .../itemscollect/rotateproxyconnlog/init.go | 11 +- .../rotateslowlog/rotate_slowlog.go | 12 +- .../scenesnapshot/engineinnodbstatus.go | 8 +- .../itemscollect/scenesnapshot/processlist.go | 8 +- .../itemscollect/slavestatus/ctl_replicate.go | 7 +- .../itemscollect/slavestatus/slave_status.go | 13 +-- .../spiderremote/spider_remote.go | 7 +- .../timezonechange/mysqltimezonechange.go | 6 +- .../mysql-monitor/pkg/mainloop/main_loop.go | 8 +- .../reverse_api/common/impl/__init__.py | 2 +- ...{list_nginx_ips.py => list_nginx_addrs.py} | 4 +- .../db_proxy/reverse_api/common/views.py | 14 +-- .../mysql/impl/list_instance_info.py | 20 +++- 52 files changed, 440 insertions(+), 305 deletions(-) create mode 100644 dbm-services/common/reverse-api/.gitignore create mode 100644 dbm-services/common/reverse-api/apis/common/list_nginx_addrs.go create mode 100644 dbm-services/common/reverse-api/apis/mysql/list_instance_info.go create mode 100644 dbm-services/common/reverse-api/config/apis.go create mode 100644 dbm-services/common/reverse-api/config/init.go create mode 100644 dbm-services/common/reverse-api/go.mod create mode 100644 dbm-services/common/reverse-api/init.go create mode 100644 dbm-services/common/reverse-api/internal/init.go create mode 100644 dbm-services/common/reverse-api/internal/reverse_call.go delete mode 100644 dbm-services/mysql/db-tools/mysql-crond/pkg/nginx_updater/init.go create mode 100644 dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/init.go create mode 100644 dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/nginx_updater/init.go delete mode 100644 dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/character_consistency.go delete mode 100644 dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/mysql_errlog.go rename dbm-ui/backend/db_proxy/reverse_api/common/impl/{list_nginx_ips.py => list_nginx_addrs.py} (87%) diff --git a/dbm-services/common/reverse-api/.gitignore b/dbm-services/common/reverse-api/.gitignore new file mode 100644 index 0000000000..9ec26fa9b7 --- /dev/null +++ b/dbm-services/common/reverse-api/.gitignore @@ -0,0 +1,2 @@ +cmd +cmd/* \ No newline at end of file diff --git a/dbm-services/common/reverse-api/apis/common/list_nginx_addrs.go b/dbm-services/common/reverse-api/apis/common/list_nginx_addrs.go new file mode 100644 index 0000000000..68a6b1e3b1 --- /dev/null +++ b/dbm-services/common/reverse-api/apis/common/list_nginx_addrs.go @@ -0,0 +1,23 @@ +package common + +import ( + "dbm-services/common/reverse-api/config" + "dbm-services/common/reverse-api/internal" + "encoding/json" + + "github.com/pkg/errors" +) + +func ListNginxAddrs(bkCloudId int) ([]string, error) { + data, err := internal.ReverseCall(config.ReverseApiCommonListNginxAddrs, bkCloudId) + if err != nil { + return nil, errors.Wrap(err, "failed to call ListNginxAddrs") + } + + var addrs []string + if err := json.Unmarshal(data, &addrs); err != nil { + return nil, errors.Wrap(err, "failed to unmarshal ListNginxAddrs") + } + + return addrs, nil +} diff --git a/dbm-services/common/reverse-api/apis/mysql/list_instance_info.go b/dbm-services/common/reverse-api/apis/mysql/list_instance_info.go new file mode 100644 index 0000000000..b83b5eac6e --- /dev/null +++ b/dbm-services/common/reverse-api/apis/mysql/list_instance_info.go @@ -0,0 +1,56 @@ +package mysql + +import ( + "dbm-services/common/reverse-api/config" + "dbm-services/common/reverse-api/internal" + "encoding/json" + + "github.com/pkg/errors" +) + +const ( + AccessLayerStorage string = "storage" + AccessLayerProxy string = "proxy" +) + +type instanceAddr struct { + Ip string `json:"ip"` + Port int `json:"port"` +} + +type commonInstanceInfo struct { + instanceAddr + ImmuteDomain string `json:"immute_domain"` + Phase string `json:"phase"` + Status string `json:"status"` + AccessLayer string `json:"access_layer"` + MachineType string `json:"machine_type"` +} + +type StorageInstanceInfo struct { + commonInstanceInfo + IsStandBy bool `json:"is_stand_by"` + InstanceRole string `json:"instance_role"` + InstanceInnerRole string `json:"instance_inner_role"` + Receivers []instanceAddr `json:"receivers"` + Ejectors []instanceAddr `json:"ejectors"` +} + +type ProxyInstanceInfo struct { + commonInstanceInfo + StorageInstanceList []instanceAddr `json:"storage_instance_list"` +} + +func ListInstanceInfo(bkCloudId int, ports ...int) ([]byte, string, error) { + data, err := internal.ReverseCall(config.ReverseApiMySQLListInstanceInfo, bkCloudId, ports...) + if err != nil { + return nil, "", errors.Wrap(err, "failed to call ListInstanceInfo") + } + var r []commonInstanceInfo + err = json.Unmarshal(data, &r) + if err != nil { + return nil, "", errors.Wrap(err, "failed to unmarshal ListInstanceInfo") + } + + return data, r[0].AccessLayer, nil +} diff --git a/dbm-services/common/reverse-api/config/apis.go b/dbm-services/common/reverse-api/config/apis.go new file mode 100644 index 0000000000..86f5ed4af6 --- /dev/null +++ b/dbm-services/common/reverse-api/config/apis.go @@ -0,0 +1,6 @@ +package config + +const ( + ReverseApiCommonListNginxAddrs = "common/list_nginx_addrs" + ReverseApiMySQLListInstanceInfo = "mysql/list_instance_info" +) diff --git a/dbm-services/common/reverse-api/config/init.go b/dbm-services/common/reverse-api/config/init.go new file mode 100644 index 0000000000..e5e1f5dc71 --- /dev/null +++ b/dbm-services/common/reverse-api/config/init.go @@ -0,0 +1,11 @@ +package config + +const CommonConfigDir = "/home/mysql/common_config" +const NginxProxyAddrsFileName = "nginx_proxy.list" +const ReverseApiBase = "apis/proxypass/reverse_api" + +type ReverseApiName string + +func (c ReverseApiName) String() string { + return string(c) +} diff --git a/dbm-services/common/reverse-api/go.mod b/dbm-services/common/reverse-api/go.mod new file mode 100644 index 0000000000..bfee1531b6 --- /dev/null +++ b/dbm-services/common/reverse-api/go.mod @@ -0,0 +1,3 @@ +module dbm-services/common/reverse-api + +go 1.21.11 diff --git a/dbm-services/common/reverse-api/init.go b/dbm-services/common/reverse-api/init.go new file mode 100644 index 0000000000..d25e873c69 --- /dev/null +++ b/dbm-services/common/reverse-api/init.go @@ -0,0 +1 @@ +package reverse_api diff --git a/dbm-services/common/reverse-api/internal/init.go b/dbm-services/common/reverse-api/internal/init.go new file mode 100644 index 0000000000..5c5a57bf27 --- /dev/null +++ b/dbm-services/common/reverse-api/internal/init.go @@ -0,0 +1,11 @@ +package internal + +import "encoding/json" + +type apiResponse struct { + Result bool `json:"result"` + Code int `json:"code"` + Message string `json:"message"` + Errors string `json:"errors"` + Data json.RawMessage `json:"data"` +} diff --git a/dbm-services/common/reverse-api/internal/reverse_call.go b/dbm-services/common/reverse-api/internal/reverse_call.go new file mode 100644 index 0000000000..d2d549ea60 --- /dev/null +++ b/dbm-services/common/reverse-api/internal/reverse_call.go @@ -0,0 +1,103 @@ +package internal + +import ( + "bufio" + "dbm-services/common/reverse-api/config" + "encoding/json" + errs "errors" + "io" + "net/http" + "net/url" + "os" + "path/filepath" + "strconv" + + "github.com/pkg/errors" +) + +func ReverseCall(api config.ReverseApiName, bkCloudId int, ports ...int) (data []byte, err error) { + addrs, err := readNginxProxyAddrs() + if err != nil { + return nil, errors.Wrap(err, "failed to read nginx proxy addresses") + } + + var errCollect []error + for _, addr := range addrs { + apiPath, _ := url.JoinPath(config.ReverseApiBase, api.String(), "/") + ep := url.URL{ + Scheme: "http", + Host: addr, + Path: apiPath, + } + + req, err := http.NewRequest(http.MethodGet, ep.String(), nil) + if err != nil { + return nil, errors.Wrap(err, "failed to create request") + } + + q := req.URL.Query() + q.Add("bk_cloud_id", strconv.Itoa(bkCloudId)) + for _, port := range ports { + q.Add("port", strconv.Itoa(port)) + } + req.URL.RawQuery = q.Encode() + + data, err = do(req) + if err == nil { + return data, nil + } + errCollect = append(errCollect, err) + } + + return nil, errs.Join(errCollect...) +} + +func do(request *http.Request) (data []byte, err error) { + resp, err := http.DefaultClient.Do(request) + if err != nil { + return nil, errors.Wrap(err, "failed to send request") + } + defer func() { + _ = resp.Body.Close() + }() + + b, err := io.ReadAll(resp.Body) + if err != nil { + return nil, errors.Wrap(err, "failed to read response body") + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(b)) + } + + var r apiResponse + err = json.Unmarshal(b, &r) + if err != nil { + return nil, errors.Wrap(err, "failed to unmarshal response body") + } + + if !r.Result { + return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, r.Errors) + } + + return r.Data, nil +} + +func readNginxProxyAddrs() (addrs []string, err error) { + f, err := os.Open(filepath.Join(config.CommonConfigDir, config.NginxProxyAddrsFileName)) + if err != nil { + return nil, errors.Wrap(err, "failed to open nginx proxy addrs") + } + defer func() { + _ = f.Close() + }() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + addrs = append(addrs, scanner.Text()) + } + if err := scanner.Err(); err != nil { + return nil, errors.Wrap(err, "failed to read nginx proxy addrs") + } + return addrs, nil +} diff --git a/dbm-services/go.work b/dbm-services/go.work index bc6bcf85d7..ff08289b98 100644 --- a/dbm-services/go.work +++ b/dbm-services/go.work @@ -31,6 +31,7 @@ use ( mongodb/db-tools/mongo-toolkit-go mongodb/db-tools/dbactuator common/db-dns/dns-api/pkg + common/reverse-api ) replace github.com/go-sql-driver/mysql => github.com/go-sql-driver/mysql v1.7.1 diff --git a/dbm-services/mysql/db-tools/mysql-crond/api/create_or_replace.go b/dbm-services/mysql/db-tools/mysql-crond/api/create_or_replace.go index a2ee89c808..d9e9c14282 100644 --- a/dbm-services/mysql/db-tools/mysql-crond/api/create_or_replace.go +++ b/dbm-services/mysql/db-tools/mysql-crond/api/create_or_replace.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "log/slog" "github.com/pkg/errors" ) @@ -15,6 +16,7 @@ type JobDefine struct { Creator string `json:"creator"` Enable bool `json:"enable"` WorkDir string `json:"work_dir"` + Overlap bool `json:"overlap"` } // CreateOrReplace TODO @@ -26,6 +28,8 @@ func (m *Manager) CreateOrReplace(job JobDefine, permanent bool) (int, error) { Job: job, Permanent: permanent, } + slog.Info("CreateOrReplace", slog.Any("job", job)) + resp, err := m.do("/create_or_replace", "POST", body) if err != nil { return 0, errors.Wrap(err, "manager call /create_or_replace") diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/config/job_config.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/config/job_config.go index 7bbb417982..adcf97f458 100644 --- a/dbm-services/mysql/db-tools/mysql-crond/pkg/config/job_config.go +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/config/job_config.go @@ -31,6 +31,7 @@ type ExternalJob struct { Schedule string `yaml:"schedule" json:"schedule" binding:"required" validate:"required"` Creator string `yaml:"creator" json:"creator" binding:"required" validate:"required"` WorkDir string `yaml:"work_dir" json:"work_dir"` + Overlap bool `yaml:"overlap" json:"overlap"` // 是否允许作业重叠执行, 默认 false // JobID 这个 id 主要用于追溯哪个 cron job (如果有) 调起本 external job JobID cron.EntryID `yaml:"-" json:"-"` ch chan struct{} @@ -87,23 +88,35 @@ func (j *ExternalJob) run() { // Run TODO func (j *ExternalJob) Run() { - select { - case v := <-j.ch: + slog.Info( + "run job", + slog.String("name", j.Name), + slog.Bool("overlap", j.Overlap), + ) + + if j.Overlap { j.run() - j.ch <- v - default: - slog.Warn("skip job", slog.String("name", j.Name)) - err := SendEvent( - mysqlCrondEventName, - fmt.Sprintf("%s skipt for last round use too much time", j.Name), - map[string]interface{}{ - "job_name": j.Name, - }, - ) - if err != nil { - slog.Error("send event", slog.String("error", err.Error())) + } else { + select { + case v := <-j.ch: + j.run() + j.ch <- v + default: + slog.Warn("skip job", slog.String("name", j.Name)) + err := SendEvent( + mysqlCrondEventName, + fmt.Sprintf("%s skipt for last round use too much time", j.Name), + map[string]interface{}{ + "job_name": j.Name, + }, + ) + if err != nil { + slog.Error("send event", slog.String("error", err.Error())) + } } } + + slog.Info("finish job", slog.String("name", j.Name)) } // SetupChannel TODO @@ -165,7 +178,9 @@ func InitJobsConfig() error { panic(err) } - j.SetupChannel() + if !j.Overlap { + j.SetupChannel() + } } return nil } diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/crond.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/crond.go index e053ccc713..113ec8a492 100644 --- a/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/crond.go +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/crond.go @@ -2,6 +2,7 @@ package crond import ( + "dbm-services/mysql/db-tools/mysql-crond/pkg/third_party" "log/slog" "sync" @@ -91,6 +92,11 @@ func Start() error { slog.Info("add heart beat job", slog.Int("entry id", int(entryID))) } + // 第三方 + for _, rg := range third_party.ThirdPartyRegisters { + rg(cronJob) + } + cronJob.Start() slog.Info("crond start") return nil diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/job_replace.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/job_replace.go index 03a6b4f9a9..803a8f5f07 100644 --- a/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/job_replace.go +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/crond/job_replace.go @@ -31,5 +31,9 @@ func CreateOrReplace(j *config.ExternalJob, permanent bool) (int, error) { ) return 0, err } + slog.Info( + "create or replace job", + slog.Any("job", j), + ) return entryID, nil } diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/nginx_updater/init.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/nginx_updater/init.go deleted file mode 100644 index d73d6a86d5..0000000000 --- a/dbm-services/mysql/db-tools/mysql-crond/pkg/nginx_updater/init.go +++ /dev/null @@ -1,53 +0,0 @@ -package nginx_updater - -import ( - "bufio" - "os" -) - -const nginxAddrFile = "/home/mysql/nginx_conf/address.list" - -func Updater() { - addrs, err := readAddr() - if err != nil { - - } - - newAddrs, err := queryNewAddr(addrs) - - f, err := os.OpenFile(nginxAddrFile, os.O_CREATE|os.O_TRUNC, 0777) - if err != nil { - } - defer func() { - _ = f.Close() - }() - - for _, ad := range newAddrs { - _, _ = f.WriteString(ad + "\n") - } -} - -func readAddr() (res []string, err error) { - f, err := os.Open(nginxAddrFile) - if err != nil { - return nil, err - } - defer func() { - _ = f.Close() - }() - - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - res = append(res, line) - } - err = scanner.Err() - if err != nil { - return nil, err - } - return res, nil -} - -func queryNewAddr(addrs []string) (res []string, err error) { - return -} diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/service/service.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/service/service.go index 7544080459..55312760a2 100644 --- a/dbm-services/mysql/db-tools/mysql-crond/pkg/service/service.go +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/service/service.go @@ -200,7 +200,11 @@ func Start(version string, buildStamp string, gitHash string, quit chan struct{} defer func() { m.Unlock() }() - body.Job.SetupChannel( /*config.RuntimeConfig.Ip*/ ) + + if !body.Job.Overlap { + body.Job.SetupChannel( /*config.RuntimeConfig.Ip*/ ) + } + entryID, err := crond.CreateOrReplace(body.Job, *body.Permanent) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, api.NewErrorResp(500, err)) diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/init.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/init.go new file mode 100644 index 0000000000..4b2705a73e --- /dev/null +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/init.go @@ -0,0 +1,15 @@ +package third_party + +import ( + "dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/nginx_updater" + + "github.com/robfig/cron/v3" +) + +var ThirdPartyRegisters []func(cron *cron.Cron) + +func init() { + ThirdPartyRegisters = []func(*cron.Cron){ + nginx_updater.Register, + } +} diff --git a/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/nginx_updater/init.go b/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/nginx_updater/init.go new file mode 100644 index 0000000000..ea848c6c91 --- /dev/null +++ b/dbm-services/mysql/db-tools/mysql-crond/pkg/third_party/nginx_updater/init.go @@ -0,0 +1,64 @@ +package nginx_updater + +import ( + "dbm-services/common/reverse-api/apis/common" + rconfig "dbm-services/common/reverse-api/config" + "dbm-services/mysql/db-tools/mysql-crond/pkg/config" + "log/slog" + "os" + "path/filepath" + + "github.com/pkg/errors" + "github.com/robfig/cron/v3" +) + +func Register(cj *cron.Cron) { + id, err := cj.AddFunc( + "@every 30m", + func() { + err := updater() + if err != nil { + slog.Error("update nginx addrs job", slog.String("err", err.Error())) + } else { + slog.Info("update nginx addrs job finished") + } + }, + ) + if err != nil { + slog.Error("register nginx addrs job", slog.String("err", err.Error())) + } else { + slog.Info("register nginx addrs job success", slog.Int("entry id", int(id))) + } +} + +func updater() error { + err := os.MkdirAll(rconfig.CommonConfigDir, 0777) + if err != nil { + return errors.Wrap(err, "can't create config directory") + } + + addrs, err := common.ListNginxAddrs(*config.RuntimeConfig.BkCloudID) + if err != nil { + return errors.Wrap(err, "list nginx addrs failed") + } + + f, err := os.OpenFile( + filepath.Join(rconfig.CommonConfigDir, rconfig.NginxProxyAddrsFileName), + os.O_TRUNC|os.O_CREATE|os.O_WRONLY, + 0777, + ) + if err != nil { + return errors.Wrap(err, "open nginx addrs failed") + } + defer func() { + _ = f.Close() + }() + + for _, addr := range addrs { + if _, err := f.WriteString(addr + "\n"); err != nil { + return errors.Wrap(err, "write nginx addrs failed") + } + } + + return nil +} diff --git a/dbm-services/mysql/db-tools/mysql-monitor/cmd/reschedule_items.go b/dbm-services/mysql/db-tools/mysql-monitor/cmd/reschedule_items.go index 3da9740af7..d407fcd1a3 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/cmd/reschedule_items.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/cmd/reschedule_items.go @@ -76,17 +76,20 @@ func reschedule(configFileDir, configFileName, staff string) error { "--items", strings.Join(itemNames, ","), "-c", configFileName, // use WorkDir } - eid, err := manager.CreateOrReplace( - ma.JobDefine{ - Name: fmt.Sprintf("mysql-monitor-%d-%s", config.MonitorConfig.Port, k), - Command: executable, - Args: args, - Schedule: k, - Creator: staff, //viper.GetString("staff"), - Enable: true, - WorkDir: configFileDir, - }, true, - ) + + jobDefine := ma.JobDefine{ + Name: fmt.Sprintf("mysql-monitor-%d-%s", config.MonitorConfig.Port, k), + Command: executable, + Args: args, + Schedule: k, + Creator: staff, //viper.GetString("staff"), + Enable: true, + WorkDir: configFileDir, + Overlap: true, + } + slog.Info("reschedule", slog.Any("job define", jobDefine)) + + eid, err := manager.CreateOrReplace(jobDefine, true) if err != nil { slog.Error("reschedule add entry", slog.String("error", err.Error())) return err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/character_consistency.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/character_consistency.go deleted file mode 100644 index f5b0989c19..0000000000 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/character_consistency.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package characterconsistency 字符集检查 -package characterconsistency diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/checker.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/checker.go index 7888332aed..df0dd2ed31 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/checker.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/characterconsistency/checker.go @@ -9,7 +9,6 @@ package characterconsistency import ( - "context" "fmt" "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" @@ -28,11 +27,9 @@ type Checker struct { // Run TODO func (c *Checker) Run() (msg string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() var characterSetServer string - err = c.db.GetContext(ctx, &characterSetServer, `SELECT @@character_set_server`) + err = c.db.Get(&characterSetServer, `SELECT @@character_set_server`) if err != nil { return "", errors.Wrap(err, "get character_set_server") // ToDo 这里需要发告警么? } @@ -52,7 +49,7 @@ func (c *Checker) Run() (msg string, err error) { SchemaName string `db:"SCHEMA_NAME"` SchemaCharset string `db:"DEFAULT_CHARACTER_SET_NAME"` } - err = c.db.SelectContext(ctx, &res, c.db.Rebind(q), args...) + err = c.db.Select(&res, c.db.Rebind(q), args...) if err != nil { return "", errors.Wrap(err, "query charset inconsistent dbs") } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/dbhaheartbeat/checker.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/dbhaheartbeat/checker.go index 4196d73ce0..d849deb4c5 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/dbhaheartbeat/checker.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/dbhaheartbeat/checker.go @@ -1,7 +1,6 @@ package dbhaheartbeat import ( - "context" "database/sql" "dbm-services/mysql/db-tools/mysql-monitor/pkg/utils" "fmt" @@ -11,7 +10,6 @@ import ( "github.com/jmoiron/sqlx" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" ) @@ -33,12 +31,8 @@ func init() { } func (c *Checker) Run() (msg string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var res sql.NullTime - err = c.db.QueryRowxContext( - ctx, + err = c.db.QueryRowx( `SELECT MAX(ck_time) FROM infodba_schema.check_heartbeat WHERE uid = @@server_id`, ).Scan(&res) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/routine.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/routine.go index a242a7d9b5..fea491b012 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/routine.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/routine.go @@ -9,7 +9,6 @@ package definer import ( - "context" "fmt" "log/slog" @@ -20,9 +19,6 @@ import ( ) func routines(db *sqlx.DB) (msg []string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - q, args, err := sqlx.In( `SELECT ROUTINE_TYPE, ROUTINE_NAME, ROUTINE_SCHEMA, DEFINER FROM information_schema.ROUTINES @@ -39,7 +35,7 @@ func routines(db *sqlx.DB) (msg []string, err error) { RoutineSchema string `db:"ROUTINE_SCHEMA"` Definer string `db:"DEFINER"` } - err = db.SelectContext(ctx, &res, db.Rebind(q), args...) + err = db.Select(&res, db.Rebind(q), args...) if err != nil { return nil, errors.Wrap(err, "query routines") } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/trigger.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/trigger.go index 8ebbd3bcbd..bf94ab6bff 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/trigger.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/trigger.go @@ -9,7 +9,6 @@ package definer import ( - "context" "fmt" "log/slog" @@ -20,9 +19,6 @@ import ( ) func triggers(db *sqlx.DB) (msg []string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - q, args, err := sqlx.In( `SELECT TRIGGER_NAME, TRIGGER_SCHEMA, DEFINER FROM information_schema.TRIGGERS @@ -38,7 +34,7 @@ func triggers(db *sqlx.DB) (msg []string, err error) { TriggerSchema string `db:"TRIGGER_SCHEMA"` Definer string `db:"DEFINER"` } - err = db.SelectContext(ctx, &res, db.Rebind(q), args...) + err = db.Select(&res, db.Rebind(q), args...) if err != nil { return nil, errors.Wrap(err, "query triggers") } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/user_list_snap_shot.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/user_list_snap_shot.go index 391bf52fc6..5af3d6d109 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/user_list_snap_shot.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/user_list_snap_shot.go @@ -9,19 +9,13 @@ package definer import ( - "context" "log/slog" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" - "github.com/jmoiron/sqlx" ) func snapshot(db *sqlx.DB) error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - err := db.SelectContext(ctx, &mysqlUsers, `SELECT user FROM mysql.user`) + err := db.Select(&mysqlUsers, `SELECT user FROM mysql.user`) if err != nil { slog.Error("query users", slog.String("error", err.Error())) return err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/view.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/view.go index 971405dab1..88f8f108d2 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/view.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/definer/view.go @@ -9,7 +9,6 @@ package definer import ( - "context" "fmt" "log/slog" @@ -20,9 +19,6 @@ import ( ) func views(db *sqlx.DB) (msg []string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - q, args, err := sqlx.In( `SELECT TABLE_NAME, TABLE_SCHEMA, DEFINER FROM information_schema.VIEWS @@ -38,7 +34,7 @@ func views(db *sqlx.DB) (msg []string, err error) { ViewSchema string `db:"TABLE_SCHEMA"` Definer string `db:"DEFINER"` } - err = db.SelectContext(ctx, &res, db.Rebind(q), args...) + err = db.Select(&res, db.Rebind(q), args...) if err != nil { return nil, errors.Wrap(err, "query views") } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/engine/init.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/engine/init.go index 350cfe0bd2..37d6260963 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/engine/init.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/engine/init.go @@ -9,7 +9,6 @@ package engine import ( - "context" "database/sql" "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" @@ -72,11 +71,8 @@ func (c *Checker) Run() (msg string, err error) { _ = regFile.Close() }() - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var dataDir sql.NullString - err = c.db.GetContext(ctx, &dataDir, `SELECT @@datadir`) + err = c.db.Get(&dataDir, `SELECT @@datadir`) if err != nil { slog.Error("ibd-statistic", slog.String("error", err.Error())) return "", err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ext3check/query_mysql_dirs.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ext3check/query_mysql_dirs.go index 42a541f0a6..abc96d2550 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ext3check/query_mysql_dirs.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ext3check/query_mysql_dirs.go @@ -9,7 +9,6 @@ package ext3check import ( - "context" "database/sql" "fmt" "path/filepath" @@ -23,14 +22,11 @@ import ( ) func mysqlDirs(db *sqlx.DB, variables []string) (dirs []string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var datadir string for _, v := range variables { var dir sql.NullString - err = db.GetContext(ctx, &dir, fmt.Sprintf(`SELECT @@%s`, v)) + err = db.Get(&dir, fmt.Sprintf(`SELECT @@%s`, v)) if err != nil && !errors.Is(err, sql.ErrNoRows) { return nil, errors.Wrap(err, fmt.Sprintf(`SELECT @@%s`, v)) } @@ -45,7 +41,7 @@ func mysqlDirs(db *sqlx.DB, variables []string) (dirs []string, err error) { } var versionStr string - err = db.GetContext(ctx, &versionStr, `SELECT SUBSTRING_INDEX(@@version, ".", 2)`) + err = db.Get(&versionStr, `SELECT SUBSTRING_INDEX(@@version, ".", 2)`) if err != nil { return nil, errors.Wrap(err, `SELECT SUBSTRING_INDEX(@@version, ".", 2)`) } @@ -58,7 +54,7 @@ func mysqlDirs(db *sqlx.DB, variables []string) (dirs []string, err error) { var binlogBase sql.NullString var relaylogBase sql.NullString - err = db.GetContext(ctx, &binlogBase, `SELECT @@log_bin_basename`) + err = db.Get(&binlogBase, `SELECT @@log_bin_basename`) if err != nil && !errors.Is(err, sql.ErrNoRows) { return nil, errors.Wrap(err, `SELECT @@log_bin_basename`) } @@ -67,7 +63,7 @@ func mysqlDirs(db *sqlx.DB, variables []string) (dirs []string, err error) { dirs = append(dirs, filepath.Dir(binlogBase.String)) } - err = db.GetContext(ctx, &relaylogBase, `SELECT @@relay_log_basename`) + err = db.Get(&relaylogBase, `SELECT @@relay_log_basename`) if err != nil && !errors.Is(err, sql.ErrNoRows) { return nil, errors.Wrap(err, `SELECT @@relay_log_basename`) } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go index 2680f9fa25..0a1392a776 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go @@ -2,12 +2,10 @@ package ibdstatistic import ( - "context" "database/sql" "log/slog" "regexp" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" "github.com/jmoiron/sqlx" @@ -50,11 +48,8 @@ type ibdStatistic struct { // Run TODO func (c *ibdStatistic) Run() (msg string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var dataDir sql.NullString - err = c.db.GetContext(ctx, &dataDir, `SELECT @@datadir`) + err = c.db.Get(&dataDir, `SELECT @@datadir`) if err != nil { slog.Error("ibd-statistic", slog.String("error", err.Error())) return "", err @@ -71,10 +66,10 @@ func (c *ibdStatistic) Run() (msg string, err error) { return "", err } - //err = reportMetrics(result) - //if err != nil { - // return "", err - //} + err = reportMetrics(result) + if err != nil { + return "", err + } err = reportLog(result) if err != nil { diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/masterslaveheartbeat/master_slave_heartbeat.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/masterslaveheartbeat/master_slave_heartbeat.go index b45e679e75..e45c21f292 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/masterslaveheartbeat/master_slave_heartbeat.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/masterslaveheartbeat/master_slave_heartbeat.go @@ -50,9 +50,6 @@ type primaryDesc struct { } func (c *Checker) updateHeartbeat() error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - masterServerId := "" binlogFormatOld := "" err := c.db.QueryRow("select @@server_id, @@binlog_format"). @@ -81,7 +78,7 @@ func (c *Checker) updateHeartbeat() error { }() if config.MonitorConfig.MachineType == "spider" { - _, err := conn.ExecContext(ctx, "set tc_admin=0") + _, err := conn.ExecContext(context.Background(), "set tc_admin=0") if err != nil { slog.Error("master-slave-heartbeat", slog.String("error", err.Error())) return err @@ -95,18 +92,18 @@ func (c *Checker) updateHeartbeat() error { VALUES('%s', @@server_id, now(), sysdate(), timestampdiff(SECOND, now(),sysdate()))`, c.heartBeatTable, masterServerId) - if _, err = conn.ExecContext(ctx, txrrSQL); err != nil { + if _, err = conn.ExecContext(context.Background(), txrrSQL); err != nil { err := errors.Wrapf(err, "update heartbeat need SET SESSION tx_isolation = 'REPEATABLE-READ'") slog.Error("master-slave-heartbeat", slog.String("error", err.Error())) return err } - if _, err = conn.ExecContext(ctx, binlogSQL); err != nil { + if _, err = conn.ExecContext(context.Background(), binlogSQL); err != nil { err := errors.WithMessage(err, "update heartbeat need binlog_format=STATEMENT") slog.Error("master-slave-heartbeat", slog.String("error", err.Error())) return err } - res, err := conn.ExecContext(ctx, insertSQL) + res, err := conn.ExecContext(context.Background(), insertSQL) if err != nil { // 不再自动创建表 // merr.Number == 1146 || merr.Number == 1054 , c.initTableHeartbeat() diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_rotate.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_rotate.go index 352e579e4e..3a5a9c1d84 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_rotate.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_rotate.go @@ -52,17 +52,14 @@ func mysqlConnLogRotate(db *sqlx.DB) (string, error) { } func prepareRotate(db *sqlx.DB) (conn *sqlx.Conn, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - conn, err = db.Connx(ctx) + conn, err = db.Connx(context.Background()) if err != nil { slog.Error("connlog rotate get conn from db", slog.String("error", err.Error())) return nil, err } var _r interface{} - err = conn.GetContext(ctx, &_r, + err = conn.GetContext(context.Background(), &_r, `SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND TABLE_TYPE='BASE TABLE'`, cst.DBASchema, "conn_log") @@ -77,7 +74,7 @@ func prepareRotate(db *sqlx.DB) (conn *sqlx.Conn, err error) { } } - _, err = conn.ExecContext(ctx, `SET SQL_LOG_BIN=0`) + _, err = conn.ExecContext(context.Background(), `SET SQL_LOG_BIN=0`) if err != nil { slog.Error("disable binlog", slog.String("error", err.Error())) return nil, err @@ -109,10 +106,8 @@ func report(conn *sqlx.Conn) error { lf := ratelimit.Writer(f, ratelimit.NewBucketWithRate(float64(speedLimit), speedLimit)) - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() rows, err := conn.QueryxContext( - ctx, + context.Background(), fmt.Sprintf( `SELECT * FROM %s.conn_log WHERE conn_time >= DATE_SUB(NOW(), INTERVAL 1 DAY)`, cst.DBASchema, @@ -176,11 +171,8 @@ func clean(conn *sqlx.Conn) error { } func cleanOneRound(conn *sqlx.Conn) (affectedRows int64, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - r, err := conn.ExecContext( - ctx, + context.Background(), fmt.Sprintf( `DELETE FROM %s.conn_log WHERE conn_time < DATE_SUB(NOW(), INTERVAL 3 DAY) LIMIT 500`, cst.DBASchema, diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_size.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_size.go index c61fd88f57..036ae8fbe5 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_size.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlconnlog/connlog_size.go @@ -9,7 +9,6 @@ package mysqlconnlog import ( - "context" "fmt" "io/fs" "log/slog" @@ -17,18 +16,14 @@ import ( "path/filepath" "strings" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/internal/cst" "github.com/jmoiron/sqlx" ) func mysqlConnLogSize(db *sqlx.DB) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var dataDir string - err := db.QueryRowxContext(ctx, `SELECT @@datadir`).Scan(&dataDir) + err := db.QueryRowx(`SELECT @@datadir`).Scan(&dataDir) if err != nil { slog.Error("select @@datadir", slog.String("error", err.Error())) return "", err @@ -72,7 +67,7 @@ func mysqlConnLogSize(db *sqlx.DB) (string, error) { slog.Info("statistic conn log size", slog.Int64("size", logSize)) if logSize >= sizeLimit { - _, err = db.ExecContext(ctx, `SET GLOBAL INIT_CONNECT = ''`) + _, err = db.Exec(`SET GLOBAL INIT_CONNECT = ''`) if err != nil { slog.Error("disable init_connect", slog.String("error", err.Error()), diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/errlog_snapshot.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/errlog_snapshot.go index 87331e8b3f..8ca72f753b 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/errlog_snapshot.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/errlog_snapshot.go @@ -10,14 +10,11 @@ package mysqlerrlog import ( "bufio" - "context" "log/slog" "os" "path/filepath" "strconv" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" - "github.com/jmoiron/sqlx" ) @@ -99,11 +96,8 @@ func loadSnapShot() (*bufio.Scanner, error) { } func findErrLogFile(db *sqlx.DB) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var errLogPath, dataDir string - err := db.QueryRowxContext(ctx, `SELECT @@LOG_ERROR, @@DATADIR`).Scan(&errLogPath, &dataDir) + err := db.QueryRowx(`SELECT @@LOG_ERROR, @@DATADIR`).Scan(&errLogPath, &dataDir) if err != nil { slog.Error("query log_error, datadir", slog.String("error", err.Error())) return "", err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/mysql_errlog.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/mysql_errlog.go deleted file mode 100644 index 8fdb2febfe..0000000000 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlerrlog/mysql_errlog.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package mysqlerrlog TODO -package mysqlerrlog diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/mysql_lock.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/mysql_lock.go index 1775e0a001..82232cdd09 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/mysql_lock.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/mysql_lock.go @@ -41,7 +41,6 @@ func mysqlLock() (string, error) { return "", err } - //var locks []string for _, p := range processList { pstr, err := p.JsonString() if err != nil { @@ -66,6 +65,7 @@ func mysqlLock() (string, error) { "lock_command": p.Command.String, "lock_info": p.Info.String, "lock_host": p.Host.String, + "lock_state": p.State.String, }) slog.Debug("mysql lock check process", slog.Bool("has long wait for table flush", hasLongWait)) continue @@ -83,28 +83,13 @@ func mysqlLock() (string, error) { "lock_command": p.Command.String, "lock_info": p.Info.String, "lock_host": p.Host.String, + "lock_state": p.State.String, }) slog.Debug("mysql lock check process", slog.Bool("has normal lock", hasNormal)) continue } - //hasLongWait, err := hasLongWaitingForTableFlush(p) - //if err != nil { - // return "", err - //} - //slog.Debug("mysql lock check process", slog.Bool("has long wait for table flush", hasLongWait)) - // - //hasNormal, err := hasNormalLock(p) - //if err != nil { - // return "", err - //} - //slog.Debug("mysql lock check process", slog.Bool("has normal lock", hasNormal)) - // - //if hasLongWait || hasNormal { - // locks = append(locks, pstr) - //} } return "", nil - //return strings.Join(locks, ","), nil } func hasLongWaitingForTableFlush(p *mysqlProcess) (bool, error) { @@ -136,7 +121,6 @@ func hasNormalLock(p *mysqlProcess) (bool, error) { return false, nil } - // reExcludeCommands := regexp2.MustCompile(`^(?!.*(?:(^binlog|load data)))`, regexp2.IgnoreCase) reExcludeSql := regexp2.MustCompile(`(?=(?:(^binlog|load data)))`, regexp2.IgnoreCase) match, err = reExcludeSql.MatchString(p.Info.String) if err != nil { @@ -150,16 +134,4 @@ func hasNormalLock(p *mysqlProcess) (bool, error) { } return p.Time.Int64 > lockThreshold, nil - - //now := time.Now() - //if now.Hour() >= 21 && now.Hour() < 9 { - // if p.Time.Int64 > 300 { - // return true, nil - // } - //} else { - // if p.Time.Int64 > 5 { - // return true, nil - // } - //} - //return false, nil } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/processlist_snapshot.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/processlist_snapshot.go index 227f62b382..f864d17805 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/processlist_snapshot.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/mysqlprocesslist/processlist_snapshot.go @@ -9,7 +9,6 @@ package mysqlprocesslist import ( - "context" "database/sql" "encoding/json" "fmt" @@ -125,11 +124,7 @@ func loadSnapShot() ([]*mysqlProcess, error) { } func queryProcessList(db *sqlx.DB) ([]mysqlProcess, error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - rows, err := db.QueryxContext( - ctx, + rows, err := db.Queryx( `SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST`) if err != nil { slog.Error("show full processlist", slog.String("error", err.Error())) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxybackend/proxy_backend.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxybackend/proxy_backend.go index ad4c8709a1..4f763e7bda 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxybackend/proxy_backend.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxybackend/proxy_backend.go @@ -11,7 +11,6 @@ package proxybackend import ( "bufio" - "context" "fmt" "log/slog" "math/big" @@ -80,12 +79,8 @@ func (c *Checker) Run() (msg string, err error) { backendAddr := splitLine[1] - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - backendInfo := make(map[string]interface{}) - err = c.db.QueryRowxContext( - ctx, + err = c.db.QueryRowx( `SELECT * FROM BACKENDS`, ).MapScan(backendInfo) if err != nil { diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/backup.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/backup.go index 41949b0ae2..328fdcfc0c 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/backup.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/backup.go @@ -59,10 +59,7 @@ func (c *Checker) backupToBackend(users []string) error { } func writeOne(stmt *sqlx.Stmt, username, host string, now time.Time) error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - _, err := stmt.ExecContext( - ctx, + _, err := stmt.Exec( config.MonitorConfig.Ip, username, host, @@ -89,11 +86,8 @@ func (c *Checker) cleanOldBackup() error { return err } - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - _, err = conn.ExecContext( - ctx, + context.Background(), `DELETE FROM infodba_schema.proxy_user_list WHERE create_at < DATE(NOW() - INTERVAL 7 DAY)`, ) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/load_users.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/load_users.go index 926b84cf1f..e53e630faf 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/load_users.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/proxyuserlist/load_users.go @@ -2,7 +2,6 @@ package proxyuserlist import ( "bufio" - "context" "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "fmt" "log/slog" @@ -11,11 +10,8 @@ import ( ) func (c *Checker) loadUsersFromMem() ([]string, error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var usersFromQuery []string - err := c.adminDB.SelectContext(ctx, &usersFromQuery, `SELECT * FROM USERS`) + err := c.adminDB.Select(&usersFromQuery, `SELECT * FROM USERS`) if err != nil { slog.Error("query user list", slog.String("error", err.Error())) return nil, err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateproxyconnlog/init.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateproxyconnlog/init.go index a0129f5511..71094063d1 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateproxyconnlog/init.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateproxyconnlog/init.go @@ -2,7 +2,6 @@ package rotateproxyconnlog import ( "bytes" - "context" "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" "fmt" @@ -105,10 +104,7 @@ func (d *Dummy) Run() (msg string, err error) { } func (d *Dummy) disableConnlog() error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - _, err := d.db.ExecContext(ctx, `refresh_connlog(0)`) + _, err := d.db.Exec(`refresh_connlog(0)`) if err != nil { slog.Error("disable connlog", slog.String("error", err.Error())) return err @@ -118,10 +114,7 @@ func (d *Dummy) disableConnlog() error { } func (d *Dummy) enableConnlog() error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - _, err := d.db.ExecContext(ctx, `refresh_connlog(1)`) + _, err := d.db.Exec(`refresh_connlog(1)`) if err != nil { slog.Error("enable connlog", slog.String("error", err.Error())) return err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateslowlog/rotate_slowlog.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateslowlog/rotate_slowlog.go index 6605839d1c..2d5ca26af1 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateslowlog/rotate_slowlog.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/rotateslowlog/rotate_slowlog.go @@ -11,7 +11,6 @@ package rotateslowlog import ( "bytes" - "context" "fmt" "log/slog" "os" @@ -19,7 +18,6 @@ import ( "path/filepath" "time" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" "github.com/jmoiron/sqlx" @@ -47,13 +45,9 @@ qq{set global slow_query_log=\@sq_log_save}, // Run 运行 func (d *Dummy) Run() (msg string, err error) { - queryCtx, queryCancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer queryCancel() - var slowLogPath string var slowLogOn bool - err = d.db.QueryRowxContext( - queryCtx, + err = d.db.QueryRowx( `SELECT @@slow_query_log, @@slow_query_log_file`, ).Scan(&slowLogOn, &slowLogPath) if err != nil { @@ -135,9 +129,7 @@ func (d *Dummy) Run() (msg string, err error) { return "", err } - execCtx, execCancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer execCancel() - _, err = d.db.ExecContext(execCtx, `FLUSH SLOW LOGS`) + _, err = d.db.Exec(`FLUSH SLOW LOGS`) if err != nil { slog.Error("flush slow logs", slog.String("error", err.Error())) return "", err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/engineinnodbstatus.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/engineinnodbstatus.go index d7f0fc32ae..c9758319b5 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/engineinnodbstatus.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/engineinnodbstatus.go @@ -1,12 +1,10 @@ package scenesnapshot import ( - "context" "fmt" "github.com/jmoiron/sqlx" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/internal/archivescenes" ) @@ -40,11 +38,7 @@ func engineInnodbStatusScene(db *sqlx.DB) error { } func queryEngineInnodbStatus(db *sqlx.DB) (res []*engineInnodbStatus, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - err = db.SelectContext( - ctx, + err = db.Select( &res, `SHOW ENGINE INNODB STATUS`, ) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/processlist.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/processlist.go index 34b26e403a..bcbad12b8c 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/processlist.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/processlist.go @@ -2,7 +2,6 @@ package scenesnapshot import ( "bytes" - "context" "database/sql" "log/slog" @@ -10,7 +9,6 @@ import ( "github.com/olekukonko/tablewriter" "github.com/spf13/cast" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/scenesnapshot/internal/archivescenes" ) @@ -28,11 +26,7 @@ type mysqlProcess struct { var processListName = "processlist" func queryProcesslist(db *sqlx.DB) (res []*mysqlProcess, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - err = db.SelectContext( - ctx, + err = db.Select( &res, `SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST`, ) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/ctl_replicate.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/ctl_replicate.go index 443e761849..f27209b79a 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/ctl_replicate.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/ctl_replicate.go @@ -9,11 +9,9 @@ package slavestatus import ( - "context" "fmt" "log/slog" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" ) @@ -76,12 +74,9 @@ func (c *ctlReplicateChecker) Run() (msg string, err error) { } func (c *ctlReplicateChecker) isPrimary() (bool, error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - res := getPrimaryRes{} - err := c.db.QueryRowxContext(ctx, `TDBCTL GET PRIMARY`).StructScan(&res) + err := c.db.QueryRowx(`TDBCTL GET PRIMARY`).StructScan(&res) if err != nil { slog.Error("TDBCTL GET PRIMARY", slog.String("error", err.Error())) return false, err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/slave_status.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/slave_status.go index 3f17b46999..067b898926 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/slave_status.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/slavestatus/slave_status.go @@ -9,7 +9,6 @@ package slavestatus import ( - "context" "fmt" "log/slog" "slices" @@ -17,7 +16,6 @@ import ( "strings" "time" - "dbm-services/mysql/db-tools/mysql-monitor/pkg/config" "dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface" "github.com/jmoiron/sqlx" @@ -79,11 +77,7 @@ func (s *slaveStatusChecker) Run() (msg string, err error) { } func (s *slaveStatusChecker) skipErr() error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - _, err := s.db.ExecContext( - ctx, + _, err := s.db.Exec( `STOP SLAVE SQL_THREAD;SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;START SLAVE SQL_THREAD`, ) if err != nil { @@ -174,10 +168,7 @@ func (s *slaveStatusChecker) collectError() (string, error) { } func (s *slaveStatusChecker) fetchSlaveStatus() error { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - - rows, err := s.db.QueryxContext(ctx, `SHOW SLAVE STATUS`) + rows, err := s.db.Queryx(`SHOW SLAVE STATUS`) if err != nil { slog.Error("show slave status", slog.String("error", err.Error())) return err diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/spiderremote/spider_remote.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/spiderremote/spider_remote.go index 5b9fa8b086..d3c8e1ca40 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/spiderremote/spider_remote.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/spiderremote/spider_remote.go @@ -9,7 +9,6 @@ package spiderremote import ( - "context" "encoding/json" "fmt" "hash/crc32" @@ -37,16 +36,12 @@ type spiderRemoteCheck struct { } func (c *spiderRemoteCheck) Run() (msg string, err error) { - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var res []struct { ServerName string `db:"Server_name"` Host string `db:"Host"` Port int `db:"Port"` } - err = c.db.SelectContext( - ctx, + err = c.db.Select( &res, `SELECT Server_name, Host, Port FROM mysql.servers WHERE Wrapper = 'mysql' ORDER BY Server_name`) if err != nil { diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/timezonechange/mysqltimezonechange.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/timezonechange/mysqltimezonechange.go index 9521b6a6c5..78de14e3cf 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/timezonechange/mysqltimezonechange.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/timezonechange/mysqltimezonechange.go @@ -2,7 +2,6 @@ package timezonechange import ( "bufio" - "context" "fmt" "log/slog" "os" @@ -20,11 +19,8 @@ func mysqlTzChange(db *sqlx.DB) (msg string, err error) { } slog.Info("get last mysql tz", slog.String("tz", lastTz)) - ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) - defer cancel() - var currentTz string - err = db.QueryRowxContext(ctx, `SELECT @@time_zone`).Scan(¤tTz) + err = db.QueryRowx(`SELECT @@time_zone`).Scan(¤tTz) if err != nil { return "", err } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/mainloop/main_loop.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/mainloop/main_loop.go index d89245a0f3..0e5aa5d518 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/mainloop/main_loop.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/mainloop/main_loop.go @@ -42,18 +42,22 @@ func Run(hardcode bool) error { lockFileName := fmt.Sprintf("%d-%s.lock", config.MonitorConfig.Port, strings.Join(iNames, ".")) lockFilePath := filepath.Join(cst.MySQLMonitorInstallPath, lockFileName) - slog.Info("man loop", slog.String("lockFilePath", lockFilePath)) + slog.Info("main loop", slog.String("lockFilePath", lockFilePath)) lk := fslock.New(lockFilePath) err := lk.TryLock() if err != nil { slog.Error("main loop", slog.String("error", err.Error())) - utils.SendMonitorEvent("db-hang", lockFileName) + utils.SendMonitorEvent( + "db-hang", + fmt.Sprintf("last round %s not finish, db may be hang", strings.Join(iNames, ",")), + ) return errors.Wrapf(err, "main loop lock file %s failed, may be last round not finish", lockFilePath) } defer func() { _ = lk.Unlock() }() + slog.Info("main loop get lock success", slog.String("lockFilePath", lockFilePath)) if hardcode && slices.Index(iNames, config.HeartBeatName) >= 0 { utils.SendMonitorMetrics(config.HeartBeatName, 1, nil) diff --git a/dbm-ui/backend/db_proxy/reverse_api/common/impl/__init__.py b/dbm-ui/backend/db_proxy/reverse_api/common/impl/__init__.py index 74292829f3..c2ac44a43d 100644 --- a/dbm-ui/backend/db_proxy/reverse_api/common/impl/__init__.py +++ b/dbm-ui/backend/db_proxy/reverse_api/common/impl/__init__.py @@ -8,4 +8,4 @@ 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. """ -from .list_nginx_ips import list_nginx_ips +from .list_nginx_addrs import list_nginx_addrs diff --git a/dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_ips.py b/dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_addrs.py similarity index 87% rename from dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_ips.py rename to dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_addrs.py index 84fc0d6a35..d486a36eaa 100644 --- a/dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_ips.py +++ b/dbm-ui/backend/db_proxy/reverse_api/common/impl/list_nginx_addrs.py @@ -14,6 +14,6 @@ from backend.db_proxy.models import DBExtension -def list_nginx_ips(bk_cloud_id: int) -> List[str]: +def list_nginx_addrs(bk_cloud_id: int) -> List[str]: nginx = DBExtension.get_extension_in_cloud(bk_cloud_id=bk_cloud_id, extension_type=ExtensionType.NGINX.value) - return [n.details["ip"] for n in nginx] + return ["{}:{}".format(n.details["ip"], n.details["dbm_port"]) for n in nginx] diff --git a/dbm-ui/backend/db_proxy/reverse_api/common/views.py b/dbm-ui/backend/db_proxy/reverse_api/common/views.py index e5d38d8ba3..1e2de36197 100644 --- a/dbm-ui/backend/db_proxy/reverse_api/common/views.py +++ b/dbm-ui/backend/db_proxy/reverse_api/common/views.py @@ -15,24 +15,24 @@ from backend.bk_web.swagger import common_swagger_auto_schema from backend.db_proxy.reverse_api.base_reverse_api_view import BaseReverseApiView -from backend.db_proxy.reverse_api.common.impl import list_nginx_ips +from backend.db_proxy.reverse_api.common.impl import list_nginx_addrs from backend.db_proxy.reverse_api.decorators import reverse_api logger = logging.getLogger("root") class CommonReverseApiView(BaseReverseApiView): - @common_swagger_auto_schema(operation_summary=_("获取NGINX IP")) - @reverse_api(url_path="list_nginx_ips") - def list_nginx_ips(self, request, *args, **kwargs): + @common_swagger_auto_schema(operation_summary=_("获取NGINX 地址")) + @reverse_api(url_path="list_nginx_addrs") + def list_nginx_addrs(self, request, *args, **kwargs): """ - 返回特定云区域的 NGINX IP 列表 + 返回特定云区域的 NGINX 地址 列表 param: bk_cloud_id: int - return: ["ip1", "ip2", ...] + return: ["ip1:90", "ip2:90", ...] """ bk_cloud_id, _, _ = self.get_api_params() logger.info(f"bk_cloud_id: {bk_cloud_id}") - res = list_nginx_ips(bk_cloud_id=bk_cloud_id) + res = list_nginx_addrs(bk_cloud_id=bk_cloud_id) logger.info(f"res: {res}") return JsonResponse( diff --git a/dbm-ui/backend/db_proxy/reverse_api/mysql/impl/list_instance_info.py b/dbm-ui/backend/db_proxy/reverse_api/mysql/impl/list_instance_info.py index e1a8c9386c..bdc1f13917 100644 --- a/dbm-ui/backend/db_proxy/reverse_api/mysql/impl/list_instance_info.py +++ b/dbm-ui/backend/db_proxy/reverse_api/mysql/impl/list_instance_info.py @@ -35,7 +35,7 @@ def list_instance_info(bk_cloud_id: int, ip: str, port_list: Optional[List[int]] def list_storageinstance_info(q: Q) -> List: res = [] for i in StorageInstance.objects.filter(q).prefetch_related( - "as_ejector__receiver__machine", "as_receiver__ejector__machine", "machine" + "as_ejector__receiver__machine", "as_receiver__ejector__machine", "machine", "cluster" ): receivers = [] ejectors = [] @@ -58,8 +58,11 @@ def list_storageinstance_info(q: Q) -> List: { "ip": i.machine.ip, "port": i.port, + "immute_domain": i.cluster.all()[0].immute_domain, "phase": i.phase, "status": i.status, + "access_layer": i.access_layer, + "machine_type": i.machine_type, "is_standby": i.is_stand_by, "instance_role": i.instance_role, "instance_inner_role": i.instance_inner_role, @@ -73,13 +76,26 @@ def list_storageinstance_info(q: Q) -> List: def list_proxyinstance_info(q: Q) -> List: res = [] - for i in ProxyInstance.objects.filter(q).prefetch_related("machine"): + for i in ProxyInstance.objects.filter(q).prefetch_related("machine", "storageinstance__machine", "cluster"): + storageinstance_list = [] + for si in i.storageinstance.all(): + storageinstance_list.append( + { + "ip": si.machine.ip, + "port": si.port, + } + ) + res.append( { "ip": i.machine.ip, "port": i.port, + "immute_domain": i.cluster.all()[0].immute_domain, "phase": i.phase, "status": i.status, + "access_layer": i.access_layer, + "machine_type": i.machine_type, + "storageinstance_list": storageinstance_list, } )