Skip to content

Commit

Permalink
Add a command line option to force chmod on the receiving unix socket. (
Browse files Browse the repository at this point in the history
#45)

fixes #44

Signed-off-by: Christian Pointner <[email protected]>
  • Loading branch information
equinox0815 authored Jul 16, 2023
1 parent f8847f0 commit ece07b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ By default, the exporter will bind on `:9123`.
In case chrony is configured to not accept command messages via UDP (`cmdport 0`) the exporter can use the unix command socket opened by chrony.
In this case use the command line option `--chrony.address=unix:///path/to/chronyd.sock` to configure the path to the chrony command socket.
On most systems chrony will be listenting on `unix:///run/chrony/chronyd.sock`. For this to work the exporter needs to run as root or the same user as chrony.
When the exporter is run as root the flag `collector.chmod-socket` is needed as well.

## Prometheus Rules

Expand Down
20 changes: 14 additions & 6 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const (
)

var (
collectTracking = kingpin.Flag("collector.tracking", "Collect tracking metrics").Default("true").Bool()
collectSources = kingpin.Flag("collector.sources", "Collect sources metrics").Default("false").Bool()
collectTracking = kingpin.Flag("collector.tracking", "Collect tracking metrics").Default("true").Bool()
collectSources = kingpin.Flag("collector.sources", "Collect sources metrics").Default("false").Bool()
collectChmodSocket = kingpin.Flag("collector.chmod-socket", "Chmod 0666 the receiving unix datagram socket").Default("false").Bool()

upMetric = typedDesc{
prometheus.NewDesc(
Expand All @@ -53,8 +54,9 @@ type Exporter struct {
address string
timeout time.Duration

collectSources bool
collectTracking bool
collectSources bool
collectTracking bool
collectChmodSocket bool

logger log.Logger
}
Expand All @@ -74,8 +76,9 @@ func NewExporter(address string, logger log.Logger) Exporter {
address: address,
timeout: 5 * time.Second,

collectSources: *collectSources,
collectTracking: *collectTracking,
collectSources: *collectSources,
collectTracking: *collectTracking,
collectChmodSocket: *collectChmodSocket,

logger: logger,
}
Expand All @@ -97,6 +100,11 @@ func (e Exporter) dial() (net.Conn, error, func()) {
if err != nil {
return nil, err, func() { os.Remove(local) }
}
if e.collectChmodSocket {
if err := os.Chmod(local, 0666); err != nil {
return nil, err, func() { conn.Close(); os.Remove(local) }
}
}
err = conn.SetReadDeadline(time.Now().Add(e.timeout))
if err != nil {
level.Debug(e.logger).Log("msg", "Couldn't set read-timeout for unix datagram socket", "err", err)
Expand Down

0 comments on commit ece07b3

Please sign in to comment.