- seperate `check & push` interval
- content for HttpDo
This commit is contained in:
lollipopkit 2023-04-26 19:13:48 +08:00
parent 8dcd0265e6
commit d8139c09bd
7 changed files with 54 additions and 52 deletions

View File

@ -1,6 +1,6 @@
{
"version": 1,
// Interval of checking and pushing
// Interval of checking
// Valid formats: 1m 1h 1d
// Default: 1m
// Values less than 1m will be ignored

View File

@ -1,6 +1,6 @@
{
"version": 1,
// 时间间隔,用于检查和推送
// 时间间隔,用于推送
// 有效格式: 1m 1h 1d
// 默认: 1m
// 小于 1m 的值将被忽略

View File

@ -117,7 +117,7 @@ func (p PushIfaceIOS) push(args []*PushPair) ([]byte, int, error) {
return util.HttpDo(
"POST",
"https://push.lolli.tech/v1/ios",
string(bodyBytes),
bodyBytes,
map[string]string{
"Content-Type": "application/json",
"AppID": "com.lollipopkit.toolbox",
@ -156,7 +156,7 @@ func (p PushIfaceServerChan) push(args []*PushPair) ([]byte, int, error) {
return util.HttpDo(
"GET",
url,
"",
nil,
nil,
)
}

View File

@ -33,7 +33,7 @@ type Rule struct {
func (r *Rule) Id() string {
return fmt.Sprintf("[%s %s %s]", r.MonitorType, r.Threshold, r.Matcher)
}
func (r *Rule) ShouldNotify(s *ServerStatus) (bool, *PushPair, error) {
func (r *Rule) ShouldNotify(s *serverStatus) (bool, *PushPair, error) {
t, err := ParseToThreshold(r.Threshold)
if err != nil {
return false, nil, errors.Join(ErrInvalidRule, err)
@ -56,7 +56,7 @@ func (r *Rule) ShouldNotify(s *ServerStatus) (bool, *PushPair, error) {
}
}
func (r *Rule) shouldNotifyCPU(ss []OneCpuStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifyCPU(ss []oneCpuStatus, t *Threshold) (bool, *PushPair, error) {
if len(ss) == 0 {
// utils.Warn("cpu is not valid, skip this rule")
return false, nil, nil
@ -104,7 +104,7 @@ func (r *Rule) shouldNotifyCPU(ss []OneCpuStatus, t *Threshold) (bool, *PushPair
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("invalid threshold type for cpu: %s", t.ThresholdType.Name()))
}
}
func (r *Rule) shouldNotifyMemory(s *MemStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifyMemory(s *memStatus, t *Threshold) (bool, *PushPair, error) {
if s == nil {
// utils.Warn("memory is not valid, skip this rule")
return false, nil, nil
@ -148,7 +148,7 @@ func (r *Rule) shouldNotifyMemory(s *MemStatus, t *Threshold) (bool, *PushPair,
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("invalid threshold type for memory: %s", t.ThresholdType.Name()))
}
}
func (r *Rule) shouldNotifySwap(s *SwapStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifySwap(s *swapStatus, t *Threshold) (bool, *PushPair, error) {
if s == nil {
// utils.Warn("swap is not valid, skip this rule")
return false, nil, nil
@ -189,12 +189,12 @@ func (r *Rule) shouldNotifySwap(s *SwapStatus, t *Threshold) (bool, *PushPair, e
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("invalid threshold type for swap: %s", t.ThresholdType.Name()))
}
}
func (r *Rule) shouldNotifyDisk(s []DiskStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifyDisk(s []diskStatus, t *Threshold) (bool, *PushPair, error) {
if len(s) == 0 {
// utils.Warn("disk is not valid, skip this rule")
return false, nil, nil
}
var disk DiskStatus
var disk diskStatus
var have bool
for _, d := range s {
if d.MountPath == r.Matcher || d.Filesystem == r.Matcher {
@ -230,13 +230,13 @@ func (r *Rule) shouldNotifyDisk(s []DiskStatus, t *Threshold) (bool, *PushPair,
return false, nil, errors.Join(ErrInvalidRule, fmt.Errorf("invalid threshold type for disk: %s", t.ThresholdType.Name()))
}
}
func (r *Rule) shouldNotifyNetwork(s []NetworkStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifyNetwork(s []networkStatus, t *Threshold) (bool, *PushPair, error) {
if len(s) == 0 {
// utils.Warn("network is not valid, skip this rule")
return false, nil, nil
}
var net NetworkStatus
var net networkStatus
var have bool
for _, n := range s {
if strings.Contains(r.Matcher, n.Interface) {
@ -304,13 +304,13 @@ func (r *Rule) shouldNotifyNetwork(s []NetworkStatus, t *Threshold) (bool, *Push
}
}
func (r *Rule) shouldNotifyTemperature(s []TemperatureStatus, t *Threshold) (bool, *PushPair, error) {
func (r *Rule) shouldNotifyTemperature(s []temperatureStatus, t *Threshold) (bool, *PushPair, error) {
if len(s) == 0 {
// utils.Warn("temperature is not valid, skip this rule")
return false, nil, nil
}
var temp TemperatureStatus
var temp temperatureStatus
var have bool
for _, t := range s {
if strings.Contains(t.Name, r.Matcher) {

View File

@ -20,34 +20,34 @@ var (
)
var (
Status = new(ServerStatus)
Status = new(serverStatus)
)
type ServerStatus struct {
CPU []OneCpuStatus
Mem *MemStatus
Swap *SwapStatus
Disk []DiskStatus
Network []NetworkStatus
Temperature []TemperatureStatus
type serverStatus struct {
CPU []oneCpuStatus
Mem *memStatus
Swap *swapStatus
Disk []diskStatus
Network []networkStatus
Temperature []temperatureStatus
}
type TemperatureStatus struct {
type temperatureStatus struct {
Value float64
Name string
}
type CpuOneTimeStatus struct {
type cpuOneTimeStatus struct {
Used int
Total int
}
type OneCpuStatus struct {
type oneCpuStatus struct {
Core int
TimeSequence[CpuOneTimeStatus]
TimeSequence[cpuOneTimeStatus]
}
func (cs *OneCpuStatus) UsedPercent() (float64, error) {
func (cs *oneCpuStatus) UsedPercent() (float64, error) {
if cs.TimeSequence.New == nil || cs.TimeSequence.Old == nil {
return 0, ErrNotReady
}
@ -59,21 +59,21 @@ func (cs *OneCpuStatus) UsedPercent() (float64, error) {
return float64(used) / float64(total) * 100, nil
}
type MemStatus struct {
type memStatus struct {
Total Size
Avail Size
Free Size
Used Size
}
type SwapStatus struct {
type swapStatus struct {
Total Size
Free Size
Used Size
Cached Size
}
type DiskStatus struct {
type diskStatus struct {
MountPath string
Filesystem string
Total Size
@ -82,23 +82,23 @@ type DiskStatus struct {
UsedPercent float64
}
type NetworkOneTimeStatus struct {
type networkOneTimeStatus struct {
Transmit Size
Receive Size
}
type NetworkStatus struct {
type networkStatus struct {
Interface string
TimeSequence[NetworkOneTimeStatus]
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 / GetIntervalInSeconds()), 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
}
@ -149,16 +149,18 @@ func ParseStatus(s string) error {
func initMem() {
if Status.Mem == nil {
Status.Mem = &MemStatus{}
Status.Mem = &memStatus{}
}
}
func initSwap() {
if Status.Swap == nil {
Status.Swap = &SwapStatus{}
Status.Swap = &swapStatus{}
}
}
func parseMemAndSwapStatus(s string) error {
initMem()
initSwap()
lines := strings.Split(s, "\n")
for i := range lines {
line := strings.TrimSpace(lines[i])
@ -173,28 +175,22 @@ func parseMemAndSwapStatus(s string) error {
switch true {
case strings.HasPrefix(line, "MemTotal:"):
initMem()
Status.Mem.Total = size
fallthrough
case strings.HasPrefix(line, "MemFree:"):
initMem()
Status.Mem.Free = size
Status.Mem.Used = Status.Mem.Total - Status.Mem.Free
fallthrough
case strings.HasPrefix(line, "MemAvailable:"):
initMem()
Status.Mem.Avail = size
case strings.HasPrefix(line, "SwapTotal:"):
initSwap()
Status.Swap.Total = size
fallthrough
case strings.HasPrefix(line, "SwapFree:"):
initSwap()
Status.Swap.Free = size
Status.Swap.Used = Status.Swap.Total - Status.Swap.Free
fallthrough
case strings.HasPrefix(line, "SwapCached:"):
initSwap()
Status.Swap.Cached = size
}
}
@ -205,7 +201,7 @@ func parseCPUStatus(s string) error {
lines := strings.Split(strings.TrimSpace(s), "\n")
count := len(lines)
if len(Status.CPU) != count {
Status.CPU = make([]OneCpuStatus, count)
Status.CPU = make([]oneCpuStatus, count)
}
for i := range lines {
line := strings.TrimSpace(lines[i])
@ -230,7 +226,7 @@ func parseCPUStatus(s string) error {
}
total += v
}
Status.CPU[i].TimeSequence.Update(&CpuOneTimeStatus{
Status.CPU[i].TimeSequence.Update(&cpuOneTimeStatus{
Used: user + sys,
Total: total,
})
@ -244,7 +240,7 @@ func parseDiskStatus(s string) error {
lines = lines[1:]
count := len(lines)
if len(Status.Disk) != count {
Status.Disk = make([]DiskStatus, count)
Status.Disk = make([]diskStatus, count)
}
for i := range lines {
line := strings.TrimSpace(lines[i])
@ -285,7 +281,7 @@ func parseTemperatureStatus(s1, s2 string) error {
}
count := len(types)
if len(Status.Temperature) != count {
Status.Temperature = make([]TemperatureStatus, count)
Status.Temperature = make([]temperatureStatus, count)
}
for i := range types {
Status.Temperature[i].Name = strings.TrimSpace(types[i])
@ -302,7 +298,7 @@ func parseNetworkStatus(s string) error {
lines := strings.Split(strings.TrimSpace(s), "\n")
count := len(lines)
if len(Status.Network) != count-2 {
Status.Network = make([]NetworkStatus, count-2)
Status.Network = make([]networkStatus, count-2)
}
for i := range lines {
if i < 2 {
@ -325,7 +321,7 @@ func parseNetworkStatus(s string) error {
return err
}
transmit := Size(transmitBytes)
Status.Network[idx].TimeSequence.Update(&NetworkOneTimeStatus{
Status.Network[idx].TimeSequence.Update(&networkOneTimeStatus{
Receive: receive,
Transmit: transmit,
})

View File

@ -1,6 +1,6 @@
package model
type TimeSequence[T CpuOneTimeStatus | NetworkOneTimeStatus] struct {
type TimeSequence[T any] struct {
Old *T
New *T
}

View File

@ -14,6 +14,8 @@ import (
var (
pushPairs = []*model.PushPair{}
pushPairsLock = new(sync.RWMutex)
lastPushTime time.Time
checkInterval = time.Second * 3
)
func init() {
@ -42,7 +44,7 @@ func run() {
panic(err)
}
for range time.NewTicker(model.GetInterval()).C {
for range time.NewTicker(checkInterval).C {
err = model.RefreshStatus()
status := model.Status
if err != nil {
@ -68,8 +70,12 @@ func run() {
if len(pushPairs) == 0 {
continue
}
if time.Now().Sub(lastPushTime) < model.GetInterval() {
continue
}
term.Info("[STATUS] refreshed, %d to push", len(pushPairs))
term.Info("[PUSH] %d to push", len(pushPairs))
pushPairsLock.RLock()
for _, push := range model.Config.Pushes {