-
Notifications
You must be signed in to change notification settings - Fork 13
/
host.go
69 lines (60 loc) · 1.55 KB
/
host.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
63
64
65
66
67
68
69
package monitord
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/upgrade"
)
// HostMonitor monitor host information
type hostMonitor struct {
duration time.Duration
}
// NewHostMonitor initialize a new host watcher
func NewHostMonitor(duration time.Duration) (pkg.HostMonitor, error) {
if duration == 0 {
duration = 2 * time.Second
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, errors.Wrap(err, "failed to initialize fs watcher")
}
// the file can not exist if the system was booted from overlay
if _, err := os.Stat(upgrade.FlistInfoFile); err == nil {
if err := watcher.Add(upgrade.FlistInfoFile); err != nil {
return nil, errors.Wrapf(err, "failed to watch '%s'", upgrade.FlistInfoFile)
}
}
return &hostMonitor{
duration: duration,
}, nil
}
func (h *hostMonitor) Uptime(ctx context.Context) <-chan time.Duration {
ch := make(chan time.Duration)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(h.duration):
data, err := ioutil.ReadFile("/proc/uptime")
if err != nil {
log.Error().Err(err).Msg("failed to read data from /proc/uptime")
continue
}
var uptime float64
if _, err := fmt.Sscanf(string(data), "%f", &uptime); err != nil {
log.Error().Err(err).Msg("failed to parse uptime data")
continue
}
ch <- time.Duration(uptime * float64(time.Second))
}
}
}()
return ch
}