From ece07b36fcc3cf59b37040213345887eeb860ad7 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 16 Jul 2023 17:36:47 +0200 Subject: [PATCH] Add a command line option to force chmod on the receiving unix socket. (#45) fixes #44 Signed-off-by: Christian Pointner --- README.md | 1 + collector/collector.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a503994..44392c2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/collector/collector.go b/collector/collector.go index 9d9d8d2..654b4dc 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -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( @@ -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 } @@ -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, } @@ -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)