|
| 1 | +// Copyright 2022 Ben Kochie |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "math" |
| 19 | + "net" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/facebook/time/ntp/chrony" |
| 23 | + "github.com/go-kit/log/level" |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + sourcesSubsystem = "sources" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + sourcesLastRx = typedDesc{ |
| 33 | + prometheus.NewDesc( |
| 34 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "last_sample_age_seconds"), |
| 35 | + "Chrony sources last good sample age in seconds", |
| 36 | + []string{"source_address", "source_name"}, |
| 37 | + nil, |
| 38 | + ), |
| 39 | + prometheus.GaugeValue, |
| 40 | + } |
| 41 | + |
| 42 | + sourcesLastSample = typedDesc{ |
| 43 | + prometheus.NewDesc( |
| 44 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "last_sample_offset_seconds"), |
| 45 | + "Chrony sources last sample margin of error in seconds", |
| 46 | + []string{"source_address", "source_name"}, |
| 47 | + nil, |
| 48 | + ), |
| 49 | + prometheus.GaugeValue, |
| 50 | + } |
| 51 | + |
| 52 | + sourcesLastSampleErr = typedDesc{ |
| 53 | + prometheus.NewDesc( |
| 54 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "last_sample_error_margin_seconds"), |
| 55 | + "Chrony sources last sample offset in seconds", |
| 56 | + []string{"source_address", "source_name"}, |
| 57 | + nil, |
| 58 | + ), |
| 59 | + prometheus.GaugeValue, |
| 60 | + } |
| 61 | + |
| 62 | + sourcesPollInterval = typedDesc{ |
| 63 | + prometheus.NewDesc( |
| 64 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "polling_interval_seconds"), |
| 65 | + "Chrony sources polling interval in seconds", |
| 66 | + []string{"source_address", "source_name"}, |
| 67 | + nil, |
| 68 | + ), |
| 69 | + prometheus.GaugeValue, |
| 70 | + } |
| 71 | + |
| 72 | + sourcesStateInfo = typedDesc{ |
| 73 | + prometheus.NewDesc( |
| 74 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "state_info"), |
| 75 | + "Chrony sources state info", |
| 76 | + []string{"source_address", "source_name", "source_state", "source_mode"}, |
| 77 | + nil, |
| 78 | + ), |
| 79 | + prometheus.GaugeValue, |
| 80 | + } |
| 81 | + |
| 82 | + sourcesStratum = typedDesc{ |
| 83 | + prometheus.NewDesc( |
| 84 | + prometheus.BuildFQName(namespace, sourcesSubsystem, "stratum"), |
| 85 | + "Chrony sources stratum", |
| 86 | + []string{"source_address", "source_name"}, |
| 87 | + nil, |
| 88 | + ), |
| 89 | + prometheus.GaugeValue, |
| 90 | + } |
| 91 | +) |
| 92 | + |
| 93 | +func (e Exporter) getSourcesMetrics(ch chan<- prometheus.Metric, client chrony.Client) { |
| 94 | + packet, err := client.Communicate(chrony.NewSourcesPacket()) |
| 95 | + if err != nil { |
| 96 | + level.Error(e.logger).Log("msg", "Couldn't get sources", "err", err) |
| 97 | + return |
| 98 | + } |
| 99 | + level.Debug(e.logger).Log("msg", "Got 'sources' response", "sources_packet", packet.GetStatus()) |
| 100 | + |
| 101 | + sources, ok := packet.(*chrony.ReplySources) |
| 102 | + if !ok { |
| 103 | + level.Error(e.logger).Log("msg", "Got wrong 'sources' response", "packet", packet) |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + results := make([]chrony.ReplySourceData, sources.NSources) |
| 108 | + |
| 109 | + for i := 0; i < int(sources.NSources); i++ { |
| 110 | + level.Debug(e.logger).Log("msg", "Fetching source", "source", i) |
| 111 | + packet, err = client.Communicate(chrony.NewSourceDataPacket(int32(i))) |
| 112 | + if err != nil { |
| 113 | + level.Error(e.logger).Log("msg", "Failed to get sourcedata response", "source", i) |
| 114 | + return |
| 115 | + } |
| 116 | + sourceData, ok := packet.(*chrony.ReplySourceData) |
| 117 | + if !ok { |
| 118 | + level.Error(e.logger).Log("msg", "Got wrong 'sourcedata' response", "packet", packet) |
| 119 | + return |
| 120 | + } |
| 121 | + results[i] = *sourceData |
| 122 | + } |
| 123 | + |
| 124 | + for _, r := range results { |
| 125 | + sourceAddress := r.IPAddr.String() |
| 126 | + // Ignore reverse lookup errors. |
| 127 | + sourceNames, _ := net.LookupAddr(sourceAddress) |
| 128 | + sourceName := strings.Join(sourceNames, ",") |
| 129 | + |
| 130 | + ch <- sourcesLastRx.mustNewConstMetric(float64(r.SinceSample), sourceAddress, sourceName) |
| 131 | + ch <- sourcesLastSample.mustNewConstMetric(r.LatestMeas, sourceAddress, sourceName) |
| 132 | + ch <- sourcesLastSampleErr.mustNewConstMetric(r.LatestMeasErr, sourceAddress, sourceName) |
| 133 | + ch <- sourcesPollInterval.mustNewConstMetric(math.Pow(2, float64(r.Poll)), sourceAddress, sourceName) |
| 134 | + ch <- sourcesStateInfo.mustNewConstMetric(1.0, sourceAddress, sourceName, r.State.String(), modeTypeString(r.Mode)) |
| 135 | + ch <- sourcesStratum.mustNewConstMetric(float64(r.Stratum), sourceAddress, sourceName) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +var modeTypeDesc = []string{ |
| 140 | + "client", |
| 141 | + "peer", |
| 142 | + "ref", |
| 143 | +} |
| 144 | + |
| 145 | +func modeTypeString(m chrony.ModeType) string { |
| 146 | + if int(m) >= len(modeTypeDesc) { |
| 147 | + return fmt.Sprintf("unknown (%d)", m) |
| 148 | + } |
| 149 | + return modeTypeDesc[m] |
| 150 | +} |
0 commit comments