#10 fix: net matcher empty -> calculate all iface

This commit is contained in:
lollipopkit 2023-08-03 17:03:28 +08:00
parent 9cd4ec1f0a
commit d84ceccf28
6 changed files with 76 additions and 26 deletions

View File

@ -255,7 +255,7 @@ func (p PushIfaceBark) push(args []*PushPair) error {
titleEscape := url.QueryEscape(title)
bodyEscape := url.QueryEscape(body)
url_ := fmt.Sprintf(
"%s/%s/%s/%s",
"%s/%s/%s/%s",
p.Server, p.Key, titleEscape, bodyEscape,
)
resp, code, err := http.Do("GET", url_, nil, nil)

View File

@ -235,17 +235,22 @@ func (r *Rule) shouldNotifyNetwork(s []networkStatus, t *Threshold) (bool, *Push
return false, nil, nil
}
var net networkStatus
var have bool
for _, n := range s {
if strings.Contains(r.Matcher, n.Interface) {
net = n
have = true
break
var net networkIface
// 如果 matcher 为空,则默认计算所有网卡
if len(s) == 0 {
net = AllNetworkStatus(Status.Network)
} else {
var have bool
for _, n := range s {
if strings.Contains(r.Matcher, n.Interface) {
net = n
have = true
break
}
}
if !have {
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("network interface not found: %s", r.Matcher))
}
}
if !have {
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("network interface not found: %s", r.Matcher))
}
// 判断是否计算出/入流量

View File

@ -87,29 +87,37 @@ type networkOneTimeStatus struct {
Transmit Size
Receive Size
}
type networkIface interface {
TransmitSpeed() (Size, error)
ReceiveSpeed() (Size, error)
Transmit() Size
Receive() Size
}
type networkStatus struct {
Interface string
TimeSequence[networkOneTimeStatus]
}
func (ns *networkStatus) TransmitSpeed() (Size, error) {
func (ns networkStatus) TransmitSpeed() (Size, error) {
if ns.TimeSequence.New == nil || ns.TimeSequence.Old == nil {
return 0, ErrNotReady
}
diff := float64(ns.TimeSequence.New.Transmit - ns.TimeSequence.Old.Transmit)
return Size(diff / CheckInterval.Seconds()), nil
}
func (ns *networkStatus) ReceiveSpeed() (Size, error) {
func (ns networkStatus) ReceiveSpeed() (Size, error) {
if ns.TimeSequence.New == nil || ns.TimeSequence.Old == nil {
return 0, ErrNotReady
}
diff := float64(ns.TimeSequence.New.Receive - ns.TimeSequence.Old.Receive)
return Size(diff / CheckInterval.Seconds()), nil
}
func (ns *networkStatus) Transmit() Size {
func (ns networkStatus) Transmit() Size {
return ns.TimeSequence.New.Transmit
}
func (ns *networkStatus) Receive() Size {
func (ns networkStatus) Receive() Size {
return ns.TimeSequence.New.Receive
}
@ -170,23 +178,23 @@ func ParseStatus(s string) error {
if len(segments) != 7 {
return errors.Join(ErrInvalidShellOutput, fmt.Errorf("expect 7 segments, but got %d", len(segments)))
}
err := parseNetworkStatus(segments[1])
err := ParseNetworkStatus(segments[1])
if err != nil {
log.Warn("parse network status failed: %s", err)
}
err = parseCPUStatus(segments[2])
err = ParseCPUStatus(segments[2])
if err != nil {
log.Warn("parse cpu status failed: %s", err)
}
err = parseDiskStatus(segments[3])
err = ParseDiskStatus(segments[3])
if err != nil {
log.Warn("parse disk status failed: %s", err)
}
err = parseMemAndSwapStatus(segments[4])
err = ParseMemAndSwapStatus(segments[4])
if err != nil {
log.Warn("parse mem status failed: %s", err)
}
err = parseTemperatureStatus(segments[5], segments[6])
err = ParseTemperatureStatus(segments[5], segments[6])
if err != nil {
log.Warn("parse temperature status failed: %s", err)
}
@ -204,7 +212,7 @@ func initSwap() {
}
}
func parseMemAndSwapStatus(s string) error {
func ParseMemAndSwapStatus(s string) error {
initMem()
initSwap()
lines := strings.Split(s, "\n")
@ -243,7 +251,7 @@ func parseMemAndSwapStatus(s string) error {
return nil
}
func parseCPUStatus(s string) error {
func ParseCPUStatus(s string) error {
lines := strings.Split(strings.TrimSpace(s), "\n")
count := len(lines)
if len(Status.CPU) != count {
@ -277,7 +285,7 @@ func parseCPUStatus(s string) error {
return nil
}
func parseDiskStatus(s string) error {
func ParseDiskStatus(s string) error {
lines := strings.Split(strings.TrimSpace(s), "\n")
lines = lines[1:]
count := len(lines)
@ -321,7 +329,7 @@ func parseDiskStatus(s string) error {
return nil
}
func parseTemperatureStatus(s1, s2 string) error {
func ParseTemperatureStatus(s1, s2 string) error {
if strings.Contains(s1, "/sys/class/thermal/thermal_zone*/type") {
return nil
}
@ -345,7 +353,7 @@ func parseTemperatureStatus(s1, s2 string) error {
return nil
}
func parseNetworkStatus(s string) error {
func ParseNetworkStatus(s string) error {
lines := strings.Split(strings.TrimSpace(s), "\n")
count := len(lines)
if len(Status.Network) != count-2 {

21
model/status_test.go Normal file
View File

@ -0,0 +1,21 @@
package model_test
import (
_ "embed"
"testing"
"github.com/lollipopkit/server_box_monitor/model"
)
var (
//go:embed test/disk
_disk string
)
func TestParseDisk(t *testing.T) {
err := model.ParseDiskStatus(_disk)
if err != nil {
t.Error(err)
}
t.Log(model.Status.Disk)
}

16
model/test/disk Normal file
View File

@ -0,0 +1,16 @@
Filesystem Size Used Avail Use% Mounted on
devtmpfs 355M 0 355M 0% /dev
tmpfs 402M 0 402M 0% /dev/shm
tmpfs 402M 42M 361M 11% /run
tmpfs 402M 0 402M 0% /sys/fs/cgroup
/dev/mapper/centosvolume-root 40G 26G 14G 66% /
/dev/sda2 1014M 602M 413M 60% /boot
/dev/sda1 100M 7.3M 93M 8% /boot/efi
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/2e25140c8e4d7cb41e50a5ee1ff9bec57f78f4d2153a89924911f14d946e2dff/merged
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/9bb79d2a05069c0fd8b7d1ac3fecd4bde7aefc020062f8eb06b27a096874e5f2/merged
shm 64M 0 64M 0% /var/lib/docker/containers/859a6f0583b00d476dbdb901a78ca0db43d8e32261a719612bd52275ae89b36c/mounts/shm
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/b4495c37bf40ab489499e10208e2fe8ef4e98d3d8a3782f882ab98151873601d/merged
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/5d09361f0a2a3e3827ae02cc70c3d61de0c2bf7e39c386f2005fb080adaa6906/merged
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/708565ffad7031b4d91da2b4d9a5f005ca3ce0960e7aaf0fd7bd2c2d956a69bc/merged
overlay 40G 26G 14G 66% /var/lib/docker/overlay2/71a4db479fb5f106a4a7877476aababb2adee2fa8378197666451bc15cb76b7a/merged
tmpfs 81M 0 81M 0% /run/user/0

View File

@ -92,7 +92,7 @@ func runCheck() {
log.Suc("[PUSH] %s success", push.Name)
}
pushPairsLock.Lock()
pushPairs = pushPairs[:0]
pushPairsLock.Unlock()