Skip to content

Commit 5648ba8

Browse files
authored
Merge pull request #62 from AgoraLab/dev/fix-bug
fix bug
2 parents d79f633 + aa51ee7 commit 5648ba8

5 files changed

Lines changed: 37 additions & 63 deletions

File tree

cmd/godplugin/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"os/user"
9+
"net/http"
910
"path/filepath"
1011
"strings"
1112

@@ -18,6 +19,7 @@ import (
1819
"golang.org/x/net/http/httpproxy"
1920

2021
_ "github.com/netdata/go.d.plugin/modules"
22+
_ "net/http/pprof"
2123
)
2224

2325
var (
@@ -28,6 +30,7 @@ var (
2830
varLibDir = os.Getenv("NETDATA_LIB_DIR")
2931
lockDir = os.Getenv("NETDATA_LOCK_DIR")
3032
watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH")
33+
pprofAddr = os.Getenv("NETDATA_GO_PLUGIN_PPROF_ADDRESSES")
3134

3235
version = "unknown"
3336
)
@@ -86,6 +89,11 @@ func init() {
8689
if v := os.Getenv("TZ"); strings.HasPrefix(v, ":") {
8790
_ = os.Unsetenv("TZ")
8891
}
92+
93+
if pprofAddr == "" {
94+
pprofAddr = "localhost:65453"
95+
}
96+
go http.ListenAndServe(pprofAddr, nil)
8997
}
9098

9199
func main() {

config/go.d/chrony.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
jobs:
8787
- address: '127.0.0.1:323'
88-
timeout: 1
88+
timeout: 500
8989

9090
# - name: remote
9191
# address: '203.0.113.0:323'

modules/chrony/charts.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,14 @@ var charts = Charts{
151151
{ID: net.IPv4zero.String(), Name: net.IPv4zero.String(), Algo: module.Absolute, Div: 1, Mul: 1},
152152
},
153153
},
154+
{
155+
ID: "residual_frequency",
156+
Title: "Residual frequency",
157+
Units: "ppm",
158+
Fam: "frequency",
159+
Ctx: "chrony.residual_frequency",
160+
Dims: module.Dims{
161+
{ID: "residual_frequency", Div: 1},
162+
},
163+
},
154164
}

modules/chrony/chrony.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func New() *Chrony {
4848
Protocol: chronyDefaultProtocol,
4949
Address: chronyDefaultCmdAddr,
5050
SentryConfig: sentry.ClientOptions{},
51-
Timeout: 1000,
51+
Timeout: 500,
5252
},
5353
charts: &charts,
5454
latestSource: net.IPv4zero,
@@ -80,7 +80,7 @@ func (c *Chrony) Init() bool {
8080
}
8181

8282
c.conn = conn
83-
return true
83+
return c.ApplyChronyVersion() == nil
8484
}
8585

8686
// Check makes check
@@ -105,42 +105,25 @@ func (c *Chrony) Charts() *Charts {
105105
func (c *Chrony) Collect() map[string]int64 {
106106
// collect all we need and sent Exception to sentry
107107
res := map[string]int64{"running": 0}
108+
var err error
108109

109-
if !c.Running() {
110-
return res
111-
}
112-
res["running"] = 1
113-
114-
tra := c.collectTracking()
115-
for k, v := range tra {
116-
res[k] = v
110+
err = c.collectTracking(res)
111+
if err != nil {
112+
c.Errorf("fetch tracking status failed: %s", err)
117113
}
118114

119-
act := c.collectActivity()
120-
for k, v := range act {
121-
res[k] = v
115+
err = c.collectActivity(res)
116+
if err != nil {
117+
c.Errorf("fetch activity status failed: %s", err)
122118
}
123119

124120
return res
125121
}
126122

127-
func (c *Chrony) Running() bool {
128-
err := c.SubmitEmptyRequest()
129-
if err != nil {
130-
c.Errorf("contract chrony failed with err: %s", err)
131-
return false
132-
}
133-
return true
134-
}
135-
136-
func (c *Chrony) collectTracking() (res map[string]int64) {
137-
res = make(map[string]int64)
123+
func (c *Chrony) collectTracking(res map[string]int64) error {
138124
tracking, err := c.FetchTracking()
139125
if err != nil {
140-
c.Errorf("fetch tracking status failed: %s", err)
141126
sentry.CaptureException((FetchingChronyError)(err.Error()))
142-
res["running"] = 0
143-
return
144127
}
145128
c.Debugf(tracking.String())
146129

@@ -154,8 +137,9 @@ func (c *Chrony) collectTracking() (res map[string]int64) {
154137
res["last_offset"] = (int64)(tracking.LastOffset.Int64())
155138
res["rms_offset"] = (int64)(tracking.RmsOffset.Int64())
156139
res["update_interval"] = (int64)(tracking.LastUpdateInterval.Int64())
157-
res["current_correction"] = (int64)(tracking.LastUpdateInterval.Int64())
140+
res["current_correction"] = (int64)(tracking.CurrentCorrection.Int64())
158141
res["ref_timestamp"] = tracking.RefTime.Time().Unix()
142+
res["residual_frequency"] = (int64)(tracking.ResidFreqPpm.Int64())
159143

160144
sourceIp := tracking.Ip.Ip()
161145

@@ -206,18 +190,14 @@ func (c *Chrony) collectTracking() (res map[string]int64) {
206190
sentry.CaptureException((OutOfSyncForTooLong)(rt))
207191
}
208192

209-
return
193+
return nil
210194
}
211195

212-
func (c *Chrony) collectActivity() (res map[string]int64) {
213-
res = make(map[string]int64)
196+
func (c *Chrony) collectActivity(res map[string]int64) error {
214197
activity, err := c.FetchActivity()
215198
if err != nil {
216-
c.Errorf("fetch activity status failed: %s", err)
217199
sentry.CaptureException((FetchingChronyError)(err.Error()))
218-
return
219200
}
220-
c.Debug(activity.String())
221201

222202
res["online_sources"] = int64(activity.Online)
223203
res["offline_sources"] = int64(activity.Offline)
@@ -229,5 +209,5 @@ func (c *Chrony) collectActivity() (res map[string]int64) {
229209
c.Debugf("sending sentry error for NoSourceOnlineError: %g", activity.Online)
230210
sentry.CaptureException((NoSourceOnlineError)(activity.Online))
231211
}
232-
return res
212+
return nil
233213
}

modules/chrony/types.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ const (
8383
)
8484

8585
type IpAddr struct {
86-
IPAddrHigh uint64
87-
IPAddrLow uint64
86+
IP [16]uint8
8887
Family uint16
8988
Pad uint16
9089
}
@@ -104,32 +103,9 @@ func (tracking *TrackingPayload) String() string {
104103

105104
func (ia IpAddr) Ip() net.IP {
106105
if ia.Family == IpaddrInet4 {
107-
m := uint32(ia.IPAddrHigh >> (32))
108-
var ip [4]uint8
109-
for i := 0; i < 4; i++ {
110-
ip[i] = uint8(m % 0x100)
111-
m = m / 0x100
112-
}
113-
return net.IPv4(ip[3], ip[2], ip[1], ip[0])
106+
return net.IP(ia.IP[:4])
114107
}
115-
116-
if ia.Family == IpaddrInet6 {
117-
res := make(net.IP, net.IPv6len)
118-
h := ia.IPAddrHigh
119-
for i := 7; i >= 0; i-- {
120-
res[i] = byte(h % 0x100)
121-
h = h / 0x100
122-
}
123-
l := ia.IPAddrLow
124-
for i := 7; i >= 0; i-- {
125-
res[i+8] = byte(l % 0x100)
126-
l = l / 0x100
127-
}
128-
129-
return res
130-
}
131-
132-
return net.IPv4zero
108+
return net.IP(ia.IP[:])
133109
}
134110

135111
func (ia IpAddr) String() string {

0 commit comments

Comments
 (0)