-
Notifications
You must be signed in to change notification settings - Fork 92
/
host_time.go
96 lines (81 loc) · 2.59 KB
/
host_time.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package collect
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/godbus/dbus"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
type NTPStatus string
type TimeInfo struct {
Timezone string `json:"timezone"`
NTPSynchronized bool `json:"ntp_synchronized"`
NTPActive bool `json:"ntp_active"`
}
type CollectHostTime struct {
hostCollector *troubleshootv1beta2.HostTime
}
func (c *CollectHostTime) Title() string {
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "TCP Port Status")
}
func (c *CollectHostTime) IsExcluded() (bool, error) {
return isExcluded(c.hostCollector.Exclude)
}
func (c *CollectHostTime) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
timeInfo := TimeInfo{}
conn, err := dbus.SystemBus()
if err != nil {
return nil, errors.Wrap(err, "failed to connect to dbus")
}
defer func() {
if err := conn.Close(); err != nil {
log.Printf("Failed to close dbus connection: %v\n", err)
}
}()
prop := "org.freedesktop.timedate1.Timezone"
variant, err := conn.Object("org.freedesktop.timedate1", "/org/freedesktop/timedate1").GetProperty(prop)
if err != nil {
return nil, errors.Wrapf(err, "failed to read property %s", prop)
}
timeInfo.Timezone = strings.Trim(variant.String(), `"`)
// UTC is reported as Etc/UTC on Ubuntu
if strings.ToLower(timeInfo.Timezone) == "etc/utc" {
timeInfo.Timezone = "UTC"
}
timeInfo.Timezone = strings.ToUpper(timeInfo.Timezone)
prop = "org.freedesktop.timedate1.NTPSynchronized"
variant, err = conn.Object("org.freedesktop.timedate1", "/org/freedesktop/timedate1").GetProperty(prop)
if err != nil {
return nil, errors.Wrapf(err, "failed to read property %s", prop)
}
switch variant.String() {
case "true":
timeInfo.NTPSynchronized = true
case "false":
timeInfo.NTPSynchronized = false
default:
return nil, fmt.Errorf("Unexpected value for property %s: %s", prop, variant.String())
}
prop = "org.freedesktop.timedate1.NTP"
variant, err = conn.Object("org.freedesktop.timedate1", "/org/freedesktop/timedate1").GetProperty(prop)
if err != nil {
return nil, errors.Wrapf(err, "failed to read property %s", prop)
}
switch variant.String() {
case "true":
timeInfo.NTPActive = true
case "false":
timeInfo.NTPActive = false
default:
return nil, fmt.Errorf("Unexpected value for property %s: %s", prop, variant.String())
}
b, err := json.Marshal(timeInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal time info")
}
return map[string][]byte{
"system/time.json": b,
}, nil
}