Skip to content

Commit dd43e3e

Browse files
committed
new & opt.
- new: custom web `listen addr` - opt.: web api `net`
1 parent 4c2d565 commit dd43e3e

File tree

11 files changed

+51
-31
lines changed

11 files changed

+51
-31
lines changed

cmd/serve.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ func init() {
1111
Aliases: []string{"s"},
1212
Usage: "Run monitor",
1313
Action: handleServe,
14+
Flags: []cli.Flag{
15+
&cli.StringFlag{
16+
Name: "addr",
17+
Aliases: []string{"a"},
18+
Usage: "Listen address",
19+
Value: "0.0.0.0:3770",
20+
EnvVars: []string{"SBM_ADDR"},
21+
},
22+
},
1423
})
1524
}
1625

1726
func handleServe(ctx *cli.Context) error {
18-
runner.Start()
27+
runner.Start(ctx.String("addr"))
1928
return nil
2029
}

docker-compose.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

model/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/lollipopkit/gommon/log"
11-
"github.com/lollipopkit/gommon/sys"
1211
"github.com/lollipopkit/gommon/rate"
12+
"github.com/lollipopkit/gommon/sys"
1313
"github.com/lollipopkit/server_box_monitor/res"
1414
)
1515

@@ -26,7 +26,7 @@ type AppConfig struct {
2626
// Values bigger than 10 seconds are not allowed.
2727
Interval string `json:"interval"`
2828
Rate string `json:"rate"`
29-
Name string `json:"name"`
29+
Name string `json:"name"`
3030
Rules []Rule `json:"rules"`
3131
Pushes []Push `json:"pushes"`
3232
}
@@ -122,7 +122,7 @@ var (
122122
"action": "send_group_msg",
123123
"params": map[string]interface{}{
124124
"group_id": 123456789,
125-
"message": "Server1\n"+res.PushFormatLocator,
125+
"message": "Server1\n" + res.PushFormatLocator,
126126
},
127127
}
128128
defaultWekhookBodyBytes, _ = json.Marshal(defaultWebhookBody)
@@ -143,7 +143,7 @@ var (
143143
Version: 2,
144144
Interval: "7s",
145145
Rate: "1/1m",
146-
Name: "Server1",
146+
Name: "Server1",
147147
Rules: []Rule{
148148
{
149149
MonitorType: MonitorTypeCPU,

model/push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (pf PushFormat) Format(args []*PushPair) string {
7979
ss = append(ss, kv)
8080
}
8181
return strings.Replace(
82-
string(pf),
83-
res.PushFormatLocator,
82+
string(pf),
83+
res.PushFormatLocator,
8484
strings.Join(ss, `\n`), 1)
8585
}
8686

model/rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,10 @@ func (r *Rule) shouldNotifyNetwork(s []networkStatus, t *Threshold) (bool, *Push
285285
case ThresholdTypeSize:
286286
size := Size(0)
287287
if in {
288-
size += net.TimeSequence.New.Receive
288+
size += net.Receive()
289289
}
290290
if out {
291-
size += net.TimeSequence.New.Transmit
291+
size += net.Transmit()
292292
}
293293
ok, err := t.True(size)
294294
if err != nil {

model/status.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ func (ns *networkStatus) ReceiveSpeed() (Size, error) {
105105
diff := float64(ns.TimeSequence.New.Receive - ns.TimeSequence.Old.Receive)
106106
return Size(diff / CheckInterval.Seconds()), nil
107107
}
108+
func (ns *networkStatus) Transmit() Size {
109+
return ns.TimeSequence.New.Transmit
110+
}
111+
func (ns *networkStatus) Receive() Size {
112+
return ns.TimeSequence.New.Receive
113+
}
108114

109115
type AllNetworkStatus []networkStatus
110116

@@ -130,9 +136,23 @@ func (nss AllNetworkStatus) ReceiveSpeed() (Size, error) {
130136
}
131137
return Size(sum), nil
132138
}
139+
func (nss AllNetworkStatus) Transmit() Size {
140+
var sum float64
141+
for _, ns := range nss {
142+
sum += float64(ns.Transmit())
143+
}
144+
return Size(sum)
145+
}
146+
func (nss AllNetworkStatus) Receive() Size {
147+
var sum float64
148+
for _, ns := range nss {
149+
sum += float64(ns.Receive())
150+
}
151+
return Size(sum)
152+
}
133153

134154
func RefreshStatus() error {
135-
output, _ := sys.Execute("bash", res.ServerBoxShellPath)
155+
output, _ := sys.Execute("sh", res.ServerBoxShellPath)
136156
err := os.WriteFile(filepath.Join(res.ServerBoxDirPath, "shell_output.log"), []byte(output), 0644)
137157
if err != nil {
138158
log.Warn("[STATUS] write shell output log failed: %s", err)

res/res.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"time"
88

99
"github.com/lollipopkit/gommon/log"
10-
"github.com/lollipopkit/gommon/sys"
1110
"github.com/lollipopkit/gommon/rate"
11+
"github.com/lollipopkit/gommon/sys"
1212
)
1313

1414
var (

runner/runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func init() {
3232
}
3333
}
3434

35-
func Start() {
36-
go runWeb()
35+
func Start(addr string) {
36+
go runWeb(addr)
3737
go runCheck()
3838
// 阻塞主线程
3939
select {}
@@ -98,7 +98,7 @@ func runCheck() {
9898
}
9999
}
100100

101-
func runWeb() {
101+
func runWeb(addr string) {
102102
e := echo.New()
103103

104104
e.Use(middleware.Recover())
@@ -110,5 +110,5 @@ func runWeb() {
110110

111111
e.GET("/status", web.Status)
112112

113-
e.Logger.Fatal(e.Start("0.0.0.0:3770"))
113+
e.Logger.Fatal(e.Start(addr))
114114
}

web/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func ok(c echo.Context, data any) error {
1212
func fail(c echo.Context, code int, data any) error {
1313
return c.JSON(500, map[string]any{
1414
"code": code,
15-
"msg": data,
15+
"msg": data,
1616
})
17-
}
17+
}

web/codes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ type respCode uint8
55
const (
66
respCodeOK respCode = iota
77
respCodeFail
8-
)
8+
)

0 commit comments

Comments
 (0)