-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifmon.go
62 lines (50 loc) · 1.23 KB
/
ifmon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// IfStatus global var conaining cumulative status of all monitored net intefaces.
var IfStatus string
// UpdateIfStatus updates network interfaces status for i3bar.
func UpdateIfStatus() {
for {
var statusSum string
for _, item := range Conf.NetIf.If {
var (
name = item.Name
)
if name == "" {
name = filepath.Base(item.Dir)
}
operstate, err := os.ReadFile(item.Dir + "/operstate")
if err != nil {
operstate = []byte("?")
log.Printf("Unable to get net if status from file %s: %s", item.Dir+"/operstate", err)
} else {
switch strings.TrimSpace(string(operstate)) {
case "up":
StatusStr := fmt.Sprintf("<span foreground=\"%s\">⍋</span>", Conf.NetIf.UpColor)
operstate = []byte(StatusStr)
case "down":
statusStr := fmt.Sprintf("<span foreground=\"%s\">⍒</span>", Conf.NetIf.DownColor)
operstate = []byte(statusStr)
default:
operstate = []byte(`?`)
}
}
if statusSum != "" {
statusSum += " "
}
statusSum += fmt.Sprintf("%s:%s", name, operstate)
}
if IfStatus != statusSum {
IfStatus = statusSum
UpdateReady <- true
}
time.Sleep(3 * time.Second)
}
}