-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mysql): mysql-monitor增加disable子命令 close #1024
- Loading branch information
Showing
5 changed files
with
257 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
dbm-services/mysql/db-tools/mysql-monitor/cmd/reschedule_items.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
ma "dbm-services/mysql/db-tools/mysql-crond/api" | ||
"dbm-services/mysql/db-tools/mysql-monitor/pkg/config" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
func reschedule(configFileDir, configFileName, staff string) error { | ||
manager := ma.NewManager(config.MonitorConfig.ApiUrl) | ||
entries, err := manager.Entries() | ||
if err != nil { | ||
slog.Error("reschedule list entries", err) | ||
return err | ||
} | ||
|
||
for _, entry := range entries { | ||
if strings.HasPrefix( | ||
entry.Job.Name, | ||
fmt.Sprintf("mysql-monitor-%d", config.MonitorConfig.Port), | ||
) { | ||
eid, err := manager.Delete(entry.Job.Name, true) | ||
if err != nil { | ||
slog.Error( | ||
"reschedule delete entry", err, | ||
slog.String("name", entry.Job.Name), | ||
) | ||
return err | ||
} | ||
slog.Info( | ||
"reschedule delete entry", | ||
slog.String("name", entry.Job.Name), | ||
slog.Int("ID", eid), | ||
) | ||
} | ||
} | ||
|
||
var hardCodeItems []*config.MonitorItem | ||
itemGroups := make(map[string][]*config.MonitorItem) | ||
for _, ele := range config.ItemsConfig { | ||
// 硬编码监控项先排除掉 | ||
if ele.Name == "db-up" || ele.Name == config.HeartBeatName { | ||
if ele.IsEnable() { | ||
hardCodeItems = append(hardCodeItems, ele) | ||
} | ||
continue | ||
} | ||
|
||
if ele.IsEnable() && ele.IsMatchMachineType() && ele.IsMatchRole() { | ||
var key string | ||
|
||
if ele.Schedule == nil { | ||
key = config.MonitorConfig.DefaultSchedule | ||
} else { | ||
key = *ele.Schedule | ||
} | ||
|
||
if _, ok := itemGroups[key]; !ok { | ||
itemGroups[key] = []*config.MonitorItem{} | ||
} | ||
itemGroups[key] = append(itemGroups[key], ele) | ||
} | ||
} | ||
|
||
for k, v := range itemGroups { | ||
var itemNames []string | ||
for _, j := range v { | ||
itemNames = append(itemNames, j.Name) | ||
} | ||
args := []string{ | ||
"run", | ||
"--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, | ||
) | ||
if err != nil { | ||
slog.Error("reschedule add entry", err) | ||
return err | ||
} | ||
slog.Info("reschedule add entry", slog.Int("entry id", eid)) | ||
} | ||
|
||
// 注册 hardcode | ||
for _, j := range hardCodeItems { | ||
args := []string{ | ||
"hardcode-run", | ||
"--items", j.Name, //strings.Join(itemNames, ","), | ||
"-c", configFileName, | ||
} | ||
|
||
eid, err := manager.CreateOrReplace( | ||
ma.JobDefine{ | ||
Name: fmt.Sprintf( | ||
"mysql-monitor-%d-hardcode-%s", config.MonitorConfig.Port, j.Name), | ||
Command: executable, | ||
Args: args, | ||
Schedule: config.HardCodeSchedule, | ||
Creator: staff, //viper.GetString("staff"), | ||
Enable: true, | ||
WorkDir: configFileDir, | ||
}, true, | ||
) | ||
if err != nil { | ||
slog.Error("reschedule add hardcode entry", err) | ||
return err | ||
} | ||
slog.Info("reschedule add hardcode entry", slog.Int("entry id", eid)) | ||
} | ||
|
||
return nil | ||
} |
86 changes: 86 additions & 0 deletions
86
dbm-services/mysql/db-tools/mysql-monitor/cmd/subcmd_disable_all.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"golang.org/x/exp/slog" | ||
|
||
"dbm-services/mysql/db-tools/mysql-monitor/pkg/config" | ||
) | ||
|
||
var subCmdDisableAll = &cobra.Command{ | ||
Use: "disable-all", | ||
Short: "disable-all items", | ||
Long: "disable-all items", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
configPath := viper.GetString("disable-config") | ||
if !filepath.IsAbs(configPath) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
slog.Error("disable-all get config abs path", err) | ||
return err | ||
} | ||
configPath = filepath.Join(cwd, configPath) | ||
} | ||
configFileDir, configFileName := filepath.Split(configPath) | ||
|
||
err := config.InitConfig(configPath) | ||
if err != nil { | ||
return err | ||
} | ||
initLogger(config.MonitorConfig.Log) | ||
|
||
emptyItemsConfig, err := os.CreateTemp("/tmp", "empty-items.yaml") | ||
if err != nil { | ||
slog.Error("disable-all create empty items config", slog.String("error", err.Error())) | ||
return err | ||
} | ||
defer func() { | ||
_ = emptyItemsConfig.Close() | ||
_ = os.Remove(emptyItemsConfig.Name()) | ||
}() | ||
slog.Info("disable-all create empty items config success") | ||
|
||
config.MonitorConfig.ItemsConfigFile = emptyItemsConfig.Name() | ||
|
||
err = config.LoadMonitorItemsConfig() | ||
if err != nil { | ||
slog.Error("disable-all load items", err) | ||
return err | ||
} | ||
|
||
disableDbUp := viper.GetBool("with-db-up") | ||
if !disableDbUp { | ||
config.InjectMonitorDbUpItem() | ||
} | ||
config.InjectMonitorHeartBeatItem() | ||
|
||
slog.Info("disable-all", | ||
slog.String("staff", viper.GetString("staff"))) | ||
err = reschedule(configFileDir, configFileName, viper.GetString("disable-staff")) | ||
if err != nil { | ||
slog.Error("disable-all sub-cmd", slog.String("error", err.Error())) | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
subCmdDisableAll.PersistentFlags().StringP("config", "c", "", "config file") | ||
_ = subCmdDisableAll.MarkPersistentFlagRequired("config") | ||
_ = viper.BindPFlag("disable-config", subCmdDisableAll.PersistentFlags().Lookup("config")) | ||
|
||
subCmdDisableAll.PersistentFlags().StringP("staff", "", "", "staff name") | ||
_ = subCmdDisableAll.MarkPersistentFlagRequired("staff") | ||
_ = viper.BindPFlag("disable-staff", subCmdDisableAll.PersistentFlags().Lookup("staff")) | ||
|
||
subCmdDisableAll.PersistentFlags().BoolP("with-db-up", "", false, "also disable db-up") | ||
_ = viper.BindPFlag("with-db-up", subCmdDisableAll.PersistentFlags().Lookup("with-db-up")) | ||
|
||
rootCmd.AddCommand(subCmdDisableAll) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.