-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.go
41 lines (35 loc) · 782 Bytes
/
units.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
package main
import (
"github.com/coreos/go-etcd/etcd"
"github.com/coreos/go-log/log"
"github.com/coreos/go-systemd/dbus"
"io/ioutil"
"strings"
"time"
)
func main() {
client := etcd.NewClient()
bootID, err := getBootID()
if err != nil {
log.Fatalln(err)
}
keyPrefix := "/system/" + bootID + "/"
statusChan, _ := dbus.SubscribeUnits(5 * time.Second)
for statusMap := range statusChan {
for name, unit := range statusMap {
key := keyPrefix + name + "/status"
if unit != nil {
client.Set(key, unit.SubState, 0)
} else {
client.Delete(key)
}
}
}
}
func getBootID() (string, error) {
contents, err := ioutil.ReadFile("/proc/sys/kernel/random/boot_id")
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}