-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsfptpd.go
101 lines (91 loc) · 3.11 KB
/
sfptpd.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
97
98
99
100
101
package main
import (
"encoding/json"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type Stats struct {
Instance string `json:"instance"`
Time string `json:"time"`
ClockMaster struct {
Name string `json:"name"`
Time string `json:"time"`
} `json:"clock-master"`
ClockSlave struct {
Name string `json:"name"`
Time string `json:"time"`
PrimaryInterface string `json:"primary-interface"`
} `json:"clock-slave"`
IsDisciplining bool `json:"is-disciplining"`
InSync bool `json:"in-sync"`
Alarms []interface{} `json:"alarms"`
Stats struct {
Offset float64 `json:"offset"`
FreqAdj float64 `json:"freq-adj"`
PTerm float64 `json:"p-term"`
ITerm float64 `json:"i-term"`
} `json:"stats"`
}
func gaugeVec(gaugeVec *prometheus.GaugeVec, instance string) prometheus.Gauge {
return gaugeVec.With(map[string]string{"instance": instance})
}
func setBool(gauge prometheus.Gauge, value bool) {
if value {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
// parseTime6 parses a time string in the format "2022-07-29 15:52:46.121677"
func parseTime6(timeStr string) (int64, error) {
t, err := time.Parse("2006-01-02 15:04:05.000000", timeStr)
if err != nil {
return -1, err
}
return t.UnixNano() / 1000000, nil
}
// parseTime9 parses a time string in the format "2022-07-29 15:52:46.121677000"
func parseTime9(timeStr string) (int64, error) {
t, err := time.Parse("2006-01-02 15:04:05.000000000", timeStr)
if err != nil {
return -1, err
}
return t.UnixNano() / 1000000, nil
}
func processLine(line string) {
var stats Stats
err := json.Unmarshal([]byte(line), &stats)
if err != nil {
log.Errorf("Error parsing JSON: %s", err)
return
}
log.Debugf("Parsed stats: %+v", stats)
gaugeVec(metricLastUpdate, stats.Instance).Set(float64(time.Now().UTC().UnixNano() / 1000000))
t, err := parseTime6(stats.Time)
if err != nil {
log.Warnf("Error parsing time: %s", err)
}
masterTime, err := parseTime9(stats.ClockMaster.Time)
if err != nil {
log.Warnf("Error parsing master time %s: %s", stats.ClockMaster.Time, err)
}
slaveTime, err := parseTime9(stats.ClockSlave.Time)
if err != nil {
log.Warnf("Error parsing slave time %s: %s", stats.ClockSlave.Time, err)
}
gaugeVec(metricTime, stats.Instance).Set(float64(t))
metricMaster.With(map[string]string{"instance": stats.Instance, "name": stats.ClockMaster.Name}).Set(float64(masterTime))
metricSlave.With(map[string]string{
"instance": stats.Instance,
"name": stats.ClockSlave.Name,
"primary_interface": stats.ClockSlave.PrimaryInterface,
}).Set(float64(slaveTime))
setBool(gaugeVec(metricIsDisciplining, stats.Instance), stats.IsDisciplining)
setBool(gaugeVec(metricInSync, stats.Instance), stats.InSync)
gaugeVec(metricAlarms, stats.Instance).Set(float64(len(stats.Alarms)))
gaugeVec(metricOffset, stats.Instance).Set(stats.Stats.Offset)
gaugeVec(metricFreqAdj, stats.Instance).Set(stats.Stats.FreqAdj)
gaugeVec(metricPTerm, stats.Instance).Set(stats.Stats.PTerm)
gaugeVec(metricITerm, stats.Instance).Set(stats.Stats.ITerm)
}