opt.: conf

This commit is contained in:
lollipopkit 2023-07-25 14:21:39 +08:00
parent 6e581bc863
commit 8fefb56032
5 changed files with 114 additions and 29 deletions

74
cmd/conf.go Normal file
View File

@ -0,0 +1,74 @@
package cmd
import (
// "strings"
// "github.com/lollipopkit/gommon/res"
// "github.com/lollipopkit/gommon/term"
"github.com/lollipopkit/server_box_monitor/model"
"github.com/urfave/cli/v2"
)
func init() {
cmds = append(cmds, &cli.Command{
Name: "conf",
Aliases: []string{"c"},
Usage: "Config file related commands",
Subcommands: []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize config file",
Action: handleConfInit,
},
// {
// Name: "edit",
// Aliases: []string{"e"},
// Usage: "Edit config file",
// Action: handleConfEdit,
// },
},
})
}
func handleConfInit(c *cli.Context) error {
return model.InitConfig()
}
// func handleConfEdit(c *cli.Context) error {
// if err := model.ReadAppConfig(); err != nil {
// return err
// }
// typeOptions := []string{"interval", "rate", "name", "rules", "pushes", "exit"}
// opOptions := []string{"add", "remove", "edit", "exit"}
// for {
// ruleIds := []string{}
// for _, rule := range model.Config.Rules {
// ruleIds = append(ruleIds, rule.Id())
// }
// pushNames := []string{}
// for _, push := range model.Config.Pushes {
// pushNames = append(pushNames, push.Name)
// }
// var buf strings.Builder
// buf.WriteString(res.GREEN + "interval: " + res.NOCOLOR + model.Config.Interval + "\n")
// buf.WriteString(res.GREEN + "rate: " + res.NOCOLOR + model.Config.Rate + "\n")
// buf.WriteString(res.GREEN + "name: " + res.NOCOLOR + model.Config.Name + "\n")
// buf.WriteString(res.GREEN + "rules: " + res.NOCOLOR + strings.Join(ruleIds, " | ") + "\n")
// buf.WriteString(res.GREEN + "pushes: " + res.NOCOLOR + strings.Join(pushNames, " | ") + "\n")
// print(buf.String())
// op := term.Option("What to do?", opOptions, len(opOptions)-1)
// if op == 3 {
// break
// }
// question := "Which type to " + opOptions[op] + "?"
// typ := term.Option(question, typeOptions, len(typeOptions)-1)
// switch typ {
// case 0:
// }
// return nil
// }

4
go.mod
View File

@ -3,8 +3,8 @@ module github.com/lollipopkit/server_box_monitor
go 1.20
require (
github.com/labstack/echo/v4 v4.11.0
github.com/lollipopkit/gommon v0.4.0
github.com/labstack/echo/v4 v4.11.1
github.com/lollipopkit/gommon v0.4.3
github.com/urfave/cli/v2 v2.25.7
)

6
go.sum
View File

@ -7,10 +7,12 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/labstack/echo/v4 v4.11.0 h1:4Dmi59tmrnFzOchz4EXuGjJhUfcEkU28iDKsiZVOQgw=
github.com/labstack/echo/v4 v4.11.0/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lollipopkit/gommon v0.4.0 h1:iBXcSvmOebBM+dR1/tkDZBmahDlyUuoVd/fT6vTQ5M4=
github.com/lollipopkit/gommon v0.4.0/go.mod h1:DEnIxhHmPQjDSkKFDxwX6oFxMjlHd87G+Dt7U4ZUyRs=
github.com/lollipopkit/gommon v0.4.3 h1:TXNJEs+PsBF44cfXFOehd6eeQBGX8zmBz+utjCpgHoQ=
github.com/lollipopkit/gommon v0.4.3/go.mod h1:DEnIxhHmPQjDSkKFDxwX6oFxMjlHd87G+Dt7U4ZUyRs=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=

View File

@ -1,6 +1,7 @@
package model
import (
"bytes"
"encoding/json"
"os"
"strconv"
@ -31,22 +32,30 @@ type AppConfig struct {
Pushes []Push `json:"pushes"`
}
func InitConfig() error {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", "\t")
err := enc.Encode(DefaultAppConfig)
if err != nil {
log.Err("[CONFIG] marshal default app config failed: %v", err)
return err
}
err = os.WriteFile(res.AppConfigPath, buf.Bytes(), 0644)
if err != nil {
log.Err("[CONFIG] write default app config failed: %v", err)
return err
}
Config = DefaultAppConfig
return nil
}
func ReadAppConfig() error {
defer initInterval()
defer initRateLimiter()
if !sys.Exist(res.AppConfigPath) {
configBytes, err := json.MarshalIndent(DefaultAppConfig, "", "\t")
if err != nil {
log.Err("[CONFIG] marshal default app config failed: %v", err)
return err
}
err = os.WriteFile(res.AppConfigPath, configBytes, 0644)
if err != nil {
log.Err("[CONFIG] write default app config failed: %v", err)
return err
}
Config = DefaultAppConfig
return nil
return InitConfig()
}
configBytes, err := os.ReadFile(res.AppConfigPath)
@ -142,20 +151,15 @@ var (
defaultWebhookIfaceBytes, _ = json.Marshal(defaultWebhookIface)
DefaultAppConfig = &AppConfig{
Version: 2,
Interval: "7s",
Rate: "1/1m",
Name: "Server1",
Version: res.ConfVersion,
Interval: res.DefaultIntervalStr,
Rate: res.DefaultRateStr,
Name: res.DefaultSeverName,
Rules: []Rule{
{
MonitorType: MonitorTypeCPU,
Threshold: ">=77%",
Matcher: "0",
},
{
MonitorType: MonitorTypeNetwork,
Threshold: ">=7.7m/s",
Matcher: "eth0",
Threshold: `>=77%`,
Matcher: "cpu",
},
},
Pushes: []Push{

View File

@ -33,8 +33,13 @@ var (
)
const (
DefaultInterval = time.Second * 7
MaxInterval = time.Second * 10
ConfVersion = 2
DefaultInterval = time.Second * 7
DefaultIntervalStr = "7s"
DefaultRateStr = "1/1m"
DefaultSeverName = "Server 1"
MaxInterval = time.Second * 10
PushFormatMsgLocator = "{{msg}}"
PushFormatNameLocator = "{{name}}"